converted recent changes to module

This commit is contained in:
azivner 2017-11-04 13:39:26 -04:00
parent af5ecd1869
commit 6d82a0e03f
4 changed files with 102 additions and 92 deletions

View File

@ -1,86 +1,92 @@
const recentChangesDialog = $("#recent-changes-dialog");
const recentChanges = (function() {
const dialog = $("#recent-changes-dialog");
async function showRecentChanges() {
recentChangesDialog.dialog({
modal: true,
width: 800,
height: 700
});
async function showDialog() {
dialog.dialog({
modal: true,
width: 800,
height: 700
});
const result = await $.ajax({
url: baseApiUrl + 'recent-changes/',
type: 'GET',
error: () => error("Error getting recent changes.")
});
const result = await $.ajax({
url: baseApiUrl + 'recent-changes/',
type: 'GET',
error: () => error("Error getting recent changes.")
});
recentChangesDialog.html('');
dialog.html('');
const groupedByDate = groupByDate(result);
const groupedByDate = groupByDate(result);
for (const [dateDay, dayChanges] of groupedByDate) {
const changesListEl = $('<ul>');
for (const [dateDay, dayChanges] of groupedByDate) {
const changesListEl = $('<ul>');
const dayEl = $('<div>').append($('<b>').html(formatDate(dateDay))).append(changesListEl);
const dayEl = $('<div>').append($('<b>').html(formatDate(dateDay))).append(changesListEl);
for (const change of dayChanges) {
const formattedTime = formatTime(getDateFromTS(change.date_modified_to));
for (const change of dayChanges) {
const formattedTime = formatTime(getDateFromTS(change.date_modified_to));
const noteLink = $("<a>", {
href: 'app#' + change.note_id,
text: change.note_title
});
const noteLink = $("<a>", {
href: 'app#' + change.note_id,
text: change.note_title
});
const revLink = $("<a>", {
href: "javascript: showNoteHistoryDialog('" + change.note_id + "', '" + change.note_history_id + "');",
text: 'rev'
});
const revLink = $("<a>", {
href: "javascript: showNoteHistoryDialog('" + change.note_id + "', '" + change.note_history_id + "');",
text: 'rev'
});
changesListEl.append($('<li>')
.append(formattedTime + ' - ')
.append(noteLink)
.append(' (').append(revLink).append(')'));
changesListEl.append($('<li>')
.append(formattedTime + ' - ')
.append(noteLink)
.append(' (').append(revLink).append(')'));
}
dialog.append(dayEl);
}
recentChangesDialog.append(dayEl);
}
}
function groupByDate(result) {
const groupedByDate = new Map();
const dayCache = {};
function groupByDate(result) {
const groupedByDate = new Map();
const dayCache = {};
for (const row of result) {
row.note_title = getFullName(row.note_id);
for (const row of result) {
row.note_title = getFullName(row.note_id);
let dateDay = getDateFromTS(row.date_modified_to);
dateDay.setHours(0);
dateDay.setMinutes(0);
dateDay.setSeconds(0);
dateDay.setMilliseconds(0);
let dateDay = getDateFromTS(row.date_modified_to);
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];
// 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;
}
if (!groupedByDate.has(dateDay)) {
groupedByDate.set(dateDay, []);
}
groupedByDate.get(dateDay).push(row);
}
else {
dayCache[dateDay] = dateDay;
}
if (!groupedByDate.has(dateDay)) {
groupedByDate.set(dateDay, []);
}
groupedByDate.get(dateDay).push(row);
return groupedByDate;
}
return groupedByDate;
}
$(document).bind('keydown', 'alt+r', showRecentChanges);
$(document).bind('keydown', 'alt+r', showDialog);
$(document).on('click', '#recent-changes-dialog a', e => {
goToInternalNote(e, () => {
recentChangesDialog.dialog('close');
$(document).on('click', '#recent-changes-dialog a', e => {
goToInternalNote(e, () => {
dialog.dialog('close');
});
});
});
return {
showDialog
};
})();

View File

@ -1,36 +1,39 @@
glob.recentNotes = [];
recentNotes = (function() {
const recentNotesSelectBox = $('#recent-notes-select-box');
const recentNotesDialog = $("#recent-notes-dialog");
const recentNotesJumpTo = $('#recentNotesJumpTo');
const recentNotesAddLink = $('#recentNotesAddLink');
const recentNotes = (function() {
const dialog = $("#recent-notes-dialog");
const selectBox = $('#recent-notes-select-box');
const jumpToButton = $('#recentNotesJumpTo');
const addLinkButton = $('#recentNotesAddLink');
const noteDetail = $('#note-detail');
let list = [];
function addRecentNote(noteTreeId, noteContentId) {
setTimeout(() => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
if (noteTreeId === glob.currentNote.detail.note_id || noteContentId === glob.currentNote.detail.note_id) {
// if it's already there, remove the note
glob.recentNotes = glob.recentNotes.filter(note => note !== noteTreeId);
list = list.filter(note => note !== noteTreeId);
glob.recentNotes.unshift(noteTreeId);
list.unshift(noteTreeId);
}
}, 1500);
}
function showRecentNotes() {
function removeRecentNote(noteIdToRemove) {
list = list.filter(note => note !== noteIdToRemove);
}
function showDialog() {
noteDetail.summernote('editor.saveRange');
recentNotesDialog.dialog({
dialog.dialog({
modal: true,
width: 800
});
recentNotesSelectBox.find('option').remove();
selectBox.find('option').remove();
// remove the current note
const recNotes = glob.recentNotes.filter(note => note !== glob.currentNote.detail.note_id);
const recNotes = list.filter(note => note !== glob.currentNote.detail.note_id);
$.each(recNotes, (key, valueNoteId) => {
const noteTitle = getFullName(valueNoteId);
@ -48,14 +51,12 @@ recentNotes = (function() {
option.attr("selected", "selected");
}
recentNotesSelectBox.append(option);
selectBox.append(option);
});
}
$(document).bind('keydown', 'alt+q', showRecentNotes);
function getSelectedNoteIdFromRecentNotes() {
return recentNotesSelectBox.find("option:selected").val();
return selectBox.find("option:selected").val();
}
function setActiveNoteBasedOnRecentNotes() {
@ -63,7 +64,7 @@ recentNotes = (function() {
getNodeByKey(noteId).setActive();
recentNotesDialog.dialog('close');
dialog.dialog('close');
}
function addLinkBasedOnRecentNotes() {
@ -71,7 +72,7 @@ recentNotes = (function() {
const linkTitle = getNoteTitle(noteId);
recentNotesDialog.dialog("close");
dialog.dialog("close");
noteDetail.summernote('editor.restoreRange');
@ -82,7 +83,7 @@ recentNotes = (function() {
});
}
recentNotesSelectBox.keydown(e => {
selectBox.keydown(e => {
const key = e.which;
if (key === 13)// the enter key code
@ -99,14 +100,18 @@ recentNotes = (function() {
e.preventDefault();
});
recentNotesSelectBox.dblclick(e => {
$(document).bind('keydown', 'alt+q', showDialog);
selectBox.dblclick(e => {
setActiveNoteBasedOnRecentNotes();
});
recentNotesJumpTo.click(setActiveNoteBasedOnRecentNotes);
recentNotesAddLink.click(addLinkBasedOnRecentNotes);
jumpToButton.click(setActiveNoteBasedOnRecentNotes);
addLinkButton.click(addLinkBasedOnRecentNotes);
return {
addRecentNote
showDialog,
addRecentNote,
removeRecentNote
};
})();

View File

@ -49,8 +49,7 @@ function deleteNode(node) {
glob.allNoteIds = glob.allNoteIds.filter(e => e !== node.key);
// remove from recent notes
glob.recentNotes = glob.recentNotes.filter(note => note !== node.key);
recentNotes.removeRecentNote(node.key);
let next = node.getNextSibling();
if (!next) {

View File

@ -12,8 +12,8 @@
</div>
<div style="flex-grow: 100;">
<button class="btn btn-xs" onclick="showRecentChanges();">Recent changes</button>
<button class="btn btn-xs" onclick="showRecentNotes();">Recent notes</button>
<button class="btn btn-xs" onclick="recentChanges.showDialog();">Recent changes</button>
<button class="btn btn-xs" onclick="recentNotes.showDialog();">Recent notes</button>
<button class="btn btn-xs" onclick="showJumpToNote();">Jump to note</button>
<button class="btn btn-xs" onclick="showEventLog();">Event log</button>
</div>