Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
displaying and saving number and boolean promoted attributes
azivner12031d369fc9
diff --git a/src/entities/attribute.js b/src/entities/attribute.jsindex ae9a99e330..547f96a38c 100644--- a/src/entities/attribute.js+++ b/src/entities/attribute.js@@ -13,10 +13,13 @@ class Attribute extends Entity {constructor(row) {super(row);- try {- this.value = JSON.parse(this.value);+ if (this.isDefinition()) {+ try {+ this.value = JSON.parse(this.value);+ }+ catch (e) {+ }}- catch(e) {}}async getNote() {@@ -24,7 +27,7 @@ class Attribute extends Entity {}isDefinition() {- return this.type === 'label' || this.type === 'relation';+ return this.type === 'label-definition' || this.type === 'relation-definition';}async beforeSaving() {diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.jsindex f02d0f4673..39e5fbe16a 100644--- a/src/public/javascripts/services/note_detail.js+++ b/src/public/javascripts/services/note_detail.js@@ -238,44 +238,70 @@ async function loadAttributes() {if (promoted.length > 0) {for (const definitionAttr of promoted) {- const valueAttrs = attributes.filter(el => el.name === definitionAttr.name && el.type === definitionAttr.type.substr(0, definitionAttr.type.length - 11));+ const definitionType = definitionAttr.type;+ const definition = definitionAttr.value;+ const valueType = definitionType.substr(0, definitionType.length - 11);++ const valueAttrs = attributes.filter(el => el.name === definitionAttr.name && el.type === valueType);if (valueAttrs.length === 0) {valueAttrs.push({attributeId: "",- type: definitionAttr.type.substr(0, definitionAttr.type.length - 11),+ type: valueType,name: definitionAttr.name,value: ""});}for (const valueAttr of valueAttrs) {+ const inputId = "promoted-input-" + idx;+ const $tr = $("<tr>");+ const $labelCell = $("<th>").append(valueAttr.name);+ const $input = $("<input>")+ .prop("id", inputId)+ .prop("attribute-id", valueAttr.attributeId)+ .prop("attribute-type", valueAttr.type)+ .prop("attribute-name", valueAttr.name)+ .prop("value", valueAttr.value)+ .addClass("form-control")+ .addClass("promoted-attribute-input");++ const $inputCell = $("<td>").append($input);+if (valueAttr.type === 'label') {- const inputId = "promoted-input-" + idx;- const $tr = $("<tr>");- const $labelCell = $("<th>").append(valueAttr.name);- const $input = $("<input>")- .prop("id", inputId)- .prop("attribute-id", valueAttr.attributeId)- .prop("attribute-type", valueAttr.type)- .prop("attribute-name", valueAttr.name)- .prop("value", valueAttr.value)- .addClass("form-control")- .addClass("promoted-attribute-input");-- const $inputCell = $("<td>").append($input);-- $tr.append($labelCell).append($inputCell);-- $promotedAttributesContainer.append($tr);+ if (definition.labelType === 'text') {+ $input.prop("type", "text");+ }+ else if (definition.labelType === 'number') {+ $input.prop("type", "number");+ }+ else if (definition.labelType === 'boolean') {+ $input.prop("type", "checkbox");++ if (valueAttr.value === "true") {+ $input.prop("checked", "checked");+ }+ }+ else if (definitionAttr.labelType === 'date') {+ $input.prop("type", "text");+ $input.addClass("date");+ }+ else if (definitionAttr.labelType === 'datetime') {+ $input.prop("type", "text");+ $input.addClass("datetime");+ }}++ $tr.append($labelCell).append($inputCell);++ $promotedAttributesContainer.append($tr);}}}else {$attributeListInner.html('');- if (attributes.length > 0) {console.log(attributes);+ if (attributes.length > 0) {for (const attribute of attributes) {if (attribute.type === 'label') {$attributeListInner.append(utils.formatLabel(attribute) + " ");@@ -368,11 +394,20 @@ messagingService.subscribeToSyncMessages(syncData => {$promotedAttributesContainer.on('change', '.promoted-attribute-input', async event => {const $attr = $(event.target);+ let value;++ if ($attr.prop("type") === "checkbox") {+ value = $attr.is(':checked') ? "true" : "false";+ }+ else {+ value = $attr.val();+ }+await server.put("notes/" + getCurrentNoteId() + "/attribute", {attributeId: $attr.prop("attribute-id"),type: $attr.prop("attribute-type"),name: $attr.prop("attribute-name"),- value: $attr.val()+ value: value});infoService.showMessage("Attribute has been saved.");diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.jsindex 7d175aa827..e96baa8248 100644--- a/src/routes/api/attributes.js+++ b/src/routes/api/attributes.js@@ -59,7 +59,6 @@ async function updateNoteAttribute(req) {const body = req.body;let attribute;-if (body.attributeId) {attribute = await repository.getAttribute(body.attributeId);}