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

100 lines
3.2 KiB
JavaScript
Raw Normal View History

import TabAwareWidget from "./tab_aware_widget.js";
import treeService from "../services/tree.js";
import linkService from "../services/link.js";
import hoistedNoteService from "../services/hoisted_note.js";
const TPL = `
<div class="dropdown note-paths-widget">
<style>
.note-path-list-button {
font-size: 120% !important;
}
.note-path-list-button::after {
display: none !important; // disabling the standard caret
}
.note-path-list {
max-height: 600px;
overflow-y: auto;
}
</style>
<button class="btn dropdown-toggle note-path-list-button bx bx-collection" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="Note paths"></button>
<ul class="note-path-list dropdown-menu dropdown-menu-right" aria-labelledby="note-path-list-button">
</ul>
</div>`;
export default class NotePathsWidget extends TabAwareWidget {
doRender() {
this.$widget = $(TPL);
2020-08-26 16:50:16 +02:00
this.overflowing();
this.$notePathList = this.$widget.find(".note-path-list");
this.$widget.on('show.bs.dropdown', () => this.renderDropdown());
}
async renderDropdown() {
this.$notePathList.empty();
2020-04-08 10:13:11 +02:00
this.$notePathList.append(
$("<div>")
.addClass("dropdown-header")
.text('This note is placed into the following paths:')
);
if (this.noteId === 'root') {
await this.addPath('root', true);
return;
}
const pathSegments = treeService.parseNotePath(this.notePath);
const activeNoteParentNoteId = pathSegments[pathSegments.length - 2]; // we know this is not root so there must be a parent
for (const parentNote of this.note.getParentNotes()) {
const parentNotePath = treeService.getSomeNotePath(parentNote);
// this is to avoid having root notes leading '/'
const notePath = parentNotePath ? (parentNotePath + '/' + this.noteId) : this.noteId;
const isCurrent = activeNoteParentNoteId === parentNote.noteId;
await this.addPath(notePath, isCurrent);
}
const cloneLink = $("<div>")
2020-04-08 10:13:11 +02:00
.addClass("dropdown-header")
.append(
$('<button class="btn btn-sm">')
.text('Clone note to new location...')
.on('click', () => import("../dialogs/clone_to.js").then(d => d.showDialog([this.noteId])))
);
this.$notePathList.append(cloneLink);
}
async addPath(notePath, isCurrent) {
2020-01-25 09:56:08 +01:00
const title = await treeService.getNotePathTitle(notePath);
const $noteLink = await linkService.createNoteLink(notePath, {title});
$noteLink
.addClass("dropdown-item");
$noteLink
.find('a')
.addClass("no-tooltip-preview");
if (isCurrent) {
$noteLink.addClass("current");
}
this.$notePathList.append($noteLink);
}
2020-02-17 22:47:50 +01:00
entitiesReloadedEvent({loadResults}) {
2020-10-21 22:45:49 +02:00
if (loadResults.getBranches().find(branch => branch.noteId === this.noteId)
|| loadResults.isNoteReloaded(this.noteId)) {
2020-02-17 22:47:50 +01:00
this.refresh();
}
}
}