Skip to content

Commit eeee68a

Browse files
committed
Updated selectize.js to latest version.
1 parent 3448eeb commit eeee68a

File tree

1 file changed

+93
-67
lines changed

1 file changed

+93
-67
lines changed

js/selectize.js

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

33
(function(factory) {
44
if (typeof exports === 'object') {
@@ -251,6 +251,41 @@
251251
return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
252252
};
253253

254+
var hook = {};
255+
256+
/**
257+
* Wraps `method` on `self` so that `fn`
258+
* is invoked before the original method.
259+
*
260+
* @param {object} self
261+
* @param {string} method
262+
* @param {function} fn
263+
*/
264+
hook.before = function(self, method, fn) {
265+
var original = self[method];
266+
self[method] = function() {
267+
fn.apply(self, arguments);
268+
return original.apply(self, arguments);
269+
};
270+
};
271+
272+
/**
273+
* Wraps `method` on `self` so that `fn`
274+
* is invoked after the original method.
275+
*
276+
* @param {object} self
277+
* @param {string} method
278+
* @param {function} fn
279+
*/
280+
hook.after = function(self, method, fn) {
281+
var original = self[method];
282+
self[method] = function() {
283+
var result = original.apply(self, arguments);
284+
fn.apply(self, arguments);
285+
return result;
286+
};
287+
};
288+
254289
var once = function(fn) {
255290
var called = false;
256291
return function() {
@@ -605,13 +640,6 @@
605640
self.isCmdDown = e[IS_MAC ? 'metaKey' : 'ctrlKey'];
606641
self.isCtrlDown = e[IS_MAC ? 'altKey' : 'ctrlKey'];
607642
self.isShiftDown = e.shiftKey;
608-
if (self.isFocused && !self.isLocked) {
609-
var tagName = (e.target.tagName || '').toLowerCase();
610-
if (tagName === 'input' || tagName === 'textarea') return;
611-
if ([KEY_SHIFT, KEY_BACKSPACE, KEY_DELETE, KEY_ESC, KEY_LEFT, KEY_RIGHT, KEY_TAB].indexOf(e.keyCode) !== -1) {
612-
return self.onKeyDown.apply(self, arguments);
613-
}
614-
}
615643
},
616644
keyup: function(e) {
617645
if (e.keyCode === KEY_CTRL) self.isCtrlDown = false;
@@ -752,7 +780,7 @@
752780
this.blur();
753781
return;
754782
case KEY_DOWN:
755-
if (!this.isOpen && this.hasOptions && this.isInputFocused) {
783+
if (!this.isOpen && this.hasOptions) {
756784
this.open();
757785
} else if (this.$activeOption) {
758786
var $next = this.getAdjacentOption(this.$activeOption, 1);
@@ -850,8 +878,8 @@
850878

851879
this.showInput();
852880
this.setActiveItem(null);
853-
this.$control.addClass('focus');
854881
this.refreshOptions(!!this.settings.openOnFocus);
882+
this.refreshClasses();
855883
};
856884

857885
/**
@@ -868,10 +896,8 @@
868896
this.setTextboxValue('');
869897
this.setActiveOption(null);
870898
this.setCaret(this.items.length);
871-
if (!this.$activeItems.length) {
872-
this.$control.removeClass('focus');
873-
this.isFocused = false;
874-
}
899+
this.isFocused = false;
900+
this.refreshClasses();
875901
};
876902

877903
/**
@@ -1709,18 +1735,15 @@
17091735
if (!this.settings.persist && this.userOptions.hasOwnProperty(value)) {
17101736
this.removeOption(value);
17111737
}
1712-
this.setCaret(i);
1713-
this.refreshOptions(false);
1714-
this.refreshClasses();
17151738

1716-
if (!this.hasOptions) { this.close(); }
1717-
else if (this.isInputFocused) { this.open(); }
1739+
if (i < this.caretPos) {
1740+
this.setCaret(this.caretPos - 1);
1741+
}
17181742

1743+
this.refreshClasses();
17191744
this.updatePlaceholder();
1720-
if (!this.items.length) this.showInput();
1721-
1722-
this.positionDropdown();
17231745
this.updateOriginalInput();
1746+
this.positionDropdown();
17241747
this.trigger('item_remove', value);
17251748
}
17261749
};
@@ -1791,6 +1814,7 @@
17911814
var isFull = this.isFull();
17921815
var isLocked = this.isLocked;
17931816
this.$control
1817+
.toggleClass('focus', this.isFocused)
17941818
.toggleClass('disabled', this.isDisabled)
17951819
.toggleClass('locked', isLocked)
17961820
.toggleClass('full', isFull).toggleClass('not-full', !isFull)
@@ -1856,10 +1880,12 @@
18561880
*/
18571881
Selectize.prototype.open = function() {
18581882
if (this.isLocked || this.isOpen || (this.settings.mode === 'multi' && this.isFull())) return;
1883+
this.focus();
18591884
this.isOpen = true;
1860-
this.positionDropdown();
1885+
this.$dropdown.css({visibility: 'hidden', display: 'block'});
18611886
this.$control.addClass('dropdown-active');
1862-
this.$dropdown.show();
1887+
this.positionDropdown();
1888+
this.$dropdown.css({visibility: 'visible'});
18631889
this.trigger('dropdown_open', this.$dropdown);
18641890
};
18651891

@@ -1940,8 +1966,8 @@
19401966

19411967
if (this.$activeItems.length) {
19421968
$tail = this.$control.children('.active:' + (direction > 0 ? 'last' : 'first'));
1943-
caret = Array.prototype.indexOf.apply(this.$control[0].childNodes, [$tail[0]]);
1944-
if (this.$activeItems.length > 1 && direction > 0) { caret--; }
1969+
caret = this.$control.children(':not(input)').index($tail);
1970+
if (direction > 0) { caret++; }
19451971

19461972
for (i = 0, n = this.$activeItems.length; i < n; i++) {
19471973
values.push($(this.$activeItems[i]).attr('data-value'));
@@ -1950,7 +1976,7 @@
19501976
e.preventDefault();
19511977
e.stopPropagation();
19521978
}
1953-
} else if ((this.isInputFocused || this.settings.mode === 'single') && this.items.length) {
1979+
} else if ((this.isFocused || this.settings.mode === 'single') && this.items.length) {
19541980
if (direction < 0 && selection.start === 0 && selection.length === 0) {
19551981
values.push(this.items[this.caretPos - 1]);
19561982
} else if (direction > 0 && selection.start === this.$control_input.val().length) {
@@ -1964,13 +1990,15 @@
19641990
}
19651991

19661992
// perform removal
1967-
while (values.length) {
1968-
this.removeItem(values.pop());
1969-
}
19701993
if (typeof caret !== 'undefined') {
19711994
this.setCaret(caret);
1972-
this.showInput();
19731995
}
1996+
while (values.length) {
1997+
this.removeItem(values.pop());
1998+
}
1999+
2000+
this.showInput();
2001+
this.refreshOptions(true);
19742002
return true;
19752003
};
19762004

@@ -1985,7 +2013,7 @@
19852013
* @param {object} e (optional)
19862014
*/
19872015
Selectize.prototype.advanceSelection = function(direction, e) {
1988-
var tail, selection, idx, valueLength, cursorAtEdge, $tail, $items;
2016+
var tail, selection, idx, valueLength, cursorAtEdge, $tail;
19892017

19902018
if (direction === 0) return;
19912019

@@ -2004,8 +2032,7 @@
20042032
} else {
20052033
$tail = this.$control.children('.active:' + tail);
20062034
if ($tail.length) {
2007-
$items = this.$control.children(':not(input)');
2008-
idx = Array.prototype.indexOf.apply($items, [$tail[0]]);
2035+
idx = this.$control.children(':not(input)').index($tail);
20092036
this.setActiveItem(null);
20102037
this.setCaret(direction > 0 ? idx + 1 : idx);
20112038
this.showInput();
@@ -2380,39 +2407,6 @@
23802407
return index >= 0 && index < $options.length ? $options.eq(index) : $();
23812408
};
23822409

2383-
if (options.equalizeHeight || options.equalizeWidth) {
2384-
this.refreshOptions = (function() {
2385-
var original = self.refreshOptions;
2386-
return function() {
2387-
var i, n, height_max, width, width_last, width_parent, $optgroups;
2388-
original.apply(self, arguments);
2389-
2390-
$optgroups = $('[data-group]', self.$dropdown_content);
2391-
n = $optgroups.length;
2392-
if (!n) return;
2393-
2394-
if (options.equalizeHeight) {
2395-
height_max = 0;
2396-
for (i = 0; i < n; i++) {
2397-
height_max = Math.max(height_max, $optgroups.eq(i).height());
2398-
}
2399-
$optgroups.css({height: height_max});
2400-
}
2401-
2402-
if (options.equalizeWidth) {
2403-
width_parent = this.$dropdown_content.innerWidth();
2404-
width = Math.round(width_parent / n);
2405-
$optgroups.css({width: width});
2406-
if (n > 1) {
2407-
width_last = width_parent - width * (n - 1);
2408-
$optgroups.eq(n - 1).css({width: width_last});
2409-
}
2410-
}
2411-
2412-
};
2413-
})();
2414-
}
2415-
24162410
this.onKeyDown = (function() {
24172411
var original = self.onKeyDown;
24182412
return function(e) {
@@ -2440,6 +2434,38 @@
24402434
};
24412435
})();
24422436

2437+
var equalizeSizes = function() {
2438+
var i, n, height_max, width, width_last, width_parent, $optgroups;
2439+
2440+
$optgroups = $('[data-group]', self.$dropdown_content);
2441+
n = $optgroups.length;
2442+
if (!n || !self.$dropdown_content.width()) return;
2443+
2444+
if (options.equalizeHeight) {
2445+
height_max = 0;
2446+
for (i = 0; i < n; i++) {
2447+
height_max = Math.max(height_max, $optgroups.eq(i).height());
2448+
}
2449+
$optgroups.css({height: height_max});
2450+
}
2451+
2452+
if (options.equalizeWidth) {
2453+
width_parent = self.$dropdown_content.innerWidth();
2454+
width = Math.round(width_parent / n);
2455+
$optgroups.css({width: width});
2456+
if (n > 1) {
2457+
width_last = width_parent - width * (n - 1);
2458+
$optgroups.eq(n - 1).css({width: width_last});
2459+
}
2460+
}
2461+
};
2462+
2463+
if (options.equalizeHeight || options.equalizeWidth) {
2464+
hook.after(this, 'positionDropdown', equalizeSizes);
2465+
hook.after(this, 'refreshOptions', equalizeSizes);
2466+
}
2467+
2468+
24432469
});
24442470
})();
24452471

0 commit comments

Comments
 (0)