Skip to content

Commit a5d61d2

Browse files
committed
clear_button plugin
1 parent 32ee80a commit a5d61d2

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

examples/plugins.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ <h2>Plugin: "remove_button"</h2>
4848
</script>
4949
</div>
5050

51+
<div class="demo">
52+
<h2>Plugin: "clear_button"</h2>
53+
<div class="control-group">
54+
<label for="input-clear">Tags:</label>
55+
<input type="text" id="input-clear" class="input-clear demo-default" value="awesome,neat">
56+
</div>
57+
<div class="control-group">
58+
<label for="input-clear-disable">Tags:</label>
59+
<input type="text" disabled id="input-clear-disable" class="input-clear demo-default" value="awesome,neat">
60+
</div>
61+
<script>
62+
$('.input-clear').selectize({
63+
plugins: ['clear_button'],
64+
persist: false,
65+
create: true
66+
});
67+
</script>
68+
</div>
69+
5170
<div class="demo">
5271
<h2>Plugin: "restore_on_backspace"</h2>
5372
<div class="control-group">

src/plugins/clear_button/plugin.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Plugin: "clear_button" (selectize.js)
3+
* Copyright (c) 2017 Alexey Kolotovchenkov & contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6+
* file except in compliance with the License. You may obtain a copy of the License at:
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under
10+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
* ANY KIND, either express or implied. See the License for the specific language
12+
* governing permissions and limitations under the License.
13+
*
14+
* @author Alexey Kolotovchenkov <[email protected]>
15+
*/
16+
17+
Selectize.define('clear_button', function(options) {
18+
var self = this;
19+
20+
options = $.extend({
21+
label : '&times;',
22+
title : 'Clear',
23+
className : 'clear'
24+
}, options);
25+
26+
self.setup = (function() {
27+
var original = self.setup;
28+
return function() {
29+
original.apply(self, arguments);
30+
31+
var html = '<a href="javascript:void(0)" class="' + options.className + '" tabindex="-1" title="' + escape_html(options.title) + '">' + options.label + '</a>';
32+
33+
self.$control.append(html);
34+
35+
// add event listener
36+
self.$control
37+
.on('mousedown', 'a.' + options.className, function(e) {
38+
// prevent toggling
39+
e.preventDefault();
40+
e.stopPropagation();
41+
})
42+
.on('click', 'a.' + options.className, function(e) {
43+
e.preventDefault();
44+
e.stopPropagation();
45+
if (self.isLocked) return;
46+
47+
self.clear();
48+
self.close();
49+
});
50+
};
51+
})();
52+
});

src/plugins/clear_button/plugin.less

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.selectize-control.plugin-clear_button {
2+
&.single .selectize-input:after {
3+
content: none;
4+
}
5+
6+
.clear {
7+
display: none;
8+
position: absolute;
9+
top: @selectize-padding-y;
10+
right: @selectize-padding-item-x;
11+
font-size: 1.5em;
12+
padding: @selectize-padding-item-y @selectize-padding-item-x;
13+
color: @selectize-color-border;
14+
outline: 0;
15+
16+
&:hover {
17+
color: @selectize-color-text;
18+
}
19+
}
20+
21+
.selectize-input.has-items {
22+
.clear {
23+
display: inline-block;
24+
}
25+
}
26+
27+
.disabled {
28+
.clear {
29+
color: @selectize-color-border;
30+
}
31+
}
32+
33+
&.single .selectize-input.has-items,
34+
&.multi .selectize-input.has-items {
35+
padding-right: @selectize-padding-x + 2*@selectize-padding-item-x;
36+
}
37+
}

src/selectize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ $.extend(Selectize.prototype, {
18111811
var self = this;
18121812

18131813
if (!self.items.length) return;
1814-
self.$control.children(':not(input)').remove();
1814+
self.$control.children('[data-value]').remove();
18151815
self.items = [];
18161816
self.lastQuery = null;
18171817
self.setCaret(0);

0 commit comments

Comments
 (0)