forked from HemantNegi/jquery.sumoselect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.sumoselect.js
864 lines (726 loc) · 29.7 KB
/
jquery.sumoselect.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
/*!
* jquery.sumoselect - v3.0.3
* http://hemantnegi.github.io/jquery.sumoselect
* 2016-12-12
*
* Copyright 2015 Hemant Negi
* Email : [email protected]
* Compressor http://refresh-sf.com/
*/
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory(require('jquery'));
} else {
factory(jQuery);
}
})(function ($) {
'namespace sumo';
$.fn.SumoSelect = function (options) {
// This is the easiest way to have default options.
var defaultOptions = {
placeholder: 'Select Here', // Dont change it here.
csvDispCount: 3, // display no. of items in multiselect. 0 to display all.
captionFormat: '{0} Selected', // format of caption text. you can set your locale.
captionFormatAllSelected: '{0} all selected!', // format of caption text when all elements are selected. set null to use captionFormat. It will not work if there are disabled elements in select.
floatWidth: 400, // Screen width of device at which the list is rendered in floating popup fashion.
forceCustomRendering: false, // force the custom modal on all devices below floatWidth resolution.
nativeOnDevice: ['Android', 'BlackBerry', 'iPhone', 'iPad', 'iPod', 'Opera Mini', 'IEMobile', 'Silk'], //
outputAsCSV: false, // true to POST data as csv ( false for Html control array ie. default select )
csvSepChar: ',', // separation char in csv mode
okCancelInMulti: false, // display ok cancel buttons in desktop mode multiselect also.
isClickAwayOk: false, // for okCancelInMulti=true. sets whether click outside will trigger Ok or Cancel (default is cancel).
triggerChangeCombined: true, // im multi select mode whether to trigger change event on individual selection or combined selection.
selectAll: false, // to display select all button in multiselect mode.|| also select all will not be available on mobile devices.
search: false, // to display input for filtering content. selectAlltext will be input text placeholder
searchText: 'Search...', // placeholder for search input
searchFn: function (haystack, needle) { // search function
return haystack.toLowerCase().indexOf(needle.toLowerCase()) < 0;
},
noMatch: 'No matches for "{0}"',
prefix: '', // some prefix usually the field name. eg. '<b>Hello</b>'
locale: ['OK', 'Cancel', 'Select All'], // all text that is used. don't change the index.
up: false, // set true to open upside.
showTitle: true, // set to false to prevent title (tooltip) from appearing
max: null // Maximum number of options selected (when multiple)
};
var ret = this.each(function () {
var selObj = this; // the original select object.
if (this.sumo || !$(this).is('select')) return; //already initialized
var settings = $.extend({}, defaultOptions, options, $(this).data());
this.sumo = {
E: $(selObj), //the jquery object of original select element.
is_multi: $(selObj).attr('multiple'), //if its a multiple select
select: '',
caption: '',
placeholder: '',
optDiv: '',
CaptionCont: '',
ul: '',
is_floating: false,
is_opened: false,
//backdrop: '',
mob: false, // if to open device default select
Pstate: [],
lastUnselected: null,
selectedCount: 0,
createElems: function () {
var O = this;
O.E.wrap('<div class="SumoSelect" tabindex="0" role="button" aria-expanded="false">');
O.select = O.E.parent();
O.caption = $('<span>');
O.CaptionCont = $(`<p class="CaptionCont SelectBox ${O.E.attr('class')}" ><label><i></i></label></p>`)
.attr('style', O.E.attr('style'))
.prepend(O.caption);
O.select.append(O.CaptionCont);
// default turn off if no multiselect
if (!O.is_multi) settings.okCancelInMulti = false;
if (O.E.attr('disabled'))
O.select.addClass('disabled').removeAttr('tabindex');
//if output as csv and is a multiselect.
if (settings.outputAsCSV && O.is_multi && O.E.attr('name')) {
//create a hidden field to store csv value.
O.select.append($('<input class="HEMANT123" type="hidden" />').attr('name', O.E.attr('name')).val(O.getSelStr()));
// so it can not post the original select.
O.E.removeAttr('name');
}
//break for mobile rendring.. if forceCustomRendering is false
if (O.isMobile() && !settings.forceCustomRendering) {
O.setNativeMobile();
return;
}
// if there is a name attr in select add a class to container div
if (O.E.attr('name')) O.select.addClass('sumo_' + O.E.attr('name').replace(/\[\]/, ''));
//hide original select
O.E.addClass('SumoUnder').attr('tabindex', '-1');
//## Creating the list...
O.optDiv = $('<div class="optWrapper ' + (settings.up ? 'up' : '') + '">');
//branch for floating list in low res devices.
O.floatingList();
//Creating the markup for the available options
O.ul = $('<ul class="options">');
O.optDiv.append(O.ul);
// Select all functionality
if (settings.selectAll && O.is_multi && !settings.max) O.SelAll();
// search functionality
if (settings.search) O.Search();
O.ul.append(O.prepItems(O.E.children()));
//if multiple then add the class multiple and add OK / CANCEL button
if (O.is_multi) O.multiSelelect();
O.select.append(O.optDiv);
O._handleMax();
O.basicEvents();
O.selAllState();
},
prepItems: function (opts, d) {
var lis = [], O = this;
$(opts).each(function (i, opt) { // parsing options to li
opt = $(opt);
lis.push(opt.is('optgroup') ?
$('<li class="group ' + (opt[0].disabled ? 'disabled' : '') + '"><label></label><ul></ul></li>')
.find('label')
.text(opt.attr('label'))
.end()
.find('ul')
.append(O.prepItems(opt.children(), opt[0].disabled))
.end()
:
O.createLi(opt, d)
);
});
return lis;
},
//## Creates a LI element from a given option and binds events to it
//## returns the jquery instance of li (not inserted in dom)
createLi: function (opt, d) {
var O = this;
if (!opt.attr('value')) opt.attr('value', opt.val());
var li = $('<li class="opt"><label>' + opt.html() + '</label></li>');
li.data('opt', opt); // store a direct reference to option.
opt.data('li', li); // store a direct reference to list item.
if (O.is_multi) li.prepend('<span><i></i></span>');
if (opt[0].disabled || d)
li = li.addClass('disabled');
O.onOptClick(li);
if (opt[0].selected) {
li.addClass('selected');
O.selectedCount++;
}
if (opt.attr('class'))
li.addClass(opt.attr('class'));
if (opt.attr('title'))
li.attr('title', opt.attr('title'));
return li;
},
//## Returns the selected items as string in a Multiselect.
getSelStr: function () {
// get the pre selected items.
var sopt = [];
this.E.find('option:checked').each(function () { sopt.push($(this).val()); });
return sopt.join(settings.csvSepChar);
},
//## THOSE OK/CANCEL BUTTONS ON MULTIPLE SELECT.
multiSelelect: function () {
var O = this;
O.optDiv.addClass('multiple');
O.okbtn = $('<p tabindex="0" class="btnOk"></p>').click(function () {
//if combined change event is set.
O._okbtn();
O.hideOpts();
});
O.okbtn[0].innerText = settings.locale[0];
O.cancelBtn = $('<p tabindex="0" class="btnCancel"></p>').click(function () {
O._cnbtn();
O.hideOpts();
});
O.cancelBtn[0].innerText = settings.locale[1];
var btns = O.okbtn.add(O.cancelBtn);
O.optDiv.append($('<div class="MultiControls">').append(btns));
// handling keyboard navigation on ok cancel buttons.
btns.on('keydown.sumo', function (e) {
var el = $(this);
switch (e.which) {
case 32: // space
case 13: // enter
el.trigger('click');
break;
case 9: //tab
if (el.hasClass('btnOk')) return;
break;
case 27: // esc
O._cnbtn();
O.hideOpts();
return;
}
e.stopPropagation();
e.preventDefault();
});
},
_okbtn: function () {
var O = this, cg = 0;
//if combined change event is set.
if (settings.triggerChangeCombined) {
//check for a change in the selection.
if (O.E.find('option:checked').length !== O.Pstate.length) {
cg = 1;
}
else {
O.E.find('option').each(function (i, e) {
if (e.selected && O.Pstate.indexOf(i) < 0) cg = 1;
});
}
if (cg) {
O.callChange();
O.setText();
}
}
},
_cnbtn: function () {
var O = this;
//remove all selections
O.E.find('option:checked').each(function () { this.selected = false; });
O.optDiv.find('li.selected').removeClass('selected');
//restore selections from saved state.
for (var i = 0; i < O.Pstate.length; i++) {
O.E.find('option')[O.Pstate[i]].selected = true;
O.ul.find('li.opt').eq(O.Pstate[i]).addClass('selected');
}
O.selAllState();
},
_handleMax: function () {
// Disable options if max reached
if(settings.max){
if (this.selectedCount >= +settings.max) {
this.optDiv.find('li.opt').not('.hidden').each(function (ix, e) {
if (!$(e).hasClass('selected')) {
$(e).addClass('temporary-disabled disabled');
}
});
} else {
// Enable options back
this.optDiv.find('li.opt').not('.hidden').each(function (ix, e) {
if ($(e).hasClass('temporary-disabled')) {
$(e).removeClass('temporary-disabled disabled');
}
});
}
}
},
SelAll: function () {
var O = this;
if (!O.is_multi) return;
O.selAll = $('<p class="select-all"><span><i></i></span><label></label></p>');
O.selAll.find('label')[0].innerText = settings.locale[2];
O.optDiv.addClass('selall');
O.selAll.on('click', function () {
O.selAll.toggleClass('selected');
O.toggSelAll(O.selAll.hasClass('selected'), 1);
//O.selAllState();
});
O.optDiv.prepend(O.selAll);
},
// search module (can be removed if not required.)
Search: function () {
var O = this,
cc = O.CaptionCont.addClass('search'),
P = $('<p class="no-match">'),
fn = (options.searchFn && typeof options.searchFn == 'function') ? options.searchFn : settings.searchFn;
O.ftxt = $('<input type="text" class="search-txt" value="">')
.on('click', function (e) {
e.stopPropagation();
});
O.ftxt.placeholder = settings.searchText;
cc.append(O.ftxt);
O.optDiv.children('ul').after(P);
O.ftxt.on('keyup.sumo', function () {
var hid = O.optDiv.find('ul.options li.opt').each(function (ix, e) {
var el = $(e),
opt = el.data('opt')[0];
opt.hidden = fn(el.text(), O.ftxt.val());
el.toggleClass('hidden', opt.hidden);
}).not('.hidden');
P.html(settings.noMatch.replace(/\{0\}/g, '<em></em>')).toggle(!hid.length);
P.find('em').text(O.ftxt.val());
O.selAllState();
});
},
selAllState: function () {
var O = this;
if (settings.selectAll && O.is_multi) {
var sc = 0, vc = 0;
O.optDiv.find('li.opt').not('.hidden .disabled').each(function (ix, e) {
if ($(e).hasClass('selected')) sc++;
vc++;
});
//select all checkbox state change.
if (sc === vc) O.selAll.removeClass('partial').addClass('selected');
else if (sc === 0) O.selAll.removeClass('selected partial');
else O.selAll.addClass('partial');//.removeClass('selected');
}
},
showOpts: function () {
var O = this;
if (O.E.attr('disabled')) return; // if select is disabled then retrun
O.E.trigger('sumo:opening', O);
O.is_opened = true;
O.select.addClass('open').attr('aria-expanded', 'true');
O.E.trigger('sumo:opened', O);
if (O.ftxt) O.ftxt.focus();
else O.select.focus();
// hide options on click outside.
$(document).on('click.sumo', function (e) {
if (!O.select.is(e.target) // if the target of the click isn't the container...
&& O.select.has(e.target).length === 0) { // ... nor a descendant of the container
if (!O.is_opened) return;
O.hideOpts();
if (settings.okCancelInMulti) {
if (settings.isClickAwayOk)
O._okbtn();
else
O._cnbtn();
}
}
});
if (O.is_floating) {
var H = O.optDiv.children('ul').outerHeight() + 2; // +2 is clear fix
if (O.is_multi) H = H + parseInt(O.optDiv.css('padding-bottom'));
O.optDiv.css('height', H);
$('body').addClass('sumoStopScroll');
}
O.setPstate();
},
//maintain state when ok/cancel buttons are available storing the indexes.
setPstate: function () {
var O = this;
if (O.is_multi && (O.is_floating || settings.okCancelInMulti)) {
O.Pstate = [];
// assuming that find returns elements in tree order
O.E.find('option').each(function (i, e) { if (e.selected) O.Pstate.push(i); });
}
},
callChange: function () {
this.E.get().forEach(e => {
e.dispatchEvent(new Event('change'));
e.dispatchEvent(new Event('click'));
});
},
hideOpts: function () {
var O = this;
if (O.is_opened) {
O.E.trigger('sumo:closing', O);
O.is_opened = false;
O.select.removeClass('open').attr('aria-expanded', 'false').find('ul li.sel').removeClass('sel');
O.E.trigger('sumo:closed', O);
$(document).off('click.sumo');
$('body').removeClass('sumoStopScroll');
// clear the search
if (settings.search) {
O.ftxt.val('');
O.ftxt.trigger('keyup.sumo');
}
}
},
setOnOpen: function () {
var O = this,
li = O.optDiv.find('li.opt:not(.hidden)').eq(settings.search ? 0 : O.E[0].selectedIndex);
if (li.hasClass('disabled')) {
li = li.next(':not(disabled)');
if (!li.length) return;
}
O.optDiv.find('li.sel').removeClass('sel');
li.addClass('sel');
O.showOpts();
},
nav: function (up) {
var O = this, c,
s = O.ul.find('li.opt:not(.disabled, .hidden)'),
sel = O.ul.find('li.opt.sel:not(.hidden)'),
idx = s.index(sel);
if (O.is_opened && sel.length) {
if (up && idx > 0)
c = s.eq(idx - 1);
else if (!up && idx < s.length - 1 && idx > -1)
c = s.eq(idx + 1);
else return; // if no items before or after
sel.removeClass('sel');
sel = c.addClass('sel');
// setting sel item to visible view.
var ul = O.ul,
st = ul.scrollTop(),
t = sel.position().top + st;
if (t >= st + ul.height() - sel.outerHeight())
ul.scrollTop(t - ul.height() + sel.outerHeight());
if (t < st)
ul.scrollTop(t);
}
else
O.setOnOpen();
},
basicEvents: function () {
var O = this;
O.CaptionCont.click(function (evt) {
O.E.trigger('click');
if (O.is_opened) O.hideOpts(); else O.showOpts();
evt.stopPropagation();
});
O.select.on('keydown.sumo', function (e) {
switch (e.which) {
case 38: // up
O.nav(true);
break;
case 40: // down
O.nav(false);
break;
case 65: // shortcut ctrl + a to select all and ctrl + shift + a to unselect all.
if (O.is_multi && !settings.max && e.ctrlKey) {
O.toggSelAll(!e.shiftKey, 1);
break;
}
else
return;
case 32: // space
if (settings.search && O.ftxt.is(e.target)) return;
break;
case 13: // enter
if (O.is_opened)
O.optDiv.find('ul li.sel').trigger('click');
else
O.setOnOpen();
break;
case 9: //tab
if (!settings.okCancelInMulti)
O.hideOpts();
return;
case 27: // esc
if (settings.okCancelInMulti) O._cnbtn();
O.hideOpts();
return;
default:
return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
$(window).on('resize.sumo', function () {
O.floatingList();
});
},
onOptClick: function (li) {
var O = this;
li.click(function () {
var li = $(this);
if (li.hasClass('disabled')) return;
if (O.is_multi) {
li.toggleClass('selected');
li.data('opt')[0].selected = li.hasClass('selected');
if (li.data('opt')[0].selected === false) {
O.lastUnselected = li.data('opt')[0].textContent;
O.selectedCount--;
} else {
O.selectedCount++;
}
if (settings.max) {
O._handleMax();
}
O.selAllState();
}
else {
li.parent().find('li.selected').removeClass('selected'); //if not multiselect then remove all selections from this list
li.toggleClass('selected');
li.data('opt')[0].selected = true;
O.selectedCount = 1;
}
//branch for combined change event.
if (!(O.is_multi && settings.triggerChangeCombined && (O.is_floating || settings.okCancelInMulti))) {
O.setText();
O.callChange();
}
if (!O.is_multi) O.hideOpts(); //if its not a multiselect then hide on single select.
});
},
// fixed some variables that were not explicitly typed (michc)
setText: function () {
var O = this;
O.placeholder = "";
if (O.is_multi) {
var sels = O.E.find(':checked').not(':disabled'); //selected options.
for (var i = 0; i < sels.length; i++) {
if (i + 1 >= settings.csvDispCount && settings.csvDispCount) {
if (sels.length === O.E.find('option').length && settings.captionFormatAllSelected) {
O.placeholder = settings.captionFormatAllSelected.replace(/\{0\}/g, sels.length) + ',';
} else {
O.placeholder = settings.captionFormat.replace(/\{0\}/g, sels.length) + ',';
}
break;
}
else O.placeholder += $(sels[i]).text() + ", ";
}
O.placeholder = O.placeholder.replace(/,([^,]*)$/, '$1'); //remove unexpected "," from last.
}
else {
O.placeholder = O.E.find(':checked').not(':disabled').text();
}
var is_placeholder = false;
if (!O.placeholder) {
is_placeholder = true;
O.placeholder = O.E.attr('placeholder');
if (!O.placeholder) //if placeholder is there then set it
O.placeholder = O.E.find('option:disabled:checked').text();
}
O.placeholder = O.placeholder ? (settings.prefix + ' ' + O.placeholder) : settings.placeholder;
//set display text
O.caption.text(O.placeholder);
if (settings.showTitle) O.CaptionCont.attr('title', O.placeholder);
//set the hidden field if post as csv is true.
var csvField = O.select.find('input.HEMANT123');
if (csvField.length) csvField.val(O.getSelStr());
//add class placeholder if its a placeholder text.
if (is_placeholder) O.caption.addClass('placeholder'); else O.caption.removeClass('placeholder');
return O.placeholder;
},
isMobile: function () {
// Adapted from http://www.detectmobilebrowsers.com
var ua = navigator.userAgent || navigator.vendor || window.opera;
// Checks for iOs, Android, Blackberry, Opera Mini, and Windows mobile devices
for (var i = 0; i < settings.nativeOnDevice.length; i++) if (ua.toString().toLowerCase().indexOf(settings.nativeOnDevice[i].toLowerCase()) > 0) return settings.nativeOnDevice[i];
return false;
},
setNativeMobile: function () {
var O = this;
O.E.addClass('SelectClass');//.css('height', O.select.outerHeight());
O.mob = true;
O.E.change(function () {
O.setText();
});
},
floatingList: function () {
var O = this;
//called on init and also on resize.
//O.is_floating = true if window width is < specified float width
O.is_floating = $(window).width() <= settings.floatWidth;
//set class isFloating
O.optDiv.toggleClass('isFloating', O.is_floating);
//remove height if not floating
if (!O.is_floating) O.optDiv.css('height', '');
//toggle class according to okCancelInMulti flag only when it is not floating
O.optDiv.toggleClass('okCancelInMulti', settings.okCancelInMulti && !O.is_floating);
},
//HELPERS FOR OUTSIDERS
// validates range of given item operations
vRange: function (i) {
var O = this;
var opts = O.E.find('option');
if (opts.length <= i || i < 0) throw "index out of bounds";
return O;
},
//toggles selection on c as boolean.
toggSel: function (c, i) {
var O = this;
var opt;
if (typeof (i) === "number") {
O.vRange(i);
opt = O.E.find('option')[i];
}
else {
opt = O.E.find('option[value="' + i + '"]')[0] || 0;
}
if (!opt || opt.disabled)
return;
if (opt.selected !== c) {
if ((settings.max && !opt.selected && O.selectedCount < settings.max) || opt.selected || (!settings.max && !opt.selected)) {
opt.selected = c;
if (!O.mob) $(opt).data('li').toggleClass('selected', c);
O.callChange();
O.setPstate();
O.setText();
O.selAllState();
}
}
},
//toggles disabled on c as boolean.
toggDis: function (c, i) {
var O = this.vRange(i);
O.E.find('option')[i].disabled = c;
if (c) O.E.find('option')[i].selected = false;
if (!O.mob) O.optDiv.find('ul.options li').eq(i).toggleClass('disabled', c).removeClass('selected');
O.setText();
},
// toggle disable/enable on complete select control
toggSumo: function (val) {
var O = this;
O.enabled = val;
O.select.toggleClass('disabled', val);
if (val) {
O.E.attr('disabled', 'disabled');
O.select.removeAttr('tabindex');
}
else {
O.E.removeAttr('disabled');
O.select.attr('tabindex', '0');
}
return O;
},
// toggles all option on c as boolean.
// set direct=false/0 bypasses okCancelInMulti behaviour.
toggSelAll: function (c, direct) {
var O = this;
O.E.find('option:not(:disabled,:hidden)')
.each(function (ix, e) {
var is_selected = e.selected,
el = $(e).data('li');
if (el.hasClass('hidden')) return;
if (!!c) {
if (!is_selected) el.trigger('click');
}
else {
if (is_selected) el.trigger('click');
}
});
if (!direct) {
if (!O.mob && O.selAll) O.selAll.removeClass('partial').toggleClass('selected', !!c);
O.callChange();
O.setText();
O.setPstate();
}
},
/* outside accessibility options
which can be accessed from the element instance.
*/
reload: function () {
var elm = this.unload();
return $(elm).SumoSelect(settings);
},
unload: function () {
var O = this;
O.select.before(O.E);
O.E.show();
if (settings.outputAsCSV && O.is_multi && O.select.find('input.HEMANT123').length) {
O.E.attr('name', O.select.find('input.HEMANT123').attr('name')); // restore the name;
}
O.select.remove();
delete selObj.sumo;
return selObj;
},
//## add a new option to select at a given index.
add: function (val, txt, i, attr) {
if (typeof val === "undefined") throw "No value to add";
var O = this;
var opts = O.E.find('option');
if (typeof txt === "number") { i = txt; txt = val; }
if (typeof txt === "undefined") { txt = val; }
var opt = $("<option></option>").val(val).html(txt);
if (attr && typeof attr == "object") {
$.each(attr, function (i, v) {
opt.attr(i, v);
});
}
if (opts.length < i) throw "index out of bounds";
if (typeof i === "undefined" || opts.length === i) { // add it to the last if given index is last no or no index provides.
O.E.append(opt);
if (!O.mob) O.ul.append(O.createLi(opt));
}
else {
opts.eq(i).before(opt);
if (!O.mob) O.ul.find('li.opt').eq(i).before(O.createLi(opt));
}
return selObj;
},
//## removes an item at a given index.
remove: function (i) {
var O = this.vRange(i);
O.E.find('option').eq(i).remove();
if (!O.mob) O.optDiv.find('ul.options li').eq(i).remove();
O.setText();
},
// removes all but the selected one
removeAll: function () {
var O = this;
var options = O.E.find('option');
for (var x = (options.length - 1); x >= 0; x--) {
if (options[x].selected !== true) {
O.remove(x);
}
}
},
find: function (val) {
var O = this;
var options = O.E.find('option');
for (var x in options) {
if (options[x].value === val) {
return parseInt(x);
}
}
return -1;
},
//## Select an item at a given index.
selectItem: function (i) { this.toggSel(true, i); },
//## UnSelect an iten at a given index.
unSelectItem: function (i) { this.toggSel(false, i); },
//## Select all items of the select.
selectAll: function () { this.toggSelAll(true); },
//## UnSelect all items of the select.
unSelectAll: function () { this.toggSelAll(false); },
//## Disable an iten at a given index.
disableItem: function (i) { this.toggDis(true, i); },
//## Removes disabled an iten at a given index.
enableItem: function (i) { this.toggDis(false, i); },
//## New simple methods as getter and setter are not working fine in ie8-
//## variable to check state of control if enabled or disabled.
enabled: true,
//## Enables the control
enable: function () { return this.toggSumo(false); },
//## Disables the control
disable: function () { return this.toggSumo(true); },
init: function () {
var O = this;
O.createElems();
O.setText();
return O;
}
};
selObj.sumo.init();
});
return ret.length === 1 ? ret[0] : ret;
};
});