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

View File

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

View File

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

View File

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