From d6e7cbc11b001a9d8fa238693dbd8a031189a0e5 Mon Sep 17 00:00:00 2001 From: Brian Reavis Date: Tue, 27 Jan 2015 00:26:08 -0700 Subject: [PATCH] Misc fixes / cleanup from latest round of merges. --- docs/api.md | 14 ++++++------ docs/events.md | 1 - docs/usage.md | 2 +- src/defaults.js | 2 +- src/selectize.js | 55 ++++++++++++++++++++++++++---------------------- test/api.js | 20 ++++++++++++++++++ 6 files changed, 59 insertions(+), 35 deletions(-) diff --git a/docs/api.md b/docs/api.md index 840a59c78..87dc1aaf9 100644 --- a/docs/api.md +++ b/docs/api.md @@ -64,20 +64,20 @@ var selectize = $select[0].selectize; Description - clear() - Resets / clears all selected items from the control. + clear(silent) + Resets / clears all selected items from the control. If "silent" is truthy, no change event will be fired on the original input. getItem(value) Returns the jQuery element of the item matching the given value. - addItem(value) - "Selects" an item. Adds it to the list at the current caret position. + addItem(value, silent) + "Selects" an item. Adds it to the list at the current caret position. If "silent" is truthy, no change event will be fired on the original input. - removeItem(value) - Removes the selected item matching the provided value. + removeItem(value, silent) + Removes the selected item matching the provided value. If "silent" is truthy, no change event will be fired on the original input. createItem(value) @@ -184,7 +184,7 @@ var selectize = $select[0].selectize; Returns the value of the control. If multiple items can be selected (e.g. <select multiple>), this returns an array. If only one item can be selected, this returns a string. - setValue(value) + setValue(value, silent) Resets the selected items to the given value. diff --git a/docs/events.md b/docs/events.md index 2eca2c9fa..a070a5564 100644 --- a/docs/events.md +++ b/docs/events.md @@ -15,7 +15,6 @@ selectize.off('event_name', handler); ### List of Events - diff --git a/docs/usage.md b/docs/usage.md index b9ebe18b1..f43578763 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -195,7 +195,7 @@ $(function() { of the sort list. This will make results sorted primarily by match quality (descending).

