@@ -19,6 +19,8 @@ var DEFAULT_SETTINGS = {
19
19
propertyToSearch : "name" ,
20
20
jsonContainer : null ,
21
21
contentType : "json" ,
22
+ excludeCurrent : false ,
23
+ excludeCurrentParam : "x" ,
22
24
23
25
// Prepopulation settings
24
26
prePopulate : null ,
@@ -839,8 +841,38 @@ $.TokenList = function (input, url_or_data, settings) {
839
841
return template . replace ( new RegExp ( "(?![^&;]+;)(?!<[^<>]*)(" + regexp_escape ( value ) + ")(?![^<>]*>)(?![^&;]+;)" , "g" ) , highlight_term ( value , term ) ) ;
840
842
}
841
843
844
+ // exclude existing tokens from dropdown, so the list is clearer
845
+ function excludeCurrent ( results ) {
846
+ // if enabled, remove existing tokens
847
+ if ( $ ( input ) . data ( "settings" ) . excludeCurrent ) {
848
+ var currentTokens = $ ( input ) . data ( "tokenInputObject" ) . getTokens ( ) ,
849
+ trimmedList = [ ] ;
850
+ if ( currentTokens . length > 0 ) {
851
+ $ . each ( results , function ( index , value ) {
852
+ var notFound = true ;
853
+ $ . each ( currentTokens , function ( cIndex , cValue ) {
854
+ if ( value [ $ ( input ) . data ( "settings" ) . propertyToSearch ] == cValue [ $ ( input ) . data ( "settings" ) . propertyToSearch ] ) {
855
+ notFound = false ;
856
+ return false ;
857
+ }
858
+ } ) ;
859
+
860
+ if ( notFound ) {
861
+ trimmedList . push ( value ) ;
862
+ }
863
+ } ) ;
864
+ results = trimmedList ;
865
+ }
866
+ }
867
+
868
+ return results ;
869
+ }
870
+
842
871
// Populate the results dropdown with some results
843
872
function populate_dropdown ( query , results ) {
873
+ // exclude current tokens if configured
874
+ results = excludeCurrent ( results ) ;
875
+
844
876
if ( results && results . length ) {
845
877
dropdown . empty ( ) ;
846
878
var dropdown_ul = $ ( "<ul/>" )
@@ -939,7 +971,7 @@ $.TokenList = function (input, url_or_data, settings) {
939
971
function run_search ( query ) {
940
972
var cache_key = query + computeURL ( ) ;
941
973
var cached_results = cache . get ( cache_key ) ;
942
- if ( cached_results ) {
974
+ if ( cached_results ) {
943
975
if ( $ . isFunction ( $ ( input ) . data ( "settings" ) . onCachedResult ) ) {
944
976
cached_results = $ ( input ) . data ( "settings" ) . onCachedResult . call ( hidden_input , cached_results ) ;
945
977
}
@@ -948,7 +980,7 @@ $.TokenList = function (input, url_or_data, settings) {
948
980
// Are we doing an ajax search or local data search?
949
981
if ( $ ( input ) . data ( "settings" ) . url ) {
950
982
var url = computeURL ( ) ;
951
- // Extract exisiting get params
983
+ // Extract existing get params
952
984
var ajax_params = { } ;
953
985
ajax_params . data = { } ;
954
986
if ( url . indexOf ( "?" ) > - 1 ) {
@@ -968,10 +1000,24 @@ $.TokenList = function (input, url_or_data, settings) {
968
1000
ajax_params . data [ $ ( input ) . data ( "settings" ) . queryParam ] = query ;
969
1001
ajax_params . type = $ ( input ) . data ( "settings" ) . method ;
970
1002
ajax_params . dataType = $ ( input ) . data ( "settings" ) . contentType ;
971
- if ( $ ( input ) . data ( "settings" ) . crossDomain ) {
1003
+ if ( $ ( input ) . data ( "settings" ) . crossDomain ) {
972
1004
ajax_params . dataType = "jsonp" ;
973
1005
}
974
1006
1007
+ // exclude current tokens?
1008
+ // send exclude list to the server, so it can also exclude existing tokens
1009
+ if ( $ ( input ) . data ( "settings" ) . excludeCurrent ) {
1010
+ var currentTokens = $ ( input ) . data ( "tokenInputObject" ) . getTokens ( ) ;
1011
+ var tokenList = $ . map ( currentTokens , function ( el ) {
1012
+ if ( typeof $ ( input ) . data ( "settings" ) . tokenValue == 'function' )
1013
+ return $ ( input ) . data ( "settings" ) . tokenValue . call ( this , el ) ;
1014
+
1015
+ return el [ $ ( input ) . data ( "settings" ) . tokenValue ] ;
1016
+ } ) ;
1017
+
1018
+ ajax_params . data [ $ ( input ) . data ( "settings" ) . excludeCurrentParam ] = tokenList . join ( $ ( input ) . data ( "settings" ) . tokenDelimiter ) ;
1019
+ }
1020
+
975
1021
// Attach the success callback
976
1022
ajax_params . success = function ( results ) {
977
1023
cache . add ( cache_key , $ ( input ) . data ( "settings" ) . jsonContainer ? results [ $ ( input ) . data ( "settings" ) . jsonContainer ] : results ) ;
0 commit comments