|
258 | 258 | var field = options.sort;
|
259 | 259 | var multiplier = options.direction === 'desc' ? -1 : 1;
|
260 | 260 | return function(a, b) {
|
261 |
| - a = a && String(self.items[a.id][field] || '').toLowerCase(); |
262 |
| - b = b && String(self.items[b.id][field] || '').toLowerCase(); |
263 |
| - if (a > b) return 1 * multiplier; |
264 |
| - if (b > a) return -1 * multiplier; |
265 |
| - return 0; |
| 261 | + return cmp(self.items[a.id][field], self.items[b.id][field]) * multiplier; |
266 | 262 | };
|
267 | 263 | })());
|
268 | 264 | }
|
|
280 | 276 | // utilities
|
281 | 277 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
282 | 278 |
|
| 279 | + var cmp = function(a, b) { |
| 280 | + if (typeof a === 'number' && typeof b === 'number') { |
| 281 | + return a > b ? 1 : (a < b ? -1 : 0); |
| 282 | + } |
| 283 | + a = String(a || '').toLowerCase(); |
| 284 | + b = String(b || '').toLowerCase(); |
| 285 | + if (a > b) return 1; |
| 286 | + if (b > a) return -1; |
| 287 | + return 0; |
| 288 | + }; |
| 289 | + |
283 | 290 | var extend = function(a, b) {
|
284 | 291 | var i, n, k, object;
|
285 | 292 | for (i = 1, n = arguments.length; i < n; i++) {
|
|
454 | 461 | };
|
455 | 462 |
|
456 | 463 | var utils = {
|
457 |
| - isArray: Array.isArray || function() { |
| 464 | + isArray: Array.isArray || function(vArg) { |
458 | 465 | return Object.prototype.toString.call(vArg) === '[object Array]';
|
459 | 466 | }
|
460 | 467 | };
|
|
463 | 470 | }));
|
464 | 471 |
|
465 | 472 | /**
|
466 |
| - * selectize.js (v0.7.5) |
| 473 | + * selectize.js (v0.7.7) |
467 | 474 | * Copyright (c) 2013 Brian Reavis & contributors
|
468 | 475 | *
|
469 | 476 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
|
|
621 | 628 | .replace(/"/g, '"');
|
622 | 629 | };
|
623 | 630 |
|
624 |
| - /** |
625 |
| - * Escapes quotation marks with backslashes. Useful |
626 |
| - * for escaping values for use in CSS attribute selectors. |
627 |
| - * |
628 |
| - * @param {string} str |
629 |
| - * @return {string} |
630 |
| - */ |
631 |
| - var escape_quotes = function(str) { |
632 |
| - return str.replace(/(['"])/g, '\\$1'); |
633 |
| - }; |
634 |
| - |
635 | 631 | var hook = {};
|
636 | 632 |
|
637 | 633 | /**
|
|
1893 | 1889 | */
|
1894 | 1890 | addOptionGroup: function(id, data) {
|
1895 | 1891 | this.optgroups[id] = data;
|
1896 |
| - this.trigger('optgroup_add', value, data); |
| 1892 | + this.trigger('optgroup_add', id, data); |
1897 | 1893 | },
|
1898 | 1894 |
|
1899 | 1895 | /**
|
|
1991 | 1987 | * @returns {object}
|
1992 | 1988 | */
|
1993 | 1989 | getOption: function(value) {
|
1994 |
| - value = hash_key(value); |
1995 |
| - return value ? this.$dropdown_content.find('[data-selectable]').filter('[data-value="' + escape_quotes(value) + '"]:first') : $(); |
| 1990 | + return this.getElementWithValue(value, this.$dropdown_content.find('[data-selectable]')); |
1996 | 1991 | },
|
1997 | 1992 |
|
1998 | 1993 | /**
|
|
2010 | 2005 | return index >= 0 && index < $options.length ? $options.eq(index) : $();
|
2011 | 2006 | },
|
2012 | 2007 |
|
| 2008 | + /** |
| 2009 | + * Finds the first element with a "data-value" attribute |
| 2010 | + * that matches the given value. |
| 2011 | + * |
| 2012 | + * @param {mixed} value |
| 2013 | + * @param {object} $els |
| 2014 | + * @return {object} |
| 2015 | + */ |
| 2016 | + getElementWithValue: function(value, $els) { |
| 2017 | + value = hash_key(value); |
| 2018 | + |
| 2019 | + if (value) { |
| 2020 | + for (var i = 0, n = $els.length; i < n; i++) { |
| 2021 | + if ($els[i].getAttribute('data-value') === value) { |
| 2022 | + return $($els[i]); |
| 2023 | + } |
| 2024 | + } |
| 2025 | + } |
| 2026 | + |
| 2027 | + return $(); |
| 2028 | + }, |
| 2029 | + |
2013 | 2030 | /**
|
2014 | 2031 | * Returns the jQuery element of the item
|
2015 | 2032 | * matching the given value.
|
|
2018 | 2035 | * @returns {object}
|
2019 | 2036 | */
|
2020 | 2037 | getItem: function(value) {
|
2021 |
| - return this.$control.children('[data-value="' + escape_quotes(hash_key(value)) + '"]'); |
| 2038 | + return this.getElementWithValue(value, this.$control.children()); |
2022 | 2039 | },
|
2023 | 2040 |
|
2024 | 2041 | /**
|
|
2627 | 2644 |
|
2628 | 2645 | dataAttr: 'data-data',
|
2629 | 2646 | optgroupField: 'optgroup',
|
2630 |
| - sortField: null, |
| 2647 | + sortField: '$order', |
2631 | 2648 | sortDirection: 'asc',
|
2632 | 2649 | valueField: 'value',
|
2633 | 2650 | labelField: 'text',
|
|
2708 | 2725 | var init_select = function($input, settings_element) {
|
2709 | 2726 | var i, n, tagName;
|
2710 | 2727 | var $children;
|
| 2728 | + var order = 0; |
2711 | 2729 | settings_element.maxItems = !!$input.attr('multiple') ? null : 1;
|
2712 | 2730 |
|
2713 | 2731 | var readData = function($el) {
|
|
2719 | 2737 | };
|
2720 | 2738 |
|
2721 | 2739 | var addOption = function($option, group) {
|
| 2740 | + var value, option; |
| 2741 | + |
2722 | 2742 | $option = $($option);
|
2723 | 2743 |
|
2724 |
| - var value = $option.attr('value') || ''; |
| 2744 | + value = $option.attr('value') || ''; |
2725 | 2745 | if (!value.length) return;
|
2726 | 2746 |
|
2727 |
| - settings_element.options[value] = readData($option) || { |
| 2747 | + option = readData($option) || { |
2728 | 2748 | 'text' : $option.text(),
|
2729 | 2749 | 'value' : value,
|
2730 | 2750 | 'optgroup' : group
|
2731 | 2751 | };
|
| 2752 | + |
| 2753 | + option.$order = ++order; |
| 2754 | + settings_element.options[value] = option; |
| 2755 | + |
2732 | 2756 | if ($option.is(':selected')) {
|
2733 | 2757 | settings_element.items.push(value);
|
2734 | 2758 | }
|
|
0 commit comments