From d64c14482bc49d284ac16cfc975b63eee6d6eda7 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 26 Nov 2021 23:39:08 +0100 Subject: [PATCH] after removing last promoted attribute, create a blank one, fixes #2388 --- .../ribbon_widgets/promoted_attributes.js | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/public/app/widgets/ribbon_widgets/promoted_attributes.js b/src/public/app/widgets/ribbon_widgets/promoted_attributes.js index 0fea1a4a8..d35fe4f0f 100644 --- a/src/public/app/widgets/ribbon_widgets/promoted_attributes.js +++ b/src/public/app/widgets/ribbon_widgets/promoted_attributes.js @@ -115,9 +115,9 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget { const $input = $("") .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-type", valueAttr.type) - .prop("attribute-name", valueAttr.name) + .attr("data-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 + .attr("data-attribute-type", valueAttr.type) + .attr("data-attribute-name", valueAttr.name) .prop("value", valueAttr.value) .addClass("form-control") .addClass("promoted-attribute-input") @@ -230,7 +230,7 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget { } if (definition.multiplicity === "multi") { - const addButton = $("") + const $addButton = $("") .addClass("bx bx-plus pointer") .prop("title", "Add new attribute") .on('click', async () => { @@ -246,12 +246,28 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget { $new.find('input').trigger('focus'); }); - const removeButton = $("") + const $removeButton = $("") .addClass("bx bx-trash pointer") .prop("title", "Remove this attribute") .on('click', async () => { - if (valueAttr.attributeId) { - await server.remove("notes/" + this.noteId + "/attributes/" + valueAttr.attributeId, this.componentId); + const attributeId = $input.attr("data-attribute-id"); + + if (attributeId) { + await server.remove("notes/" + this.noteId + "/attributes/" + attributeId, this.componentId); + } + + // if it's the last one the create new empty form immediately + const sameAttrSelector = `input[data-attribute-type='${valueAttr.type}'][data-attribute-name='${valueName}']`; + + if (this.$widget.find(sameAttrSelector).length <= 1) { + const $new = await this.createPromotedAttributeCell(definitionAttr, { + attributeId: "", + type: valueAttr.type, + name: valueName, + value: "" + }, valueName); + + $wrapper.after($new); } $wrapper.remove(); @@ -259,9 +275,9 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget { $multiplicityCell .append("  ") - .append(addButton) + .append($addButton) .append("  ") - .append(removeButton); + .append($removeButton); } return $wrapper; @@ -275,7 +291,7 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget { if ($attr.prop("type") === "checkbox") { value = $attr.is(':checked') ? "true" : "false"; } - else if ($attr.prop("attribute-type") === "relation") { + else if ($attr.attr("data-attribute-type") === "relation") { const selectedPath = $attr.getSelectedNotePath(); value = selectedPath ? treeService.getNoteIdFromNotePath(selectedPath) : ""; @@ -285,13 +301,13 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget { } const result = await server.put(`notes/${this.noteId}/attribute`, { - attributeId: $attr.prop("attribute-id"), - type: $attr.prop("attribute-type"), - name: $attr.prop("attribute-name"), + attributeId: $attr.attr("data-attribute-id"), + type: $attr.attr("data-attribute-type"), + name: $attr.attr("data-attribute-name"), value: value }, this.componentId); - $attr.prop("attribute-id", result.attributeId); + $attr.attr("data-attribute-id", result.attributeId); } entitiesReloadedEvent({loadResults}) {