From 5e09c91c1dacdb78f0e4ab7a45f0283ade694ba2 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Thu, 22 May 2014 09:37:17 -0400 Subject: [PATCH 1/3] Delaying updating attributes and caret during bulk insertion --- src/selectize.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/selectize.js b/src/selectize.js index 8ea0b6de8..0b93df931 100644 --- a/src/selectize.js +++ b/src/selectize.js @@ -1292,18 +1292,23 @@ $.extend(Selectize.prototype, { if (!self.options.hasOwnProperty(value)) return; if (inputMode === 'single') self.clear(); - if (inputMode === 'multi' && self.isFull()) return; + if (inputMode === 'multi' && self.isFull()) { + self.refreshState(); + return; + } $item = $(self.render('item', self.options[value])); self.items.splice(self.caretPos, 0, value); self.insertAtCaret($item); - self.refreshState(); + if (!this.isPending) { + self.refreshState(); + } if (self.isSetup) { $options = self.$dropdown_content.find('[data-selectable]'); // update menu / remove the option (if this is not one item being added as part of series) - if (!this.isPending) { + if (!self.isPending) { $option = self.getOption(value); value_next = self.getAdjacentOption($option, 1).attr('data-value'); self.refreshOptions(self.isFocused && inputMode !== 'single'); @@ -1751,17 +1756,19 @@ $.extend(Selectize.prototype, { i = Math.max(0, Math.min(self.items.length, i)); } - // the input must be moved by leaving it in place and moving the - // siblings, due to the fact that focus cannot be restored once lost - // on mobile webkit devices - var j, n, fn, $children, $child; - $children = self.$control.children(':not(input)'); - for (j = 0, n = $children.length; j < n; j++) { - $child = $($children[j]).detach(); - if (j < i) { - self.$control_input.before($child); - } else { - self.$control.append($child); + if(!self.isPending) { + // the input must be moved by leaving it in place and moving the + // siblings, due to the fact that focus cannot be restored once lost + // on mobile webkit devices + var j, n, fn, $children, $child; + $children = self.$control.children(':not(input)'); + for (j = 0, n = $children.length; j < n; j++) { + $child = $($children[j]).detach(); + if (j < i) { + self.$control_input.before($child); + } else { + self.$control.append($child); + } } } From 30a3c0e1e9ed69a44f6b328556ccdbe0f258e29c Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Thu, 22 May 2014 09:42:00 -0400 Subject: [PATCH 2/3] Consolidated the refreshState call to capture just becoming full during bulk (or any) addItem call --- src/selectize.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/selectize.js b/src/selectize.js index 0b93df931..a733fef24 100644 --- a/src/selectize.js +++ b/src/selectize.js @@ -1282,7 +1282,7 @@ $.extend(Selectize.prototype, { var $item, $option, $options; var self = this; var inputMode = self.settings.mode; - var i, active, value_next; + var i, active, value_next, wasFull; value = hash_key(value); if (self.items.indexOf(value) !== -1) { @@ -1292,15 +1292,13 @@ $.extend(Selectize.prototype, { if (!self.options.hasOwnProperty(value)) return; if (inputMode === 'single') self.clear(); - if (inputMode === 'multi' && self.isFull()) { - self.refreshState(); - return; - } + if (inputMode === 'multi' && self.isFull()) return; $item = $(self.render('item', self.options[value])); + wasFull = self.isFull(); self.items.splice(self.caretPos, 0, value); self.insertAtCaret($item); - if (!this.isPending) { + if (!this.isPending || (!wasFull && self.isFull())) { self.refreshState(); } From 0bc0b6329b07a88df6a487231609a47c1f77a4d5 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Thu, 22 May 2014 10:34:22 -0400 Subject: [PATCH 3/3] Used method call to isFull to avoid duplicate code --- src/selectize.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/selectize.js b/src/selectize.js index a733fef24..289babc28 100644 --- a/src/selectize.js +++ b/src/selectize.js @@ -1298,7 +1298,7 @@ $.extend(Selectize.prototype, { wasFull = self.isFull(); self.items.splice(self.caretPos, 0, value); self.insertAtCaret($item); - if (!this.isPending || (!wasFull && self.isFull())) { + if (!self.isPending || (!wasFull && self.isFull())) { self.refreshState(); } @@ -1316,7 +1316,7 @@ $.extend(Selectize.prototype, { } // hide the menu if the maximum number of items have been selected or no options are left - if (!$options.length || (self.settings.maxItems !== null && self.items.length >= self.settings.maxItems)) { + if (!$options.length || self.isFull()) { self.close(); } else { self.positionDropdown();