|
@@ -1638,8 +1638,9 @@ S2.define('select2/selection/placeholder',[
|
|
|
});
|
|
|
|
|
|
S2.define('select2/selection/allowClear',[
|
|
|
- 'jquery'
|
|
|
-], function ($) {
|
|
|
+ 'jquery',
|
|
|
+ '../keys'
|
|
|
+], function ($, KEYS) {
|
|
|
function AllowClear () { }
|
|
|
|
|
|
AllowClear.prototype.bind = function (decorated, container, $container) {
|
|
@@ -1647,8 +1648,8 @@ S2.define('select2/selection/allowClear',[
|
|
|
|
|
|
decorated.call(this, container, $container);
|
|
|
|
|
|
- if (self.placeholder == null) {
|
|
|
- if (self.options.get('debug') && window.console && console.error) {
|
|
|
+ if (this.placeholder == null) {
|
|
|
+ if (this.options.get('debug') && window.console && console.error) {
|
|
|
console.error(
|
|
|
'Select2: The `allowClear` option should be used in combination ' +
|
|
|
'with the `placeholder` option.'
|
|
@@ -1658,34 +1659,61 @@ S2.define('select2/selection/allowClear',[
|
|
|
|
|
|
this.$selection.on('mousedown', '.select2-selection__clear',
|
|
|
function (evt) {
|
|
|
- // Ignore the event if it is disabled
|
|
|
- if (self.options.get('disabled')) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ self._handleClear(evt);
|
|
|
+ });
|
|
|
|
|
|
- evt.stopPropagation();
|
|
|
+ container.on('keypress', function (evt) {
|
|
|
+ self._handleKeyboardClear(evt, container);
|
|
|
+ });
|
|
|
+ };
|
|
|
|
|
|
- var data = $(this).data('data');
|
|
|
+ AllowClear.prototype._handleClear = function (_, evt) {
|
|
|
+ console.log(arguments);
|
|
|
|
|
|
- for (var d = 0; d < data.length; d++) {
|
|
|
- var unselectData = {
|
|
|
- data: data[d]
|
|
|
- };
|
|
|
+ // Ignore the event if it is disabled
|
|
|
+ if (this.options.get('disabled')) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // Trigger the `unselect` event, so people can prevent it from being
|
|
|
- // cleared.
|
|
|
- self.trigger('unselect', unselectData);
|
|
|
+ var $clear = this.$selection.find('.select2-selection__clear');
|
|
|
|
|
|
- // If the event was prevented, don't clear it out.
|
|
|
- if (unselectData.prevented) {
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
+ // Ignore the event if nothing has been selected
|
|
|
+ if ($clear.length === 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- self.$element.val(self.placeholder.id).trigger('change');
|
|
|
+ evt.stopPropagation();
|
|
|
|
|
|
- self.trigger('toggle');
|
|
|
- });
|
|
|
+ var data = $clear.data('data');
|
|
|
+
|
|
|
+ for (var d = 0; d < data.length; d++) {
|
|
|
+ var unselectData = {
|
|
|
+ data: data[d]
|
|
|
+ };
|
|
|
+
|
|
|
+ // Trigger the `unselect` event, so people can prevent it from being
|
|
|
+ // cleared.
|
|
|
+ this.trigger('unselect', unselectData);
|
|
|
+
|
|
|
+ // If the event was prevented, don't clear it out.
|
|
|
+ if (unselectData.prevented) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ this.$element.val(this.placeholder.id).trigger('change');
|
|
|
+
|
|
|
+ this.trigger('toggle');
|
|
|
+ };
|
|
|
+
|
|
|
+ AllowClear.prototype._handleKeyboardClear = function (_, evt, container) {
|
|
|
+ if (container.isOpen()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
|
|
|
+ this._handleClear(evt);
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
AllowClear.prototype.update = function (decorated, data) {
|