Skip to content

Instantly share code, notes, and snippets.

@mdamien
Created May 9, 2016 14:54
Show Gist options
  • Save mdamien/bff088d4a05e2734c4b0dda79e0593b3 to your computer and use it in GitHub Desktop.
Save mdamien/bff088d4a05e2734c4b0dda79e0593b3 to your computer and use it in GitHub Desktop.
double click to edit for selectize
import Selectize from 'selectize';
// https://gist.github.com/bolasblack/1e099fa2055c846106ac
Selectize.define('editable_tag', () => {
this._createItemWhenInputBlur = function() {
if (this.$control_input.val() && this.editingTag) {
this.createItem(null, false);
this.editingTag = false;
return this.editingTag;
}
};
this.setup = (() => {
const original = this.setup;
return () => {
const res = original.apply(this, arguments);
this.$control.on('dblclick', (event) => {
const value = $(event.target).data('value');
const itemIndex = this.items.indexOf(value);
if (itemIndex >= 0 && itemIndex < this.items.length) {
const option = this.options[this.items[itemIndex]];
if (!option.isLocked && this.deleteSelection(event)) {
this.setTextboxValue(option[this.settings.labelField]);
this.setCaret(itemIndex);
}
this.editingTag = true;
event.preventDefault();
return;
}
});
return res;
};
})();
this.setTextboxValue = (() => {
const original = this.setTextboxValue;
return () => {
if (!this._restoringUpdateItem) {
this._restoringUpdateItem = true;
this._createItemWhenInputBlur();
this._restoringUpdateItem = false;
}
return original.apply(this, arguments);
};
})();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment