trilium/src/public/javascripts/dialogs/recent_changes.js

120 lines
3.6 KiB
JavaScript
Raw Normal View History

import linkService from '../services/link.js';
import utils from '../services/utils.js';
import server from '../services/server.js';
import treeService from "../services/tree.js";
import treeCache from "../services/tree_cache.js";
const $dialog = $("#recent-changes-dialog");
const $content = $("#recent-changes-content");
2017-11-05 05:03:15 +08:00
export async function showDialog() {
utils.closeActiveDialog();
glob.activeDialog = $dialog;
$dialog.modal();
const result = await server.get('recent-changes');
// preload all notes into cache
await treeCache.getNotes(result.map(r => r.noteId), true);
$content.empty();
if (result.length === 0) {
$content.append("No changes yet ...");
}
const groupedByDate = groupByDate(result);
for (const [dateDay, dayChanges] of groupedByDate) {
2020-01-03 17:48:36 +08:00
const $changesList = $('<ul>');
2020-01-03 17:48:36 +08:00
const dayEl = $('<div>').append($('<b>').html(utils.formatDate(dateDay))).append($changesList);
2017-10-07 10:46:30 +08:00
for (const change of dayChanges) {
const formattedTime = utils.formatTime(utils.parseDate(change.date));
2020-01-03 17:48:36 +08:00
let $noteLink;
2017-12-04 06:46:56 +08:00
if (change.current_isDeleted) {
2020-01-03 17:48:36 +08:00
$noteLink = $("<span>").text(change.current_title);
if (change.canBeUndeleted) {
2020-01-03 20:14:43 +08:00
const $undeleteLink = $(`<a href="javascript:">`)
.text("undelete")
.on('click', async () => {
const confirmDialog = await import('../dialogs/confirm.js');
const text = 'Do you want to undelete this note and its sub-notes?';
if (await confirmDialog.confirm(text)) {
await server.put(`notes/${change.noteId}/undelete`);
$dialog.modal('hide');
await treeCache.reloadNotes([change.noteId]);
treeService.activateNote(change.noteId);
}
});
2020-01-03 17:48:36 +08:00
$noteLink
.append(' (')
2020-01-03 20:14:43 +08:00
.append($undeleteLink)
2020-01-03 17:48:36 +08:00
.append(')');
}
2017-11-05 01:39:26 +08:00
}
else {
const note = await treeCache.getNote(change.noteId);
const notePath = await treeService.getSomeNotePath(note);
if (notePath) {
$noteLink = await linkService.createNoteLink(notePath, {
title: change.title,
showNotePath: true
});
}
else {
$noteLink = $("<span>").text(note.title);
}
2017-11-05 01:39:26 +08:00
}
2020-01-03 17:48:36 +08:00
$changesList.append($('<li>')
.append(formattedTime + ' - ')
2020-01-03 17:48:36 +08:00
.append($noteLink));
}
$content.append(dayEl);
}
}
function groupByDate(result) {
const groupedByDate = new Map();
const dayCache = {};
for (const row of result) {
let dateDay = utils.parseDate(row.date);
dateDay.setHours(0);
dateDay.setMinutes(0);
dateDay.setSeconds(0);
dateDay.setMilliseconds(0);
// this stupidity is to make sure that we always use the same day object because Map uses only
// reference equality
if (dayCache[dateDay]) {
dateDay = dayCache[dateDay];
}
else {
dayCache[dateDay] = dateDay;
}
2017-11-05 01:39:26 +08:00
if (!groupedByDate.has(dateDay)) {
groupedByDate.set(dateDay, []);
}
groupedByDate.get(dateDay).push(row);
}
2020-01-03 17:48:36 +08:00
return groupedByDate;
}