packageDocumentation module import { immerable } from "immer" ; import shortid from "shortid" ; import { PropertyDescription , PropertyRecord , PropertyValueFormat } from "@itwin/appui-abstract" ; import { PropertyCategory } from "../../PropertyDataProvider" ; beta export enum FlatGridItemType { Category , Primitive , Array , Struct } beta export type CategorizedPropertyTypes = FlatGridItemType . Array | FlatGridItemType . Primitive | FlatGridItemType . Struct ; beta export interface IMutableFlatPropertyGridItem { readonly type : FlatGridItemType ; readonly key : string ; readonly selectionKey : string ; readonly parentSelectionKey : string | undefined ; readonly parentCategorySelectionKey : string | undefined ; readonly depth : number ; readonly label : string ; lastInNumberOfCategories : number ; isLastInRootCategory : boolean ; isExpanded : boolean ; getDescendantsAndSelf ( ) : IMutableFlatGridItem [ ] ; getVisibleDescendantsAndSelf ( ) : IMutableFlatGridItem [ ] ; getChildren ( ) : IMutableFlatGridItem [ ] ; getLastVisibleDescendantOrSelf ( ) : IMutableFlatGridItem ; } beta export abstract class MutableFlatPropertyGridItem implements IMutableFlatPropertyGridItem { public [ immerable ] = true ; public readonly key : string = shortid . generate ( ) ; protected _isExpanded : boolean = false ; protected _lastInNumberOfCategories : number = 0 ; protected _isLastInRootCategory : boolean = false ; public constructor ( private _depth , private _parentSelectionKey , private _parentCategorySelectionKey ) { if ( this . _depth < 0 ) throw new Error ( "Depth cannot be negative" ) ; } public abstract type : FlatGridItemType ; public abstract label : string ; public abstract selectionKey : string ; public get depth ( ) { return this . _depth ; } public get parentSelectionKey ( ) { return this . _parentSelectionKey ; } public get parentCategorySelectionKey ( ) { return this . _parentCategorySelectionKey ; } public get isExpanded ( ) { return this . _isExpanded ; } public set isExpanded ( value ) { this . _isExpanded = value ; } public abstract getChildren ( ) ; public abstract getSelf ( ) ; protected getDescendants ( ) { const descendants = [ ] ; this . getChildren ( ) . forEach ( ( child ) => descendants . push ( ... child . getDescendantsAndSelf ( ) ) ) ; return descendants ; } /**
     * Gets a flat list of all FlatGridItems beneath this flat grid item including itself in depth first visiting order.
     */ public getDescendantsAndSelf ( ) { return [ this . getSelf ( ) , ... this . getDescendants ( ) ] ; } /**
     * Gets a flat list of visible FlatGridItems beneath this flat grid item.
     */ public getVisibleDescendants ( ) { const descendants = [ ] ; if ( this . isExpanded ) this . getChildren ( ) . forEach ( ( child ) => descendants . push ( ... child . getVisibleDescendantsAndSelf ( ) ) ) ; return descendants ; } /**
     * Gets a flat list of visible FlatGridItems beneath this flat grid item and itself in depth first visiting order.
     */ public getVisibleDescendantsAndSelf ( ) { return [ this . getSelf ( ) , ... this . getVisibleDescendants ( ) ] ; } returns public getLastVisibleDescendantOrSelf ( ) { if ( ! this . isExpanded ) return this . getSelf ( ) ; const children = this . getChildren ( ) ; if ( children . length < 1 ) return this . getSelf ( ) ; return children [ children . length - 1 ] . getLastVisibleDescendantOrSelf ( ) ; } internal public get lastInNumberOfCategories ( ) : number { if ( this . isExpanded && this . getChildren ( ) . length > 0 ) return 0 ; return this . _lastInNumberOfCategories ; } public set lastInNumberOfCategories ( value ) { this . _lastInNumberOfCategories = value ; const children = this . getChildren ( ) ; if ( children . length < 1 ) return ; children [ children . length - 1 ] . lastInNumberOfCategories = value ; } internal public get isLastInRootCategory ( ) : boolean { if ( this . isExpanded && this . getChildren ( ) . length > 0 ) return false ; return this . _isLastInRootCategory ; } public set isLastInRootCategory ( value ) { this . _isLastInRootCategory = value ; const children = this . getChildren ( ) ; if ( children . length < 1 ) return ; children [ children . length - 1 ] . isLastInRootCategory = value ; } } beta export interface IMutableGridCategoryItem extends IMutableFlatPropertyGridItem { type : FlatGridItemType . Category ; name : string ; derivedCategory : PropertyCategory ; isRootCategory : boolean ; getChildCategories ( ) : IMutableGridCategoryItem [ ] ; getDescendantCategoriesAndSelf ( ) : IMutableGridCategoryItem [ ] ; } beta export interface IMutableCategorizedPropertyItem extends IMutableFlatPropertyGridItem { readonly type : CategorizedPropertyTypes ; readonly derivedRecord : PropertyRecord ; readonly parentCategorySelectionKey : string ; readonly parentSelectionKey : string ; getChildren ( ) : IMutableCategorizedPropertyItem [ ] ; } beta export type IMutableFlatGridItem = IMutableCategorizedPropertyItem | IMutableGridCategoryItem ; beta export abstract class MutableCategorizedProperty extends MutableFlatPropertyGridItem implements Partial < IMutableCategorizedPropertyItem > { private _derivedRecord : PropertyRecord ; private _selectionKey : string ; public constructor ( type , record , parentSelectionKey , parentCategorySelectionKey , depth , overrideName ? , overrideDisplayLabel ? ) { super ( depth , parentSelectionKey , parentCategorySelectionKey ) ; const recordType = this . valueTypeToFlatGridType ( record . value . valueFormat ) ; if ( recordType !== type ) { const expectedTypeStr = FlatGridItemType [ type ] ; const actualTypeStr = FlatGridItemType [ recordType ] ; throw Error ( `Record with incorrect value format passed to property: expected ${ expectedTypeStr }, got ${ actualTypeStr }` ) ; } const overridenProperty = this . makeOverridenProperty ( record . property , overrideName , overrideDisplayLabel ) ; this . _derivedRecord = this . makeDerivedRecord ( record , overridenProperty ) ; this . _selectionKey = `${ this . parentSelectionKey }_${ this . derivedRecord . property . name }` ; this . _isExpanded = ! ! record . autoExpand ; } public abstract override type : CategorizedPropertyTypes ; public abstract override getChildren ( ) ; param valueType returns private valueTypeToFlatGridType ( valueType ) { switch ( valueType ) { case PropertyValueFormat . Primitive : return FlatGridItemType . Primitive ; case PropertyValueFormat . Array : return FlatGridItemType . Array ; case PropertyValueFormat . Struct : return FlatGridItemType . Struct ; default : const unhandledType = valueType ; throw Error ( `Property Value Format not handled: ${ unhandledType }` ) ; } } param propertyDescription param overrideName param overrideDisplay private makeOverridenProperty ( propertyDescription , overrideName ? , overrideDisplay ? ) { const { name , displayLabel , ... property } = { ... propertyDescription } ; const newName = overrideName ?? name ; const newDisplayLabel = overrideDisplay ?? displayLabel ; return { ... property , name : newName , displayLabel : newDisplayLabel } ; } /**
     * Gets derived property record that has it's property description field overriden
     */ private makeDerivedRecord ( record , overridenPropertyDescription ) { const { value , property , ... others } = record ; const newRecord = new PropertyRecord ( value , overridenPropertyDescription ) ; return Object . assign ( newRecord , others ) ; } public getSelf ( ) { return this ; } public override get parentCategorySelectionKey ( ) { return super . parentCategorySelectionKey ! ; } public override get parentSelectionKey ( ) { return super . parentSelectionKey ! ; } /**
     * Unique selection key made of parent selectionKey and this property name.
     */ public get selectionKey ( ) { return this . _selectionKey ; } public get label ( ) { return this . _derivedRecord . property . displayLabel ; } /**
     * Record with overriden property description.
     */ public get derivedRecord ( ) { return this . _derivedRecord ; } }