trilium/src/public/javascripts/mobile.js

151 lines
5 KiB
JavaScript
Raw Normal View History

import treeService from "./services/tree.js";
import treeCache from "./services/tree_cache.js";
import treeBuilder from "./services/tree_builder.js";
import contextMenuWidget from "./services/context_menu.js";
2020-02-18 02:42:52 +08:00
import branchService from "./services/branches.js";
import utils from "./services/utils.js";
2020-01-12 18:15:23 +08:00
import appContext from "./services/app_context.js";
import noteCreateService from "./services/note_create.js";
import glob from "./services/glob.js";
2019-05-23 02:53:59 +08:00
const $leftPane = $("#left-pane");
const $tree = $("#tree");
2018-12-25 01:39:31 +08:00
const $detail = $("#detail");
2018-12-25 03:38:38 +08:00
function togglePanes() {
if (!$leftPane.is(":visible") || !$detail.is(":visible")) {
2018-12-25 03:38:38 +08:00
$detail.toggleClass("d-none");
$leftPane.toggleClass("d-none");
2018-12-25 03:38:38 +08:00
}
}
function showDetailPane() {
if (!$detail.is(":visible")) {
$detail.removeClass("d-none");
$leftPane.addClass("d-none");
}
}
2019-05-14 05:08:59 +08:00
$detail.on("click", ".close-detail-button",() => {
// no page is opened
document.location.hash = '-';
togglePanes();
});
async function showTree() {
const treeData = await treeBuilder.prepareTree();
$tree.fancytree({
autoScroll: true,
extensions: ["dnd5", "clones"],
source: treeData,
scrollParent: $tree,
minExpandLevel: 2, // root can't be collapsed
2019-05-23 02:53:59 +08:00
click: (event, data) => {
if (data.targetType !== 'expander' && data.node.isActive()) {
2019-05-23 03:25:13 +08:00
// this is important for single column mobile view, otherwise it's not possible to see again previously displayed note
2019-05-23 02:53:59 +08:00
$tree.fancytree('getTree').reactivate(true);
return false;
}
},
2019-05-14 05:08:59 +08:00
activate: async (event, data) => {
const node = data.node;
2018-12-28 05:36:11 +08:00
treeService.clearSelectedNodes();
showDetailPane();
2020-01-25 16:56:08 +08:00
const notePath = await treeService.getNotePath(node);
2019-05-09 02:14:41 +08:00
},
expand: (event, data) => treeService.setExpandedToServer(data.node.data.branchId, true),
collapse: (event, data) => treeService.setExpandedToServer(data.node.data.branchId, false),
init: (event, data) => treeService.treeInitialized(), // don't collapse to short form
dnd5: dragAndDropSetup,
lazyLoad: function(event, data) {
const noteId = data.node.data.noteId;
data.result = treeCache.getNote(noteId).then(note => treeBuilder.prepareBranch(note));
},
clones: {
highlightActiveClones: true
},
// this is done to automatically lazy load all expanded search notes after tree load
loadChildren: (event, data) => {
data.node.visit((subNode) => {
// Load all lazy/unloaded child nodes
// (which will trigger `loadChildren` recursively)
if (subNode.isUndefined() && subNode.isExpanded()) {
subNode.load();
}
});
}
});
treeService.setTree($.ui.fancytree.getTree("#tree"));
2018-12-29 05:05:04 +08:00
}
2019-05-14 05:08:59 +08:00
$detail.on("click", ".note-menu-button", async e => {
// FIXME
2020-01-12 18:15:23 +08:00
const node = appContext.getMainNoteTree().getActiveNode();
const branch = treeCache.getBranch(node.data.branchId);
2018-12-29 05:05:04 +08:00
const note = await treeCache.getNote(node.data.noteId);
const parentNote = await treeCache.getNote(branch.parentNoteId);
const isNotRoot = note.noteId !== 'root';
const items = [
{ title: "Insert note after", cmd: "insertNoteAfter", uiIcon: "plus",
enabled: isNotRoot && parentNote.type !== 'search' },
{ title: "Insert child note", cmd: "insertChildNote", uiIcon: "plus",
enabled: note.type !== 'search' },
{ title: "Delete this note", cmd: "delete", uiIcon: "trash",
enabled: isNotRoot && parentNote.type !== 'search' }
];
2018-12-29 05:05:04 +08:00
2019-05-14 05:08:59 +08:00
contextMenuWidget.initContextMenu(e, {
getContextMenuItems: () => items,
selectContextMenuItem: async (event, cmd) => {
if (cmd === "insertNoteAfter") {
const parentNoteId = node.data.parentNoteId;
2020-01-25 16:56:08 +08:00
const isProtected = await treeService.getParentProtectedStatus(node);
2019-05-14 05:08:59 +08:00
noteCreateService.createNote(parentNoteId, {
isProtected: isProtected,
target: 'after',
targetBranchId: node.data.branchId
});
2019-05-14 05:08:59 +08:00
}
else if (cmd === "insertChildNote") {
noteCreateService.createNote(node.data.noteId);
2019-05-14 05:08:59 +08:00
}
else if (cmd === "delete") {
2020-02-18 02:42:52 +08:00
if (await branchService.deleteNotes([node])) {
2019-05-23 02:53:59 +08:00
// move to the tree
togglePanes();
}
2019-05-14 05:08:59 +08:00
}
else {
throw new Error("Unrecognized command " + cmd);
}
2018-12-29 06:57:11 +08:00
}
});
});
2018-12-29 05:05:04 +08:00
2019-11-10 00:39:48 +08:00
$("#switch-to-desktop-button").on('click', () => {
2018-12-29 16:13:52 +08:00
utils.setCookie('trilium-device', 'desktop');
2018-12-29 05:05:04 +08:00
2018-12-29 16:13:52 +08:00
utils.reloadApp();
});
2018-12-29 07:09:16 +08:00
2019-11-10 00:39:48 +08:00
$("#log-out-button").on('click', () => {
2019-11-10 00:45:22 +08:00
$("#logout-form").trigger('submit');
2018-12-29 06:57:11 +08:00
});
// this is done so that startNotePath is not used
if (!document.location.hash) {
document.location.hash = '-';
}
showTree();