Skip to content

Commit ea2e931

Browse files
committed
Unify remove_button code + 'onDelete' bugfix
- the remove-button plugin was splitted in two functions (for 'multi' and 'single' mode). This led to divergence in behaviour, fixes, and triggered events (856307c). This commit unifies the code, congruently with the rest of the codebase, making the behaviour again more stable and similar, regardless of the mode (single/multi). - the remove-button plugin, when in 'single' mode, was not able to trigger the 'onDelete' callback, preventing from doing checks on the values; while that was possible just witht he 'multi' mode. Now both modes are able to trigger the callback.
1 parent bc5d943 commit ea2e931

File tree

1 file changed

+49
-95
lines changed

1 file changed

+49
-95
lines changed

src/plugins/remove_button/plugin.js

Lines changed: 49 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -22,105 +22,59 @@ Selectize.define('remove_button', function(options) {
2222
append : true
2323
}, options);
2424

25-
var singleClose = function(thisRef, options) {
26-
25+
var self = this;
26+
if (this.settings.mode === 'single') {
2727
options.className = options.className || 'remove-single';
28+
}
2829

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-
var pos = html_container.search(/(<\/[^>]+>\s*)$/);
41-
return html_container.substring(0, pos) + html_element + html_container.substring(pos);
42-
};
43-
44-
thisRef.setup = (function() {
45-
var original = self.setup;
46-
return function() {
47-
// override the item rendering method to add the button to each
48-
if (options.append) {
49-
var id = $(self.$input.context).attr('id');
50-
var selectizer = $('#'+id);
51-
52-
var render_item = self.settings.render.item;
53-
self.settings.render.item = function(data) {
54-
return append(render_item.apply(thisRef, arguments), html);
55-
};
56-
}
57-
58-
original.apply(thisRef, arguments);
59-
60-
// add event listener
61-
thisRef.$control.on('click', '.' + options.className, function(e) {
62-
e.preventDefault();
63-
if (self.isLocked) return;
64-
65-
// use removeItem - clear() will not trigger the 'item_remove' event
66-
var $item = $(e.currentTarget).parent();
67-
self.removeItem($item);
68-
});
69-
70-
};
71-
})();
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+
var pos = html_container.search(/(<\/[^>]+>\s*)$/);
41+
return html_container.substring(0, pos) + html_element + html_container.substring(pos);
7242
};
7343

74-
var multiClose = function(thisRef, options) {
75-
76-
var self = thisRef;
77-
var html = '<a href="javascript:void(0)" class="' + options.className + '" tabindex="-1" title="' + escape_html(options.title) + '">' + options.label + '</a>';
78-
79-
/**
80-
* Appends an element as a child (with raw HTML).
81-
*
82-
* @param {string} html_container
83-
* @param {string} html_element
84-
* @return {string}
85-
*/
86-
var append = function(html_container, html_element) {
87-
var pos = html_container.search(/(<\/[^>]+>\s*)$/);
88-
return html_container.substring(0, pos) + html_element + html_container.substring(pos);
89-
};
90-
91-
thisRef.setup = (function() {
92-
var original = self.setup;
93-
return function() {
94-
// override the item rendering method to add the button to each
95-
if (options.append) {
96-
var render_item = self.settings.render.item;
97-
self.settings.render.item = function(data) {
98-
return append(render_item.apply(thisRef, arguments), html);
99-
};
100-
}
101-
102-
original.apply(thisRef, arguments);
103-
104-
// add event listener
105-
thisRef.$control.on('click', '.' + options.className, function(e) {
106-
e.preventDefault();
107-
if (self.isLocked) return;
108-
109-
var $item = $(e.currentTarget).parent();
44+
self.setup = (function() {
45+
var original = self.setup;
46+
return function() {
47+
// override the item rendering method to add the button to each
48+
if (options.append) {
49+
var render_item = self.settings.render.item;
50+
self.settings.render.item = function(data) {
51+
return append(render_item.apply(self, arguments), html);
52+
};
53+
}
54+
55+
original.apply(self, arguments);
56+
57+
// add event listener
58+
self.$control.on('click', '.' + options.className, function(e) {
59+
e.preventDefault();
60+
if (self.isLocked) return;
61+
62+
// use deleteSelection()
63+
// clear() will not trigger the 'item_remove' event
64+
// removeItem() will not trigger the 'onDelete' callback
65+
var $item = $(e.currentTarget).parent();
66+
67+
if (self.settings.mode === 'single') {
68+
self.setCaret(); // 1 for 'single'
69+
e['keyCode'] = KEY_BACKSPACE; // makes deleteSelection calculate the correct direction
70+
} else {
11071
self.setActiveItem($item);
111-
if (self.deleteSelection()) {
112-
self.setCaret(self.items.length);
113-
}
114-
});
115-
116-
};
117-
})();
118-
};
72+
}
73+
if (self.deleteSelection(e)) {
74+
self.setCaret(self.items.length);
75+
}
76+
});
11977

120-
if (this.settings.mode === 'single') {
121-
singleClose(this, options);
122-
return;
123-
} else {
124-
multiClose(this, options);
125-
}
78+
};
79+
})();
12680
});

0 commit comments

Comments
 (0)