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

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-12-20 17:30:47 +01:00
import SwitchWidget from "./switch.js";
2022-12-04 13:16:05 +01:00
import server from "../services/server.js";
import toastService from "../services/toast.js";
import { t } from "../services/i18n.js";
2021-10-05 22:08:02 +02:00
2021-12-20 17:30:47 +01:00
export default class BookmarkSwitchWidget extends SwitchWidget {
2022-12-04 13:16:05 +01:00
isEnabled() {
2025-01-09 18:07:02 +02:00
return (
super.isEnabled() &&
2022-12-04 13:16:05 +01:00
// it's not possible to bookmark root because that would clone it under bookmarks and thus create a cycle
2025-01-09 18:07:02 +02:00
!["root", "_hidden"].includes(this.noteId)
);
2022-12-04 13:16:05 +01:00
}
2021-12-20 17:30:47 +01:00
doRender() {
super.doRender();
2021-10-05 22:08:02 +02:00
this.$switchOnName.text(t("bookmark_switch.bookmark"));
this.$switchOnButton.attr("title", t("bookmark_switch.bookmark_this_note"));
2021-10-05 22:08:02 +02:00
this.$switchOffName.text(t("bookmark_switch.bookmark"));
this.$switchOffButton.attr("title", t("bookmark_switch.remove_bookmark"));
2021-12-20 17:30:47 +01:00
}
2021-10-05 22:08:02 +02:00
2022-12-04 13:16:05 +01:00
async toggle(state) {
2022-12-24 12:26:32 +01:00
const resp = await server.put(`notes/${this.noteId}/toggle-in-parent/_lbBookmarks/${!!state}`);
2021-10-05 22:08:02 +02:00
2022-12-04 13:16:05 +01:00
if (!resp.success) {
toastService.showError(resp.message);
}
2021-10-05 22:08:02 +02:00
}
async refreshWithNote(note) {
2025-01-09 18:07:02 +02:00
const isBookmarked = !!note.getParentBranches().find((b) => b.parentNoteId === "_lbBookmarks");
2021-10-05 22:08:02 +02:00
2021-12-20 17:30:47 +01:00
this.$switchOn.toggle(!isBookmarked);
this.$switchOff.toggle(isBookmarked);
2021-10-05 22:08:02 +02:00
}
2025-01-09 18:07:02 +02:00
entitiesReloadedEvent({ loadResults }) {
if (loadResults.getBranchRows().find((b) => b.noteId === this.noteId)) {
2022-12-04 13:16:05 +01:00
this.refresh();
2021-10-05 22:08:02 +02:00
}
}
}