Skip to content

Commit ffc13aa

Browse files
committed
Updated selectize.js to latest version.
1 parent 86fe70c commit ffc13aa

File tree

1 file changed

+112
-41
lines changed

1 file changed

+112
-41
lines changed

js/selectize.js

Lines changed: 112 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@
585585
}));
586586

587587
/**
588-
* selectize.js (v0.9.1)
588+
* selectize.js (v0.10.0)
589589
* Copyright (c) 2013 Brian Reavis & contributors
590590
*
591591
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
@@ -1077,6 +1077,7 @@
10771077
isCmdDown : false,
10781078
isCtrlDown : false,
10791079
ignoreFocus : false,
1080+
ignoreBlur : false,
10801081
ignoreHover : false,
10811082
hasOptions : false,
10821083
currentResults : null,
@@ -1113,6 +1114,16 @@
11131114
self.settings.hideSelected = self.settings.mode === 'multi';
11141115
}
11151116

1117+
if (self.settings.create) {
1118+
self.canCreate = function(input) {
1119+
var filter = self.settings.createFilter;
1120+
return input.length
1121+
&& (typeof filter !== 'function' || filter(input))
1122+
&& (typeof filter !== 'string' || new RegExp(filter).test(input))
1123+
&& (!(filter instanceof RegExp) || filter.test(input));
1124+
};
1125+
}
1126+
11161127
self.initializePlugins(self.settings.plugins);
11171128
self.setupCallbacks();
11181129
self.setupTemplates();
@@ -1139,6 +1150,7 @@
11391150
var eventNS = self.eventNS;
11401151
var $window = $(window);
11411152
var $document = $(document);
1153+
var $input = self.$input;
11421154

11431155
var $wrapper;
11441156
var $control;
@@ -1154,8 +1166,8 @@
11541166
var classes_plugins;
11551167

11561168
inputMode = self.settings.mode;
1157-
tab_index = self.$input.attr('tabindex') || '';
1158-
classes = self.$input.attr('class') || '';
1169+
tab_index = $input.attr('tabindex') || '';
1170+
classes = $input.attr('class') || '';
11591171

11601172
$wrapper = $('<div>').addClass(settings.wrapperClass).addClass(classes).addClass(inputMode);
11611173
$control = $('<div>').addClass(settings.inputClass).addClass('items').appendTo($wrapper);
@@ -1165,7 +1177,7 @@
11651177
$dropdown_content = $('<div>').addClass(settings.dropdownContentClass).appendTo($dropdown);
11661178

11671179
$wrapper.css({
1168-
width: self.$input[0].style.width
1180+
width: $input[0].style.width
11691181
});
11701182

11711183
if (self.plugins.names.length) {
@@ -1175,19 +1187,19 @@
11751187
}
11761188

11771189
if ((settings.maxItems === null || settings.maxItems > 1) && self.tagType === TAG_SELECT) {
1178-
self.$input.attr('multiple', 'multiple');
1190+
$input.attr('multiple', 'multiple');
11791191
}
11801192

11811193
if (self.settings.placeholder) {
11821194
$control_input.attr('placeholder', settings.placeholder);
11831195
}
11841196

1185-
if (self.$input.attr('autocorrect')) {
1186-
$control_input.attr('autocorrect', self.$input.attr('autocorrect'));
1197+
if ($input.attr('autocorrect')) {
1198+
$control_input.attr('autocorrect', $input.attr('autocorrect'));
11871199
}
11881200

1189-
if (self.$input.attr('autocapitalize')) {
1190-
$control_input.attr('autocapitalize', self.$input.attr('autocapitalize'));
1201+
if ($input.attr('autocapitalize')) {
1202+
$control_input.attr('autocapitalize', $input.attr('autocapitalize'));
11911203
}
11921204

11931205
self.$wrapper = $wrapper;
@@ -1213,7 +1225,7 @@
12131225
keypress : function() { return self.onKeyPress.apply(self, arguments); },
12141226
resize : function() { self.positionDropdown.apply(self, []); },
12151227
blur : function() { return self.onBlur.apply(self, arguments); },
1216-
focus : function() { return self.onFocus.apply(self, arguments); },
1228+
focus : function() { self.ignoreBlur = false; return self.onFocus.apply(self, arguments); },
12171229
paste : function() { return self.onPaste.apply(self, arguments); }
12181230
});
12191231

@@ -1254,20 +1266,20 @@
12541266
// store original children and tab index so that they can be
12551267
// restored when the destroy() method is called.
12561268
this.revertSettings = {
1257-
$children : self.$input.children().detach(),
1258-
tabindex : self.$input.attr('tabindex')
1269+
$children : $input.children().detach(),
1270+
tabindex : $input.attr('tabindex')
12591271
};
12601272

1261-
self.$input.attr('tabindex', -1).hide().after(self.$wrapper);
1273+
$input.attr('tabindex', -1).hide().after(self.$wrapper);
12621274

12631275
if ($.isArray(settings.items)) {
12641276
self.setValue(settings.items);
12651277
delete settings.items;
12661278
}
12671279

12681280
// feature detect for the validation API
1269-
if (self.$input[0].validity) {
1270-
self.$input.on('invalid' + eventNS, function(e) {
1281+
if ($input[0].validity) {
1282+
$input.on('invalid' + eventNS, function(e) {
12711283
e.preventDefault();
12721284
self.isInvalid = true;
12731285
self.refreshState();
@@ -1280,17 +1292,21 @@
12801292
self.updatePlaceholder();
12811293
self.isSetup = true;
12821294

1283-
if (self.$input.is(':disabled')) {
1295+
if ($input.is(':disabled')) {
12841296
self.disable();
12851297
}
12861298

12871299
self.on('change', this.onChange);
1300+
1301+
$input.data('selectize', self);
1302+
$input.addClass('selectized');
12881303
self.trigger('initialize');
12891304

12901305
// preload options
12911306
if (settings.preload === true) {
12921307
self.onSearchChange('');
12931308
}
1309+
12941310
},
12951311

12961312
/**
@@ -1505,6 +1521,7 @@
15051521
case KEY_TAB:
15061522
if (self.settings.selectOnTab && self.isOpen && self.$activeOption) {
15071523
self.onOptionSelect({currentTarget: self.$activeOption});
1524+
e.preventDefault();
15081525
}
15091526
if (self.settings.create && self.createItem()) {
15101527
e.preventDefault();
@@ -1599,6 +1616,14 @@
15991616
self.isFocused = false;
16001617
if (self.ignoreFocus) return;
16011618

1619+
// necessary to prevent IE closing the dropdown when the scrollbar is clicked
1620+
if (!self.ignoreBlur && document.activeElement === self.$dropdown_content[0]) {
1621+
self.ignoreBlur = true;
1622+
self.onFocus(e);
1623+
1624+
return;
1625+
}
1626+
16021627
if (self.settings.create && self.settings.createOnBlur) {
16031628
self.createItem(false);
16041629
}
@@ -2077,7 +2102,7 @@
20772102
}
20782103

20792104
// add create option
2080-
has_create_option = self.settings.create && results.query.length;
2105+
has_create_option = self.settings.create && self.canCreate(results.query);
20812106
if (has_create_option) {
20822107
$dropdown_content.prepend(self.render('option_create', {input: query}));
20832108
$create = $($dropdown_content[0].childNodes[0]);
@@ -2331,7 +2356,7 @@
23312356
var $item, $option, $options;
23322357
var self = this;
23332358
var inputMode = self.settings.mode;
2334-
var i, active, value_next;
2359+
var i, active, value_next, wasFull;
23352360
value = hash_key(value);
23362361

23372362
if (self.items.indexOf(value) !== -1) {
@@ -2344,15 +2369,18 @@
23442369
if (inputMode === 'multi' && self.isFull()) return;
23452370

23462371
$item = $(self.render('item', self.options[value]));
2372+
wasFull = self.isFull();
23472373
self.items.splice(self.caretPos, 0, value);
23482374
self.insertAtCaret($item);
2349-
self.refreshState();
2375+
if (!self.isPending || (!wasFull && self.isFull())) {
2376+
self.refreshState();
2377+
}
23502378

23512379
if (self.isSetup) {
23522380
$options = self.$dropdown_content.find('[data-selectable]');
23532381

23542382
// update menu / remove the option (if this is not one item being added as part of series)
2355-
if (!this.isPending) {
2383+
if (!self.isPending) {
23562384
$option = self.getOption(value);
23572385
value_next = self.getAdjacentOption($option, 1).attr('data-value');
23582386
self.refreshOptions(self.isFocused && inputMode !== 'single');
@@ -2362,7 +2390,7 @@
23622390
}
23632391

23642392
// hide the menu if the maximum number of items have been selected or no options are left
2365-
if (!$options.length || (self.settings.maxItems !== null && self.items.length >= self.settings.maxItems)) {
2393+
if (!$options.length || self.isFull()) {
23662394
self.close();
23672395
} else {
23682396
self.positionDropdown();
@@ -2428,7 +2456,7 @@
24282456
var self = this;
24292457
var input = $.trim(self.$control_input.val() || '');
24302458
var caret = self.caretPos;
2431-
if (!input.length) return false;
2459+
if (!self.canCreate(input)) return false;
24322460
self.lock();
24332461

24342462
if (typeof triggerDropdown === 'undefined') {
@@ -2485,10 +2513,11 @@
24852513
* and CSS classes.
24862514
*/
24872515
refreshState: function() {
2488-
var self = this;
2489-
var invalid = self.isRequired && !self.items.length;
2490-
if (!invalid) self.isInvalid = false;
2491-
self.$control_input.prop('required', invalid);
2516+
var invalid, self = this;
2517+
if (self.isRequired) {
2518+
if (self.items.length) self.isInvalid = false;
2519+
self.$control_input.prop('required', invalid);
2520+
}
24922521
self.refreshClasses();
24932522
},
24942523

