Skip to content

Commit

Permalink
Added removeOptionGroup, clearOptionGroups (#669).
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Jan 27, 2015
1 parent d6e7cbc commit 3aa09d1
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 26 deletions.
8 changes: 8 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ var selectize = $select[0].selectize;
<td valign="top"><code>addOptionGroup(id, data)</code></td>
<td valign="top">Registers a new optgroup for options to be bucketed into. The "id" argument refers to a value of the property in option identified by the "optgroupField" setting.</td>
</tr>
<tr>
<td valign="top"><code>removeOptionGroup(id)</code></td>
<td valign="top">Removes a single option group.</td>
</tr>
<tr>
<td valign="top"><code>clearOptionGroups()</code></td>
<td valign="top">Removes all existing option groups.</td>
</tr>
<tr>
<th valign="top" colspan="3" align="left"><a href="#methods_events" name="methods_events">Events</a></th>
</tr>
Expand Down
20 changes: 20 additions & 0 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ selectize.off('event_name', handler);
<td valign="top">value</td>
<td valign="top">Invoked when an option is removed from the available options.</td>
</tr>
<tr>
<td valign="top"><code>"option_clear"</code></td>
<td valign="top"></td>
<td valign="top">Invoked when all options are removed from the control.</td>
</tr>
<tr>
<td valign="top"><code>"optgroup_add"</code></td>
<td valign="top">id, data</td>
<td valign="top">Invoked when a new option is added to the available options list.</td>
</tr>
<tr>
<td valign="top"><code>"optgroup_remove"</code></td>
<td valign="top">id</td>
<td valign="top">Invoked when an option group is removed.</td>
</tr>
<tr>
<td valign="top"><code>"optgroup_clear"</code></td>
<td valign="top"></td>
<td valign="top">Invoked when all option groups are removed.</td>
</tr>
<tr>
<td valign="top"><code>"dropdown_open"</code></td>
<td valign="top">$dropdown</td>
Expand Down
31 changes: 17 additions & 14 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ Selectize.defaults = {
copyClassesToDropdown: true,

/*
load : null, // function(query, callback) { ... }
score : null, // function(search) { ... }
onInitialize : null, // function() { ... }
onChange : null, // function(value) { ... }
onItemAdd : null, // function(value, $item) { ... }
onItemRemove : null, // function(value) { ... }
onClear : null, // function() { ... }
onOptionAdd : null, // function(value, data) { ... }
onOptionRemove : null, // function(value) { ... }
onOptionClear : null, // function() { ... }
onDropdownOpen : null, // function($dropdown) { ... }
onDropdownClose : null, // function($dropdown) { ... }
onType : null, // function(str) { ... }
onDelete : null, // function(values) { ... }
load : null, // function(query, callback) { ... }
score : null, // function(search) { ... }
onInitialize : null, // function() { ... }
onChange : null, // function(value) { ... }
onItemAdd : null, // function(value, $item) { ... }
onItemRemove : null, // function(value) { ... }
onClear : null, // function() { ... }
onOptionAdd : null, // function(value, data) { ... }
onOptionRemove : null, // function(value) { ... }
onOptionClear : null, // function() { ... }
onOptionGroupAdd : null, // function(id, data) { ... }
onOptionGroupRemove : null, // function(id) { ... }
onOptionGroupClear : null, // function() { ... }
onDropdownOpen : null, // function($dropdown) { ... }
onDropdownClose : null, // function($dropdown) { ... }
onType : null, // function(str) { ... }
onDelete : null, // function(values) { ... }
*/

render: {
Expand Down
49 changes: 37 additions & 12 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,21 @@ $.extend(Selectize.prototype, {
*/
setupCallbacks: function() {
var key, fn, callbacks = {
'initialize' : 'onInitialize',
'change' : 'onChange',
'item_add' : 'onItemAdd',
'item_remove' : 'onItemRemove',
'clear' : 'onClear',
'option_add' : 'onOptionAdd',
'option_remove' : 'onOptionRemove',
'option_clear' : 'onOptionClear',
'dropdown_open' : 'onDropdownOpen',
'dropdown_close' : 'onDropdownClose',
'type' : 'onType',
'load' : 'onLoad'
'initialize' : 'onInitialize',
'change' : 'onChange',
'item_add' : 'onItemAdd',
'item_remove' : 'onItemRemove',
'clear' : 'onClear',
'option_add' : 'onOptionAdd',
'option_remove' : 'onOptionRemove',
'option_clear' : 'onOptionClear',
'optgroup_add' : 'onOptionGroupAdd',
'optgroup_remove' : 'onOptionGroupRemove',
'optgroup_clear' : 'onOptionGroupClear',
'dropdown_open' : 'onDropdownOpen',
'dropdown_close' : 'onDropdownClose',
'type' : 'onType',
'load' : 'onLoad'
};

for (key in callbacks) {
Expand Down Expand Up @@ -1153,6 +1156,28 @@ $.extend(Selectize.prototype, {
this.trigger('optgroup_add', id, data);
},

/**
* Removes an existing option group.
*
* @param {string} id
*/
removeOptionGroup: function(id) {
if (this.optgroups.hasOwnProperty(id)) {
delete this.optgroups[id];
this.renderCache = {};
this.trigger('optgroup_remove', id);
}
},

/**
* Clears all existing option groups.
*/
clearOptionGroups: function() {
this.optgroups = {};
this.renderCache = {};
this.trigger('optgroup_clear');
},

/**
* Updates an option available for selection. If
* it is visible in the selected items or options
Expand Down
29 changes: 29 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@
});
});

describe('removeOptionGroup()', function() {
var test;

before(function() {
test = setup_test('<select>', {valueField: 'value', labelField: 'value'});
});
it('should remove group', function() {
var data = {label: 'Group Label'};
test.selectize.addOptionGroup('group_id', data);
test.selectize.removeOptionGroup('group_id');
expect(test.selectize.optgroups).to.not.have.property('group_id');
});
});

describe('clearOptionGroups()', function() {
var test;

before(function() {
test = setup_test('<select>', {valueField: 'value', labelField: 'value'});
});
it('should clear all groups', function() {
var data = {label: 'Group Label'};
test.selectize.addOptionGroup('group_id', data);
test.selectize.addOptionGroup('group_id2', data);
test.selectize.clearOptionGroups();
expect(test.selectize.optgroups).to.deep.equal({});
});
});

describe('addOption()', function() {
var test;

Expand Down
23 changes: 23 additions & 0 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ describe('Events', function() {
});
});

describe('optgroup_remove', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('optgroup_remove', function(id) {
expect(id).to.be.equal('id');
done();
});
test.selectize.addOptionGroup('id', {label: 'Group'});
test.selectize.removeOptionGroup('id');
});
});

describe('optgroup_clear', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.selectize.on('optgroup_clear', function() {
done();
});
test.selectize.addOptionGroup('id', {label: 'Group'});
test.selectize.clearOptionGroups();
});
});

describe('option_add', function() {
it('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
Expand Down

0 comments on commit 3aa09d1

Please sign in to comment.