2018-03-25 13:41:29 -04:00
|
|
|
import linkService from '../services/link.js';
|
2018-03-25 13:02:39 -04:00
|
|
|
import utils from '../services/utils.js';
|
2018-03-25 14:49:20 -04:00
|
|
|
import server from '../services/server.js';
|
2019-09-02 20:26:18 +02:00
|
|
|
import treeService from "../services/tree.js";
|
|
|
|
import treeCache from "../services/tree_cache.js";
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
const $dialog = $("#recent-changes-dialog");
|
2018-11-06 17:47:40 +01:00
|
|
|
const $content = $("#recent-changes-content");
|
2017-11-04 17:03:15 -04:00
|
|
|
|
2019-08-20 21:40:47 +02:00
|
|
|
export async function showDialog() {
|
2019-06-10 22:45:03 +02:00
|
|
|
utils.closeActiveDialog();
|
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
glob.activeDialog = $dialog;
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2018-11-06 17:47:40 +01:00
|
|
|
$dialog.modal();
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2018-11-06 17:47:40 +01:00
|
|
|
const result = await server.get('recent-changes');
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2019-09-02 20:26:18 +02:00
|
|
|
// preload all notes into cache
|
|
|
|
await treeCache.getNotes(result.map(r => r.noteId), true);
|
|
|
|
|
2018-11-06 17:47:40 +01:00
|
|
|
$content.empty();
|
2018-08-18 15:21:44 +02:00
|
|
|
|
|
|
|
if (result.length === 0) {
|
2018-11-06 17:47:40 +01:00
|
|
|
$content.append("No changes yet ...");
|
2018-08-18 15:21:44 +02:00
|
|
|
}
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
const groupedByDate = groupByDate(result);
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
for (const [dateDay, dayChanges] of groupedByDate) {
|
2020-01-03 10:48:36 +01:00
|
|
|
const $changesList = $('<ul>');
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2020-01-03 10:48:36 +01:00
|
|
|
const dayEl = $('<div>').append($('<b>').html(utils.formatDate(dateDay))).append($changesList);
|
2017-10-06 22:46:30 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
for (const change of dayChanges) {
|
2019-09-02 20:26:18 +02:00
|
|
|
const formattedTime = utils.formatTime(utils.parseDate(change.date));
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2020-01-03 10:48:36 +01:00
|
|
|
let $noteLink;
|
2017-12-03 17:46:56 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (change.current_isDeleted) {
|
2020-01-03 10:48:36 +01:00
|
|
|
$noteLink = $("<span>").text(change.current_title);
|
|
|
|
|
|
|
|
if (change.canBeUndeleted) {
|
2020-01-03 13:14:43 +01: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 10:48:36 +01:00
|
|
|
$noteLink
|
|
|
|
.append(' (')
|
2020-01-03 13:14:43 +01:00
|
|
|
.append($undeleteLink)
|
2020-01-03 10:48:36 +01:00
|
|
|
.append(')');
|
|
|
|
}
|
2017-11-04 13:39:26 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-09-02 20:26:18 +02:00
|
|
|
const note = await treeCache.getNote(change.noteId);
|
|
|
|
const notePath = await treeService.getSomeNotePath(note);
|
|
|
|
|
2020-01-03 21:15:45 +01:00
|
|
|
if (notePath) {
|
|
|
|
$noteLink = await linkService.createNoteLink(notePath, {
|
|
|
|
title: change.title,
|
|
|
|
showNotePath: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$noteLink = $("<span>").text(note.title);
|
|
|
|
}
|
2017-11-04 13:39:26 -04:00
|
|
|
}
|
|
|
|
|
2020-01-03 10:48:36 +01:00
|
|
|
$changesList.append($('<li>')
|
2018-03-25 11:09:17 -04:00
|
|
|
.append(formattedTime + ' - ')
|
2020-01-03 10:48:36 +01:00
|
|
|
.append($noteLink));
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:47:40 +01:00
|
|
|
$content.append(dayEl);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function groupByDate(result) {
|
|
|
|
const groupedByDate = new Map();
|
|
|
|
const dayCache = {};
|
|
|
|
|
|
|
|
for (const row of result) {
|
2019-09-02 20:26:18 +02:00
|
|
|
let dateDay = utils.parseDate(row.date);
|
2018-03-25 11:09:17 -04:00
|
|
|
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-04 13:39:26 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!groupedByDate.has(dateDay)) {
|
|
|
|
groupedByDate.set(dateDay, []);
|
2017-11-04 11:32:05 -04:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
groupedByDate.get(dateDay).push(row);
|
2017-11-04 11:32:05 -04:00
|
|
|
}
|
2020-01-03 10:48:36 +01:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return groupedByDate;
|
|
|
|
}
|