trilium/src/public/javascripts/services/note_detail_file.js

76 lines
2.5 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import server from "./server.js";
import protectedSessionHolder from "./protected_session_holder.js";
import noteDetailService from "./note_detail.js";
2019-05-03 04:24:43 +08:00
class NoteDetailFile {
/**
* @param {NoteContext} ctx
*/
constructor(ctx) {
this.$component = ctx.$noteTabContent.find('.note-detail-file');
this.$fileNoteId = ctx.$noteTabContent.find(".file-note-id");
this.$fileName = ctx.$noteTabContent.find(".file-filename");
this.$fileType = ctx.$noteTabContent.find(".file-filetype");
this.$fileSize = ctx.$noteTabContent.find(".file-filesize");
this.$previewRow = ctx.$noteTabContent.find(".file-preview-row");
this.$previewContent = ctx.$noteTabContent.find(".file-preview-content");
this.$downloadButton = ctx.$noteTabContent.find(".file-download");
this.$openButton = ctx.$noteTabContent.find(".file-open");
2019-05-03 04:24:43 +08:00
this.$downloadButton.click(() => utils.download(this.getFileUrl()));
2019-05-03 04:24:43 +08:00
this.$openButton.click(() => {
if (utils.isElectron()) {
const open = require("open");
2019-05-03 04:24:43 +08:00
open(this.getFileUrl());
}
else {
window.location.href = this.getFileUrl();
}
});
}
async show() {
const activeNote = noteDetailService.getActiveNote();
const attributes = await server.get('notes/' + activeNote.noteId + '/attributes');
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);
this.$component.show();
2019-05-03 04:24:43 +08:00
this.$fileNoteId.text(activeNote.noteId);
this.$fileName.text(attributeMap.originalFileName || "?");
this.$fileSize.text((attributeMap.fileSize || "?") + " bytes");
this.$fileType.text(activeNote.mime);
2019-05-03 04:24:43 +08:00
if (activeNote.content) {
this.$previewRow.show();
this.$previewContent.text(activeNote.content);
}
else {
this.$previewRow.hide();
}
2018-11-17 01:40:58 +08:00
2019-05-03 04:24:43 +08:00
// open doesn't work for protected notes since it works through browser which isn't in protected session
this.$openButton.toggle(!activeNote.isProtected);
}
2019-05-03 04:24:43 +08:00
getFileUrl() {
// electron needs absolute URL so we extract current host, port, protocol
return utils.getHost() + "/api/notes/" + noteDetailService.getActiveNoteId() + "/download";
}
2019-04-22 21:01:05 +08:00
2019-05-03 04:24:43 +08:00
getContent() {}
2019-05-03 04:24:43 +08:00
focus() {}
2019-05-03 04:24:43 +08:00
onNoteChange() {}
2019-05-03 04:24:43 +08:00
cleanup() {}
2019-05-03 04:24:43 +08:00
scrollToTop() {}
}
2019-05-03 04:24:43 +08:00
export default NoteDetailFile;