2017-02-20 08:51:47 -05:00
|
|
|
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]');
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return [].map.call(ins, (anIn) => BindInOut.init(anIn));
|
2017-02-20 08:51:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static init(anIn, anOut) {
|
|
|
|
const out = anOut || document.querySelector(`*[data-bind-out="${anIn.dataset.bindIn}"]`);
|
2017-03-06 10:15:01 -05:00
|
|
|
|
2017-03-06 13:51:08 -05:00
|
|
|
if (!out) return null;
|
2017-03-06 10:15:01 -05:00
|
|
|
|
2017-02-20 08:51:47 -05:00
|
|
|
const bindInOut = new BindInOut(anIn, out);
|
|
|
|
|
|
|
|
return bindInOut.addEvents().updateOut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:55:22 -05:00
|
|
|
export default BindInOut;
|