Skip to content

Commit 6a174ea

Browse files
committed
Merge pull request #848 from ChoppyThing/master
Plugin removeButton: Make it work with single option mode
2 parents 898a44d + 856307c commit 6a174ea

File tree

2 files changed

+105
-46
lines changed

2 files changed

+105
-46
lines changed

src/plugins/remove_button/plugin.js

Lines changed: 98 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,56 +15,109 @@
1515
*/
1616

1717
Selectize.define('remove_button', function(options) {
18-
if (this.settings.mode === 'single') return;
19-
2018
options = $.extend({
21-
label : '×',
22-
title : 'Remove',
23-
className : 'remove',
24-
append : true
25-
}, options);
26-
27-
var self = this;
28-
var html = '<a href="javascript:void(0)" class="' + options.className + '" tabindex="-1" title="' + escape_html(options.title) + '">' + options.label + '</a>';
29-
30-
/**
31-
* Appends an element as a child (with raw HTML).
32-
*
33-
* @param {string} html_container
34-
* @param {string} html_element
35-
* @return {string}
36-
*/
37-
var append = function(html_container, html_element) {
38-
var pos = html_container.search(/(<\/[^>]+>\s*)$/);
39-
return html_container.substring(0, pos) + html_element + html_container.substring(pos);
40-
};
41-
42-
this.setup = (function() {
43-
var original = self.setup;
44-
return function() {
45-
// override the item rendering method to add the button to each
46-
if (options.append) {
47-
var render_item = self.settings.render.item;
48-
self.settings.render.item = function(data) {
49-
return append(render_item.apply(this, arguments), html);
19+
label : '&times;',
20+
title : 'Remove',
21+
className : 'remove',
22+
append : true
23+
}, options);
24+
25+
var singleClose = function(thisRef, options) {
26+
27+
options.className = 'remove-single';
28+
29+
var self = thisRef;
30+
var html = '<a href="javascript:void(0)" class="' + options.className + '" tabindex="-1" title="' + escape_html(options.title) + '">' + options.label + '</a>';
31+
32+
/**
33+
* Appends an element as a child (with raw HTML).
34+
*
35+
* @param {string} html_container
36+
* @param {string} html_element
37+
* @return {string}
38+
*/
39+
var append = function(html_container, html_element) {
40+
return html_container + html_element;
41+
};
42+
43+
thisRef.setup = (function() {
44+
var original = self.setup;
45+
return function() {
46+
// override the item rendering method to add the button to each
47+
if (options.append) {
48+
var id = $(self.$input.context).attr('id');
49+
var selectizer = $('#'+id);
50+
51+
var render_item = self.settings.render.item;
52+
self.settings.render.item = function(data) {
53+
return append(render_item.apply(thisRef, arguments), html);
54+
};
55+
}
56+
57+
original.apply(thisRef, arguments);
58+
59+
// add event listener
60+
thisRef.$control.on('click', '.' + options.className, function(e) {
61+
e.preventDefault();
62+
if (self.isLocked) return;
63+
64+
self.clear();
65+
});
66+
5067
};
51-
}
68+
})();
69+
};
70+
71+
var multiClose = function(thisRef, options) {
5272

53-
original.apply(this, arguments);
73+
var self = thisRef;
74+
var html = '<a href="javascript:void(0)" class="' + options.className + '" tabindex="-1" title="' + escape_html(options.title) + '">' + options.label + '</a>';
5475

55-
// add event listener
56-
this.$control.on('click', '.' + options.className, function(e) {
57-
e.preventDefault();
58-
if (self.isLocked) return;
76+
/**
77+
* Appends an element as a child (with raw HTML).
78+
*
79+
* @param {string} html_container
80+
* @param {string} html_element
81+
* @return {string}
82+
*/
83+
var append = function(html_container, html_element) {
84+
var pos = html_container.search(/(<\/[^>]+>\s*)$/);
85+
return html_container.substring(0, pos) + html_element + html_container.substring(pos);
86+
};
5987

60-
var $item = $(e.currentTarget).parent();
61-
self.setActiveItem($item);
62-
if (self.deleteSelection()) {
63-
self.setCaret(self.items.length);
64-
}
65-
});
88+
thisRef.setup = (function() {
89+
var original = self.setup;
90+
return function() {
91+
// override the item rendering method to add the button to each
92+
if (options.append) {
93+
var render_item = self.settings.render.item;
94+
self.settings.render.item = function(data) {
95+
return append(render_item.apply(thisRef, arguments), html);
96+
};
97+
}
6698

99+
original.apply(thisRef, arguments);
100+
101+
// add event listener
102+
thisRef.$control.on('click', '.' + options.className, function(e) {
103+
e.preventDefault();
104+
if (self.isLocked) return;
105+
106+
var $item = $(e.currentTarget).parent();
107+
self.setActiveItem($item);
108+
if (self.deleteSelection()) {
109+
self.setCaret(self.items.length);
110+
}
111+
});
112+
113+
};
114+
})();
67115
};
68-
})();
69116

70-
});
117+
if (this.settings.mode === 'single') {
118+
singleClose(this, options);
119+
return;
120+
} else {
121+
multiClose(this, options);
122+
}
123+
});

src/plugins/remove_button/plugin.less

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@
3434
.disabled [data-value] .remove {
3535
border-left-color: lighten(desaturate(@selectize-color-item-border, 100%), @selectize-lighten-disabled-item-border);
3636
}
37-
}
37+
.remove-single {
38+
position: absolute;
39+
right: 28px;
40+
top: 6px;
41+
font-size: 23px;
42+
}
43+
}

0 commit comments

Comments
 (0)