Skip to content

Commit

Permalink
Misc fixes / cleanup from latest round of merges.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Jan 27, 2015
1 parent daffe43 commit d6e7cbc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
14 changes: 7 additions & 7 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ var selectize = $select[0].selectize;
<th valign="top" align="left">Description</th>
</tr>
<tr>
<td valign="top"><code>clear()</code></td>
<td valign="top">Resets / clears all selected items from the control.</td>
<td valign="top"><code>clear(silent)</code></td>
<td valign="top">Resets / clears all selected items from the control. If "silent" is truthy, no change event will be fired on the original input.</td>
</tr>
<tr>
<td valign="top"><code>getItem(value)</code></td>
<td valign="top">Returns the jQuery element of the item matching the given value.</td>
</tr>
<tr>
<td valign="top"><code>addItem(value)</code></td>
<td valign="top">"Selects" an item. Adds it to the list at the current caret position.</td>
<td valign="top"><code>addItem(value, silent)</code></td>
<td valign="top">"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.</td>
</tr>
<tr>
<td valign="top"><code>removeItem(value)</code></td>
<td valign="top">Removes the selected item matching the provided value.</td>
<td valign="top"><code>removeItem(value, silent)</code></td>
<td valign="top">Removes the selected item matching the provided value. If "silent" is truthy, no change event will be fired on the original input.</td>
</tr>
<tr>
<td valign="top"><code>createItem(value)</code></td>
Expand Down Expand Up @@ -184,7 +184,7 @@ var selectize = $select[0].selectize;
<td valign="top">Returns the value of the control. If multiple items can be selected (e.g. &lt;select multiple&gt;), this returns an array. If only one item can be selected, this returns a string.</td>
</tr>
<tr>
<td valign="top"><code>setValue(value)</code></td>
<td valign="top"><code>setValue(value, silent)</code></td>
<td valign="top">Resets the selected items to the given value.</td>
</tr>
<tr>
Expand Down
1 change: 0 additions & 1 deletion docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ selectize.off('event_name', handler);

### List of Events


<table width="100%">
<tr>
<th valign="top" width="200px" align="left">Event</th>
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ $(function() {
of the sort list. This will make results sorted primarily by match quality (descending).<br><br>

You can overwrite the "$score" function. For more information, see the <a href="https://github.com/brianreavis/sifter.js#sifterjs">sifter documentation</a>.
You can override the "$score" function. For more information, see the <a href="https://github.com/brianreavis/sifter.js#sifterjs">sifter documentation</a>.
</td>
<td valign="top"><code>string|array</code></td>
<td valign="top"><code>'$order'</code></td>
Expand Down
2 changes: 1 addition & 1 deletion src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
55 changes: 30 additions & 25 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ $.extend(Selectize.prototype, {
this.$input.trigger('change');
},


/**
* Triggered on <input> paste.
*
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
},

Expand Down Expand Up @@ -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);

Expand All @@ -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);
},

/**
Expand Down Expand Up @@ -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);
}
},

Expand All @@ -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;
Expand Down Expand Up @@ -1378,7 +1381,7 @@ $.extend(Selectize.prototype, {

self.updatePlaceholder();
self.trigger('item_add', value, $item);
self.updateOriginalInput({isSilent: isSilent});
self.updateOriginalInput({silent: silent});
}
});
},
Expand All @@ -1389,7 +1392,7 @@ $.extend(Selectize.prototype, {
*
* @param {string} value
*/
removeItem: function(value) {
removeItem: function(value, silent) {
var self = this;
var $item, i, idx;

Expand All @@ -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) {
Expand All @@ -1416,7 +1419,7 @@ $.extend(Selectize.prototype, {

self.refreshState();
self.updatePlaceholder();
self.updateOriginalInput();
self.updateOriginalInput({silent: silent});
self.positionDropdown();
self.trigger('item_remove', value);
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -1568,7 +1571,7 @@ $.extend(Selectize.prototype, {
}

if (self.isSetup) {
if (!opts.isSilent) {
if (!opts.silent) {
self.trigger('change', self.$input.val());
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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');
Expand Down
20 changes: 20 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
{value: 'a'},
{value: 'b'},
{value: 'c'},
{value: 'x'},
{value: '$1'},
{value: '\''},
{value: '"'},
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit d6e7cbc

Please sign in to comment.