converted note history to module

This commit is contained in:
azivner 2017-11-04 13:55:46 -04:00
parent 6d82a0e03f
commit 394fd6f527
3 changed files with 74 additions and 59 deletions

View File

@ -1,61 +1,76 @@
glob.historyItems = null; const noteHistory = (function() {
const dialogEl = $("#note-history-dialog");
const listEl = $("#note-history-list");
const contentEl = $("#note-history-content");
const titleEl = $("#note-history-title");
function showCurrentNoteHistory() { let historyItems = [];
showNoteHistoryDialog(glob.currentNote.detail.note_id);
}
function showNoteHistoryDialog(noteId, noteHistoryId) { async function showCurrentNoteHistory() {
$("#note-history-dialog").dialog({ await showNoteHistoryDialog(glob.currentNote.detail.note_id);
modal: true,
width: 800,
height: 700
});
$("#note-history-list").empty();
$("#note-history-content").empty();
$.ajax({
url: baseApiUrl + 'notes-history/' + noteId,
type: 'GET',
success: result => {
glob.historyItems = result;
for (const row of result) {
const dateModified = getDateFromTS(row.date_modified_to);
$("#note-history-list").append($('<option>', {
value: row.note_history_id,
text: formatDateTime(dateModified)
}));
}
if (result.length > 0) {
if (!noteHistoryId) {
noteHistoryId = $("#note-history-list option:first").val();
}
$("#note-history-list").val(noteHistoryId).trigger('change');
}
},
error: () => error("Error getting note history.")
});
}
$(document).bind('keydown', 'alt+h', showCurrentNoteHistory);
$("#note-history-list").on('change', () => {
const optVal = $("#note-history-list").find(":selected").val();
const historyItem = glob.historyItems.find(r => r.note_history_id === optVal);
let noteTitle = historyItem.note_title;
let noteText = historyItem.note_text;
if (historyItem.encryption > 0) {
noteTitle = decryptString(noteTitle);
noteText = decryptString(noteText);
} }
$("#note-history-title").html(noteTitle); // weird hack because browser doesn't like we're returning promise and displays promise page
$("#note-history-content").html(noteText); function showNoteHistoryDialogNotAsync(noteId, noteHistoryId) {
}); showNoteHistoryDialog(noteId, noteHistoryId);
}
async function showNoteHistoryDialog(noteId, noteHistoryId) {
dialogEl.dialog({
modal: true,
width: 800,
height: 700
});
listEl.empty();
contentEl.empty();
historyItems = await $.ajax({
url: baseApiUrl + 'notes-history/' + noteId,
type: 'GET',
error: () => error("Error getting note history.")
});
for (const item of historyItems) {
const dateModified = getDateFromTS(item.date_modified_to);
$("#note-history-list").append($('<option>', {
value: item.note_history_id,
text: formatDateTime(dateModified)
}));
}
if (historyItems.length > 0) {
if (!noteHistoryId) {
noteHistoryId = listEl.find("option:first").val();
}
listEl.val(noteHistoryId).trigger('change');
}
}
$(document).bind('keydown', 'alt+h', showCurrentNoteHistory);
listEl.on('change', () => {
const optVal = listEl.find(":selected").val();
const historyItem = historyItems.find(r => r.note_history_id === optVal);
let noteTitle = historyItem.note_title;
let noteText = historyItem.note_text;
if (historyItem.encryption > 0) {
noteTitle = decryptString(noteTitle);
noteText = decryptString(noteText);
}
titleEl.html(noteTitle);
contentEl.html(noteText);
});
return {
showCurrentNoteHistory,
showNoteHistoryDialog,
showNoteHistoryDialogNotAsync
};
})();

View File

@ -32,7 +32,7 @@ const recentChanges = (function() {
}); });
const revLink = $("<a>", { const revLink = $("<a>", {
href: "javascript: showNoteHistoryDialog('" + change.note_id + "', '" + change.note_history_id + "');", href: "javascript: noteHistory.showNoteHistoryDialogNotAsync('" + change.note_id + "', '" + change.note_history_id + "');",
text: 'rev' text: 'rev'
}); });

View File

@ -84,7 +84,7 @@
<input autocomplete="off" value="" id="note-title" style="font-size: x-large; border: 0; flex-grow: 100;" tabindex="1"> <input autocomplete="off" value="" id="note-title" style="font-size: x-large; border: 0; flex-grow: 100;" tabindex="1">
<button class="btn btn-xs" onclick="showCurrentNoteHistory();">History</button> <button class="btn btn-xs" onclick="noteHistory.showCurrentNoteHistory();">History</button>
</div> </div>
</div> </div>