import { PropertyProxy } from '@fluid-experimental/property-proxy' ; import { InputValidator } from './InputValidator' ; import { PropertyFactory } from '@fluid-experimental/property-properties' ; import { TypeIdHelper } from "@fluid-experimental/property-changeset" ; import { IDataCreationOptions , IInspectorRow } from './InspectorTableTypes' ; const EXCLUDE_PROPS = [ 'BaseProperty' , 'Enum' , 'ContainerProperty' ] ; export const fetchRegisteredTemplates = ( ) => { const toTemplateList = ( x ) => ( { value : x , label : x } ) ; const localTemplates = PropertyFactory . _localPrimitivePropertiesAndTemplates . getItems ( ) ; const primitiveLocalTemplates = [ ] ; const customLocalTemplates = [ ] ; Object . keys ( localTemplates ) . forEach ( ( item ) => { if ( ! TypeIdHelper . isTemplateTypeid ( item ) && EXCLUDE_PROPS . indexOf ( item ) === - 1 ) { primitiveLocalTemplates . push ( item ) ; } else if ( TypeIdHelper . isTemplateTypeid ( item ) ) { customLocalTemplates . push ( item ) ; } } ) ; const templates = [ [ 'Primitives' , primitiveLocalTemplates . map ( toTemplateList ) ] , [ 'Custom' , customLocalTemplates . map ( toTemplateList ) ] ] ; return templates ; } ; export const handlePropertyDataCreationOptionGeneration = ( rowData , nameOnly ) : IDataCreationOptions => { if ( nameOnly ) { return { name : 'property' } ; } const templates = fetchRegisteredTemplates ( ) ; return { name : 'property' , options : templates } ; } ; param name param typeid param context param parent const createProperty = ( name , typeid , context , parent ) => { if ( ! ( [ 'set' , 'array' ] . includes ( parent . getProperty ( ) . getContext ( ) ) ) ) { InputValidator . validateNotEmpty ( name ) ; } const newProp = PropertyFactory . create ( typeid , context ) ; if ( Array . isArray ( parent ) ) { parent . push ( newProp ) ; } else if ( parent instanceof Map ) { if ( parent . has ( name ) ) { throw new Error ( 'Key already exists in the map: ' + name ) ; } else { parent . set ( name , newProp ) ; } } else if ( parent instanceof Set ) { parent . add ( newProp ) ; } else { if ( parent . getProperty ( ) . has ( name ) ) { throw new Error ( 'Key already exists in this property: ' + name ) ; } else { parent [ name ] = newProp ; } } return parent . getProperty ( ) . getRoot ( ) . getWorkspace ( ) . commit ( ) ; } ; export const handlePropertyDataCreation = ( rowData , name , typeid , context ) => { return createProperty ( name , typeid , context , PropertyProxy . proxify ( rowData . parent ! ) ) ; } ;