From 2089c328394d759c6f8596265ffb5fecce030bd5 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 5 Aug 2018 20:08:56 +0200 Subject: [PATCH] attribute UI & saving now fully working --- src/public/javascripts/dialogs/attributes.js | 71 ++++++++++++++------ src/routes/api/attributes.js | 2 + src/services/repository.js | 5 ++ src/views/index.ejs | 13 ++-- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/src/public/javascripts/dialogs/attributes.js b/src/public/javascripts/dialogs/attributes.js index 85338ac9f..65999d0f0 100644 --- a/src/public/javascripts/dialogs/attributes.js +++ b/src/public/javascripts/dialogs/attributes.js @@ -15,11 +15,12 @@ function AttributesModel() { this.availableTypes = [ { text: "Label", value: "label" }, - { text: "Label definition", value: "definition" }, - { text: "Relation", value: "relation" } + { text: "Label definition", value: "label-definition" }, + { text: "Relation", value: "relation" }, + { text: "Relation definition", value: "relation-definition" } ]; - this.availableValueTypes = [ + this.availableLabelTypes = [ { text: "Text", value: "text" }, { text: "Integer", value: "integer" }, { text: "Decimal", value: "decimal" }, @@ -36,7 +37,7 @@ function AttributesModel() { self.getTargetAttribute(event.target).valueHasMutated(); }; - this.valueTypeChanged = function(data, event) { + this.labelTypeChanged = function(data, event) { self.getTargetAttribute(event.target).valueHasMutated(); }; @@ -52,26 +53,32 @@ function AttributesModel() { }); }; - this.loadAttributes = async function() { - const noteId = noteDetailService.getCurrentNoteId(); - - const attributes = await server.get('notes/' + noteId + '/attributes'); - + function prepareAttributes(attributes) { for (const attr of attributes) { attr.labelValue = attr.type === 'label' ? attr.value : ''; attr.relationValue = attr.type === 'relation' ? attr.value : ''; - attr.definition = { - valueType: "text", + attr.labelDefinition = attr.type === 'label-definition' ? JSON.parse(attr.value) : { + labelType: "text", multiplicityType: "singlevalue", - showInUi: "true" + isPromoted: true + }; + attr.relationDefinition = attr.type === 'relation-definition' ? JSON.parse(attr.value) : { + multiplicityType: "singlevalue", + isPromoted: true }; - - delete attr.value; } self.attributes(attributes.map(ko.observable)); addLastEmptyRow(); + } + + this.loadAttributes = async function() { + const noteId = noteDetailService.getCurrentNoteId(); + + const attributes = await server.get('notes/' + noteId + '/attributes'); + + prepareAttributes(attributes); // attribute might not be rendered immediatelly so could not focus setTimeout(() => $(".attribute-name:last").focus(), 100); @@ -125,15 +132,37 @@ function AttributesModel() { .map(attribute => attribute()) .filter(attribute => attribute.attributeId !== "" || attribute.name !== ""); + for (const attr of attributesToSave) { + if (attr.type === 'label') { + attr.value = attr.labelValue; + } + else if (attr.type === 'relation') { + attr.value = attr.relationValue; + } + else if (attr.type === 'label-definition') { + attr.value = JSON.stringify(attr.labelDefinition); + } + else if (attr.type === 'relation-definition') { + attr.value = JSON.stringify(attr.relationDefinition); + } + + delete attr.labelValue; + delete attr.relationValue; + delete attr.labelDefinition; + delete attr.relationDefinition; + } + const attributes = await server.put('notes/' + noteId + '/attributes', attributesToSave); - self.attributes(attributes.map(ko.observable)); - - addLastEmptyRow(); + prepareAttributes(attributes); infoService.showMessage("Attributes have been saved."); - noteDetailService.loadAttributeList(); + // FIXME FIXME FIXME FIXME FIXME + // FIXME FIXME FIXME FIXME FIXME + // FIXME FIXME FIXME FIXME FIXME + // FIXME FIXME FIXME FIXME FIXME + //noteDetailService.loadAttributeList(); }; function addLastEmptyRow() { @@ -150,10 +179,10 @@ function AttributesModel() { isInheritable: false, isDeleted: 0, position: 0, - definition: { + labelDefinition: { valueType: "text", multiplicityType: "singlevalue", - showInUi: "true" + isPromoted: true } })); } @@ -177,7 +206,7 @@ function AttributesModel() { for (let attributes = self.attributes(), i = 0; i < attributes.length; i++) { const attribute = attributes[i](); - if (index !== i && cur.name === attribute.name) { + if (index !== i && cur.name === attribute.name && cur.type === attribute.type) { return true; } } diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.js index f582070ec..76056294a 100644 --- a/src/routes/api/attributes.js +++ b/src/routes/api/attributes.js @@ -31,9 +31,11 @@ async function updateNoteAttributes(req) { attributeEntity.noteId = noteId; } + attributeEntity.type = attribute.type; attributeEntity.name = attribute.name; attributeEntity.value = attribute.value; attributeEntity.position = attribute.position; + attributeEntity.isInheritable = attribute.isInheritable; attributeEntity.isDeleted = attribute.isDeleted; await attributeEntity.save(); diff --git a/src/services/repository.js b/src/services/repository.js index 0f5f5e042..182c18f8d 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -37,6 +37,10 @@ async function getImage(imageId) { return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]); } +async function getAttribute(attributeId) { + return await getEntity("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]); +} + async function getLabel(labelId) { return await getEntity("SELECT * FROM labels WHERE labelId = ?", [labelId]); } @@ -75,6 +79,7 @@ module.exports = { getNote, getBranch, getImage, + getAttribute, getLabel, getRelation, getOption, diff --git a/src/views/index.ejs b/src/views/index.ejs index 1bd43bab6..33cd33ae3 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -591,21 +591,20 @@ -
+
+ data-bind="value: relationValue, valueUpdate: 'blur', event: { blur: $parent.attributeChanged }">
-
- +
+ - + - Show In UI + Promoted