import { t } from "../services/i18n.js"; import NoteContextAwareWidget from "./note_context_aware_widget.js"; const TPL = `
`; export default class SwitchWidget extends NoteContextAwareWidget { private $switchButton!: JQuery; private $switchToggle!: JQuery; private $switchName!: JQuery; protected $helpButton!: JQuery; protected switchOnName = ""; protected switchOnTooltip = ""; protected switchOffName = ""; protected switchOffTooltip = ""; private disabledTooltip = ""; private currentState = false; doRender() { this.$widget = $(TPL); this.$switchButton = this.$widget.find(".switch-button"); this.$switchToggle = this.$widget.find(".switch-toggle"); this.$switchToggle.on("click", (e) => { this.toggle(!this.currentState); // Prevent the check box from being toggled by the click, the value of the check box // should be set exclusively by the 'isToggled' property setter. e.preventDefault(); }); this.$switchName = this.$widget.find(".switch-name"); this.$helpButton = this.$widget.find(".switch-help-button"); } toggle(state: boolean) { if (state) { this.switchOn(); } else { this.switchOff(); } } switchOff() {} switchOn() {} /** Gets or sets whether the switch is toggled. */ get isToggled() { return this.currentState; } set isToggled(state) { this.currentState = !!state; this.$switchButton.toggleClass("on", this.currentState); this.$switchToggle.prop("checked", this.currentState); if (this.currentState) { this.$switchName.text(this.switchOffName); this.$switchButton.attr("title", this.switchOffTooltip); } else { this.$switchName.text(this.switchOnName); this.$switchButton.attr("title", this.switchOnTooltip); } } /** Gets or sets whether the switch is enabled. */ get canToggle() { return (!this.$switchButton.hasClass("disabled")); } set canToggle(isEnabled) { this.$switchButton.toggleClass("disabled", !isEnabled); this.$switchToggle.attr("disabled", !isEnabled ? "disabled" : null); if (isEnabled) { this.isToggled = this.currentState; // Reapply the correct tooltip } else { this.$switchButton.attr("title", this.disabledTooltip); } } }