- You can overwrite the "$score" function. For more information, see the sifter documentation. + You can override the "$score" function. For more information, see the sifter documentation. diff --git a/src/defaults.js b/src/defaults.js index ea89b726d..b7e871ab7 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -2,7 +2,7 @@ Selectize.count = 0; Selectize.defaults = { plugins: [], delimiter: ',', - splitOn: null, // Regex or string for splitting up values from a paste command + splitOn: null, // regexp or string for splitting up values from a paste command persist: true, diacritics: true, create: false, diff --git a/src/selectize.js b/src/selectize.js index aa1721975..160279d29 100644 --- a/src/selectize.js +++ b/src/selectize.js @@ -380,7 +380,6 @@ $.extend(Selectize.prototype, { this.$input.trigger('change'); }, - /** * Triggered on paste. * @@ -392,13 +391,14 @@ $.extend(Selectize.prototype, { if (self.isFull() || self.isInputHidden || self.isLocked) { e.preventDefault(); } else { - // If a regex or string is included, this will split the pasted input and create Items for each separate value + // If a regex or string is included, this will split the pasted + // input and create Items for each separate value if (self.settings.splitOn) { setTimeout(function() { var splitInput = $.trim(self.$control_input.val() || '').split(self.settings.splitOn); - splitInput.forEach(function(input) { - self.createItem(input); - }); + for (var i = 0, n = splitInput.length; i < n; i++) { + self.createItem(splitInput[i]); + } }, 0); } } @@ -448,8 +448,8 @@ $.extend(Selectize.prototype, { if (self.isOpen) { e.preventDefault(); e.stopPropagation(); + self.close(); } - self.close(); return; case KEY_N: if (!e.ctrlKey || e.altKey) break; @@ -728,12 +728,12 @@ $.extend(Selectize.prototype, { * * @param {mixed} value */ - setValue: function(value, isSilent) { - var events = isSilent ? [] : ['change']; + setValue: function(value, silent) { + var events = silent ? [] : ['change']; debounce_events(this, events, function() { this.clear(); - this.addItems(value, isSilent); + this.addItems(value, silent); }); }, @@ -1218,8 +1218,9 @@ $.extend(Selectize.prototype, { * Removes a single option. * * @param {string} value + * @param {boolean} silent */ - removeOption: function(value) { + removeOption: function(value, silent) { var self = this; value = hash_key(value); @@ -1232,7 +1233,7 @@ $.extend(Selectize.prototype, { delete self.options[value]; self.lastQuery = null; self.trigger('option_remove', value); - self.removeItem(value); + self.removeItem(value, silent); }, /** @@ -1314,12 +1315,13 @@ $.extend(Selectize.prototype, { * at the current caret position. * * @param {string} value + * @param {boolean} silent */ - addItems: function(values, isSilent) { + addItems: function(values, silent) { var items = $.isArray(values) ? values : [values]; for (var i = 0, n = items.length; i < n; i++) { this.isPending = (i < n - 1); - this.addItem(items[i], isSilent); + this.addItem(items[i], silent); } }, @@ -1328,9 +1330,10 @@ $.extend(Selectize.prototype, { * at the current caret position. * * @param {string} value + * @param {boolean} silent */ - addItem: function(value, isSilent) { - var events = isSilent ? [] : ['change']; + addItem: function(value, silent) { + var events = silent ? [] : ['change']; debounce_events(this, events, function() { var $item, $option, $options; @@ -1378,7 +1381,7 @@ $.extend(Selectize.prototype, { self.updatePlaceholder(); self.trigger('item_add', value, $item); - self.updateOriginalInput({isSilent: isSilent}); + self.updateOriginalInput({silent: silent}); } }); }, @@ -1389,7 +1392,7 @@ $.extend(Selectize.prototype, { * * @param {string} value */ - removeItem: function(value) { + removeItem: function(value, silent) { var self = this; var $item, i, idx; @@ -1407,7 +1410,7 @@ $.extend(Selectize.prototype, { self.items.splice(i, 1); self.lastQuery = null; if (!self.settings.persist && self.userOptions.hasOwnProperty(value)) { - self.removeOption(value); + self.removeOption(value, silent); } if (i < self.caretPos) { @@ -1416,7 +1419,7 @@ $.extend(Selectize.prototype, { self.refreshState(); self.updatePlaceholder(); - self.updateOriginalInput(); + self.updateOriginalInput({silent: silent}); self.positionDropdown(); self.trigger('item_remove', value); } @@ -1445,9 +1448,9 @@ $.extend(Selectize.prototype, { input = triggerDropdown; } - if (!input.length) return false; - - if (!self.canCreate(input)) return false; + if (!self.canCreate(input)) { + return false; + } self.lock(); @@ -1568,7 +1571,7 @@ $.extend(Selectize.prototype, { } if (self.isSetup) { - if (!opts.isSilent) { + if (!opts.silent) { self.trigger('change', self.$input.val()); } } @@ -1645,8 +1648,10 @@ $.extend(Selectize.prototype, { /** * Resets / clears all selected items * from the control. + * + * @param {boolean} silent */ - clear: function() { + clear: function(silent) { var self = this; if (!self.items.length) return; @@ -1656,7 +1661,7 @@ $.extend(Selectize.prototype, { self.setCaret(0); self.setActiveItem(null); self.updatePlaceholder(); - self.updateOriginalInput(); + self.updateOriginalInput({silent: silent}); self.refreshState(); self.showInput(); self.trigger('clear'); diff --git a/test/api.js b/test/api.js index 2a1ea7a8d..103ff03a5 100644 --- a/test/api.js +++ b/test/api.js @@ -195,6 +195,7 @@ {value: 'a'}, {value: 'b'}, {value: 'c'}, + {value: 'x'}, {value: '$1'}, {value: '\''}, {value: '"'}, @@ -229,6 +230,16 @@ test.selectize.addItem(0); expect(test.selectize.items.indexOf('0')).to.not.be.equal(-1); }); + it('should not fire "change" if silent is truthy', function(done) { + var watcher = function(e) { throw new Error('Change fired'); }; + test.$select.on('change', watcher); + test.selectize.addItem('x', true); + expect(test.selectize.items.indexOf('x')).to.not.be.equal(-1); + window.setTimeout(function() { + test.$select.off('change', watcher); + done(); + }, 0); + }); it('should update DOM', function() { test.selectize.addItem('c'); expect(test.selectize.$control.find('[data-value=c]').length).to.be.equal(1); @@ -474,6 +485,15 @@ expect(test.selectize.$control.find('[data-value=2]').length).to.be.equal(0); expect(test.selectize.$control.find('[data-value=3]').length).to.be.equal(0); }); + it('should not fire "change" if silent is truthy', function(done) { + var watcher = function(e) { throw new Error('Change fired'); }; + test.$select.on('change', watcher); + test.selectize.clear(true); + window.setTimeout(function() { + test.$select.off('change', watcher); + done(); + }, 0); + }); it('should not give control focus', function(done) { test.selectize.clear(); window.setTimeout(function() {
Event string|array '$order'