after removing last promoted attribute, create a blank one, fixes #2388

This commit is contained in:
zadam 2021-11-26 23:39:08 +01:00
parent 564366861e
commit d64c14482b

View File

@ -115,9 +115,9 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget {
const $input = $("<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 = $("<span>")
const $addButton = $("<span>")
.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 = $("<span>")
const $removeButton = $("<span>")
.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(" &nbsp;")
.append(addButton)
.append($addButton)
.append(" &nbsp;")
.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}) {