From 12031d369fc979938a6d7ff4c2f403b9261eb7a0 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 6 Aug 2018 15:23:22 +0200 Subject: [PATCH] displaying and saving number and boolean promoted attributes --- src/entities/attribute.js | 11 ++- .../javascripts/services/note_detail.js | 75 ++++++++++++++----- src/routes/api/attributes.js | 1 - 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/src/entities/attribute.js b/src/entities/attribute.js index ae9a99e33..547f96a38 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.js index f02d0f467..39e5fbe16 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 = $(""); + const $labelCell = $("").append(valueAttr.name); + const $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 = $("").append($input); + if (valueAttr.type === 'label') { - const inputId = "promoted-input-" + idx; - const $tr = $(""); - const $labelCell = $("").append(valueAttr.name); - const $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"); + 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"); - const $inputCell = $("").append($input); - - $tr.append($labelCell).append($inputCell); - - $promotedAttributesContainer.append($tr); + 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.js index 7d175aa82..e96baa824 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); }