(root: JQuery, state: Record<string, boolean> | undefined)
| 40 | private readonly state: Record<string, boolean> = {}; |
| 41 | |
| 42 | constructor(root: JQuery, state: Record<string, boolean> | undefined) { |
| 43 | super(); |
| 44 | this.buttons = root.find('.button-checkbox'); |
| 45 | |
| 46 | for (const element of this.buttons) { |
| 47 | const widget = $(element); |
| 48 | const button = widget.find('button'); |
| 49 | const checkbox = widget.find('input:checkbox'); |
| 50 | const bind = button.data('bind'); |
| 51 | |
| 52 | // copy relevant parts of the state |
| 53 | this.state[bind] = state && bind in state ? state[bind] : checkbox.is(':checked'); |
| 54 | |
| 55 | // Event Handlers |
| 56 | button.on('click', e => { |
| 57 | checkbox.prop('checked', !checkbox.is(':checked')); |
| 58 | checkbox.triggerHandler('change'); |
| 59 | e.stopPropagation(); |
| 60 | }); |
| 61 | checkbox.on('change', () => { |
| 62 | this.updateButtonDisplay(button, checkbox); |
| 63 | }); |
| 64 | |
| 65 | this.updateButtonDisplay(button, checkbox, this.state[bind]); |
| 66 | |
| 67 | // Inject the icon if applicable |
| 68 | if (button.find('.state-icon').length === 0) { |
| 69 | button.prepend( |
| 70 | '<i class="state-icon ' + settings[button.data('state') as keyof typeof settings].icon + '"></i> ', |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private updateButtonDisplay(button: JQuery, checkbox: JQuery, forcedState?: boolean) { |
| 77 | if (forcedState !== undefined) { |
nothing calls this directly
no test coverage detected