1
- /*! jquery.atwho - v1.0.1 %>
1
+ /*! jquery.atwho - v1.1.0 %>
2
2
* Copyright (c) 2015 chord.luo <chord.luo@gmail.com>;
3
3
* homepage: http://ichord.github.com/At.js
4
4
* Licensed MIT
5
5
*/
6
6
( function ( root , factory ) {
7
7
if ( typeof define === 'function' && define . amd ) {
8
8
// AMD. Register as an anonymous module.
9
- define ( [ "jquery" ] , function ( $ ) {
10
- return ( root . returnExportsGlobal = factory ( $ ) ) ;
9
+ define ( [ "jquery" ] , function ( jquery ) {
10
+ return ( root . returnExportsGlobal = factory ( jquery ) ) ;
11
11
} ) ;
12
12
} else if ( typeof exports === 'object' ) {
13
13
// Node. Does not work with strict CommonJS, but
17
17
} else {
18
18
factory ( jQuery ) ;
19
19
}
20
- } ( this , function ( $ ) {
20
+ } ( this , function ( jquery ) {
21
21
22
22
var Api , App , Controller , DEFAULT_CALLBACKS , EditableController , KEY_CODE , Model , TextareaController , View ,
23
23
__slice = [ ] . slice ,
@@ -35,13 +35,15 @@ App = (function() {
35
35
}
36
36
37
37
App . prototype . createContainer = function ( doc ) {
38
- if ( ( this . $el = $ ( "#atwho-container" , doc ) ) . length === 0 ) {
39
- return $ ( doc . body ) . append ( this . $el = $ ( "<div id='atwho-container'></div>" ) ) ;
38
+ var _ref ;
39
+ if ( ( _ref = this . $el ) != null ) {
40
+ _ref . remove ( ) ;
40
41
}
42
+ return $ ( doc . body ) . append ( this . $el = $ ( "<div class='atwho-container'></div>" ) ) ;
41
43
} ;
42
44
43
45
App . prototype . setupRootElement = function ( iframe , asRoot ) {
44
- var error , _ref ;
46
+ var error ;
45
47
if ( asRoot == null ) {
46
48
asRoot = false ;
47
49
}
@@ -57,17 +59,10 @@ App = (function() {
57
59
} catch ( _error ) {
58
60
error = _error ;
59
61
this . iframe = null ;
60
- throw new Error ( "iframe auto-discovery is failed.\nPlease use `serIframe` to set the target iframe manually." ) ;
61
- }
62
- }
63
- if ( this . iframeAsRoot = asRoot ) {
64
- if ( ( _ref = this . $el ) != null ) {
65
- _ref . remove ( ) ;
62
+ throw new Error ( "iframe auto-discovery is failed.\nPlease use `setIframe` to set the target iframe manually." ) ;
66
63
}
67
- return this . createContainer ( this . document ) ;
68
- } else {
69
- return this . createContainer ( document ) ;
70
64
}
65
+ return this . createContainer ( ( this . iframeAsRoot = asRoot ) ? this . document : document ) ;
71
66
} ;
72
67
73
68
App . prototype . controller = function ( at ) {
@@ -107,7 +102,19 @@ App = (function() {
107
102
} ;
108
103
109
104
App . prototype . listen = function ( ) {
110
- return this . $inputor . on ( 'keyup.atwhoInner' , ( function ( _this ) {
105
+ return this . $inputor . on ( 'compositionstart' , ( function ( _this ) {
106
+ return function ( e ) {
107
+ var _ref ;
108
+ if ( ( _ref = _this . controller ( ) ) != null ) {
109
+ _ref . view . hide ( ) ;
110
+ }
111
+ return _this . isComposing = true ;
112
+ } ;
113
+ } ) ( this ) ) . on ( 'compositionend' , ( function ( _this ) {
114
+ return function ( e ) {
115
+ return _this . isComposing = false ;
116
+ } ;
117
+ } ) ( this ) ) . on ( 'keyup.atwhoInner' , ( function ( _this ) {
111
118
return function ( e ) {
112
119
return _this . onKeyup ( e ) ;
113
120
} ;
@@ -226,9 +233,13 @@ App = (function() {
226
233
break ;
227
234
case KEY_CODE . TAB :
228
235
case KEY_CODE . ENTER :
236
+ case KEY_CODE . SPACE :
229
237
if ( ! view . visible ( ) ) {
230
238
return ;
231
239
}
240
+ if ( ! this . controller ( ) . getOpt ( 'spaceSelectsMatch' ) && e . keyCode === KEY_CODE . SPACE ) {
241
+ return ;
242
+ }
232
243
e . preventDefault ( ) ;
233
244
view . choose ( e ) ;
234
245
break ;
@@ -501,6 +512,9 @@ EditableController = (function(_super) {
501
512
502
513
EditableController . prototype . catchQuery = function ( e ) {
503
514
var $inserted , $query , index , inserted , lastNode , matched , offset , query , range , _range ;
515
+ if ( this . app . isComposing ) {
516
+ return ;
517
+ }
504
518
if ( ! ( range = this . _getRange ( ) ) ) {
505
519
return ;
506
520
}
@@ -544,7 +558,8 @@ EditableController = (function(_super) {
544
558
matched = this . callbacks ( "matcher" ) . call ( this , this . at , _range . toString ( ) , this . getOpt ( 'startWithSpace' ) ) ;
545
559
if ( $query . length === 0 && typeof matched === 'string' && ( index = range . startOffset - this . at . length - matched . length ) >= 0 ) {
546
560
range . setStart ( range . startContainer , index ) ;
547
- range . surroundContents ( ( $query = $ ( "<span class='atwho-query'/>" , this . app . document ) ) [ 0 ] ) ;
561
+ $query = $ ( "<span/>" , this . app . document ) . attr ( this . getOpt ( "editableAtwhoQueryAttrs" ) ) . addClass ( 'atwho-query' ) ;
562
+ range . surroundContents ( $query . get ( 0 ) ) ;
548
563
lastNode = $query . contents ( ) . last ( ) . get ( 0 ) ;
549
564
if ( / f i r e f o x / i. test ( navigator . userAgent ) ) {
550
565
range . setStart ( lastNode , lastNode . length ) ;
@@ -560,18 +575,21 @@ EditableController = (function(_super) {
560
575
el : $query
561
576
} ;
562
577
this . trigger ( "matched" , [ this . at , query . text ] ) ;
578
+ return this . query = query ;
563
579
} else {
564
580
this . view . hide ( ) ;
565
- query = null ;
581
+ this . query = {
582
+ el : $query
583
+ } ;
566
584
if ( $query . text ( ) . indexOf ( this . at ) >= 0 ) {
567
585
if ( this . _movingEvent ( e ) && $query . hasClass ( 'atwho-inserted' ) ) {
568
586
$query . removeClass ( 'atwho-query' ) ;
569
587
} else if ( false !== this . callbacks ( 'afterMatchFailed' ) . call ( this , this . at , $query ) ) {
570
588
this . _setRange ( "after" , this . _unwrap ( $query . text ( $query . text ( ) ) . contents ( ) . first ( ) ) ) ;
571
589
}
572
590
}
591
+ return null ;
573
592
}
574
- return this . query = query ;
575
593
} ;
576
594
577
595
EditableController . prototype . rect = function ( ) {
@@ -847,7 +865,8 @@ KEY_CODE = {
847
865
UP : 38 ,
848
866
RIGHT : 39 ,
849
867
DOWN : 40 ,
850
- BACKSPACE : 8
868
+ BACKSPACE : 8 ,
869
+ SPACE : 32
851
870
} ;
852
871
853
872
DEFAULT_CALLBACKS = {
@@ -862,7 +881,7 @@ DEFAULT_CALLBACKS = {
862
881
}
863
882
_a = decodeURI ( "%C3%80" ) ;
864
883
_y = decodeURI ( "%C3%BF" ) ;
865
- regexp = new RegExp ( "" + flag + "([A-Za-z" + _a + "-" + _y + "0-9_\+\-]*)$|" + flag + "([^\\x00-\\xff]*)$" , 'gi' ) ;
884
+ regexp = new RegExp ( "" + flag + "([A-Za-z" + _a + "-" + _y + "0-9_\.\ +\-]*)$|" + flag + "([^\\x00-\\xff]*)$" , 'gi' ) ;
866
885
match = regexp . exec ( subtext ) ;
867
886
if ( match ) {
868
887
return match [ 2 ] || match [ 1 ] ;
@@ -900,9 +919,13 @@ DEFAULT_CALLBACKS = {
900
919
} ) ;
901
920
} ,
902
921
tplEval : function ( tpl , map ) {
903
- var error ;
922
+ var error , template ;
923
+ template = tpl ;
904
924
try {
905
- return tpl . replace ( / \$ \{ ( [ ^ \} ] * ) \} / g, function ( tag , key , pos ) {
925
+ if ( typeof tpl !== 'string' ) {
926
+ template = tpl ( map ) ;
927
+ }
928
+ return template . replace ( / \$ \{ ( [ ^ \} ] * ) \} / g, function ( tag , key , pos ) {
906
929
return map [ key ] ;
907
930
} ) ;
908
931
} catch ( _error ) {
@@ -988,7 +1011,9 @@ $.fn.atwho["default"] = {
988
1011
limit : 5 ,
989
1012
maxLen : 20 ,
990
1013
displayTimeout : 300 ,
991
- delay : null
1014
+ delay : null ,
1015
+ spaceSelectsMatch : false ,
1016
+ editableAtwhoQueryAttrs : { }
992
1017
} ;
993
1018
994
1019
0 commit comments