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";
|
2024-08-16 11:39:26 +08:00
|
|
|
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() {
|
|
|
|
return super.isEnabled()
|
|
|
|
// it's not possible to bookmark root because that would clone it under bookmarks and thus create a cycle
|
2022-12-21 16:11:00 +01: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
|
|
|
|
2024-08-16 11:39:26 +08: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
|
|
|
|
2024-08-16 11:39:26 +08: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
|
|
|
}
|
|
|
|
|
2023-05-05 23:17:23 +02:00
|
|
|
async refreshWithNote(note) {
|
2022-12-24 12:26:32 +01: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
|
|
|
}
|
|
|
|
|
|
|
|
entitiesReloadedEvent({loadResults}) {
|
2023-06-05 16:12:02 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|