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";
|
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
|
|
|
|
2021-12-20 17:30:47 +01:00
|
|
|
this.$switchOnName.text("Bookmark");
|
|
|
|
this.$switchOnButton.attr("title", "Bookmark this note to the left side panel");
|
2021-10-05 22:08:02 +02:00
|
|
|
|
2021-12-20 17:30:47 +01:00
|
|
|
this.$switchOffName.text("Bookmark");
|
|
|
|
this.$switchOffButton.attr("title", "Remove bookmark");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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}) {
|
2022-12-04 13:16:05 +01:00
|
|
|
if (loadResults.getBranches().find(b => b.noteId === this.noteId)) {
|
|
|
|
this.refresh();
|
2021-10-05 22:08:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|