Notes/apps/client/src/widgets/shared_switch.ts

82 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-12-20 17:30:47 +01:00
import SwitchWidget from "./switch.js";
import branchService from "../services/branches.js";
import server from "../services/server.js";
import utils from "../services/utils.js";
import syncService from "../services/sync.js";
2022-11-25 15:29:57 +01:00
import dialogService from "../services/dialog.js";
2024-09-08 22:11:52 +03:00
import { t } from "../services/i18n.js";
2025-03-16 00:45:46 +02:00
import type FNote from "../entities/fnote.js";
import type { EventData } from "../components/app_context.js";
2021-12-20 17:30:47 +01:00
export default class SharedSwitchWidget extends SwitchWidget {
2025-03-16 00:45:46 +02:00
isEnabled() {
2025-03-16 00:45:46 +02:00
return super.isEnabled()
&& !["root", "_share", "_hidden"].includes(this.noteId ?? "")
&& !this.noteId?.startsWith("_options");
}
2021-12-20 17:30:47 +01:00
doRender() {
super.doRender();
this.switchOnName = t("shared_switch.shared");
this.switchOnTooltip = t("shared_switch.toggle-on-title");
2021-12-20 17:30:47 +01:00
this.switchOffName = t("shared_switch.shared");
this.switchOffTooltip = t("shared_switch.toggle-off-title");
2021-12-20 17:30:47 +01:00
this.$helpButton.attr("data-help-page", "sharing.html").show();
2025-01-09 18:07:02 +02:00
this.$helpButton.on("click", (e) => utils.openHelp($(e.target)));
2021-12-20 17:30:47 +01:00
}
async switchOn() {
2025-03-16 00:45:46 +02:00
if (!this.noteId) {
return;
}
2025-01-09 18:07:02 +02:00
await branchService.cloneNoteToParentNote(this.noteId, "_share");
syncService.syncNow(true);
2021-12-20 17:30:47 +01:00
}
async switchOff() {
2025-03-16 00:45:46 +02:00
const shareBranch = this.note?.getParentBranches().find((b) => b.parentNoteId === "_share");
2021-12-20 17:30:47 +01:00
if (!shareBranch) {
return;
}
2025-03-16 00:45:46 +02:00
if (this.note?.getParentBranches().length === 1) {
2025-01-09 18:07:02 +02:00
if (!(await dialogService.confirm(t("shared_switch.shared-branch")))) {
2021-12-20 17:30:47 +01:00
return;
}
}
2021-12-21 16:12:59 +01:00
await server.remove(`branches/${shareBranch.branchId}?taskId=no-progress-reporting`);
syncService.syncNow(true);
2021-12-20 17:30:47 +01:00
}
2025-03-16 00:45:46 +02:00
async refreshWithNote(note: FNote) {
2025-01-09 18:07:02 +02:00
const isShared = note.hasAncestor("_share");
const canBeUnshared = isShared && note.getParentBranches().find((b) => b.parentNoteId === "_share");
2021-12-20 17:30:47 +01:00
const switchDisabled = isShared && !canBeUnshared;
this.isToggled = isShared;
2021-12-20 17:30:47 +01:00
if (switchDisabled) {
this.disabledTooltip = t("shared_switch.inherited");
this.canToggle = false;
2025-01-09 18:07:02 +02:00
} else {
this.disabledTooltip = "";
this.canToggle = true;
2021-12-20 17:30:47 +01:00
}
}
2025-03-16 00:45:46 +02:00
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
2025-01-09 18:07:02 +02:00
if (loadResults.getBranchRows().find((b) => b.noteId === this.noteId)) {
2021-12-20 17:30:47 +01:00
this.refresh();
}
}
}