debian-mirror-gitlab/app/assets/javascripts/droplab/plugins/input_setter.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable */
const InputSetter = {
init(hook) {
this.hook = hook;
this.destroyed = false;
this.config = hook.config.InputSetter || (this.hook.config.InputSetter = {});
this.eventWrapper = {};
this.addEvents();
},
addEvents() {
this.eventWrapper.setInputs = this.setInputs.bind(this);
this.hook.list.list.addEventListener('click.dl', this.eventWrapper.setInputs);
},
removeEvents() {
this.hook.list.list.removeEventListener('click.dl', this.eventWrapper.setInputs);
},
setInputs(e) {
if (this.destroyed) return;
const selectedItem = e.detail.selected;
if (!Array.isArray(this.config)) this.config = [this.config];
2021-03-08 18:12:59 +05:30
this.config.forEach((config) => this.setInput(config, selectedItem));
2017-08-17 22:00:37 +05:30
},
setInput(config, selectedItem) {
const input = config.input || this.hook.trigger;
const newValue = selectedItem.getAttribute(config.valueAttribute);
const inputAttribute = config.inputAttribute;
if (input.hasAttribute(inputAttribute)) return input.setAttribute(inputAttribute, newValue);
2018-12-13 13:39:08 +05:30
if (input.tagName === 'INPUT') return (input.value = newValue);
return (input.textContent = newValue);
2017-08-17 22:00:37 +05:30
},
destroy() {
this.destroyed = true;
this.removeEvents();
},
};
export default InputSetter;