2021-03-30 05:29:03 +00:00
import WidgetFactory from "utils/WidgetFactory" ;
import { getAllPathsFromPropertyConfig } from "entities/Widget/utils" ;
import { getEntityDynamicBindingPathList } from "utils/DynamicBindingUtils" ;
import _ from "lodash" ;
2022-01-28 11:10:05 +00:00
2021-03-30 05:29:03 +00:00
import { FlattenedWidgetProps } from "reducers/entityReducers/canvasWidgetsReducer" ;
2022-01-28 11:10:05 +00:00
import { setOverridingProperty } from "./utils" ;
import {
OverridingPropertyPaths ,
PropertyOverrideDependency ,
OverridingPropertyType ,
DataTreeWidget ,
ENTITY_TYPE ,
} from "./dataTreeFactory" ;
2021-03-30 05:29:03 +00:00
export const generateDataTreeWidget = (
widget : FlattenedWidgetProps ,
2022-01-28 11:10:05 +00:00
widgetMetaProps : Record < string , unknown > = { } ,
2021-03-30 05:29:03 +00:00
) : DataTreeWidget = > {
2022-01-28 11:10:05 +00:00
const derivedProps : any = { } ;
const blockedDerivedProps : Record < string , true > = { } ;
const unInitializedDefaultProps : Record < string , undefined > = { } ;
const propertyOverrideDependency : PropertyOverrideDependency = { } ;
const overridingPropertyPaths : OverridingPropertyPaths = { } ;
2021-03-30 05:29:03 +00:00
const defaultMetaProps = WidgetFactory . getWidgetMetaPropertiesMap (
widget . type ,
) ;
2021-06-18 07:42:57 +00:00
2021-03-30 05:29:03 +00:00
const derivedPropertyMap = WidgetFactory . getWidgetDerivedPropertiesMap (
widget . type ,
) ;
const defaultProps = WidgetFactory . getWidgetDefaultPropertiesMap ( widget . type ) ;
2021-06-18 07:42:57 +00:00
2021-03-30 05:29:03 +00:00
const propertyPaneConfigs = WidgetFactory . getWidgetPropertyPaneConfig (
widget . type ,
) ;
const dynamicBindingPathList = getEntityDynamicBindingPathList ( widget ) ;
2022-01-28 11:10:05 +00:00
// Ensure all dynamic bindings are strings as they will be evaluated
2021-03-30 05:29:03 +00:00
dynamicBindingPathList . forEach ( ( dynamicPath ) = > {
const propertyPath = dynamicPath . key ;
const propertyValue = _ . get ( widget , propertyPath ) ;
if ( _ . isObject ( propertyValue ) ) {
// Stringify this because composite controls may have bindings in the sub controls
_ . set ( widget , propertyPath , JSON . stringify ( propertyValue ) ) ;
}
} ) ;
2022-01-28 11:10:05 +00:00
// Derived props are stored in different maps for further treatment
2021-03-30 05:29:03 +00:00
Object . keys ( derivedPropertyMap ) . forEach ( ( propertyName ) = > {
// TODO regex is too greedy
2022-01-28 11:10:05 +00:00
// Replace the references to `this` with the widget name reference
// in the derived property bindings
2021-03-30 05:29:03 +00:00
derivedProps [ propertyName ] = derivedPropertyMap [ propertyName ] . replace (
/this./g ,
` ${ widget . widgetName } . ` ,
) ;
2022-01-28 11:10:05 +00:00
// Add these to the dynamicBindingPathList as well
2021-03-30 05:29:03 +00:00
dynamicBindingPathList . push ( {
key : propertyName ,
} ) ;
} ) ;
2022-01-28 11:10:05 +00:00
2021-06-04 07:09:36 +00:00
Object . keys ( derivedProps ) . forEach ( ( propertyName ) = > {
2022-01-28 11:10:05 +00:00
// Do not log errors for the derived property bindings
2021-06-04 07:09:36 +00:00
blockedDerivedProps [ propertyName ] = true ;
} ) ;
2022-01-28 11:10:05 +00:00
const overridingMetaPropsMap : Record < string , boolean > = { } ;
Object . entries ( defaultProps ) . forEach (
( [ propertyName , defaultPropertyName ] ) = > {
if ( ! ( defaultPropertyName in widget ) ) {
unInitializedDefaultProps [ defaultPropertyName ] = undefined ;
}
// defaultProperty on eval needs to override the widget's property eg: defaultText overrides text
setOverridingProperty ( {
propertyOverrideDependency ,
overridingPropertyPaths ,
value : defaultPropertyName ,
key : propertyName ,
type : OverridingPropertyType . DEFAULT ,
} ) ;
if ( propertyName in defaultMetaProps ) {
// Overriding properties will override the values of a property when evaluated
setOverridingProperty ( {
propertyOverrideDependency ,
overridingPropertyPaths ,
value : ` meta. ${ propertyName } ` ,
key : propertyName ,
type : OverridingPropertyType . META ,
} ) ;
overridingMetaPropsMap [ propertyName ] = true ;
}
} ,
) ;
const overridingMetaProps : Record < string , unknown > = { } ;
// overridingMetaProps has all meta property value either from metaReducer or default set by widget whose dependent property also has default property.
Object . entries ( defaultMetaProps ) . forEach ( ( [ key , value ] ) = > {
if ( overridingMetaPropsMap [ key ] ) {
overridingMetaProps [ key ] =
key in widgetMetaProps ? widgetMetaProps [ key ] : value ;
}
} ) ;
2021-04-26 05:41:32 +00:00
const {
bindingPaths ,
triggerPaths ,
validationPaths ,
} = getAllPathsFromPropertyConfig ( widget , propertyPaneConfigs , {
. . . derivedPropertyMap ,
. . . defaultMetaProps ,
. . . unInitializedDefaultProps ,
. . . _ . keyBy ( dynamicBindingPathList , "key" ) ,
2022-01-28 11:10:05 +00:00
. . . overridingPropertyPaths ,
2021-04-26 05:41:32 +00:00
} ) ;
2022-01-28 11:10:05 +00:00
2021-03-30 05:29:03 +00:00
return {
. . . widget ,
2022-01-28 11:10:05 +00:00
. . . unInitializedDefaultProps ,
2021-03-30 05:29:03 +00:00
. . . defaultMetaProps ,
. . . widgetMetaProps ,
. . . derivedProps ,
2021-06-18 07:42:57 +00:00
defaultProps ,
defaultMetaProps : Object.keys ( defaultMetaProps ) ,
2021-03-30 05:29:03 +00:00
dynamicBindingPathList ,
2021-06-04 07:09:36 +00:00
logBlackList : {
. . . widget . logBlackList ,
. . . blockedDerivedProps ,
} ,
2022-01-28 11:10:05 +00:00
meta : {
. . . overridingMetaProps ,
2022-02-25 20:46:04 +00:00
. . . widgetMetaProps ,
2022-01-28 11:10:05 +00:00
} ,
propertyOverrideDependency ,
overridingPropertyPaths ,
2021-03-30 05:29:03 +00:00
bindingPaths ,
triggerPaths ,
2021-04-21 14:34:25 +00:00
validationPaths ,
2021-03-30 05:29:03 +00:00
ENTITY_TYPE : ENTITY_TYPE.WIDGET ,
2022-01-20 10:59:03 +00:00
privateWidgets : {
. . . widget . privateWidgets ,
} ,
2021-03-30 05:29:03 +00:00
} ;
} ;