diff --git a/src/public/app/widgets/bookmark_switch.js b/src/public/app/widgets/bookmark_switch.js index acf940c7a..1bd28ccd2 100644 --- a/src/public/app/widgets/bookmark_switch.js +++ b/src/public/app/widgets/bookmark_switch.js @@ -15,11 +15,11 @@ export default class BookmarkSwitchWidget extends SwitchWidget { doRender() { super.doRender(); - this.$switchOnName.text(t("bookmark_switch.bookmark")); - this.$switchOnButton.attr("title", t("bookmark_switch.bookmark_this_note")); + this.switchOnName = t("bookmark_switch.bookmark"); + this.switchOnTooltip = t("bookmark_switch.bookmark_this_note"); - this.$switchOffName.text(t("bookmark_switch.bookmark")); - this.$switchOffButton.attr("title", t("bookmark_switch.remove_bookmark")); + this.switchOffName = t("bookmark_switch.bookmark"); + this.switchOffTooltip = t("bookmark_switch.remove_bookmark"); } async toggle(state) { @@ -33,8 +33,7 @@ export default class BookmarkSwitchWidget extends SwitchWidget { async refreshWithNote(note) { const isBookmarked = !!note.getParentBranches().find((b) => b.parentNoteId === "_lbBookmarks"); - this.$switchOn.toggle(!isBookmarked); - this.$switchOff.toggle(isBookmarked); + this.isToggled = isBookmarked; } entitiesReloadedEvent({ loadResults }) { diff --git a/src/public/app/widgets/protected_note_switch.js b/src/public/app/widgets/protected_note_switch.js index 5833e0dda..134a3633a 100644 --- a/src/public/app/widgets/protected_note_switch.js +++ b/src/public/app/widgets/protected_note_switch.js @@ -6,11 +6,11 @@ export default class ProtectedNoteSwitchWidget extends SwitchWidget { doRender() { super.doRender(); - this.$switchOnName.text(t("protect_note.toggle-on")); - this.$switchOnButton.attr("title", t("protect_note.toggle-on-hint")); + this.switchOnName = t("protect_note.toggle-on"); + this.switchOnTooltip = t("protect_note.toggle-on-hint"); - this.$switchOffName.text(t("protect_note.toggle-off")); - this.$switchOffButton.attr("title", t("protect_note.toggle-off-hint")); + this.switchOffName = t("protect_note.toggle-off"); + this.switchOffTooltip = t("protect_note.toggle-off-hint"); } switchOn() { @@ -22,8 +22,7 @@ export default class ProtectedNoteSwitchWidget extends SwitchWidget { } async refreshWithNote(note) { - this.$switchOn.toggle(!note.isProtected); - this.$switchOff.toggle(!!note.isProtected); + this.isToggled = note.isProtected; } entitiesReloadedEvent({ loadResults }) { diff --git a/src/public/app/widgets/shared_switch.js b/src/public/app/widgets/shared_switch.js index 7e8766ec4..e739ecf32 100644 --- a/src/public/app/widgets/shared_switch.js +++ b/src/public/app/widgets/shared_switch.js @@ -14,11 +14,11 @@ export default class SharedSwitchWidget extends SwitchWidget { doRender() { super.doRender(); - this.$switchOnName.text(t("shared_switch.shared")); - this.$switchOnButton.attr("title", t("shared_switch.toggle-on-title")); + this.switchOnName = t("shared_switch.shared"); + this.switchOnTooltip = t("shared_switch.toggle-on-title"); - this.$switchOffName.text(t("shared_switch.shared")); - this.$switchOffButton.attr("title", t("shared_switch.toggle-off-title")); + this.switchOffName = t("shared_switch.shared"); + this.switchOffTooltip = t("shared_switch.toggle-off-title"); this.$helpButton.attr("data-help-page", "sharing.html").show(); this.$helpButton.on("click", (e) => utils.openHelp($(e.target))); @@ -53,15 +53,14 @@ export default class SharedSwitchWidget extends SwitchWidget { const canBeUnshared = isShared && note.getParentBranches().find((b) => b.parentNoteId === "_share"); const switchDisabled = isShared && !canBeUnshared; - this.$switchOn.toggle(!isShared); - this.$switchOff.toggle(!!isShared); + this.isToggled = isShared; if (switchDisabled) { - this.$widget.attr("title", t("shared_switch.inherited")); - this.$switchOff.addClass("switch-disabled"); + //this.$widget.attr("title", t("shared_switch.inherited")); + //this.$switchOff.addClass("switch-disabled"); } else { - this.$widget.removeAttr("title"); - this.$switchOff.removeClass("switch-disabled"); + //this.$widget.removeAttr("title"); + //this.$switchOff.removeClass("switch-disabled"); } } diff --git a/src/public/app/widgets/switch.js b/src/public/app/widgets/switch.js index 3e76cc632..6d99543b5 100644 --- a/src/public/app/widgets/switch.js +++ b/src/public/app/widgets/switch.js @@ -3,64 +3,34 @@ import NoteContextAwareWidget from "./note_context_aware_widget.js"; const TPL = `
- -
- - +
+   - - -
-
- - -   - - - -
- +
`; export default class SwitchWidget extends NoteContextAwareWidget { + + switchOnName; + switchOnTooltip; + + switchOffName; + switchOffTooltip; + + currentState = false; + doRender() { this.$widget = $(TPL); + this.$switchButton = this.$widget.find(".switch-button"); + this.$switchButton.on("click", () => this.toggle(!this.currentState)); - this.$switchOn = this.$widget.find(".switch-on"); - this.$switchOnName = this.$widget.find(".switch-on-name"); - this.$switchOnButton = this.$widget.find(".switch-on-button"); - - this.$switchOnButton.on("click", () => this.toggle(true)); - - this.$switchOff = this.$widget.find(".switch-off"); - this.$switchOffName = this.$widget.find(".switch-off-name"); - this.$switchOffButton = this.$widget.find(".switch-off-button"); - - this.$switchOffButton.on("click", () => this.toggle(false)); + this.$switchName = this.$widget.find(".switch-name"); this.$helpButton = this.$widget.find(".switch-help-button"); } @@ -123,4 +89,25 @@ export default class SwitchWidget extends NoteContextAwareWidget { switchOff() {} switchOn() {} + + /** Gets or sets whether the switch is toggled. */ + get isToggled() { + return this.currentState; + } + + set isToggled(state) { + this.currentState = !!state; + + if (this.currentState) { + this.$switchName.text(this.switchOffName); + + this.$switchButton.attr("title", this.switchOffTooltip); + this.$switchButton.addClass("on"); + } else { + this.$switchName.text(this.switchOnName); + + this.$switchButton.attr("title", this.switchOnTooltip); + this.$switchButton.removeClass("on"); + } + } } diff --git a/src/public/app/widgets/template_switch.js b/src/public/app/widgets/template_switch.js index e36494c23..6205b95c6 100644 --- a/src/public/app/widgets/template_switch.js +++ b/src/public/app/widgets/template_switch.js @@ -13,11 +13,11 @@ export default class TemplateSwitchWidget extends SwitchWidget { doRender() { super.doRender(); - this.$switchOnName.text(t("template_switch.template")); - this.$switchOnButton.attr("title", t("template_switch.toggle-on-hint")); + this.switchOnName = t("template_switch.template"); + this.switchOnTooltip = t("template_switch.toggle-on-hint"); - this.$switchOffName.text("Template"); - this.$switchOffButton.attr("title", t("template_switch.toggle-off-hint")); + this.switchOffName = t("template_switch.template"); + this.switchOffTooltip = t("template_switch.toggle-off-hint"); this.$helpButton.attr("data-help-page", "template.html").show(); } @@ -34,8 +34,7 @@ export default class TemplateSwitchWidget extends SwitchWidget { async refreshWithNote(note) { const isTemplate = note.hasLabel("template"); - this.$switchOn.toggle(!isTemplate); - this.$switchOff.toggle(!!isTemplate); + this.isToggled = isTemplate; } entitiesReloadedEvent({ loadResults }) {