From c9182677508c8747038e1df0163eed54800ba64d Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 27 Mar 2018 00:27:38 -0400 Subject: [PATCH] separated attachments out of note detail --- .../javascripts/services/note_detail.js | 43 ++--------------- .../services/note_detail_attachment.js | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 src/public/javascripts/services/note_detail_attachment.js diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index 55c413e35..c1acb22e8 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -11,13 +11,13 @@ import treeCache from "./tree_cache.js"; import NoteFull from "../entities/note_full.js"; import noteDetailCode from './note_detail_code.js'; import noteDetailText from './note_detail_text.js'; +import noteDetailAttachment from './note_detail_attachment.js'; const $noteTitle = $("#note-title"); const $noteDetailComponents = $(".note-detail-component"); const $noteDetailSearch = $('#note-detail-search'); const $noteDetailRender = $('#note-detail-render'); -const $noteDetailAttachment = $('#note-detail-attachment'); const $protectButton = $("#protect-button"); const $unprotectButton = $("#unprotect-button"); @@ -25,11 +25,6 @@ const $noteDetailWrapper = $("#note-detail-wrapper"); const $noteIdDisplay = $("#note-id-display"); const $labelList = $("#label-list"); const $labelListInner = $("#label-list-inner"); -const $attachmentFileName = $("#attachment-filename"); -const $attachmentFileType = $("#attachment-filetype"); -const $attachmentFileSize = $("#attachment-filesize"); -const $attachmentDownload = $("#attachment-download"); -const $attachmentOpen = $("#attachment-open"); const $searchString = $("#search-string"); let currentNote = null; @@ -148,17 +143,6 @@ async function showRenderNote() { await bundleService.executeBundle(bundle); } -async function showFileNote() { - const labels = await server.get('notes/' + currentNote.noteId + '/labels'); - const labelMap = utils.toObject(labels, l => [l.name, l.value]); - - $noteDetailAttachment.show(); - - $attachmentFileName.text(labelMap.original_file_name); - $attachmentFileSize.text(labelMap.file_size + " bytes"); - $attachmentFileType.text(currentNote.mime); -} - function showSearchNote() { $noteDetailSearch.show(); @@ -216,7 +200,7 @@ async function loadNoteToEditor(noteId) { await showRenderNote(); } else if (currentNote.type === 'file') { - await showFileNote(); + await noteDetailAttachment.showFileNote(); } else if (currentNote.type === 'text') { await noteDetailText.showTextNote(); @@ -249,8 +233,8 @@ async function loadLabelList() { $labelListInner.html(''); if (labels.length > 0) { - for (const attr of labels) { - $labelListInner.append(utils.formatLabel(attr) + " "); + for (const label of labels) { + $labelListInner.append(utils.formatLabel(label) + " "); } $labelList.show(); @@ -283,25 +267,6 @@ function focus() { } } -$attachmentDownload.click(() => utils.download(getAttachmentUrl())); - -$attachmentOpen.click(() => { - if (utils.isElectron()) { - const open = require("open"); - - open(getAttachmentUrl()); - } - else { - window.location.href = getAttachmentUrl(); - } -}); - -function getAttachmentUrl() { - // electron needs absolute URL so we extract current host, port, protocol - return utils.getHost() + "/api/attachments/download/" + getCurrentNoteId() - + "?protectedSessionId=" + encodeURIComponent(protectedSessionHolder.getProtectedSessionId()); -} - messagingService.subscribeToMessages(syncData => { if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === getCurrentNoteId())) { infoService.showMessage('Reloading note because of background changes'); diff --git a/src/public/javascripts/services/note_detail_attachment.js b/src/public/javascripts/services/note_detail_attachment.js new file mode 100644 index 000000000..350559d71 --- /dev/null +++ b/src/public/javascripts/services/note_detail_attachment.js @@ -0,0 +1,48 @@ +import utils from "./utils.js"; +import server from "./server.js"; +import protectedSessionHolder from "./protected_session_holder.js"; +import noteDetailService from "./note_detail.js"; + +const $noteDetailAttachment = $('#note-detail-attachment'); + +const $attachmentFileName = $("#attachment-filename"); +const $attachmentFileType = $("#attachment-filetype"); +const $attachmentFileSize = $("#attachment-filesize"); +const $attachmentDownload = $("#attachment-download"); +const $attachmentOpen = $("#attachment-open"); + +async function showFileNote() { + const currentNote = noteDetailService.getCurrentNote(); + + const labels = await server.get('notes/' + currentNote.noteId + '/labels'); + const labelMap = utils.toObject(labels, l => [l.name, l.value]); + + $noteDetailAttachment.show(); + + $attachmentFileName.text(labelMap.original_file_name); + $attachmentFileSize.text(labelMap.file_size + " bytes"); + $attachmentFileType.text(currentNote.mime); +} + +$attachmentDownload.click(() => utils.download(getAttachmentUrl())); + +$attachmentOpen.click(() => { + if (utils.isElectron()) { + const open = require("open"); + + open(getAttachmentUrl()); + } + else { + window.location.href = getAttachmentUrl(); + } +}); + +function getAttachmentUrl() { + // electron needs absolute URL so we extract current host, port, protocol + return utils.getHost() + "/api/attachments/download/" + noteDetailService.getCurrentNoteId() + + "?protectedSessionId=" + encodeURIComponent(protectedSessionHolder.getProtectedSessionId()); +} + +export default { + showFileNote +} \ No newline at end of file