import TabAwareWidget from "./tab_aware_widget.js";
import AttributeDetailWidget from "./attribute_detail.js";
import attributeRenderer from "../services/attribute_renderer.js";
import AttributeEditorWidget from "./attribute_editor.js";
const TPL = `
`;
export default class AttributeListWidget extends TabAwareWidget {
constructor() {
super();
this.attributeEditorWidget = new AttributeEditorWidget().setParent(this);
this.attributeDetailWidget = new AttributeDetailWidget().setParent(this);
this.child(this.attributeEditorWidget, this.attributeDetailWidget);
}
doRender() {
this.$widget = $(TPL);
this.$attrDisplay = this.$widget.find('.attr-display');
this.$ownedExpander = this.$widget.find('.attr-owned-expander');
this.$ownedExpander.on('click', () => {
if (this.$attrDisplay.is(":visible")) {
this.$attrDisplay.slideUp(200);
}
else {
this.$attrDisplay.slideDown(200);
}
});
this.$ownedExpanderText = this.$ownedExpander.find('.attr-expander-text');
this.$inheritedAttributes = this.$widget.find('.inherited-attributes');
this.$inheritedExpander = this.$widget.find('.attr-inherited-expander');
this.$inheritedExpander.on('click', () => {
if (this.$inheritedAttributes.is(":visible")) {
this.$inheritedAttributes.slideUp(200);
}
else {
this.$inheritedAttributes.slideDown(200);
}
});
this.$inheritedExpanderText = this.$inheritedExpander.find('.attr-expander-text');
this.$inheritedEmptyExpander = this.$widget.find('.attr-inherited-empty-expander');
this.$widget.find('.attr-editor-placeholder').replaceWith(this.attributeEditorWidget.render());
this.$widget.append(this.attributeDetailWidget.render());
}
async refreshWithNote(note) {
const ownedAttributes = note.getOwnedAttributes();
this.$ownedExpanderText.text(ownedAttributes.length + ' owned ' + this.attrPlural(ownedAttributes.length));
const inheritedAttributes = note.getAttributes().filter(attr => attr.noteId !== this.noteId);
if (inheritedAttributes.length === 0) {
this.$inheritedExpander.hide();
this.$inheritedEmptyExpander.show();
}
else {
this.$inheritedExpander.show();
this.$inheritedEmptyExpander.hide();
}
this.$inheritedExpanderText.text(inheritedAttributes.length + ' inherited ' + this.attrPlural(inheritedAttributes.length));
this.$inheritedAttributes.empty();
await this.renderInheritedAttributes(inheritedAttributes, this.$inheritedAttributes);
}
attrPlural(number) {
return 'attribute' + (number === 1 ? '' : 's');
}
renderInheritedAttributes(attributes, $container) {
for (const attribute of attributes) {
const $span = $("")
.on('click', e => this.attributeDetailWidget.showAttributeDetail({
attribute: {
noteId: attribute.noteId,
type: attribute.type,
name: attribute.name,
value: attribute.value
},
isOwned: false,
x: e.pageX,
y: e.pageY
}));
$container.append($span);
attributeRenderer.renderAttribute(attribute, $span, false);
}
}
}