Notes/src/public/app/services/note_list_renderer.js

301 lines
8.9 KiB
JavaScript
Raw Normal View History

import linkService from "./link.js";
import noteContentRenderer from "./note_content_renderer.js";
import treeCache from "./tree_cache.js";
2020-10-23 00:11:44 +02:00
import attributeService from "./attributes.js";
const TPL = `
<div class="note-list">
<style>
.note-list {
overflow: hidden;
position: relative;
height: 100%;
}
.note-list.card-view .note-list-container {
display: flex;
}
.note-list.card-view .note-book-card {
width: 300px;
}
.note-list.card-view .note-expander {
display: none;
}
.note-book-card {
border-radius: 10px;
background-color: var(--accented-background-color);
2020-10-22 22:49:22 +02:00
padding: 10px 15px 15px 8px;
margin: 5px 5px 5px 0;
overflow: hidden;
display: flex;
flex-direction: column;
flex-shrink: 0;
}
2020-10-22 22:49:22 +02:00
.note-book-card .note-book-content {
display: none;
padding: 10px
}
.note-book-card.expanded .note-book-content {
display: block;
}
.note-book-title {
margin-bottom: 0;
}
.note-book-card .note-book-card {
border: 1px solid var(--main-border-color);
}
2020-10-22 22:49:22 +02:00
.note-book-content.type-image, .note-book-content.type-file, .note-book-content.type-protected-session {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.note-book-card.type-image .note-book-content img, .note-book-card.type-text .note-book-content img {
max-width: 100%;
max-height: 100%;
}
.note-book-title {
flex-grow: 0;
}
.note-list-wrapper {
height: 100%;
overflow: auto;
}
.note-expander {
font-size: x-large;
position: relative;
2020-10-22 22:49:22 +02:00
top: 3px;
padding-right: 3px;
cursor: pointer;
}
2020-10-22 23:50:52 +02:00
.note-list-pager {
text-align: center;
}
</style>
<div class="btn-group floating-button" style="right: 20px; top: 10px;">
2020-10-22 23:07:35 +02:00
<button type="button"
class="collapse-all-button btn icon-button bx bx-layer-minus"
title="Collapse all notes"></button>
&nbsp;
<button type="button"
class="expand-children-button btn icon-button bx bx-move-vertical"
title="Expand all children"></button>
&nbsp;
<button type="button"
class="list-view-button btn icon-button bx bx-menu"
title="List view"></button>
&nbsp;
<button type="button"
class="grid-view-button btn icon-button bx bx-grid-alt"
title="Grid view"></button>
</div>
2020-10-22 23:50:52 +02:00
<div class="note-list-wrapper">
<div class="note-list-pager"></div>
<div class="note-list-container"></div>
<div class="note-list-pager"></div>
</div>
</div>`;
class NoteListRenderer {
constructor(parentNote, notes) {
this.$noteList = $(TPL);
this.parentNote = parentNote;
this.notes = notes;
this.page = 1;
this.pageSize = 2;
2020-10-24 00:02:38 +02:00
this.viewType = 'list';
2020-10-22 23:07:35 +02:00
2020-10-24 00:02:38 +02:00
this.$noteList.find('.list-view-button').on('click', () => this.toggleViewType('list'));
this.$noteList.find('.grid-view-button').on('click', () => this.toggleViewType('grid'));
2020-10-22 23:50:52 +02:00
this.$noteList.find('.expand-children-button').on('click', async () => {
for (let i = 1; i < 30; i++) { // protection against infinite cycle
const $unexpandedCards = this.$noteList.find('.note-book-card:not(.expanded)');
2020-10-22 23:50:52 +02:00
if ($unexpandedCards.length === 0) {
break;
}
2020-10-22 23:50:52 +02:00
await this.toggleCards($unexpandedCards, true);
2020-10-22 23:50:52 +02:00
if (!this.parentNote.hasLabel('expanded')) {
await attributeService.addLabel(this.parentNote.noteId, 'expanded');
}
}
});
2020-10-22 23:50:52 +02:00
this.$noteList.find('.collapse-all-button').on('click', async () => {
const $expandedCards = this.$noteList.find('.note-book-card.expanded');
2020-10-22 23:50:52 +02:00
await this.toggleCards($expandedCards, false);
2020-10-22 23:50:52 +02:00
// owned is important - we shouldn't remove inherited expanded labels
for (const expandedAttr of this.parentNote.getOwnedLabels('expanded')) {
await attributeService.removeAttributeById(this.parentNote.noteId, expandedAttr.attributeId);
}
});
2020-10-22 23:50:52 +02:00
}
2020-10-24 00:02:38 +02:00
async toggleViewType(type) {
if (type !== 'list' && type !== 'grid') {
throw new Error(`Invalid view type ${type}`);
}
this.viewType = type;
this.$noteList
.removeClass('grid-view')
.removeClass('list-view')
.addClass(this.viewType + '-view');
await this.renderList();
}
async renderList() {
const $container = this.$noteList.find('.note-list-container').empty();
const imageLinks = this.parentNote ? this.parentNote.getRelations('imageLink') : [];
2020-10-22 23:07:35 +02:00
const startIdx = (this.page - 1) * this.pageSize;
const endIdx = startIdx + this.pageSize;
2020-10-22 23:07:35 +02:00
const pageNotes = this.notes.slice(startIdx, Math.min(endIdx, this.notes.length));
2020-10-23 00:11:44 +02:00
for (const note of pageNotes) {
// image is already visible in the parent note so no need to display it separately in the book
if (imageLinks.find(rel => rel.value === note.noteId)) {
continue;
2020-10-23 00:11:44 +02:00
}
2020-10-22 23:07:35 +02:00
const $card = await this.renderNote(note);
2020-10-22 23:07:35 +02:00
$container.append($card);
2020-10-23 00:11:44 +02:00
}
this.renderPager();
return this.$noteList;
}
2020-10-22 22:49:22 +02:00
renderPager() {
2020-10-24 00:02:38 +02:00
const $pager = this.$noteList.find('.note-list-pager').empty();
for (let i = 1; i <= Math.ceil(this.notes.length / this.pageSize); i++) {
$pager.append(
i === this.page
? $('<span>').text(i).css('text-decoration', 'underline').css('font-weight', "bold")
: $('<a href="javascript:">')
.text(i)
.on('click', () => {
this.page = i;
this.renderList();
}),
" &nbsp; "
);
}
}
2020-10-22 22:49:22 +02:00
async toggleCards(cards, expand) {
for (const card of cards) {
const $card = $(card);
const noteId = $card.attr('data-note-id');
const note = await treeCache.getNote(noteId);
2020-10-22 22:49:22 +02:00
await this.toggleContent($card, note, expand);
}
2020-10-22 22:49:22 +02:00
}
async renderNoteContent(note) {
const $content = $('<div class="note-book-content">');
2020-10-22 22:49:22 +02:00
try {
const {renderedContent, type} = await noteContentRenderer.getRenderedContent(note);
2020-10-22 22:49:22 +02:00
$content.append(renderedContent);
$content.addClass("type-" + type);
} catch (e) {
console.log(`Caught error while rendering note ${note.noteId} of type ${note.type}: ${e.message}, stack: ${e.stack}`);
2020-10-22 22:49:22 +02:00
$content.append("rendering error");
}
2020-10-22 22:49:22 +02:00
const imageLinks = note.getRelations('imageLink');
const childNotes = (await note.getChildNotes())
.filter(childNote => !imageLinks.find(rel => rel.value === childNote.noteId));
for (const childNote of childNotes) {
$content.append(await this.renderNote(childNote, false));
}
return $content;
}
// TODO: we should also render (promoted) attributes
async renderNote(note, expand) {
const notePath = /*this.notePath + '/' + */ note.noteId;
const $expander = $('<span class="note-expander bx bx-chevron-right"></span>');
const $card = $('<div class="note-book-card">')
.attr('data-note-id', note.noteId)
.append(
$('<h5 class="note-book-title">')
.append($expander)
.append(await linkService.createNoteLink(notePath, {showTooltip: false}))
);
2020-10-22 22:49:22 +02:00
$expander.on('click', () => toggleContent($card, note, !$card.hasClass("expanded")));
await this.toggleContent($card, note, expand);
return $card;
2020-10-22 22:49:22 +02:00
}
async toggleContent($card, note, expand) {
2020-10-24 00:02:38 +02:00
if (this.viewType === 'list' && ((expand && $card.hasClass("expanded")) || (!expand && !$card.hasClass("expanded")))) {
return;
}
const $expander = $card.find('> .note-book-title .note-expander');
if (expand) {
$card.addClass("expanded");
$expander.addClass("bx-chevron-down").removeClass("bx-chevron-right");
}
else {
$card.removeClass("expanded");
$expander.addClass("bx-chevron-right").removeClass("bx-chevron-down");
}
if (expand && $card.find('.note-book-content').length === 0) {
$card.append(await this.renderNoteContent(note));
}
}
}
export default NoteListRenderer;