2017-04-07 09:57:03 -04:00
|
|
|
import Hook from './hook';
|
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
class HookInput extends Hook {
|
|
|
|
constructor(trigger, list, plugins, config) {
|
|
|
|
super(trigger, list, plugins, config);
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
this.type = 'input';
|
|
|
|
this.event = 'input';
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
this.eventWrapper = {};
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
this.addEvents();
|
|
|
|
this.addPlugins();
|
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
addPlugins() {
|
2017-04-07 09:57:03 -04:00
|
|
|
this.plugins.forEach(plugin => plugin.init(this));
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
addEvents() {
|
2017-04-07 09:57:03 -04:00
|
|
|
this.eventWrapper.mousedown = this.mousedown.bind(this);
|
|
|
|
this.eventWrapper.input = this.input.bind(this);
|
|
|
|
this.eventWrapper.keyup = this.keyup.bind(this);
|
|
|
|
this.eventWrapper.keydown = this.keydown.bind(this);
|
|
|
|
|
|
|
|
this.trigger.addEventListener('mousedown', this.eventWrapper.mousedown);
|
|
|
|
this.trigger.addEventListener('input', this.eventWrapper.input);
|
|
|
|
this.trigger.addEventListener('keyup', this.eventWrapper.keyup);
|
|
|
|
this.trigger.addEventListener('keydown', this.eventWrapper.keydown);
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
removeEvents() {
|
2017-04-07 09:57:03 -04:00
|
|
|
this.hasRemovedEvents = true;
|
|
|
|
|
|
|
|
this.trigger.removeEventListener('mousedown', this.eventWrapper.mousedown);
|
|
|
|
this.trigger.removeEventListener('input', this.eventWrapper.input);
|
|
|
|
this.trigger.removeEventListener('keyup', this.eventWrapper.keyup);
|
|
|
|
this.trigger.removeEventListener('keydown', this.eventWrapper.keydown);
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
input(e) {
|
|
|
|
if (this.hasRemovedEvents) return;
|
2017-04-07 09:57:03 -04:00
|
|
|
|
|
|
|
this.list.show();
|
|
|
|
|
|
|
|
const inputEvent = new CustomEvent('input.dl', {
|
|
|
|
detail: {
|
|
|
|
hook: this,
|
|
|
|
text: e.target.value,
|
|
|
|
},
|
|
|
|
bubbles: true,
|
2017-05-12 13:16:47 -04:00
|
|
|
cancelable: true,
|
2017-04-07 09:57:03 -04:00
|
|
|
});
|
|
|
|
e.target.dispatchEvent(inputEvent);
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
mousedown(e) {
|
2017-04-07 09:57:03 -04:00
|
|
|
if (this.hasRemovedEvents) return;
|
|
|
|
|
|
|
|
const mouseEvent = new CustomEvent('mousedown.dl', {
|
|
|
|
detail: {
|
|
|
|
hook: this,
|
|
|
|
text: e.target.value,
|
|
|
|
},
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
});
|
|
|
|
e.target.dispatchEvent(mouseEvent);
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
keyup(e) {
|
2017-04-07 09:57:03 -04:00
|
|
|
if (this.hasRemovedEvents) return;
|
|
|
|
|
|
|
|
this.keyEvent(e, 'keyup.dl');
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
keydown(e) {
|
2017-04-07 09:57:03 -04:00
|
|
|
if (this.hasRemovedEvents) return;
|
|
|
|
|
|
|
|
this.keyEvent(e, 'keydown.dl');
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
keyEvent(e, eventName) {
|
2017-04-07 09:57:03 -04:00
|
|
|
this.list.show();
|
|
|
|
|
|
|
|
const keyEvent = new CustomEvent(eventName, {
|
|
|
|
detail: {
|
|
|
|
hook: this,
|
|
|
|
text: e.target.value,
|
|
|
|
which: e.which,
|
|
|
|
key: e.key,
|
|
|
|
},
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
});
|
|
|
|
e.target.dispatchEvent(keyEvent);
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
restoreInitialState() {
|
2017-04-07 09:57:03 -04:00
|
|
|
this.list.list.innerHTML = this.list.initialState;
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
removePlugins() {
|
2017-04-07 09:57:03 -04:00
|
|
|
this.plugins.forEach(plugin => plugin.destroy());
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
2017-05-12 13:16:47 -04:00
|
|
|
destroy() {
|
2017-04-07 09:57:03 -04:00
|
|
|
this.restoreInitialState();
|
|
|
|
|
|
|
|
this.removeEvents();
|
|
|
|
this.removePlugins();
|
|
|
|
|
|
|
|
this.list.destroy();
|
|
|
|
}
|
2017-05-12 13:16:47 -04:00
|
|
|
}
|
2017-04-07 09:57:03 -04:00
|
|
|
|
|
|
|
export default HookInput;
|