Skip to content

Build your personal knowledge base with Trilium Notes

converted recent changes to module

azivner6d82a0e03f09
diff --git a/public/javascripts/recent_changes.js b/public/javascripts/recent_changes.js
index 246e10d600..05fe012738 100644
--- a/public/javascripts/recent_changes.js
+++ b/public/javascripts/recent_changes.js
@@ -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
- });
-
- const result = await $.ajax({
- url: baseApiUrl + 'recent-changes/',
- type: 'GET',
- error: () => error("Error getting recent changes.")
- });
-
- recentChangesDialog.html('');
-
- const groupedByDate = groupByDate(result);
-
- for (const [dateDay, dayChanges] of groupedByDate) {
- const changesListEl = $('<ul>');
+ async function showDialog() {
+ dialog.dialog({
+ modal: true,
+ width: 800,
+ height: 700
+ });
- const dayEl = $('<div>').append($('<b>').html(formatDate(dateDay))).append(changesListEl);
+ const result = await $.ajax({
+ url: baseApiUrl + 'recent-changes/',
+ type: 'GET',
+ error: () => error("Error getting recent changes.")
+ });
- for (const change of dayChanges) {
- const formattedTime = formatTime(getDateFromTS(change.date_modified_to));
+ dialog.html('');
- const noteLink = $("<a>", {
- href: 'app#' + change.note_id,
- text: change.note_title
- });
+ const groupedByDate = groupByDate(result);
- const revLink = $("<a>", {
- href: "javascript: showNoteHistoryDialog('" + change.note_id + "', '" + change.note_history_id + "');",
- text: 'rev'
- });
+ for (const [dateDay, dayChanges] of groupedByDate) {
+ const changesListEl = $('<ul>');
- changesListEl.append($('<li>')
- .append(formattedTime + ' - ')
- .append(noteLink)
- .append(' (').append(revLink).append(')'));
- }
+ const dayEl = $('<div>').append($('<b>').html(formatDate(dateDay))).append(changesListEl);
- recentChangesDialog.append(dayEl);
- }
-}
+ for (const change of dayChanges) {
+ const formattedTime = formatTime(getDateFromTS(change.date_modified_to));
-function groupByDate(result) {
- const groupedByDate = new Map();
- const dayCache = {};
+ const noteLink = $("<a>", {
+ href: 'app#' + change.note_id,
+ text: change.note_title
+ });
- for (const row of result) {
- row.note_title = getFullName(row.note_id);
+ const revLink = $("<a>", {
+ href: "javascript: showNoteHistoryDialog('" + change.note_id + "', '" + change.note_history_id + "');",
+ text: 'rev'
+ });
- let dateDay = getDateFromTS(row.date_modified_to);
- dateDay.setHours(0);
- dateDay.setMinutes(0);
- dateDay.setSeconds(0);
- dateDay.setMilliseconds(0);
+ changesListEl.append($('<li>')
+ .append(formattedTime + ' - ')
+ .append(noteLink)
+ .append(' (').append(revLink).append(')'));
+ }
- // 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;
+ dialog.append(dayEl);
}
+ }
- if (!groupedByDate.has(dateDay)) {
- groupedByDate.set(dateDay, []);
+ function groupByDate(result) {
+ const groupedByDate = new Map();
+ const dayCache = {};
+
+ 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);
+
+ // 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);
}
-
- 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');
+ });
});
-});
\ No newline at end of file
+
+ return {
+ showDialog
+ };
+})();
\ No newline at end of file
diff --git a/public/javascripts/recent_notes.js b/public/javascripts/recent_notes.js
index 92d933fa29..ce8831cfe6 100644
--- a/public/javascripts/recent_notes.js
+++ b/public/javascripts/recent_notes.js
@@ -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
};
})();
\ No newline at end of file
diff --git a/public/javascripts/tree_mutations.js b/public/javascripts/tree_mutations.js
index e0c39da38d..ca667e3808 100644
--- a/public/javascripts/tree_mutations.js
+++ b/public/javascripts/tree_mutations.js
@@ -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) {
diff --git a/views/index.ejs b/views/index.ejs
index afcbd999d9..b724879dba 100644
--- a/views/index.ejs
+++ b/views/index.ejs
@@ -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>