2018-03-18 23:13:46 +08:00
|
|
|
|
|
|
|
class ToggleSwitch {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
2019-02-02 23:49:57 +08:00
|
|
|
this.input = elem.querySelector('input[type=hidden]');
|
|
|
|
this.checkbox = elem.querySelector('input[type=checkbox]');
|
2018-03-18 23:13:46 +08:00
|
|
|
|
2019-06-04 17:45:44 +08:00
|
|
|
this.checkbox.addEventListener('change', this.stateChange.bind(this));
|
2018-03-18 23:13:46 +08:00
|
|
|
}
|
|
|
|
|
2019-06-04 17:45:44 +08:00
|
|
|
stateChange() {
|
|
|
|
this.input.value = (this.checkbox.checked ? 'true' : 'false');
|
2019-08-18 20:11:30 +08:00
|
|
|
|
|
|
|
// Dispatch change event from hidden input so they can be listened to
|
|
|
|
// like a normal checkbox.
|
|
|
|
const changeEvent = new Event('change');
|
|
|
|
this.input.dispatchEvent(changeEvent);
|
2018-03-18 23:13:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default ToggleSwitch;
|