|
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) */ |
2 | 2 |
|
3 | 3 | (function(factory) {
|
4 | 4 | if (typeof exports === 'object') {
|
|
251 | 251 | return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
|
252 | 252 | };
|
253 | 253 |
|
| 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 | + |
254 | 289 | var once = function(fn) {
|
255 | 290 | var called = false;
|
256 | 291 | return function() {
|
|
605 | 640 | self.isCmdDown = e[IS_MAC ? 'metaKey' : 'ctrlKey'];
|
606 | 641 | self.isCtrlDown = e[IS_MAC ? 'altKey' : 'ctrlKey'];
|
607 | 642 | 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 |
| - } |
615 | 643 | },
|
616 | 644 | keyup: function(e) {
|
617 | 645 | if (e.keyCode === KEY_CTRL) self.isCtrlDown = false;
|
|
752 | 780 | this.blur();
|
753 | 781 | return;
|
754 | 782 | case KEY_DOWN:
|
755 |
| - if (!this.isOpen && this.hasOptions && this.isInputFocused) { |
| 783 | + if (!this.isOpen && this.hasOptions) { |
756 | 784 | this.open();
|
757 | 785 | } else if (this.$activeOption) {
|
758 | 786 | var $next = this.getAdjacentOption(this.$activeOption, 1);
|
|
850 | 878 |
|
851 | 879 | this.showInput();
|
852 | 880 | this.setActiveItem(null);
|
853 |
| - this.$control.addClass('focus'); |
854 | 881 | this.refreshOptions(!!this.settings.openOnFocus);
|
| 882 | + this.refreshClasses(); |
855 | 883 | };
|
856 | 884 |
|
857 | 885 | /**
|
|
868 | 896 | this.setTextboxValue('');
|
869 | 897 | this.setActiveOption(null);
|
870 | 898 | 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(); |
875 | 901 | };
|
876 | 902 |
|
877 | 903 | /**
|
|
1709 | 1735 | if (!this.settings.persist && this.userOptions.hasOwnProperty(value)) {
|
1710 | 1736 | this.removeOption(value);
|
1711 | 1737 | }
|
1712 |
| - this.setCaret(i); |
1713 |
| - this.refreshOptions(false); |
1714 |
| - this.refreshClasses(); |
1715 | 1738 |
|
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 | + } |
1718 | 1742 |
|
| 1743 | + this.refreshClasses(); |
1719 | 1744 | this.updatePlaceholder();
|
1720 |
| - if (!this.items.length) this.showInput(); |
1721 |
| - |
1722 |
| - this.positionDropdown(); |
1723 | 1745 | this.updateOriginalInput();
|
| 1746 | + this.positionDropdown(); |
1724 | 1747 | this.trigger('item_remove', value);
|
1725 | 1748 | }
|
1726 | 1749 | };
|
|
1791 | 1814 | var isFull = this.isFull();
|
1792 | 1815 | var isLocked = this.isLocked;
|
1793 | 1816 | this.$control
|
| 1817 | + .toggleClass('focus', this.isFocused) |
1794 | 1818 | .toggleClass('disabled', this.isDisabled)
|
1795 | 1819 | .toggleClass('locked', isLocked)
|
1796 | 1820 | .toggleClass('full', isFull).toggleClass('not-full', !isFull)
|
|
1856 | 1880 | */
|
1857 | 1881 | Selectize.prototype.open = function() {
|
1858 | 1882 | if (this.isLocked || this.isOpen || (this.settings.mode === 'multi' && this.isFull())) return;
|
| 1883 | + this.focus(); |
1859 | 1884 | this.isOpen = true;
|
1860 |
| - this.positionDropdown(); |
| 1885 | + this.$dropdown.css({visibility: 'hidden', display: 'block'}); |
1861 | 1886 | this.$control.addClass('dropdown-active');
|
1862 |
| - this.$dropdown.show(); |
| 1887 | + this.positionDropdown(); |
| 1888 | + this.$dropdown.css({visibility: 'visible'}); |
1863 | 1889 | this.trigger('dropdown_open', this.$dropdown);
|
1864 | 1890 | };
|
1865 | 1891 |
|
|
1940 | 1966 |
|
1941 | 1967 | if (this.$activeItems.length) {
|
1942 | 1968 | $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++; } |
1945 | 1971 |
|
1946 | 1972 | for (i = 0, n = this.$activeItems.length; i < n; i++) {
|
1947 | 1973 | values.push($(this.$activeItems[i]).attr('data-value'));
|
|
1950 | 1976 | e.preventDefault();
|
1951 | 1977 | e.stopPropagation();
|
1952 | 1978 | }
|
1953 |
| - } else if ((this.isInputFocused || this.settings.mode === 'single') && this.items.length) { |
| 1979 | + } else if ((this.isFocused || this.settings.mode === 'single') && this.items.length) { |
1954 | 1980 | if (direction < 0 && selection.start === 0 && selection.length === 0) {
|
1955 | 1981 | values.push(this.items[this.caretPos - 1]);
|
1956 | 1982 | } else if (direction > 0 && selection.start === this.$control_input.val().length) {
|
|
1964 | 1990 | }
|
1965 | 1991 |
|
1966 | 1992 | // perform removal
|
1967 |
| - while (values.length) { |
1968 |
| - this.removeItem(values.pop()); |
1969 |
| - } |
1970 | 1993 | if (typeof caret !== 'undefined') {
|
1971 | 1994 | this.setCaret(caret);
|
1972 |
| - this.showInput(); |
1973 | 1995 | }
|
| 1996 | + while (values.length) { |
| 1997 | + this.removeItem(values.pop()); |
| 1998 | + } |
| 1999 | + |
| 2000 | + this.showInput(); |
| 2001 | + this.refreshOptions(true); |
1974 | 2002 | return true;
|
1975 | 2003 | };
|
1976 | 2004 |
|
|
1985 | 2013 | * @param {object} e (optional)
|
1986 | 2014 | */
|
1987 | 2015 | Selectize.prototype.advanceSelection = function(direction, e) {
|
1988 |
| - var tail, selection, idx, valueLength, cursorAtEdge, $tail, $items; |
| 2016 | + var tail, selection, idx, valueLength, cursorAtEdge, $tail; |
1989 | 2017 |
|
1990 | 2018 | if (direction === 0) return;
|
1991 | 2019 |
|
|
2004 | 2032 | } else {
|
2005 | 2033 | $tail = this.$control.children('.active:' + tail);
|
2006 | 2034 | 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); |
2009 | 2036 | this.setActiveItem(null);
|
2010 | 2037 | this.setCaret(direction > 0 ? idx + 1 : idx);
|
2011 | 2038 | this.showInput();
|
|
2380 | 2407 | return index >= 0 && index < $options.length ? $options.eq(index) : $();
|
2381 | 2408 | };
|
2382 | 2409 |
|
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 |
| - |
2416 | 2410 | this.onKeyDown = (function() {
|
2417 | 2411 | var original = self.onKeyDown;
|
2418 | 2412 | return function(e) {
|
|
2440 | 2434 | };
|
2441 | 2435 | })();
|
2442 | 2436 |
|
| 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 | + |
2443 | 2469 | });
|
2444 | 2470 | })();
|
2445 | 2471 |
|
|
0 commit comments