gitlab-org--gitlab-foss/app/assets/javascripts/droplab/hook_button.js

59 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-04-07 09:57:03 -04:00
import Hook from './hook';
class HookButton extends Hook {
constructor(trigger, list, plugins, config) {
super(trigger, list, plugins, config);
2017-04-07 09:57:03 -04:00
this.type = 'button';
this.event = 'click';
2017-04-07 09:57:03 -04:00
this.eventWrapper = {};
2017-04-07 09:57:03 -04:00
this.addEvents();
this.addPlugins();
}
2017-04-07 09:57:03 -04:00
addPlugins() {
2017-04-07 09:57:03 -04:00
this.plugins.forEach(plugin => plugin.init(this));
}
2017-04-07 09:57:03 -04:00
clicked(e) {
const buttonEvent = new CustomEvent('click.dl', {
2017-04-07 09:57:03 -04:00
detail: {
hook: this,
},
bubbles: true,
cancelable: true,
2017-04-07 09:57:03 -04:00
});
e.target.dispatchEvent(buttonEvent);
this.list.toggle();
}
2017-04-07 09:57:03 -04:00
addEvents() {
2017-04-07 09:57:03 -04:00
this.eventWrapper.clicked = this.clicked.bind(this);
this.trigger.addEventListener('click', this.eventWrapper.clicked);
}
2017-04-07 09:57:03 -04:00
removeEvents() {
2017-04-07 09:57:03 -04:00
this.trigger.removeEventListener('click', this.eventWrapper.clicked);
}
2017-04-07 09:57:03 -04:00
restoreInitialState() {
2017-04-07 09:57:03 -04:00
this.list.list.innerHTML = this.list.initialState;
}
2017-04-07 09:57:03 -04:00
removePlugins() {
2017-04-07 09:57:03 -04:00
this.plugins.forEach(plugin => plugin.destroy());
}
2017-04-07 09:57:03 -04:00
destroy() {
2017-04-07 09:57:03 -04:00
this.restoreInitialState();
this.removeEvents();
this.removePlugins();
}
}
2017-04-07 09:57:03 -04:00
export default HookButton;