debian-mirror-gitlab/app/assets/javascripts/droplab/hook_input.js

118 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
import Hook from './hook';
2017-09-10 17:25:29 +05:30
class HookInput extends Hook {
constructor(trigger, list, plugins, config) {
super(trigger, list, plugins, config);
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
this.type = 'input';
this.event = 'input';
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
this.eventWrapper = {};
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
this.addEvents();
this.addPlugins();
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
addPlugins() {
2021-03-08 18:12:59 +05:30
this.plugins.forEach((plugin) => plugin.init(this));
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
addEvents() {
2017-08-17 22:00:37 +05:30
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-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
removeEvents() {
2017-08-17 22:00:37 +05:30
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-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
input(e) {
if (this.hasRemovedEvents) return;
2017-08-17 22:00:37 +05:30
this.list.show();
const inputEvent = new CustomEvent('input.dl', {
detail: {
hook: this,
text: e.target.value,
},
bubbles: true,
2017-09-10 17:25:29 +05:30
cancelable: true,
2017-08-17 22:00:37 +05:30
});
e.target.dispatchEvent(inputEvent);
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
mousedown(e) {
2017-08-17 22:00:37 +05:30
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-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
keyup(e) {
2017-08-17 22:00:37 +05:30
if (this.hasRemovedEvents) return;
this.keyEvent(e, 'keyup.dl');
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
keydown(e) {
2017-08-17 22:00:37 +05:30
if (this.hasRemovedEvents) return;
this.keyEvent(e, 'keydown.dl');
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
keyEvent(e, eventName) {
2017-08-17 22:00:37 +05:30
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-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
restoreInitialState() {
2017-08-17 22:00:37 +05:30
this.list.list.innerHTML = this.list.initialState;
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
removePlugins() {
2021-03-08 18:12:59 +05:30
this.plugins.forEach((plugin) => plugin.destroy());
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
destroy() {
2017-08-17 22:00:37 +05:30
this.restoreInitialState();
this.removeEvents();
this.removePlugins();
this.list.destroy();
}
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
export default HookInput;