debian-mirror-gitlab/app/assets/javascripts/behaviors/bind_in_out.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

48 lines
1,021 B
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
class BindInOut {
constructor(bindIn, bindOut) {
this.in = bindIn;
this.out = bindOut;
this.eventWrapper = {};
this.eventType = /(INPUT|TEXTAREA)/.test(bindIn.tagName) ? 'keyup' : 'change';
}
addEvents() {
this.eventWrapper.updateOut = this.updateOut.bind(this);
this.in.addEventListener(this.eventType, this.eventWrapper.updateOut);
return this;
}
updateOut() {
this.out.textContent = this.in.value;
return this;
}
removeEvents() {
this.in.removeEventListener(this.eventType, this.eventWrapper.updateOut);
return this;
}
static initAll() {
const ins = document.querySelectorAll('*[data-bind-in]');
2021-03-08 18:12:59 +05:30
return [].map.call(ins, (anIn) => BindInOut.init(anIn));
2017-08-17 22:00:37 +05:30
}
static init(anIn, anOut) {
const out = anOut || document.querySelector(`*[data-bind-out="${anIn.dataset.bindIn}"]`);
if (!out) return null;
const bindInOut = new BindInOut(anIn, out);
return bindInOut.addEvents().updateOut();
}
}
export default BindInOut;