]> BookStack Code Mirror - bookstack/blob - resources/js/components/toggle-switch.js
b9b96afc5d07728d992e9cd49eab9e29a6df1f2a
[bookstack] / resources / js / components / toggle-switch.js
1
2 class ToggleSwitch {
3
4     constructor(elem) {
5         this.elem = elem;
6         this.input = elem.querySelector('input[type=hidden]');
7         this.checkbox = elem.querySelector('input[type=checkbox]');
8
9         this.checkbox.addEventListener('change', this.stateChange.bind(this));
10     }
11
12     stateChange() {
13         this.input.value = (this.checkbox.checked ? 'true' : 'false');
14
15         // Dispatch change event from hidden input so they can be listened to
16         // like a normal checkbox.
17         const changeEvent = new Event('change');
18         this.input.dispatchEvent(changeEvent);
19     }
20
21 }
22
23 export default ToggleSwitch;