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

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-10-05 22:08:02 +02:00
import attributeService from "../services/attributes.js";
2021-12-20 17:30:47 +01:00
import SwitchWidget from "./switch.js";
2021-10-05 22:08:02 +02:00
2021-12-20 17:30:47 +01:00
export default class BookmarkSwitchWidget extends SwitchWidget {
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
2021-12-20 17:30:47 +01:00
async switchOff() {
for (const label of this.note.getLabels('bookmarked')) {
await attributeService.removeAttributeById(this.noteId, label.attributeId);
}
}
2021-10-05 22:08:02 +02:00
2021-12-20 17:30:47 +01:00
switchOn() {
return attributeService.setLabel(this.noteId, 'bookmarked');
2021-10-05 22:08:02 +02:00
}
refreshWithNote(note) {
const isBookmarked = note.hasLabel('bookmarked');
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}) {
for (const attr of loadResults.getAttributes()) {
if (attr.type === 'label'
&& attr.name === 'bookmarked'
&& attributeService.isAffecting(attr, this.note)) {
this.refresh();
break;
}
}
}
}