@@ -20,7 +20,7 @@ var DEFAULT_SETTINGS = {
20
20
jsonContainer : null ,
21
21
contentType : "json" ,
22
22
23
- // Prepopulation settings
23
+ // Prepopulation settings
24
24
prePopulate : null ,
25
25
processPrePopulate : false ,
26
26
@@ -51,7 +51,10 @@ var DEFAULT_SETTINGS = {
51
51
idPrefix : "token-input-" ,
52
52
53
53
// Keep track if the input is currently in disabled mode
54
- disabled : false
54
+ disabled : false ,
55
+
56
+ // Set to true if you need to enter new tokens (not only from backend)
57
+ allowNewItems : false
55
58
} ;
56
59
57
60
// Default classes to use when theming
@@ -248,8 +251,8 @@ $.TokenList = function (input, url_or_data, settings) {
248
251
if ( dropdown_item . length ) {
249
252
select_dropdown_item ( dropdown_item ) ;
250
253
}
254
+ return false ;
251
255
}
252
- return false ;
253
256
break ;
254
257
255
258
case KEY . BACKSPACE :
@@ -276,11 +279,21 @@ $.TokenList = function (input, url_or_data, settings) {
276
279
case KEY . ENTER :
277
280
case KEY . NUMPAD_ENTER :
278
281
case KEY . COMMA :
279
- if ( selected_dropdown_item ) {
282
+ if ( selected_dropdown_item && $ ( selected_dropdown_item ) . data ( "tokeninput" ) != undefined ) {
280
283
add_token ( $ ( selected_dropdown_item ) . data ( "tokeninput" ) ) ;
281
284
hidden_input . change ( ) ;
282
285
return false ;
286
+ } else if ( $ ( this ) . val ( ) . length && settings . allowNewItems ) {
287
+ token_object = { }
288
+ token_object [ settings . propertyToSearch ] = token_object [ settings . tokenValue ] = $ ( this ) . val ( )
289
+ add_token ( token_object ) ;
290
+ return false ;
291
+ } else if ( event . keyCode !== KEY . TAB ) {
292
+ // Prevent commas or accidental form submissions
293
+ // via enter while keeping tabbing intact
294
+ return false ;
283
295
}
296
+
284
297
break ;
285
298
286
299
case KEY . ESCAPE :
@@ -531,7 +544,10 @@ $.TokenList = function (input, url_or_data, settings) {
531
544
token_list . children ( ) . each ( function ( ) {
532
545
var existing_token = $ ( this ) ;
533
546
var existing_data = $ . data ( existing_token . get ( 0 ) , "tokeninput" ) ;
534
- if ( existing_data && existing_data . id === item . id ) {
547
+ if ( existing_data && existing_data . id && existing_data . id === item . id ) {
548
+ found_existing_token = existing_token ;
549
+ return false ;
550
+ } else if ( existing_data && existing_data . name . toLowerCase ( ) === item . name . toLowerCase ( ) ) {
535
551
found_existing_token = existing_token ;
536
552
return false ;
537
553
}
@@ -751,6 +767,11 @@ $.TokenList = function (input, url_or_data, settings) {
751
767
dropdown . html ( "<p>" + settings . noResultsText + "</p>" ) ;
752
768
show_dropdown ( ) ;
753
769
}
770
+ // If we're allowing new items hide the dropdown results
771
+ // when there's no matching results
772
+ if ( settings . allowNewItems ) {
773
+ dropdown . empty ( ) ;
774
+ }
754
775
}
755
776
}
756
777
@@ -775,7 +796,7 @@ $.TokenList = function (input, url_or_data, settings) {
775
796
// Do a search and show the "searching" dropdown if the input is longer
776
797
// than settings.minChars
777
798
function do_search ( ) {
778
- var query = input_box . val ( ) ;
799
+ var query = input_box . val ( ) . toLowerCase ( ) ;
779
800
780
801
if ( query && query . length ) {
781
802
if ( selected_token ) {
@@ -909,4 +930,4 @@ $.TokenList.Cache = function (options) {
909
930
return data [ query ] ;
910
931
} ;
911
932
} ;
912
- } ( jQuery ) ) ;
933
+ } ( jQuery ) ) ;
0 commit comments