diff options
author | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-04-07 13:57:03 +0000 |
---|---|---|
committer | Jacob Schatz <jschatz@gitlab.com> | 2017-04-07 13:57:03 +0000 |
commit | dab60475c79ff272c2a941326d609fed5fa4032e (patch) | |
tree | 8b9a5b9f269c6d39f6dcafc8b432ae2d9af7fbd4 /app/assets/javascripts/droplab/plugins/input_setter.js | |
parent | 780a8968a50ff1b23eea6a72600f5f19a01cb81a (diff) | |
download | gitlab-ce-dab60475c79ff272c2a941326d609fed5fa4032e.tar.gz |
Update droplab
Diffstat (limited to 'app/assets/javascripts/droplab/plugins/input_setter.js')
-rw-r--r-- | app/assets/javascripts/droplab/plugins/input_setter.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/app/assets/javascripts/droplab/plugins/input_setter.js b/app/assets/javascripts/droplab/plugins/input_setter.js new file mode 100644 index 00000000000..c292cfa7b8f --- /dev/null +++ b/app/assets/javascripts/droplab/plugins/input_setter.js @@ -0,0 +1,52 @@ +/* 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]; + + this.config.forEach(config => this.setInput(config, selectedItem)); + }, + + setInput(config, selectedItem) { + const input = config.input || this.hook.trigger; + const newValue = selectedItem.getAttribute(config.valueAttribute); + const inputAttribute = config.inputAttribute; + + if (!newValue) return; + + if (input.hasAttribute(inputAttribute)) return input.setAttribute(inputAttribute, newValue); + if (input.tagName === 'INPUT') return input.value = newValue; + return input.textContent = newValue; + }, + + destroy() { + this.destroyed = true; + + this.removeEvents(); + }, +}; + +export default InputSetter; |