@@ -2535,7 +2564,7 @@
25352564
updateOriginalInput: function() {
25362565
var i, n, options, self = this;
25372566

2538-
if (self.$input[0].tagName.toLowerCase() === 'select') {
2567+
if (self.tagType === TAG_SELECT) {
25392568
options = [];
25402569
for (i = 0, n = self.items.length; i < n; i++) {
25412570
options.push('<option value="' + escape_html(self.items[i]) + '" selected="selected"></option>');
@@ -2546,6 +2575,7 @@
25462575
self.$input.html(options.join(''));
25472576
} else {
25482577
self.$input.val(self.getValue());
2578+
self.$input.attr('value',self.$input.val());
25492579
}
25502580

25512581
if (self.isSetup) {
@@ -2631,7 +2661,9 @@
26312661
if (!self.items.length) return;
26322662
self.$control.children(':not(input)').remove();
26332663
self.items = [];
2664+
self.lastQuery = null;
26342665
self.setCaret(0);
2666+
self.setActiveItem(null);
26352667
self.updatePlaceholder();
26362668
self.updateOriginalInput();
26372669
self.refreshState();
@@ -2800,17 +2832,19 @@
28002832
i = Math.max(0, Math.min(self.items.length, i));
28012833
}
28022834

2803-
// the input must be moved by leaving it in place and moving the
2804-
// siblings, due to the fact that focus cannot be restored once lost
2805-
// on mobile webkit devices
2806-
var j, n, fn, $children, $child;
2807-
$children = self.$control.children(':not(input)');
2808-
for (j = 0, n = $children.length; j < n; j++) {
2809-
$child = $($children[j]).detach();
2810-
if (j < i) {
2811-
self.$control_input.before($child);
2812-
} else {
2813-
self.$control.append($child);
2835+
if(!self.isPending) {
2836+
// the input must be moved by leaving it in place and moving the
2837+
// siblings, due to the fact that focus cannot be restored once lost
2838+
// on mobile webkit devices
2839+
var j, n, fn, $children, $child;
2840+
$children = self.$control.children(':not(input)');
2841+
for (j = 0, n = $children.length; j < n; j++) {
2842+
$child = $($children[j]).detach();
2843+
if (j < i) {
2844+
self.$control_input.before($child);
2845+
} else {
2846+
self.$control.append($child);
2847+
}
28142848
}
28152849
}
28162850

@@ -2876,9 +2910,13 @@
28762910
.html('')
28772911
.append(revertSettings.$children)
28782912
.removeAttr('tabindex')
2913+
.removeClass('selectized')
28792914
.attr({tabindex: revertSettings.tabindex})
28802915
.show();
28812916

2917+
self.$control_input.removeData('grow');
2918+
self.$input.removeData('selectize');
2919+
28822920
$(window).off(eventNS);
28832921
$(document).off(eventNS);
28842922
$(document.body).off(eventNS);
@@ -2937,8 +2975,25 @@
29372975
}
29382976

29392977
return html;
2978+
},
2979+
2980+
/**
2981+
* Clears the render cache for a template. If
2982+
* no template is given, clears all render
2983+
* caches.
2984+
*
2985+
* @param {string} templateName
2986+
*/
2987+
clearCache: function(templateName) {
2988+
var self = this;
2989+
if (typeof templateName === 'undefined') {
2990+
self.renderCache = {};
2991+
} else {
2992+
delete self.renderCache[templateName];
2993+
}
29402994
}
29412995

2996+
29422997
});
29432998

29442999

@@ -2950,6 +3005,7 @@
29503005
diacritics: true,
29513006
create: false,
29523007
createOnBlur: false,
3008+
createFilter: null,
29533009
highlight: true,
29543010
openOnFocus: true,
29553011
maxOptions: 1000,
@@ -3150,13 +3206,12 @@
31503206
}
31513207

