138 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-04-26 09:40:02 +02:00
import CollapsibleWidget from "../collapsible_widget.js";
2021-01-20 22:17:40 +01:00
import server from "../../services/server.js";
2019-07-21 11:32:38 +02:00
const TPL = `
<table class="note-info-widget-table">
<style>
.note-info-widget-table {
2020-09-05 22:49:30 +02:00
max-width: 100%;
display: block;
overflow-x: auto;
white-space: nowrap;
}
.note-info-widget-table td, .note-info-widget-table th {
padding: 5px;
}
.note-info-mime {
max-width: 13em;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
</style>
2019-07-21 11:32:38 +02:00
<tr>
<th>Note ID:</th>
<td class="note-info-note-id"></td>
<th>Type:</th>
<td>
<span class="note-info-type"></span>
<span class="note-info-mime"></span>
</td>
2019-07-21 11:32:38 +02:00
</tr>
2020-09-05 22:49:30 +02:00
<tr>
<th>Created:</th>
<td class="note-info-date-created"></td>
<th>Modified:</th>
<td class="note-info-date-modified"></td>
</tr>
<tr title="Note size provides rough estimate of storage requirements for this note. It takes into account note's content and content of its note revisions.">
<th>Note size:</th>
<td colspan="3">
2021-01-22 15:16:08 +01:00
<button class="btn btn-sm calculate-button" style="padding: 0px 10px 0px 10px;">
<span class="bx bx-calculator"></span> calculate
</button>
2021-01-22 15:16:08 +01:00
<span class="note-sizes-wrapper">
<span class="note-size"></span>
(subtree size: <span class="subtree-size"></span>)
</span>
</td>
</tr>
2019-07-21 11:32:38 +02:00
</table>
`;
2020-02-02 21:16:20 +01:00
export default class NoteInfoWidget extends CollapsibleWidget {
isEnabled() {
return super.isEnabled() && !this.note.hasLabel('noteInfoWidgetDisabled');
}
2020-03-16 22:14:18 +01:00
get widgetTitle() { return "Note info"; }
2019-07-21 11:32:38 +02:00
async doRenderBody() {
this.$body.html(TPL);
2020-02-02 18:46:50 +01:00
this.$noteId = this.$body.find(".note-info-note-id");
this.$dateCreated = this.$body.find(".note-info-date-created");
this.$dateModified = this.$body.find(".note-info-date-modified");
this.$type = this.$body.find(".note-info-type");
this.$mime = this.$body.find(".note-info-mime");
2021-01-22 15:16:08 +01:00
this.$noteSizesWrapper = this.$body.find('.note-sizes-wrapper');
this.$noteSize = this.$body.find(".note-size");
this.$subTreeSize = this.$body.find(".subtree-size");
2021-01-22 15:16:08 +01:00
this.$calculateButton = this.$body.find(".calculate-button");
this.$calculateButton.on('click', async () => {
this.$noteSizesWrapper.show();
this.$calculateButton.hide();
this.$noteSize.empty().append($('<span class="bx bx-loader bx-spin"></span>'));
this.$subTreeSize.empty().append($('<span class="bx bx-loader bx-spin"></span>'));
const noteSizeResp = await server.get(`stats/note-size/${this.noteId}`);
this.$noteSize.text(this.formatSize(noteSizeResp.noteSize));
const subTreeSizeResp = await server.get(`stats/subtree-size/${this.noteId}`);
this.$subTreeSize.text(this.formatSize(subTreeSizeResp.subTreeSize));
});
}
2019-07-21 11:32:38 +02:00
2020-02-01 11:33:31 +01:00
async refreshWithNote(note) {
const noteComplement = await this.tabContext.getNoteComplement();
2020-02-02 18:46:50 +01:00
this.$noteId.text(note.noteId);
this.$dateCreated
.text(noteComplement.dateCreated.substr(0, 16))
2020-02-01 11:15:58 +01:00
.attr("title", noteComplement.dateCreated);
2020-02-02 18:46:50 +01:00
this.$dateModified
.text(noteComplement.combinedDateModified.substr(0, 16))
.attr("title", noteComplement.combinedDateModified);
2020-02-02 18:46:50 +01:00
this.$type.text(note.type);
if (note.mime) {
this.$mime.text('(' + note.mime + ')');
}
else {
this.$mime.empty();
}
2021-01-20 22:17:40 +01:00
2021-01-22 15:16:08 +01:00
this.$calculateButton.show();
this.$noteSizesWrapper.hide();
}
formatSize(size) {
size = Math.max(Math.round(size / 1024), 1);
2021-01-20 22:17:40 +01:00
if (size < 1024) {
return `${size} KiB`;
}
else {
return `${Math.round(size / 102.4) / 10} MiB`;
}
2019-07-21 11:32:38 +02:00
}
2020-02-16 19:23:49 +01:00
entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteReloaded(this.noteId) || loadResults.isNoteContentReloaded(this.noteId)) {
this.refresh();
}
}
}