Skip to content

Commit b564875

Browse files
committed
Updated selectize.js to latest version.
1 parent 37ecd9f commit b564875

File tree

1 file changed

+121
-34
lines changed

1 file changed

+121
-34
lines changed

js/selectize.js

Lines changed: 121 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! selectize.js - v0.2.4 | https://github.com/brianreavis/selectize.js | Apache License (v2) */
1+
/*! selectize.js - v0.3.0 | https://github.com/brianreavis/selectize.js | Apache License (v2) */
22

33
(function(factory) {
44
if (typeof exports === 'object') {
@@ -81,6 +81,7 @@
8181

8282
var IS_MAC = /Mac/.test(navigator.userAgent);
8383

84+
var KEY_A = 65;
8485
var KEY_COMMA = 188;
8586
var KEY_RETURN = 13;
8687
var KEY_ESC = 27;
@@ -316,12 +317,14 @@
316317

317318
this.highlightedValue = null;
318319
this.isOpen = false;
320+
this.isDisabled = false;
319321
this.isLocked = false;
320322
this.isFocused = false;
321323
this.isInputFocused = false;
322324
this.isInputHidden = false;
323325
this.isSetup = false;
324326
this.isShiftDown = false;
327+
this.isCmdDown = false;
325328
this.isCtrlDown = false;
326329
this.ignoreFocus = false;
327330
this.hasOptions = false;
@@ -430,6 +433,7 @@
430433

431434
$(document).on({
432435
keydown: function(e) {
436+
self.isCmdDown = e[IS_MAC ? 'metaKey' : 'ctrlKey'];
433437
self.isCtrlDown = e[IS_MAC ? 'altKey' : 'ctrlKey'];
434438
self.isShiftDown = e.shiftKey;
435439
if (self.isFocused && !self.isLocked) {
@@ -484,6 +488,10 @@
484488
this.updatePlaceholder();
485489
this.isSetup = true;
486490

491+
if (this.$input.is(':disabled')) {
492+
this.disable();
493+
}
494+
487495
// preload options
488496
if (this.settings.preload) {
489497
this.onSearchChange('');
@@ -538,6 +546,13 @@
538546
}
539547

540548
switch (keyCode) {
549+
case KEY_A:
550+
if (this.isCmdDown) {
551+
this.selectAll();
552+
e.preventDefault();
553+
return;
554+
}
555+
break;
541556
case KEY_ESC:
542557
this.blur();
543558
return;
@@ -549,41 +564,40 @@
549564
if ($next.length) this.setActiveOption($next, true, true);
550565
}
551566
e.preventDefault();
552-
break;
567+
return;
553568
case KEY_UP:
554569
if (this.$activeOption) {
555570
var $prev = this.$activeOption.prev();
556571
if ($prev.length) this.setActiveOption($prev, true, true);
557572
}
558573
e.preventDefault();
559-
break;
574+
return;
560575
case KEY_RETURN:
561576
if (this.$activeOption) {
562577
this.onOptionSelect({currentTarget: this.$activeOption});
563578
}
564579
e.preventDefault();
565-
break;
580+
return;
566581
case KEY_LEFT:
567582
this.advanceSelection(-1, e);
568-
break;
583+
return;
569584
case KEY_RIGHT:
570585
this.advanceSelection(1, e);
571-
break;
586+
return;
572587
case KEY_TAB:
573588
if (this.settings.create && $.trim(this.$control_input.val()).length) {
574589
this.createItem();
575590
e.preventDefault();
576591
}
577-
break;
592+
return;
578593
case KEY_BACKSPACE:
579594
case KEY_DELETE:
580595
this.deleteSelection(e);
581-
break;
582-
default:
583-
if (this.isFull() || this.isInputHidden) {
584-
e.preventDefault();
585-
return;
586-
}
596+
return;
597+
}
598+
if (this.isFull() || this.isInputHidden) {
599+
e.preventDefault();
600+
return;
587601
}
588602
};
589603

@@ -613,24 +627,14 @@
613627
* @param {string} value
614628
*/
615629
Selectize.prototype.onSearchChange = function(value) {
616-
if (!this.settings.load) return;
617-
if (this.loadedSearches.hasOwnProperty(value)) return;
618630
var self = this;
619-
var $wrapper = this.$wrapper.addClass('loading');
620-
621-
this.loading++;
622-
this.loadedSearches[value] = true;
623-
this.settings.load.apply(this, [value, function(results) {
624-
self.loading = Math.max(self.loading - 1, 0);
625-
if (results && results.length) {
626-
self.addOption(results);
627-
self.refreshOptions(false);
628-
if (self.isInputFocused) self.open();
629-
}
630-
if (!self.loading) {
631-
$wrapper.removeClass('loading');
632-
}
633-
}]);
631+
var fn = self.settings.load;
632+
if (!fn) return;
633+
if (self.loadedSearches.hasOwnProperty(value)) return;
634+
self.loadedSearches[value] = true;
635+
self.load(function(callback) {
636+
fn.apply(self, [value, callback]);
637+
});
634638
};
635639

636640
/**
@@ -642,6 +646,11 @@
642646
Selectize.prototype.onFocus = function(e) {
643647
this.isInputFocused = true;
644648
this.isFocused = true;
649+
if (this.isDisabled) {
650+
this.blur();
651+
e.preventDefault();
652+
return false;
653+
}
645654
if (this.ignoreFocus) return;
646655

647656
this.showInput();
@@ -723,6 +732,32 @@
723732
}
724733
};
725734

735+
/**
736+
* Invokes the provided method that provides
737+
* results to a callback---which are then added
738+
* as options to the control.
739+
*
740+
* @param {function} fn
741+
*/
742+
Selectize.prototype.load = function(fn) {
743+
var self = this;
744+
var $wrapper = self.$wrapper.addClass('loading');
745+
746+
self.loading++;
747+
fn.apply(self, [function(results) {
748+
self.loading = Math.max(self.loading - 1, 0);
749+
if (results && results.length) {
750+
self.addOption(results);
751+
self.refreshOptions(false);
752+
if (self.isInputFocused) self.open();
753+
}
754+
if (!self.loading) {
755+
$wrapper.removeClass('loading');
756+
}
757+
self.trigger('onLoad', results);
758+
}]);
759+
};
760+
726761
/**
727762
* Sets the input field of the control to the specified value.
728763
*
@@ -859,11 +894,21 @@
859894
}
860895
};
861896

897+
/**
898+
* Selects all items (CTRL + A).
899+
*/
900+
Selectize.prototype.selectAll = function() {
901+
this.$activeItems = Array.prototype.slice.apply(this.$control.children(':not(input)').addClass('active'));
902+
this.isFocused = true;
903+
if (this.$activeItems.length) this.hideInput();
904+
};
905+
862906
/**
863907
* Hides the input element out of view, while
864908
* retaining its focus.
865909
*/
866910
Selectize.prototype.hideInput = function() {
911+
this.close();
867912
this.setTextboxValue('');
868913
this.$control_input.css({opacity: 0, position: 'absolute', left: -10000});
869914
this.isInputHidden = true;
@@ -885,6 +930,7 @@
885930
* @param {boolean} trigger
886931
*/
887932
Selectize.prototype.focus = function(trigger) {
933+
if (this.isDisabled) return;
888934
var self = this;
889935
self.ignoreFocus = true;
890936
self.$control_input[0].focus();
@@ -1198,6 +1244,11 @@
11981244
* the options list dropdown (use `refreshOptions`
11991245
* for that).
12001246
*
1247+
* Usage:
1248+
*
1249+
* this.addOption(value, data)
1250+
* this.addOption(data)
1251+
*
12011252
* @param {string} value
12021253
* @param {object} data
12031254
*/
@@ -1244,7 +1295,7 @@
12441295
};
12451296

12461297
/**
1247-
* Removes an option.
1298+
* Removes a single option.
12481299
*
12491300
* @param {string} value
12501301
*/
@@ -1254,6 +1305,19 @@
12541305
delete this.options[value];
12551306
this.lastQuery = null;
12561307
this.trigger('onOptionRemove', value);
1308+
this.removeItem(value);
1309+
};
1310+
1311+
/**
1312+
* Clears all options.
1313+
*/
1314+
Selectize.prototype.clearOptions = function() {
1315+
this.loadedSearches = {};
1316+
this.userOptions = {};
1317+
this.options = {};
1318+
this.lastQuery = null;
1319+
this.trigger('onOptionClear');
1320+
this.clear();
12571321
};
12581322

12591323
/**
@@ -1456,9 +1520,11 @@
14561520
Selectize.prototype.refreshClasses = function() {
14571521
var isFull = this.isFull();
14581522
var isLocked = this.isLocked;
1459-
this.$control.toggleClass('locked', isLocked);
1460-
this.$control.toggleClass('full', isFull).toggleClass('not-full', !isFull);
1461-
this.$control.toggleClass('has-items', this.items.length > 0);
1523+
this.$control
1524+
.toggleClass('disabled', this.isDisabled)
1525+
.toggleClass('locked', isLocked)
1526+
.toggleClass('full', isFull).toggleClass('not-full', !isFull)
1527+
.toggleClass('has-items', this.items.length > 0);
14621528
this.$control_input.data('grow', !isFull && !isLocked);
14631529
};
14641530

@@ -1534,6 +1600,7 @@
15341600
if (!this.isOpen) return;
15351601
this.$dropdown.hide();
15361602
this.$control.removeClass('dropdown-active');
1603+
this.setActiveOption(null);
15371604
this.isOpen = false;
15381605
this.trigger('onDropdownClose', this.$dropdown);
15391606
};
@@ -1566,6 +1633,7 @@
15661633
this.updatePlaceholder();
15671634
this.updateOriginalInput();
15681635
this.refreshClasses();
1636+
this.showInput();
15691637
this.trigger('onClear');
15701638
};
15711639

@@ -1741,6 +1809,24 @@
17411809
this.refreshClasses();
17421810
};
17431811

1812+
/**
1813+
* Disables user input on the control completely.
1814+
* While disabled, it cannot receive focus.
1815+
*/
1816+
Selectize.prototype.disable = function() {
1817+
this.isDisabled = true;
1818+
this.lock();
1819+
};
1820+
1821+
/**
1822+
* Enables the control so that it can respond
1823+
* to focus and user input.
1824+
*/
1825+
Selectize.prototype.enable = function() {
1826+
this.isDisabled = false;
1827+
this.unlock();
1828+
};
1829+
17441830
/**
17451831
* A helper method for rendering "item" and
17461832
* "option" templates, given the data.
@@ -1833,6 +1919,7 @@
18331919
onClear : null, // function() { ... }
18341920
onOptionAdd : null, // function(value, data) { ... }
18351921
onOptionRemove : null, // function(value) { ... }
1922+
onOptionClear : null, // function() { ... }
18361923
onDropdownOpen : null, // function($dropdown) { ... }
18371924
onDropdownClose : null, // function($dropdown) { ... }
18381925
onType : null, // function(str) { ... }

0 commit comments

Comments
 (0)