packageDocumentation module import { produce } from "immer" ; import { PropertyData } from "../PropertyDataProvider" ; import { IMutableFlatGridItem } from "./flat-items/MutableFlatGridItem" ; import { IMutableGridItemFactory } from "./flat-items/MutableGridItemFactory" ; import { IMutablePropertyGridModel , IPropertyGridModel , MutablePropertyGridModel } from "./PropertyGridModel" ; import { PropertyGridModelChangeEvent } from "./PropertyGridModelChangeEvent" ; beta export interface IPropertyGridModelSource { onModelChanged : PropertyGridModelChangeEvent ; setPropertyData : ( data : PropertyData ) => void ; modifyModel ( callback ) : void ; getModel ( ) : IPropertyGridModel | undefined ; } beta export class PropertyGridModelSource implements IPropertyGridModelSource { private _model ? : IMutablePropertyGridModel ; /** Event that is emitted every time property model is changed. */ public onModelChanged = new PropertyGridModelChangeEvent ( ) ; public constructor ( private _gridFactory ) { } private getFullModelMap ( fullModel ) { const fullModelMap = new Map < string , IMutableFlatGridItem > ( ) ; for ( const item of fullModel ) { const key = item . selectionKey ; fullModelMap . set ( key , item ) ; } return fullModelMap ; } private moveOldModelState ( oldModel , newModel ) { const oldModelMap = this . getFullModelMap ( oldModel . getFlatGrid ( ) ) ; const flatGrid = newModel . getFlatGrid ( ) ; flatGrid . forEach ( ( gridItem ) => { const oldGridItem = oldModelMap . get ( gridItem . selectionKey ) ; if ( oldGridItem ) gridItem . isExpanded = oldGridItem . isExpanded ; } ) ; } public setPropertyData ( data ) { const newModel = new MutablePropertyGridModel ( data , this . _gridFactory ) ; if ( this . _model !== undefined && data . reusePropertyDataState ) this . moveOldModelState ( this . _model , newModel ) ; this . _model = newModel ; this . onModelChanged . raiseEvent ( ) ; } /**
     * Modifies property grid model using provided callback.
     * If changes to model are detected, onModelChanged emits an event to all subscribers.
     */ public modifyModel ( callback ) { if ( ! this . _model ) return ; this . _model = produce ( this . _model , callback ) ; this . onModelChanged . raiseEvent ( ) ; } /** Returns property grid model. */ public getModel ( ) { return this . _model ; } }