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

106 lines
3.5 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import server from "./server.js";
2019-11-09 18:58:52 +08:00
import toastService from "./toast.js";
import noteDetailService from "./note_detail.js";
2019-05-03 04:24:43 +08:00
class NoteDetailFile {
/**
2019-05-09 01:55:24 +08:00
* @param {TabContext} ctx
2019-05-03 04:24:43 +08:00
*/
constructor(ctx) {
2019-05-04 03:50:14 +08:00
this.ctx = ctx;
2019-05-09 01:55:24 +08: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-11-09 18:58:52 +08:00
this.$uploadNewRevisionButton = ctx.$tabContent.find(".file-upload-new-revision");
this.$uploadNewRevisionInput = ctx.$tabContent.find(".file-upload-new-revision-input");
2019-11-09 18:58:52 +08:00
this.$downloadButton.on('click', () => utils.download(this.getFileUrl()));
2019-11-09 18:58:52 +08:00
this.$openButton.on('click', () => {
2019-05-03 04:24:43 +08:00
if (utils.isElectron()) {
const open = require("open");
2019-10-17 01:42:42 +08:00
open(this.getFileUrl(), {url: true});
2019-05-03 04:24:43 +08:00
}
else {
window.location.href = this.getFileUrl();
}
});
2019-11-09 18:58:52 +08:00
this.$uploadNewRevisionButton.on("click", () => {
this.$uploadNewRevisionInput.trigger("click");
});
this.$uploadNewRevisionInput.on('change', async () => {
const formData = new FormData();
formData.append('upload', this.$uploadNewRevisionInput[0].files[0]);
const result = await $.ajax({
url: baseApiUrl + 'notes/' + this.ctx.note.noteId + '/file',
headers: server.getHeaders(),
data: formData,
type: 'PUT',
timeout: 60 * 60 * 1000,
contentType: false, // NEEDED, DON'T REMOVE THIS
processData: false, // NEEDED, DON'T REMOVE THIS
});
if (result.uploaded) {
toastService.showMessage("New file revision has been uploaded.");
await noteDetailService.reload();
}
else {
toastService.showError("Upload of a new file revision failed.");
}
});
2019-05-03 04:24:43 +08:00
}
2019-05-12 18:58:55 +08:00
async render() {
2019-05-04 03:50:14 +08:00
const attributes = await server.get('notes/' + this.ctx.note.noteId + '/attributes');
2019-05-03 04:24:43 +08:00
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);
this.$component.show();
2019-05-04 03:50:14 +08:00
this.$fileNoteId.text(this.ctx.note.noteId);
2019-05-03 04:24:43 +08:00
this.$fileName.text(attributeMap.originalFileName || "?");
2019-11-09 16:36:08 +08:00
this.$fileSize.text(this.ctx.note.contentLength + " bytes");
2019-05-04 03:50:14 +08:00
this.$fileType.text(this.ctx.note.mime);
2019-05-04 03:50:14 +08:00
if (this.ctx.note.content) {
2019-05-03 04:24:43 +08:00
this.$previewRow.show();
2019-05-04 03:50:14 +08:00
this.$previewContent.text(this.ctx.note.content);
2019-05-03 04:24:43 +08:00
}
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
2019-05-04 03:50:14 +08:00
this.$openButton.toggle(!this.ctx.note.isProtected);
}
2019-05-03 04:24:43 +08:00
getFileUrl() {
return utils.getUrlForDownload("api/notes/" + this.ctx.note.noteId + "/download");
}
2019-04-22 21:01:05 +08:00
show() {}
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;