-
Notifications
You must be signed in to change notification settings - Fork 10
/
jqueryui-multisearch.js
1161 lines (908 loc) · 41.1 KB
/
jqueryui-multisearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Copyright (c) 2013 Ben Olson (https://github.com/bseth99/jqueryui-multisearch)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Dependencies: jQuery, jQuery UI (base widget and position plugin), Underscore (or Lodash)
*
*/
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
define(['jquery', 'underscore'], factory);
} else {
// Browser globals
root.$ = factory( root.$, root._ );
}
}(this, function ( $, _ ) { 'use strict';
var regexp_special_chars = new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g');
function regexp_escape(term) {
return term.replace(regexp_special_chars, '\\$&');
}
$.widget( 'osb.multisearch', {
optionData: null,
optionIndex: -1,
itemData: null,
itemIndex: -1,
localCache: null,
/**
* Available options for each widget instance
*/
options: {
/*
* The data source to search. Can be a string, function, or array of objects
*
* - A string should be a valid remote datasource.
*
* - A function can implement a data search and should call the passed in
* callback with the results:
*
* function ( term, callback ) { ... }
*
* - An array repesents a local dataset and all searches will be done locally
* on the contents.
*/
source: null,
/*
* Hash of options that are used in the $.ajax call on a remote resource.
* Only used when source is a string representing the path of the resource.
* Currently accepts overrides for dataType and method options. Also adds
* a custom options:
*
* - searchTerm: the parameter name that will contain the search term
* ( ie path/to/resource?term=abc )
*
* Defaults:
* searchTerm: 'term',
* dataType: 'json',
* method: 'GET'
*/
ajaxOptions: null,
/*
* An array of fields in the result object which represent the unique key for the object.
* Used to detect duplicate items. If not set, will default to ['id']
*/
keyAttrs: null,
/*
* An array of fields in the result object to search for the entered criteria.
* If not set, will default to ['name']
*/
searchAttrs: null,
/*
* A function that returns an HTML string repesenting the item to add to the suggestion picker
* as a search is entered into the input.. The hash returned by the remote server or local cache
* is passed to the function. This can be a Underscore template() function.
*/
formatPickerItem: function( data ) {
return '<li><a href="#">'+data.name+'</a></li>';
},
/*
* A function that returns an HTML string repesenting the item to add to the selected
* items list. Called each time a search term is selected from the suggestion picker or,
* when not found items are allowed, a new entry is completed. The hash from the result
* set is passed to the function. This can be a Underscore template() function.
*/
formatSelectedItem: function( data ) {
return '<a href="#">'+data.name+'</a>';
},
/*
* When adding items that are not found in the suggestion picker, this function is called
* to define the object that is expected in the formatSelectedItem() template. Generally,
* you'll leave the keyAttrs attributes null and set the primary display field with the
* text from the input box which is passed to the function.
*/
buildNewItem: function( text ) {
return { id: null, name: text };
},
/*
* How many characters need to be typed to trigger a search
*/
minSearchChars: 2,
/*
* How quickly can successive searches be triggered. The value is in milliseconds
* and uses Underscore's throttle() function to control triggering calls to the
* remote resource. This does not affect local cache searching.
*/
searchThrottle: 200,
/*
* How many results to show in the picker. Even if 200 are returned by the server, you
* can control how many are actually displayed in the suggestion picker. Set it to zero
* to show everything. Ensure you enable some kind of scrolling on the element you define
* as the picker/picker-list role if you allow longer lists of items.
*/
maxShowOptions: 5,
/*
* When to start refining a search against the local cache. Each remote search is saved by term
* if the remote result set has less than minLocalCache items in it, each subsequent character typed
* will use that result as a basis for searching. This can reduce the number of hits on the remote
* resource and can be fine-tuned to match your needs. Set it to zero to disable local refinements
* against the cache. If you do use it, make sure any limits on the server side are set high enough
* to ensure that a search term that refines the remote search below this threshold will contain all
* possible items that could be found if the subsequent searches were run against the server.
*/
minLocalCache: 50,
/*
* Can items not found in the suggestion picker be added to the seleced item list. If allowed,
* buildNewItem() will be called to allow setting defaults on the object that represents the
* search data to be prefilled before calling formatSelectedItem(). The adding and added events
* will have the notfound flag set to true when an item will be added that in not in the picker.
*/
preventNotFound: false,
/*
* Using the keyAttrs, should duplicates be prevented. A duplicate event is triggered if one is found
* allowing custom UI logic to be defined externally. Otherwise, nothing will happen. The suggestion
* picker will remain open and it will appear that the widget is not responding. Items that are not
* found in the picker but are added to the selected item list will not be considered a duplicate
* unless you provide custom logic during adding that would assign a key.
*/
preventDuplicates: true,
/*
* Automatically resize the input box to match the text. Helpful for inline/floating elements so
* the input only wraps as needed. Disable if using block elements that you want to fill the
* parent space.
*/
useAutoWidth: true,
/*
* Use jQueryUI.position plugin to position the picker box relative to the input control. The default is the
* basic drop-down menu box under the input entry box. Depending on the structure, you may want to adjust this
* setting.
*/
pickerPosition: {
my: 'left top',
at: 'left bottom',
},
/*
* Where is the input relative to the item list. Determines how the keyboard navigation
* moves through the items and where new items are added. Valid values are "start" and "end"
* defaults to "end". Depending on where you position the input box, adjust this setting
* so the UI interactions make sense.
*/
inputPosition: 'end',
/*
* This will force the user to manually have to choose an item from the dropdown list. Reason being is if there is
* 1 result that matches your query, it will always autocorrect the first result in the list instead of allowing you
* to add a new one with the current input
*/
ignoreAutocomplete: false,
/*
* For each field defined in searchAttrs, search for the input text using the function below. This is used
* for both local cache searches and hit highlighting. It should match with the search method from the remote.
* The default is to look for the search string anywhere in the target field. If you want to match only from
* the leading edge of the field, you'll need to override this function.
*/
localMatcher: function( needle, haystack ) { return haystack.toLowerCase().indexOf( needle.toLowerCase() ) > -1; }
},
/**
*
* API instance methods. These methods all suppress any events that would
* normally be triggered by user interaction.
*
***/
/*
* Getter/Setter for list of selected items. Use it to retreive the list of items
* entered by the user or seed the list with existing items (from a database, etc).
*
* Getting the value returns a shallow clone of the objects in the item list. If
* you're using nested objects from a shared dataset, be aware you may be referencing
* them in the returned set.
*
* Setting the value will destory the current selections. If you want more control,
* use add/remove to selectively update the list.
*
*/
value: function( items ) {
var self = this;
if ( items ) {
this.remove();
_.each( items, function( m ) { self.add( m ); });
} else {
return _.map( this.itemData, function( i ) { return _.clone( i ); });
}
},
/*
* Add an item to the selection list. Optional second arguement can be used to specify
* the position to insert the item. If the item already exists, it will be merged
* and trigger rendering the content of the item again such that updated data can be
* applied to the list.
*/
add: function( item, pos ) {
var options = { silent: true },
idx;
if ( typeof(pos) !== 'undefined' ) options.at = pos;
if ( ( idx = this._findByKeys( item ) ) > -1 ) {
this._removeItem( idx );
if ( typeof(options.at) === 'undefined' || options.at === null )
options.at = idx;
}
this._addItem( item, options );
},
/*
* Remove one or all items form the selected list:
*
* - No arguements will remove everything
* - Pass an Integer representing the ordinal index of the item to remove
* - Pass an Object containing the keys of the item to remove.
*/
remove: function( item ) {
var idx;
if ( arguments.length === 0 ) {
// Clear
this._getSelectedChildren().remove();
this.itemData = [];
this.itemIndex = -1;
} else if ( _.isObject( item ) ) {
// Lookup by keys
if ( ( idx = this._findByKeys( item ) ) > -1 )
this._removeItem( idx, { silent: true } );
} else {
// Number index
this._removeItem( item, { silent: true } );
}
},
/*
* Available events. Uses the standard jQuery UI interface for triggering events. You can either
* define a callback in the options hash or listen to the event by binding to multisearch + event name
* ie $.on( 'multisearchadding', ... ).
*
* Handler function will receive an event and ui argument. The ui object is defined below with each
* event that is triggered.
*
*
* - duplicate: trigger when preventDuplicates is true and a selected item from the picker matches
* an item already in the item list based on keyAttrs
*
* ui: {
* existing: Object representing the duplicate already present in the item list
* adding: Object representing the item that is attempting to be added
* }
*
*
* - adding: triggered before actually adding the item to the item list. Return false to
* prevent the the action. You can modify data to affect what is passed to the
* template function and retained in the item list.
*
* ui: {
* data: Object containing the selected item
* notfound: Flag indicating whether the item was found in the picker list
* }
*
*
* - added: once the item is added, this event is triggered with the data and element
*
* ui: {
* data: Object containing the selected item
* element: jQuery object of the newly added element representing the data in the UI
* }
*
*
* - removing: prior to removing the item, this event is triggered to allow canceling
* the operation by returning false
*
* ui: {
* data: Object containing the selected item
* }
*
*
* - removed: upon removing the item, this event is triggered
*
* ui: {
* data: Object containing the selected item
* }
*
*
* - searching: triggered before searching but immediately after displaying the picker
*
* ui: {
* term: String that will be used in the search
* picker: jQuery object representing the main picker container element
* }
*
*
* - searched: triggered after searching has completed and results have been returned by the source
*
* ui: {
* term: String that will be used in the search
* picker: jQuery object representing the main picker container element
* list: Array of objects returned by the search
* }
*
*
* - itemaction: triggered when an element with data-action is clicked in the item list
*
* ui: {
* data: Object containing the selected item
* element: jQuery object of the element representing the data in the UI
* }
*
*
* - itemselect: triggered when an item is clicked in the item list (no data-action defined)
*
* ui: {
* data: Object containing the selected item
* element: jQuery object of the element representing the data in the UI
* }
*
*
* Hover/Active events are triggered either when the user uses the mouse (hover/click) or
* uses the keyboard to navigate (arrows, space bar). A handler can return false to prevent
* the default action which simply changes classes on the elements. Each event provides the
* following ui object:
*
* ui: {
* target: A jQuery object representing the item receiving the event
* siblings: jQuery object of the siblings of the target filtering out any elements that
* do not have the data-role defined. Generally, you need to remove classes from
* the siblings when adding them to the target.
* }
*
* Here is the list of interaction events:
*
* - selectedactive: When a selected item is clicked or selected with the space bar
* - selectedhoverin: When mousing into an item or using the arrows to navigate
* - selectedhoverout: When leaving a selected item
* - pickerhoverin: When mouseover or arrows to a suggested item in the picker list
* - pickerhoverout: When leaving the item
*
*/
_findByKeys: function( item ) {
var keys = this.options.keyAttrs,
vals, find, idx = -1;
vals = _.values( _.pick( item, keys ) );
if ( vals.length == keys.length ) {
find = _.findWhere( this.itemData, _.object( keys, vals ) );
if ( find ) {
idx = _.indexOf( this.itemData, find );
}
}
return idx;
},
_create: function() {
var opt = this.options;
opt.keyAttrs = opt.keyAttrs || [ 'id' ];
opt.searchAttrs = opt.searchAttrs || [ 'name' ];
this.localCache = {};
this.optionData = [];
this.itemData = [];
this.search_text = '';
this.element.addClass( 'osb-multisearch' );
this.$input = this.element.find( '[data-role="input"]' ).attr({ 'autocomplete': 'off', 'autocapitalize': 'off', 'spellcheck': 'false' });
this.$picker = this.element.find( '[data-role="picker"]' ).css( 'position', 'absolute' ).hide();
this.pickerVisible = false;
this.$pickerList = this.element.find( '[data-role="picker-list"]' );
this.$itemList = this.element.find( '[data-role="selected-list"]' );
this.$input.on( 'keydown.multisearch', $.proxy( this, '_processInput' ) );
this.$picker.on( 'click.multisearch mouseenter.multisearch mouseleave.multisearch', '[data-role="picker-item"]', $.proxy( this, '_processPicker' ) );
this.$itemList.on( 'click.multisearch mouseenter.multisearch mouseleave keydown.multisearch', '[data-role="selected-item"]', $.proxy( this, '_processSelected' ) );
this._initAutoWidth();
this._initRemote();
var self = this;
$( document ).on( 'click.multisearch', function( event ) {
if ( self.element.has( event.target ).length === 0 )
self._hidePicker();
});
},
_initRemote: function() {
var self = this,
opt = this.options;
var cb = function( term, data ) {
self._trigger( 'searched', null, { term: term, picker: self.$picker, list: data } );
self.localCache[term] = data;
self.optionData = data.slice( 0, opt.maxShowOptions );
self._renderPickerItems();
}
if ( typeof( opt.source ) == 'string' ) {
opt.ajaxOptions =
opt.ajaxOptions || {
searchTerm: 'term',
dataType: 'json',
method: 'GET'
};
this._remoteSearch = _.throttle( function() {
if ( self.localCache[self.search_text] ) {
cb( self.search_text, self.localCache[self.search_text] );
} else {
if ( self._xhr )
self._xhr.abort();
self._xhr =
$.ajax({
url: opt.source,
data: opt.ajaxOptions.searchTerm+'='+self.search_text,
dataType: opt.ajaxOptions.dataType,
method: opt.ajaxOptions.method
}).done( _.partial( cb, self.search_text ) );
}
},
opt.searchThrottle,
{ leading: false }
);
} else if ( $.isFunction( opt.source ) ) {
this._remoteSearch = _.throttle( function() {
// Need to capture the text as of now..
if ( self.localCache[self.search_text] ) {
cb( self.search_text, self.localCache[self.search_text] );
} else {
opt.source.call( self, self.search_text, _.partial( cb, self.search_text ) );
}
},
opt.searchThrottle,
{ leading: false }
);
} else {
this._remoteSearch = function() {
var results = _.filter( opt.source, function ( item ) { return self._matcher.call( self, item ); });
self.optionData = results.slice( 0, opt.maxShowOptions );
self._renderPickerItems();
}
}
},
_initAutoWidth: function() {
if ( this.options.useAutoWidth ) {
this.$sizer = $( '<div></div>' )
.css({
position: 'absolute',
top: -9999,
left: -9999,
width: '100%',
fontSize: this.$input.css( 'font-size' ),
fontFamily: this.$input.css( 'font-family' ),
fontWeight: this.$input.css( 'font-weight' ),
letterSpacing: this.$input.css( 'letter-spacing' ),
paddingLeft: this.$input.css( 'padding-left' ),
paddingRight: this.$input.css( 'padding-right' ),
marginLeft: this.$input.css( 'margin-left' ),
marginRight: this.$input.css( 'margin-right' ),
whiteSpace: 'nowrap'
}).insertAfter( this.$input );
// The first call will initialize the rest since we need to be sure
// the elements are in the DOM.
this.$input.on( 'keyup', $.proxy( this, '_autoSizeInput' ) );
}
},
_destroy: function() {
this.element.removeClass( 'osb-multisearch' );
$( document ).off( '.multisearch' );
this.$input.off( '.multisearch' );
this.$picker.off( '.multisearch' );
this.$itemList.off( '.multisearch' );
this.$sizer.remove();
this.$pickerList.html('');
this._getSelectedChildren().remove();
this.$picker.show();
this.pickerVisible = true;
this._remoteSearch = null;
this.localCache = null;
this.optionData = null;
this.itemData = null;
},
_processInput: function ( event ) {
switch( event.keyCode ) {
case jQuery.ui.keyCode.UP:
case jQuery.ui.keyCode.DOWN:
case jQuery.ui.keyCode.LEFT:
case jQuery.ui.keyCode.RIGHT:
if( this.$input.val().length ) {
if ( event.keyCode === jQuery.ui.keyCode.DOWN || event.keyCode === jQuery.ui.keyCode.RIGHT ) {
if ( this.optionIndex < this.options.maxShowOptions - 1 )
this._overPickerItem( this._getPickerChildren().eq( ++this.optionIndex ) );
} else {
if ( this.optionIndex > 0 )
this._offPickerItem( this._getPickerChildren().eq( --this.optionIndex ) );
}
return false;
} else {
if ( event.keyCode === jQuery.ui.keyCode.DOWN || event.keyCode === jQuery.ui.keyCode.RIGHT ) {
this._gotoPrevItem();
} else {
this._gotoNextItem();
}
return false;
}
break;
case jQuery.ui.keyCode.SPACE:
if( !this.$input.val().length ) {
if ( this.itemIndex > -1 && this.itemIndex < this.itemData.length ) {
this._getSelectedChildren().eq( this.itemIndex ).trigger( 'click' );
} else {
this._clearSelectedItem();
}
return false;
}
break;
case jQuery.ui.keyCode.DELETE:
if( !this.$input.val().length ) {
if ( this.itemIndex > -1 && this.itemIndex < this.itemData.length ) {
var e = jQuery.Event( 'keydown' );
e.keyCode = jQuery.ui.keyCode.DELETE;
this._getSelectedChildren().eq( this.itemIndex ).trigger( e );
}
return false;
}
break;
case jQuery.ui.keyCode.BACKSPACE:
if( !this.$input.val().length ) {
// No more characters. Start deleting existing
// items that have been selected.
if ( this.options.inputPosition == 'end' ) {
if ( this.itemData.length > 0 ) {
if ( this.itemIndex > -1 && this.itemIndex < this.itemData.length ) {
this._removeSelectedItem();
} else {
this.itemIndex = this.itemData.length - 1;
this._getSelectedChildren().eq( this.itemIndex ).trigger( 'mouseenter' );
}
this._hidePicker();
}
}
return false;
} else if( this.$input.val().length === 1 ) {
// The last character is going to be
// deleted. Hide the picker.
this._hidePicker();
} else {
// New search string
_.defer( $.proxy( this, '_search' ) );
}
break;
case jQuery.ui.keyCode.TAB:
case jQuery.ui.keyCode.ENTER:
if ( this.search_text.length > 0) {
if( this.optionIndex > -1 && !this.options.ignoreAutocomplete && this.pickerVisible) {
this._addSelectedItem();
} else {
if ( !this.options.preventNotFound ) {
this._addItem( this.options.buildNewItem( this.search_text ) );
}
}
this._clearSelectedItem();
event.stopPropagation();
event.preventDefault();
return false;
}
break;
case jQuery.ui.keyCode.ESCAPE:
this._hidePicker();
this.optionIndex = -1;
return true;
default:
if( String.fromCharCode( event.which ) ) {
this._clearSelectedItem();
_.defer( $.proxy( this, '_search' ) );
}
break;
}
},
_processPicker: function( event ) {
var $currentTarget = $( event.currentTarget );
switch( event.type ) {
case 'click':
this.optionIndex = this._getPickerChildren().index( $currentTarget );
this._addSelectedItem();
this.$input.focus();
break;
case 'mouseenter':
this._overPickerItem( $currentTarget );
break;
case 'mouseleave':
this._offPickerItem( $currentTarget );
break;
}
},
_processSelected: function( event ) {
var $target = $( event.target ),
$currentTarget = $( event.currentTarget ),
action;
switch( event.type ) {
case 'click':
this.itemIndex = this._getSelectedChildren().index( $currentTarget );
this._activeSelectedItem( $currentTarget );
if ( !$target.is( '[data-action]' ) )
$target = $target.parents( '[data-action]' ).first();
if ( $target.length && $currentTarget.has( $target ).length ) {
action = $target.attr( 'data-action' );
if ( this._trigger( 'itemaction', null, { action: action, data: this.itemData[this.itemIndex], element: $currentTarget } ) ) {
switch ( action ) {
case 'remove':
var e = jQuery.Event( 'keydown' );
e.keyCode = jQuery.ui.keyCode.DELETE;
$currentTarget.trigger( e );
event.stopPropagation();
event.preventDefault();
return false;
}
}
} else {
this._trigger( 'itemselect', null, { data: this.itemData[this.itemIndex], element: $currentTarget } )
}
break;
case 'mouseenter':
this._overSelectedItem( $currentTarget );
break;
case 'mouseleave':
this._offSelectedItem( $currentTarget );
break;
case 'keydown':
switch( event.keyCode ) {
case jQuery.ui.keyCode.UP:
case jQuery.ui.keyCode.DOWN:
case jQuery.ui.keyCode.LEFT:
case jQuery.ui.keyCode.RIGHT:
if ( event.keyCode === jQuery.ui.keyCode.DOWN || event.keyCode === jQuery.ui.keyCode.RIGHT ) {
this._gotoPrevItem();
} else {
this._gotoNextItem();
}
return false;
case jQuery.ui.keyCode.SPACE:
if ( this.itemIndex > -1 && this.itemIndex < this.itemData.length ) {
this._getSelectedChildren().eq( this.itemIndex ).trigger( 'click' );
}
return false;
case jQuery.ui.keyCode.BACKSPACE:
case jQuery.ui.keyCode.DELETE:
if ( this.itemIndex > -1 && this.itemIndex < this.itemData.length ) {
this._removeSelectedItem();
this.$input.focus();
}
event.stopPropagation();
event.preventDefault();
return false;
}
break;
}
},
_addSelectedItem: function() {
var item = this.optionData[ this.optionIndex ];
this._addItem( _.clone( item ) );
this.optionIndex = -1;
},
_addItem: function( item, options ) {
var opt = options || {},
where = this.options.inputPosition,
addOk = true,
notFound = false,
silent = opt.silent || false,
at = opt.at === null || typeof( opt.at ) == 'undefined' ? ( where == 'start' ? 0 : this.itemData.length ) : opt.at,
keys = this.options.keyAttrs,
idx, $el;
if ( item ) {
notFound = !_.values( _.pick( item, keys ) ).join('').length;
if ( !notFound && this.options.preventDuplicates ) {
addOk = ( idx = this._findByKeys( item ) ) == -1;
if ( !addOk )
this._trigger( 'duplicate', null, { existing: this.itemData[idx], adding: item } );
}
if ( addOk ) {
if ( silent || this._trigger( 'adding', null, { data: item, notfound: notFound } ) ) {
$el = $( this.options.formatSelectedItem( item ) ).attr({ 'data-role': 'selected-item', 'tabIndex': -1 });
if ( at == this.itemData.length ) {
if ( this.$itemList.has( this.$input ).length > 0 ) {
if ( where == 'start' )
$el.insertAfter( this.$input );
else
$el.insertBefore( this.$input );
} else {
this.$itemList.append( $el );
}
this.itemData.push( item );
} else {
$el.insertBefore( this._getSelectedChildren().eq( at ) );
this.itemData.splice( at, 0, item );
}
this.$input.val('');
this.search_text = '';
this._hidePicker();
if ( this.itemIndex == -1 || this.itemIndex == this.itemData.length - 1 ) this._gotoFirstItem();
else if ( at <= this.itemIndex ) this.itemIndex++;
if ( !silent )
this._trigger( 'added', null, { data: item, element: $el, notfound: notFound } );
}
}
}
},
_removeSelectedItem: function() {
this._removeItem( this.itemIndex );
},
_removeItem: function( idx, options ) {
var opt = options || {},
silent = opt.silent || false,
item = this.itemData[ idx ];
if ( item ) {
if ( silent || this._trigger( 'removing', null, { data: item } ) ) {
this._getSelectedChildren().eq( idx ).remove();
this.itemData.splice( idx, 1 );
if ( this.itemData.length > 0 ) {
if ( idx < this.itemIndex )
this.itemIndex--;
if ( this.itemIndex > -1 && this.itemIndex < this.itemData.length ) {
this._overSelectedItem( this._getSelectedChildren().eq( this.itemIndex ) );
}
}
if ( !silent )
this._trigger( 'removed', null, { data: item } );
}
}
},
_getSelectedChildren: function() {
return this.$itemList.children( '[data-role="selected-item"]' );
},
_clearSelectedItem: function() {
this.$itemList.children().removeClass( 'active hover' );
this._gotoFirstItem();
},
_activeSelectedItem: function( $target ) {
// Allow externally managed styling, otherwise, fallback to defaults.
if ( this._trigger( 'selectedactive', null, { target: $target, siblings: $target.siblings( '[data-role="selected-item"]' ) } ) ) {
$target.addClass( 'active' ).siblings( '[data-role="selected-item"]' ).removeClass( 'active' );
}
},
_overSelectedItem: function( $target ) {
if ( this._trigger( 'selectedhoverin', null, { target: $target, siblings: $target.siblings( '[data-role="selected-item"]' ) } ) ) {
$target.addClass( 'hover' ).siblings( '[data-role="selected-item"]' ).removeClass( 'hover' );
}
},
_offSelectedItem: function( $target ) {
if ( this._trigger( 'selectedhoverout', null, { target: $target, siblings: $target.siblings( '[data-role="selected-item"]' ) } ) ) {
$target.removeClass( 'hover' );
if ( this.itemIndex > -1 )
this._overSelectedItem( this._getSelectedChildren().eq( this.itemIndex ) );
}
},
_gotoPrevItem: function() {
if ( this.itemIndex < this.itemData.length - 1 ) {
this.$itemList.children().not( this.$input ).eq( ++this.itemIndex ).trigger( 'mouseenter' );
} else if ( this.itemIndex == this.itemData.length - 1 ) {
this.itemIndex++;
this.$itemList.children().removeClass( 'hover' );
}
},
_gotoNextItem: function() {
if ( this.itemIndex > 0 ) {
this.$itemList.children().not( this.$input ).eq( --this.itemIndex ).trigger( 'mouseenter' );
} else if ( this.itemIndex === 0 ) {
this.itemIndex--;
this.$itemList.children().removeClass( 'hover' );
}
},
_gotoFirstItem: function() {
// Relative to input's position
if ( this.options.inputPosition == 'start' )
this.itemIndex = -1;
else
this.itemIndex = this.itemData.length;
},
_renderPickerItems: function() {
var self = this;