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

102 lines
3.7 KiB
JavaScript
Raw Normal View History

import utils from "../services/utils.js";
import linkService from "../services/link.js";
2019-08-27 02:21:43 +08:00
import ws from "../services/ws.js";
2019-08-16 03:08:41 +08:00
import StandardWidget from "./standard_widget.js";
class AttributesWidget extends StandardWidget {
2019-08-17 16:45:20 +08:00
getWidgetTitle() { return "Attributes"; }
getHelp() {
return {
title: "Attributes are key-value records owned by assigned to this note.",
url: "https://github.com/zadam/trilium/wiki/Attributes"
};
}
2019-08-17 16:45:20 +08:00
getHeaderActions() {
const $showFullButton = $("<a>").append("show dialog").addClass('widget-header-action');
$showFullButton.click(async () => {
const attributesDialog = await import("../dialogs/attributes.js");
attributesDialog.showDialog();
});
2019-08-17 16:45:20 +08:00
return [$showFullButton];
}
2019-08-16 03:08:41 +08:00
async doRenderBody() {
const attributes = await this.ctx.attributes.getAttributes();
const ownedAttributes = attributes.filter(attr => attr.noteId === this.ctx.note.noteId);
2019-08-15 16:04:03 +08:00
if (attributes.length === 0) {
2019-08-16 03:08:41 +08:00
this.$body.text("No attributes yet...");
2019-08-15 16:04:03 +08:00
return;
}
const $attributesContainer = $("<div>");
await this.renderAttributes(ownedAttributes, $attributesContainer);
const inheritedAttributes = attributes.filter(attr => attr.noteId !== this.ctx.note.noteId);
if (inheritedAttributes.length > 0) {
const $inheritedAttrs = $("<span>").append($("<strong>").text("Inherited: "));
const $showInheritedAttributes = $("<a>")
.attr("href", "javascript:")
.text("+show inherited")
.click(() => {
$showInheritedAttributes.hide();
$inheritedAttrs.show();
});
const $hideInheritedAttributes = $("<a>")
.attr("href", "javascript:")
.text("-hide inherited")
.click(() => {
$showInheritedAttributes.show();
$inheritedAttrs.hide();
});
$attributesContainer.append($showInheritedAttributes);
$attributesContainer.append($inheritedAttrs);
await this.renderAttributes(inheritedAttributes, $inheritedAttrs);
$inheritedAttrs.append($hideInheritedAttributes);
$inheritedAttrs.hide();
}
2019-08-16 03:08:41 +08:00
this.$body.empty().append($attributesContainer);
}
async renderAttributes(attributes, $container) {
for (const attribute of attributes) {
if (attribute.type === 'label') {
$container.append(utils.formatLabel(attribute) + " ");
} else if (attribute.type === 'relation') {
if (attribute.value) {
$container.append('@' + attribute.name + "=");
$container.append(await linkService.createNoteLink(attribute.value));
$container.append(" ");
} else {
2019-08-27 02:21:43 +08:00
ws.logError(`Relation ${attribute.attributeId} has empty target`);
}
} else if (attribute.type === 'label-definition' || attribute.type === 'relation-definition') {
$container.append(attribute.name + " definition ");
} else {
2019-08-27 02:21:43 +08:00
ws.logError("Unknown attr type: " + attribute.type);
}
}
}
eventReceived(name, data) {
if (name === 'syncData') {
if (data.find(sd => sd.entityName === 'attributes' && sd.noteId === this.ctx.note.noteId)) {
// no need to invalidate attributes since the Attribute class listens to this as well
// (and is guaranteed to run first)
this.doRenderBody();
}
}
}
}
export default AttributesWidget;