31523208
instance = new Selectize($input, $.extend(true, {}, defaults, settings_element, settings_user));
3153-
$input.data('selectize', instance);
3154-
$input.addClass('selectized');
31553209
});
31563210
};
31573211

31583212
$.fn.selectize.defaults = Selectize.defaults;
31593213

3214+
31603215
Selectize.define('drag_drop', function(options) {
31613216
if (!$.fn.sortable) throw new Error('The "drag_drop" plugin requires jQuery UI "sortable".');
31623217
if (this.settings.mode !== 'multi') return;
@@ -3285,6 +3340,22 @@
32853340
};
32863341
})();
32873342

3343+
var getScrollbarWidth = function() {
3344+
var div;
3345+
var width = getScrollbarWidth.width;
3346+
var doc = document;
3347+
3348+
if (typeof width === 'undefined') {
3349+
div = doc.createElement('div');
3350+
div.innerHTML = '<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>';
3351+
div = div.firstChild;
3352+
doc.body.appendChild(div);
3353+
width = getScrollbarWidth.width = div.offsetWidth - div.clientWidth;
3354+
doc.body.removeChild(div);
3355+
}
3356+
return width;
3357+
};
3358+
32883359
var equalizeSizes = function() {
32893360
var i, n, height_max, width, width_last, width_parent, $optgroups;
32903361

@@ -3301,7 +3372,7 @@
33013372
}
33023373

33033374
if (options.equalizeWidth) {
3304-
width_parent = self.$dropdown_content.innerWidth();
3375+
width_parent = self.$dropdown_content.innerWidth() - getScrollbarWidth();
33053376
width = Math.round(width_parent / n);
33063377
$optgroups.css({width: width});
33073378
if (n > 1) {

0 commit comments

Comments
 (0)