trilium/src/public/javascripts/widgets/note_revisions.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-07-26 04:31:09 +08:00
import server from "../services/server.js";
const TPL = `
<ul class="note-revision-list" style="max-height: 150px; overflow: auto;">
</ul>
`;
class NoteRevisionsWidget {
/**
* @param {TabContext} ctx
* @param {jQuery} $widget
*/
constructor(ctx, $widget) {
this.ctx = ctx;
this.$widget = $widget;
2019-08-15 16:04:03 +08:00
this.$widget.on('show.bs.collapse', () => this.renderBody());
this.$widget.on('show.bs.collapse', () => this.ctx.stateChanged());
this.$widget.on('hide.bs.collapse', () => this.ctx.stateChanged());
2019-07-26 04:31:09 +08:00
this.$title = this.$widget.find('.widget-title');
this.$title.text("Note revisions");
2019-08-15 16:04:03 +08:00
this.$bodyWrapper = this.$widget.find('.body-wrapper');
2019-07-26 04:31:09 +08:00
}
async renderBody() {
2019-08-15 16:04:03 +08:00
if (!this.isVisible()) {
return;
}
2019-07-26 04:31:09 +08:00
const $body = this.$widget.find('.card-body');
const revisionItems = await server.get(`notes/${this.ctx.note.noteId}/revisions`);
if (revisionItems.length === 0) {
$body.text("No revisions yet...");
return;
}
$body.html(TPL);
const $list = $body.find('.note-revision-list');
for (const item of revisionItems) {
$list.append($('<li>').append($("<a>", {
'data-action': 'note-revision',
'data-note-path': this.ctx.note.noteId,
'data-note-revision-id': item.noteRevisionId,
href: 'javascript:'
}).text(item.dateModifiedFrom)));
}
}
syncDataReceived(syncData) {
if (syncData.find(sd => sd.entityName === 'note_revisions' && sd.noteId === this.ctx.note.noteId)) {
this.renderBody();
}
}
2019-08-15 16:04:03 +08:00
getWidgetState() {
return {
id: 'attributes',
visible: this.isVisible()
};
}
isVisible() {
return this.$bodyWrapper.is(":visible");
}
2019-07-26 04:31:09 +08:00
}
export default NoteRevisionsWidget;