UNPKG

service-utilities

Version:

Utility Package for FIORI UI5

125 lines (111 loc) 3.5 kB
/** * @module ControlExpansion * @description Utility class for contorl related methods * @author jpanti * @version 1.0.0 * @created 2025-08-01 * @lastModified 2025-08-01 * @license ISC */ sap.ui.define( [ "sap/ui/model/Filter", "sap/ui/model/FilterOperator", "sap/m/Input", "sap/m/Button", "sap/m/SegmentedButton", "sap/m/SegmentedButtonItem", "sap/m/ToolbarSpacer", "sap/ui/layout/form/FormElement", ], ( Filter, FilterOperator, Input, Button, SegmentedButton, SegmentedButtonItem, ToolbarSpacer, FormElement ) => { "use strict"; return { // press Attribute getDataOnPress(oEvent, bindedModelName) { const oItem = oEvent.getSource(); const oContext = oItem.getBindingContext(bindedModelName); return oContext.getObject(); }, // selectionChange Attribute of Button Segment; select attribute of Icon Tab Header getKeyOnSelectionChange: (oEvent) => oEvent.getParameter("item").getProperty("key"), // find control by id findControlById(oController, sIdToFind) { return ( this.findControlByIdFast(oController, sIdToFind) ?? this.findControlByIdManual(oController.getView(), sIdToFind) ); }, findControlByIdManual(oParentView, sIdToFind) { // Check if the parent itself matches if (oParentView.getId && oParentView.getId().endsWith(sIdToFind)) { return oParentView; } // Check its aggregations for (const sAggregationName in oParentView.mAggregations) { const vAggregation = oParentView.getAggregation(sAggregationName); if (Array.isArray(vAggregation)) { for (const oChild of vAggregation) { const found = this.findControlByIdManual(oChild, sIdToFind); if (found) return found; } } else if (vAggregation) { const found = this.findControlByIdManual(vAggregation, sIdToFind); if (found) return found; } } // Not found return null; }, findControlByIdFast(oController, sIdToFind) { return oController.byId(sIdToFind); }, applySearchFilters(oController, listName, filterKeys, sQuery = "") { var aFilters = []; if (sQuery && sQuery.length > 0) { aFilters.push( new Filter({ filters: filterKeys.map( (key) => new Filter(key, FilterOperator.Contains, sQuery) ), and: false, }) ); } var oList = this.findControlById(oController, listName); var oBinding = oList.getBinding("items"); oBinding.filter(aFilters); }, createElementInput(attributes) { return new Input(attributes); }, createElementSegmentButton(attributes) { const items = (attributes["items"] ?? []).map( (item) => new SegmentedButtonItem(item) ); attributes.items = items; return new SegmentedButton(attributes); }, createElementFormElement(attributes, ...children) { attributes["fields"] = children; return new FormElement(attributes); }, createElementButton(attributes) { return new Button(attributes); }, createElementToolbarSpacer(attributes) { return new ToolbarSpacer(attributes); }, }; } );