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";
|
2020-02-02 22:04:28 +01:00
|
|
|
import appContext from "../services/app_context.js";
|
2020-03-29 19:37:34 +02:00
|
|
|
import hoistedNoteService from "../services/hoisted_note.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
|
|
|
|
2020-03-29 19:43:04 +02:00
|
|
|
export async function showDialog(ancestorNoteId) {
|
2020-02-09 10:00:13 +01:00
|
|
|
utils.openDialog($dialog);
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2020-03-29 19:43:04 +02:00
|
|
|
if (!ancestorNoteId) {
|
|
|
|
ancestorNoteId = hoistedNoteService.getHoistedNoteId();
|
|
|
|
}
|
|
|
|
|
2020-06-15 23:22:11 +02:00
|
|
|
const recentChangesRows = await server.get('recent-changes/' + ancestorNoteId);
|
2017-11-04 11:32:05 -04:00
|
|
|
|
2019-09-02 20:26:18 +02:00
|
|
|
// preload all notes into cache
|
2020-06-15 23:22:11 +02:00
|
|
|
await treeCache.getNotes(recentChangesRows.map(r => r.noteId), true);
|
2019-09-02 20:26:18 +02:00
|
|
|
|
2018-11-06 17:47:40 +01:00
|
|
|
$content.empty();
|
2018-08-18 15:21:44 +02:00
|
|
|
|
2020-06-15 23:22:11 +02:00
|
|
|
if (recentChangesRows.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
|
|
|
|
2020-06-15 23:22:11 +02:00
|
|
|
const groupedByDate = groupByDate(recentChangesRows);
|
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-04-26 14:39:13 +02:00
|
|
|
const dayEl = $('<div>').append($('<b>').text(dateDay)).append($changesList);
|
2017-10-06 22:46:30 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
for (const change of dayChanges) {
|
2020-04-26 14:39:13 +02:00
|
|
|
const formattedTime = change.date.substr(11, 5);
|
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]);
|
|
|
|
|
2020-02-07 21:08:55 +01:00
|
|
|
appContext.tabManager.getActiveTabContext().setNote(change.noteId);
|
2020-01-03 13:14:43 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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>')
|
2020-04-26 14:39:13 +02:00
|
|
|
.append(
|
|
|
|
$("<span>")
|
|
|
|
.text(formattedTime)
|
|
|
|
.attr("title", change.date)
|
|
|
|
)
|
|
|
|
.append(' - ')
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 23:22:11 +02:00
|
|
|
function groupByDate(rows) {
|
2018-03-25 11:09:17 -04:00
|
|
|
const groupedByDate = new Map();
|
|
|
|
|
2020-06-15 23:22:11 +02:00
|
|
|
for (const row of rows) {
|
2020-04-26 14:39:13 +02:00
|
|
|
const dateDay = row.date.substr(0, 10);
|
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;
|
|
|
|
}
|