fixing promoted attributes

This commit is contained in:
zadam 2020-07-01 00:02:13 +02:00
parent c012620338
commit bf073690e0
2 changed files with 51 additions and 27 deletions

View File

@ -31,15 +31,6 @@ class Attribute {
return this.type === 'relation' ? this.value : undefined; return this.type === 'relation' ? this.value : undefined;
} }
get jsonValue() {
try {
return JSON.parse(this.value);
}
catch (e) {
return null;
}
}
get isAutoLink() { get isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name); return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
} }
@ -79,6 +70,42 @@ class Attribute {
return false; return false;
} }
isDefinition() {
return this.type === 'label' && (this.name.startsWith('label:') || this.name.startsWith('relation:'));
}
getDefinition() {
const tokens = this.value.split(',').map(t => t.trim());
const defObj = {};
for (const token of tokens) {
if (token === 'promoted') {
defObj.isPromoted = true;
}
else if (['text', 'number', 'boolean', 'date', 'url'].includes(token)) {
defObj.labelType = token;
}
else if (['single', 'multi'].includes(token)) {
defObj.multiplicity = token;
}
else if (token.startsWith('precision')) {
const chunks = token.split('=');
defObj.numberPrecision = parseInt(chunks[1]);
}
else if (token.startsWith('inverse')) {
const chunks = token.split('=');
defObj.inverseRelation = chunks[1];
}
else {
console.log("Unrecognized attribute definition token:", token);
}
}
return defObj;
}
} }
export default Attribute; export default Attribute;

View File

@ -42,40 +42,37 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
const attributes = note.getAttributes(); const attributes = note.getAttributes();
const promoted = attributes const promoted = attributes
.filter(attr => attr.type === 'label-definition' || attr.type === 'relation-definition') .filter(attr => attr.isDefinition())
.filter(attr => !attr.name.startsWith("child:"))
.filter(attr => { .filter(attr => {
const json = attr.jsonValue; const def = attr.getDefinition();
return json && json.isPromoted; return def && def.isPromoted;
}); });
const hidePromotedAttributes = attributes.some(attr => attr.type === 'label' && attr.name === 'hidePromotedAttributes'); if (promoted.length > 0 && !note.hasLabel('hidePromotedAttributes')) {
if (promoted.length > 0 && !hidePromotedAttributes) {
const $tbody = $("<tbody>"); const $tbody = $("<tbody>");
for (const definitionAttr of promoted) { for (const definitionAttr of promoted) {
const definitionType = definitionAttr.type; const definitionType = definitionAttr.name.startsWith('label:') ? 'label' : 'relation';
const valueType = definitionType.substr(0, definitionType.length - 11); const valueName = definitionAttr.name.substr(definitionType.length + 1);
let valueAttrs = attributes.filter(el => el.name === definitionAttr.name && el.type === valueType); let valueAttrs = attributes.filter(el => el.name === definitionAttr.name && el.type === definitionType);
if (valueAttrs.length === 0) { if (valueAttrs.length === 0) {
valueAttrs.push({ valueAttrs.push({
attributeId: "", attributeId: "",
type: valueType, type: definitionType,
name: definitionAttr.name, name: valueName,
value: "" value: ""
}); });
} }
if (definitionAttr.value.multiplicityType === 'singlevalue') { if (definitionAttr.value.multiplicity === 'single') {
valueAttrs = valueAttrs.slice(0, 1); valueAttrs = valueAttrs.slice(0, 1);
} }
for (const valueAttr of valueAttrs) { for (const valueAttr of valueAttrs) {
const $tr = await this.createPromotedAttributeRow(definitionAttr, valueAttr); const $tr = await this.createPromotedAttributeRow(definitionAttr, valueAttr, valueName);
$tbody.append($tr); $tbody.append($tr);
} }
@ -93,10 +90,10 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
return attributes; return attributes;
} }
async createPromotedAttributeRow(definitionAttr, valueAttr) { async createPromotedAttributeRow(definitionAttr, valueAttr, valueName) {
const definition = definitionAttr.jsonValue; const definition = definitionAttr.getDefinition();
const $tr = $("<tr>"); const $tr = $("<tr>");
const $labelCell = $("<th>").append(valueAttr.name); const $labelCell = $("<th>").append(valueName);
const $input = $("<input>") const $input = $("<input>")
.prop("tabindex", 200 + definitionAttr.position) .prop("tabindex", 200 + definitionAttr.position)
.prop("attribute-id", valueAttr.noteId === this.noteId ? valueAttr.attributeId : '') // if not owned, we'll force creation of a new attribute instead of updating the inherited one .prop("attribute-id", valueAttr.noteId === this.noteId ? valueAttr.attributeId : '') // if not owned, we'll force creation of a new attribute instead of updating the inherited one
@ -209,7 +206,7 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
return; return;
} }
if (definition.multiplicityType === "multivalue") { if (definition.multiplicity === "multivalue") {
const addButton = $("<span>") const addButton = $("<span>")
.addClass("bx bx-plus pointer") .addClass("bx bx-plus pointer")
.prop("title", "Add new attribute") .prop("title", "Add new attribute")