import server from "../services/server.js"; import ws from "../services/ws.js"; import treeService from "../services/tree.js"; import noteAutocompleteService from "../services/note_autocomplete.js"; import TabAwareWidget from "./tab_aware_widget.js"; const TPL = `
`; export default class PromotedAttributesWidget extends TabAwareWidget { doRender() { this.$widget = $(TPL); this.$container = this.$widget.find(".promoted-attributes-container"); } async refreshWithNote(note) { this.$container.empty(); const promotedAttributes = this.getPromotedAttributes(); const attributes = note.getAttributes(); const cells = []; if (promotedAttributes.length === 0) { this.toggleInt(false); return; } for (const definitionAttr of promotedAttributes) { const definitionType = definitionAttr.name.startsWith('label:') ? 'label' : 'relation'; const valueName = definitionAttr.name.substr(definitionType.length + 1); let valueAttrs = attributes.filter(el => el.name === valueName && el.type === definitionType); if (valueAttrs.length === 0) { valueAttrs.push({ attributeId: "", type: definitionType, name: valueName, value: "" }); } if (definitionAttr.value.multiplicity === 'single') { valueAttrs = valueAttrs.slice(0, 1); } for (const valueAttr of valueAttrs) { const $cell = await this.createPromotedAttributeCell(definitionAttr, valueAttr, valueName); cells.push($cell); } } // we replace the whole content in one step so there can't be any race conditions // (previously we saw promoted attributes doubling) this.$container.empty().append(...cells); this.toggleInt(true); } getPromotedAttributes() { if (this.note.hasLabel('hidePromotedAttributes')) { return []; } return this.note.getAttributes() .filter(attr => attr.isDefinition()) .filter(attr => { const def = attr.getDefinition(); return def && def.isPromoted; }); } async createPromotedAttributeCell(definitionAttr, valueAttr, valueName) { const definition = definitionAttr.getDefinition(); 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) .prop("value", valueAttr.value) .addClass("form-control") .addClass("promoted-attribute-input") .on('change', event => this.promotedAttributeChanged(event)); const $actionCell = $("
"); const $multiplicityCell = $("") .addClass("multiplicity") .attr("nowrap", true); const $wrapper = $('