Notes/src/public/app/widgets/switch.js

127 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-09-08 22:18:54 +03:00
import { t } from "../services/i18n.js";
2021-12-20 17:30:47 +01:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
const TPL = `
<div class="switch-widget">
<style>
2021-12-20 17:30:47 +01:00
.switch-widget {
display: flex;
align-items: center;
}
2025-01-21 04:49:07 +02:00
.switch-widget {
2021-12-20 17:30:47 +01:00
display: flex;
position: relative;
2021-12-20 17:30:47 +01:00
}
.switch-widget .switch-button {
position: relative;
2021-12-20 17:30:47 +01:00
}
2021-12-20 17:30:47 +01:00
.switch-widget .slider.checked {
background-color: var(--main-text-color);
}
2021-12-20 17:30:47 +01:00
.switch-widget .slider.checked:before {
transform: translateX(26px);
}
2021-12-20 17:30:47 +01:00
.switch-widget .switch-disabled {
opacity: 70%;
pointer-events: none;
}
2021-12-20 17:30:47 +01:00
.switch-widget .switch-help-button {
font-weight: 900;
border: 0;
background: none;
cursor: pointer;
2022-01-12 20:13:11 +01:00
color: var(--main-text-color);
2021-12-20 17:30:47 +01:00
}
.switch-widget .switch-button {
background: red !important;
}
.switch-widget .switch-button.on {
background: green !important;
}
.switch-widget input[type="checkbox"] {
pointer-events: none;
}
2021-12-20 17:30:47 +01:00
</style>
2025-01-21 04:49:07 +02:00
<div class="switch-widget">
<span class="switch-name"></span>
2021-12-20 17:30:47 +01:00
&nbsp;
2025-01-21 04:49:07 +02:00
<label class="switch-button">
[...]
2025-01-21 04:49:07 +02:00
<input class="switch-toggle" type="checkbox" />
</label>
2021-12-20 17:30:47 +01:00
</div>
2024-09-08 22:18:54 +03:00
<button class="switch-help-button" type="button" data-help-page="" title="${t("open-help-page")}" style="display: none;">?</button>
2021-12-20 17:30:47 +01:00
</div>`;
export default class SwitchWidget extends NoteContextAwareWidget {
switchOnName;
switchOnTooltip;
2021-12-20 17:30:47 +01:00
switchOffName;
switchOffTooltip;
2021-12-20 17:30:47 +01:00
currentState = false;
2021-12-20 17:30:47 +01:00
doRender() {
this.$widget = $(TPL);
this.$switchButton = this.$widget.find(".switch-button");
2025-01-21 04:49:07 +02:00
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");
2021-12-20 17:30:47 +01:00
this.$helpButton = this.$widget.find(".switch-help-button");
}
2022-12-04 13:16:05 +01:00
toggle(state) {
if (state) {
this.switchOn();
} else {
this.switchOff();
}
}
2021-12-20 17:30:47 +01:00
switchOff() {}
switchOn() {}
/** Gets or sets whether the switch is toggled. */
get isToggled() {
return this.currentState;
}
set isToggled(state) {
this.currentState = !!state;
2025-01-21 04:49:07 +02:00
this.$switchButton.toggleClass("on", this.currentState);
this.$switchToggle.prop("checked", this.currentState);
console.log(this.currentState);
2025-01-21 04:49:07 +02:00
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);
}
}
2021-12-20 17:30:47 +01:00
}