Skip to content

Instantly share code, notes, and snippets.

@Traineratwot
Created January 5, 2022 13:08
Show Gist options
  • Save Traineratwot/c2cf90eeb278f6872b395ee1e6c45efc to your computer and use it in GitHub Desktop.
Save Traineratwot/c2cf90eeb278f6872b395ee1e6c45efc to your computer and use it in GitHub Desktop.
class ThreeWayCheckbox {
private elem: JQuery<HTMLElement>;
state = 0;
private callbacks: Function[] = [];
constructor(elem: JQuery<HTMLElement>) {
this.elem = elem
try {
this.elem.on('click', (e) => {
switch (this.state) {
case 0:
this.elem.prop('checked', true)
// @ts-ignore
this.elem[0].indeterminate = false
this.state = 1
break;
case 1:
this.elem.prop('checked', false)
// @ts-ignore
this.elem[0].indeterminate = true
this.state = -1
break;
case -1:
this.elem.prop('checked', false)
// @ts-ignore
this.elem[0].indeterminate = false
this.state = 0
break;
}
// @ts-ignore
this.elem.trigger('blur')
this.__change()
})
} catch (e) {
}
}
change(callback: (name: string, state: number) => void) {
this.callbacks.push(callback)
}
__change() {
for (const fn in this.callbacks) {
this.callbacks[fn](this.elem.attr('name'), this.state);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment