2018-03-26 01:41:29 +08:00
|
|
|
import linkService from '../services/link.js';
|
2018-03-26 01:02:39 +08:00
|
|
|
import utils from '../services/utils.js';
|
2018-03-26 02:49:20 +08:00
|
|
|
import server from '../services/server.js';
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const $dialog = $("#recent-changes-dialog");
|
2018-11-07 00:47:40 +08:00
|
|
|
const $content = $("#recent-changes-content");
|
2017-11-05 05:03:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function showDialog() {
|
2019-06-11 04:45:03 +08:00
|
|
|
utils.closeActiveDialog();
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
glob.activeDialog = $dialog;
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
$dialog.modal();
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
const result = await server.get('recent-changes');
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
$content.empty();
|
2018-08-18 21:21:44 +08:00
|
|
|
|
|
|
|
if (result.length === 0) {
|
2018-11-07 00:47:40 +08:00
|
|
|
$content.append("No changes yet ...");
|
2018-08-18 21:21:44 +08:00
|
|
|
}
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const groupedByDate = groupByDate(result);
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const [dateDay, dayChanges] of groupedByDate) {
|
|
|
|
const changesListEl = $('<ul>');
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const dayEl = $('<div>').append($('<b>').html(utils.formatDate(dateDay))).append(changesListEl);
|
2017-10-07 10:46:30 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const change of dayChanges) {
|
2019-03-13 03:58:31 +08:00
|
|
|
const formattedTime = utils.formatTime(utils.parseDate(change.utcDateModifiedTo));
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
let noteLink;
|
2017-12-04 06:46:56 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (change.current_isDeleted) {
|
|
|
|
noteLink = change.current_title;
|
2017-11-05 01:39:26 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-04-20 08:59:44 +08:00
|
|
|
noteLink = await linkService.createNoteLink(change.noteId, change.title);
|
2017-11-05 01:39:26 +08:00
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
changesListEl.append($('<li>')
|
|
|
|
.append(formattedTime + ' - ')
|
2018-11-07 00:47:40 +08:00
|
|
|
.append(noteLink));
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
$content.append(dayEl);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function groupByDate(result) {
|
|
|
|
const groupedByDate = new Map();
|
|
|
|
const dayCache = {};
|
|
|
|
|
|
|
|
for (const row of result) {
|
2019-03-13 03:58:31 +08:00
|
|
|
let dateDay = utils.parseDate(row.utcDateModifiedTo);
|
2018-03-25 23:09:17 +08: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-05 01:39:26 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!groupedByDate.has(dateDay)) {
|
|
|
|
groupedByDate.set(dateDay, []);
|
2017-11-04 23:32:05 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
groupedByDate.get(dateDay).push(row);
|
2017-11-04 23:32:05 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
return groupedByDate;
|
|
|
|
}
|
2017-11-04 23:32:05 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
showDialog
|
|
|
|
};
|