11/*
22 * jQuery Plugin: Tokenizing Autocomplete Text Entry
3- * Version 1.6.0
3+ * Version 1.6.1
44 *
55 * Copyright (c) 2009 James Smith (http://loopj.com)
66 * Licensed jointly under the GPL and MIT licenses,
@@ -30,6 +30,7 @@ var DEFAULT_SETTINGS = {
3030 searchingText : "Searching..." ,
3131 deleteText : "×" ,
3232 animateDropdown : true ,
33+ placeholder : null ,
3334 theme : null ,
3435 zindex : 999 ,
3536 resultsLimit : null ,
@@ -54,6 +55,7 @@ var DEFAULT_SETTINGS = {
5455
5556 // Behavioral settings
5657 allowFreeTagging : false ,
58+ allowTabOut : false ,
5759
5860 // Callbacks
5961 onResult : null ,
@@ -167,7 +169,7 @@ var methods = {
167169 $ ( this ) . data ( "settings" , $ . extend ( { } , $ ( this ) . data ( "settings" ) , options || { } ) ) ;
168170 return this ;
169171 }
170- }
172+ } ;
171173
172174// Expose the .tokenInput function to jQuery as a plugin
173175$ . fn . tokenInput = function ( method ) {
@@ -251,14 +253,12 @@ $.TokenList = function (input, url_or_data, settings) {
251253 } )
252254 . blur ( function ( ) {
253255 hide_dropdown ( ) ;
254- $ ( this ) . val ( "" ) ;
255- token_list . removeClass ( $ ( input ) . data ( "settings" ) . classes . focused ) ;
256-
256+
257257 if ( $ ( input ) . data ( "settings" ) . allowFreeTagging ) {
258258 add_freetagging_tokens ( ) ;
259- } else {
260- $ ( this ) . val ( "" ) ;
261259 }
260+
261+ $ ( this ) . val ( "" ) ;
262262 token_list . removeClass ( $ ( input ) . data ( "settings" ) . classes . focused ) ;
263263 } )
264264 . bind ( "keyup keydown blur update" , resize_input )
@@ -334,9 +334,16 @@ $.TokenList = function (input, url_or_data, settings) {
334334 hidden_input . change ( ) ;
335335 } else {
336336 if ( $ ( input ) . data ( "settings" ) . allowFreeTagging ) {
337- add_freetagging_tokens ( ) ;
337+ if ( $ ( input ) . data ( "settings" ) . allowTabOut && $ ( this ) . val ( ) === "" ) {
338+ return true ;
339+ } else {
340+ add_freetagging_tokens ( ) ;
341+ }
338342 } else {
339343 $ ( this ) . val ( "" ) ;
344+ if ( $ ( input ) . data ( "settings" ) . allowTabOut ) {
345+ return true ;
346+ }
340347 }
341348 event . stopPropagation ( ) ;
342349 event . preventDefault ( ) ;
@@ -356,6 +363,10 @@ $.TokenList = function (input, url_or_data, settings) {
356363 }
357364 } ) ;
358365
366+ // Keep reference for placeholder
367+ if ( settings . placeholder )
368+ input_box . attr ( "placeholder" , settings . placeholder )
369+
359370 // Keep a reference to the original input box
360371 var hidden_input = $ ( input )
361372 . hide ( )
@@ -440,6 +451,7 @@ $.TokenList = function (input, url_or_data, settings) {
440451 $ . each ( li_data , function ( index , value ) {
441452 insert_token ( value ) ;
442453 checkTokenLimit ( ) ;
454+ input_box . attr ( "placeholder" , null )
443455 } ) ;
444456 }
445457
@@ -463,11 +475,11 @@ $.TokenList = function (input, url_or_data, settings) {
463475 delete_token ( $ ( this ) ) ;
464476 }
465477 } ) ;
466- }
478+ } ;
467479
468480 this . add = function ( item ) {
469481 add_token ( item ) ;
470- }
482+ } ;
471483
472484 this . remove = function ( item ) {
473485 token_list . children ( "li" ) . each ( function ( ) {
@@ -485,15 +497,18 @@ $.TokenList = function (input, url_or_data, settings) {
485497 }
486498 }
487499 } ) ;
488- }
500+ } ;
489501
490502 this . getTokens = function ( ) {
491503 return saved_tokens ;
492- }
504+ } ;
493505
494506 this . toggleDisabled = function ( disable ) {
495507 toggleDisabled ( disable ) ;
496- }
508+ } ;
509+
510+ // Resize input to maximum width so the placeholder can be seen
511+ resize_input ( ) ;
497512
498513 //
499514 // Private functions
@@ -531,9 +546,13 @@ $.TokenList = function (input, url_or_data, settings) {
531546 function resize_input ( ) {
532547 if ( input_val === ( input_val = input_box . val ( ) ) ) { return ; }
533548
549+ // Get width left on the current line
550+ var width_left = token_list . width ( ) - input_box . offset ( ) . left - token_list . offset ( ) . left ;
534551 // Enter new content into resizer and resize input accordingly
535552 input_resizer . html ( _escapeHTML ( input_val ) ) ;
536- input_box . width ( input_resizer . width ( ) + 30 ) ;
553+ // Get maximum width, minimum the size of input and maximum the widget's width
554+ input_box . width ( Math . min ( token_list . width ( ) ,
555+ Math . max ( width_left , input_resizer . width ( ) + 30 ) ) ) ;
537556 }
538557
539558 function is_printable_character ( keycode ) {
@@ -615,7 +634,7 @@ $.TokenList = function (input, url_or_data, settings) {
615634 token_list . children ( ) . each ( function ( ) {
616635 var existing_token = $ ( this ) ;
617636 var existing_data = $ . data ( existing_token . get ( 0 ) , "tokeninput" ) ;
618- if ( existing_data && existing_data . id === item . id ) {
637+ if ( existing_data && existing_data [ settings . tokenValue ] === item [ settings . tokenValue ] ) {
619638 found_existing_token = existing_token ;
620639 return false ;
621640 }
@@ -629,9 +648,14 @@ $.TokenList = function (input, url_or_data, settings) {
629648 }
630649 }
631650
651+ // Squeeze input_box so we force no unnecessary line break
652+ input_box . width ( 0 ) ;
653+
632654 // Insert the new tokens
633655 if ( $ ( input ) . data ( "settings" ) . tokenLimit == null || token_count < $ ( input ) . data ( "settings" ) . tokenLimit ) {
634656 insert_token ( item ) ;
657+ // Remove the placeholder so it's not seen after you've added a token
658+ input_box . attr ( "placeholder" , null )
635659 checkTokenLimit ( ) ;
636660 }
637661
@@ -714,6 +738,9 @@ $.TokenList = function (input, url_or_data, settings) {
714738
715739 // Remove this token from the saved list
716740 saved_tokens = saved_tokens . slice ( 0 , index ) . concat ( saved_tokens . slice ( index + 1 ) ) ;
741+ if ( saved_tokens . length == 0 ) {
742+ input_box . attr ( "placeholder" , settings . placeholder )
743+ }
717744 if ( index < selected_token_index ) selected_token_index -- ;
718745
719746 // Update the hidden input
@@ -756,7 +783,7 @@ $.TokenList = function (input, url_or_data, settings) {
756783 dropdown
757784 . css ( {
758785 position : "absolute" ,
759- top : $ ( token_list ) . offset ( ) . top + $ ( token_list ) . outerHeight ( ) ,
786+ top : $ ( token_list ) . offset ( ) . top + $ ( token_list ) . height ( ) ,
760787 left : $ ( token_list ) . offset ( ) . left ,
761788 width : $ ( token_list ) . width ( ) ,
762789 'z-index' : $ ( input ) . data ( "settings" ) . zindex
@@ -1013,3 +1040,4 @@ $.TokenList.Cache = function (options) {
10131040 } ;
10141041} ;
10151042} ( jQuery ) ) ;
1043+
0 commit comments