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

75 lines
2.3 KiB
JavaScript
Raw Normal View History

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