2019-08-07 02:05:05 +08:00
|
|
|
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";
|
2020-02-03 03:02:08 +08:00
|
|
|
import CollapsibleWidget from "./collapsible_widget.js";
|
2019-08-16 03:08:41 +08:00
|
|
|
|
2020-02-03 01:46:50 +08:00
|
|
|
class AttributesWidget extends CollapsibleWidget {
|
2019-08-17 16:45:20 +08:00
|
|
|
getWidgetTitle() { return "Attributes"; }
|
2019-08-07 02:05:05 +08:00
|
|
|
|
2019-09-10 03:23:04 +08:00
|
|
|
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() {
|
2019-08-07 02:05:05 +08:00
|
|
|
const $showFullButton = $("<a>").append("show dialog").addClass('widget-header-action');
|
2019-11-10 00:39:48 +08:00
|
|
|
$showFullButton.on('click', async () => {
|
2019-08-21 03:40:47 +08:00
|
|
|
const attributesDialog = await import("../dialogs/attributes.js");
|
2019-08-07 02:05:05 +08:00
|
|
|
attributesDialog.showDialog();
|
|
|
|
});
|
|
|
|
|
2019-08-17 16:45:20 +08:00
|
|
|
return [$showFullButton];
|
2019-08-07 02:05:05 +08:00
|
|
|
}
|
|
|
|
|
2020-01-25 21:37:12 +08:00
|
|
|
async refreshWithNote(note) {
|
|
|
|
const attributes = await note.getAttributes();
|
|
|
|
const ownedAttributes = note.getOwnedAttributes();
|
2019-08-07 02:05:05 +08:00
|
|
|
|
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;
|
2019-08-07 02:05:05 +08:00
|
|
|
}
|
|
|
|
|
2019-08-07 05:20:27 +08:00
|
|
|
const $attributesContainer = $("<div>");
|
|
|
|
|
|
|
|
await this.renderAttributes(ownedAttributes, $attributesContainer);
|
2019-08-07 02:43:07 +08:00
|
|
|
|
2020-01-15 04:23:32 +08:00
|
|
|
const inheritedAttributes = attributes.filter(attr => attr.noteId !== this.tabContext.note.noteId);
|
2019-08-07 02:43:07 +08:00
|
|
|
|
|
|
|
if (inheritedAttributes.length > 0) {
|
|
|
|
const $inheritedAttrs = $("<span>").append($("<strong>").text("Inherited: "));
|
|
|
|
const $showInheritedAttributes = $("<a>")
|
|
|
|
.attr("href", "javascript:")
|
|
|
|
.text("+show inherited")
|
2019-11-10 00:39:48 +08:00
|
|
|
.on('click', () => {
|
2019-08-07 02:43:07 +08:00
|
|
|
$showInheritedAttributes.hide();
|
|
|
|
$inheritedAttrs.show();
|
|
|
|
});
|
|
|
|
|
|
|
|
const $hideInheritedAttributes = $("<a>")
|
|
|
|
.attr("href", "javascript:")
|
|
|
|
.text("-hide inherited")
|
2019-11-10 00:39:48 +08:00
|
|
|
.on('click', () => {
|
2019-08-07 02:43:07 +08:00
|
|
|
$showInheritedAttributes.show();
|
|
|
|
$inheritedAttrs.hide();
|
|
|
|
});
|
|
|
|
|
2019-08-07 05:20:27 +08:00
|
|
|
$attributesContainer.append($showInheritedAttributes);
|
|
|
|
$attributesContainer.append($inheritedAttrs);
|
2019-08-07 02:43:07 +08:00
|
|
|
|
|
|
|
await this.renderAttributes(inheritedAttributes, $inheritedAttrs);
|
|
|
|
|
|
|
|
$inheritedAttrs.append($hideInheritedAttributes);
|
|
|
|
$inheritedAttrs.hide();
|
|
|
|
}
|
2019-08-07 05:20:27 +08:00
|
|
|
|
2019-08-16 03:08:41 +08:00
|
|
|
this.$body.empty().append($attributesContainer);
|
2019-08-07 02:43:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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`);
|
2019-08-07 02:05:05 +08:00
|
|
|
}
|
2019-08-07 02:43:07 +08:00
|
|
|
} 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);
|
2019-08-07 02:05:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 05:20:27 +08:00
|
|
|
|
2020-01-30 04:38:58 +08:00
|
|
|
entitiesReloadedListener({loadResults}) {
|
2020-01-31 05:38:31 +08:00
|
|
|
if (loadResults.getAttributes().find(attr => attr.noteId === this.noteId)) {
|
2020-01-30 04:38:58 +08:00
|
|
|
this.refresh();
|
2019-08-07 05:20:27 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 02:05:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default AttributesWidget;
|