/*! * SAPUI5 * Copyright (c) 2025 SAP SE or an SAP affiliate company. All rights reserved. */ import { SearchResultSetItemAttributeBase, SearchResultSetItemAttributeBaseOptions, } from "./SearchResultSetItemAttributeBase"; import { SearchResultSetItemAttributeGroupMembership } from "./SearchResultSetItemAttributeGroupMembership"; import { AttributeGroupMetadata } from "./AttributeGroupMetadata"; import { SearchResultSetItemAttribute } from "./SearchResultSetItemAttribute"; import { SearchResultSetItemAttributeValueForm } from "./SearchResultSetItemAttributeValueForm"; export interface SearchResultSetItemAttributeGroupOptions extends SearchResultSetItemAttributeBaseOptions { id: string; metadata: AttributeGroupMetadata; template?: string; attributes: Array; displayAttributes?: Array; } export class SearchResultSetItemAttributeGroup extends SearchResultSetItemAttributeBase { // _meta: { // properties: { // template: { // required: false // }, // attributes: { // required: true, // default: function () { // return []; // } // } // displayAttributes: { // required: false, // default: function () { // return []; // } // } // } // } template: string; attributes: Array = []; displayAttributes: Array = []; constructor(properties: SearchResultSetItemAttributeGroupOptions) { super(properties); this.template = properties.template ?? this.template; this.attributes = properties.attributes ?? this.attributes; this.displayAttributes = properties.displayAttributes ?? this.displayAttributes; } hasHighlight(): boolean { for (const subAttributeMembersip of this.attributes) { if (subAttributeMembersip.attribute.hasHighlight()) { return true; } } return false; } toString(): string { let valueFormatted = "", pos = 0; let match; const regex = RegExp("{[a-z]+}", "gi"); while ((match = regex.exec(this.template)) !== null) { valueFormatted += this.template.substring(pos, match.index); const attributeName = match[0].slice(1, -1); valueFormatted += (this.attributes[attributeName] && this.attributes[attributeName].valueFormatted) || ""; // TODO: What if this.attributes[attributeName] is a group? pos = regex.lastIndex; } valueFormatted += this.template.substring(pos); return this.label + ": " + valueFormatted; } isAttributeDisplayed(attributeId: string): boolean { if (Array.isArray(this.displayAttributes)) { return this.displayAttributes.includes(attributeId); } return false; } getSubAttributes(): Array { const attributes: Array = []; for (const attributeMemberShip of this.attributes) { attributes.push(...attributeMemberShip.attribute.getSubAttributes()); } return attributes; } get isHighlighted(): boolean { const subAttributes = this.getSubAttributes(); for (const subAttribute of subAttributes) { if (subAttribute.isHighlighted === true) { return true; } } return false; } getValueFormatted(): string { return this.formatAttributeGroupValue(SearchResultSetItemAttributeValueForm.valueFormatted) || ""; } getValueHighlighted(): string { return ( this.formatAttributeGroupValue(SearchResultSetItemAttributeValueForm.valueHighlighted) || this.getValueFormatted() || "" ); } private formatAttributeGroupValue(valueForm: SearchResultSetItemAttributeValueForm): string { const attributesMapByNameInGroup: { [key: string]: string } = {}; let returnValue: string = ""; for (let i = 0; i < this.attributes.length; i++) { const attributeGroupMembership = this.attributes[i]; const subAttribute = attributeGroupMembership.attribute; const attributeNameInGroup = attributeGroupMembership.metadata.nameInGroup; attributesMapByNameInGroup[attributeNameInGroup] = valueForm === SearchResultSetItemAttributeValueForm.valueFormatted ? subAttribute.getValueFormatted() : subAttribute.getValueHighlighted(); } if (Object.keys(attributesMapByNameInGroup).length > 0) { returnValue += this.formatBasedOnGroupTemplate(this.template, attributesMapByNameInGroup); } return returnValue; } private formatBasedOnGroupTemplate( template: string, attributesMapByNameInGroup: { [key: string]: string } ): string { if (!(template && attributesMapByNameInGroup)) { return ""; } const regex = /{\w+}/gi; let value = ""; let pos = 0; let match; while ((match = regex.exec(template)) !== null) { value += template.substring(pos, match.index); const attributeName = match[0].slice(1, -1); const attributeValue = attributesMapByNameInGroup[attributeName]; value += attributeValue || ""; pos = regex.lastIndex; } value += template.substring(pos); return value; } }