|
@@ -73,22 +73,22 @@ export { CustomStructureProperty }
|
|
|
|
|
|
namespace CustomStructureProperty {
|
|
|
export interface Provider<Params extends PD.Params, Value> {
|
|
|
- label: string
|
|
|
- descriptor: CustomPropertyDescriptor
|
|
|
- getParams: (data: Structure) => Params
|
|
|
- isApplicable: (data: Structure) => boolean
|
|
|
- attach: (data: Structure, props?: Partial<PD.Values<Params>>) => Task<void>
|
|
|
- getValue: (data: Structure) => ValueBox<Value | undefined>
|
|
|
- setProps: (data: Structure, props: PD.Values<Params>) => void
|
|
|
+ readonly label: string
|
|
|
+ readonly descriptor: CustomPropertyDescriptor
|
|
|
+ readonly getParams: (data: Structure) => Params
|
|
|
+ readonly isApplicable: (data: Structure) => boolean
|
|
|
+ readonly attach: (data: Structure, props?: Partial<PD.Values<Params>>) => Task<void>
|
|
|
+ readonly getValue: (data: Structure) => ValueBox<Value | undefined>
|
|
|
+ readonly setProps: (data: Structure, props: PD.Values<Params>) => void
|
|
|
}
|
|
|
|
|
|
export interface ProviderBuilder<Params extends PD.Params, Value> {
|
|
|
- label: string
|
|
|
- defaultParams: Params
|
|
|
- getParams: (data: Structure) => Params
|
|
|
- isApplicable: (data: Structure) => boolean
|
|
|
- compute: (ctx: RuntimeContext, data: Structure, props: PD.Values<Params>) => Promise<Value>
|
|
|
- descriptor: CustomPropertyDescriptor
|
|
|
+ readonly label: string
|
|
|
+ readonly descriptor: CustomPropertyDescriptor
|
|
|
+ readonly defaultParams: Params
|
|
|
+ readonly getParams: (data: Structure) => Params
|
|
|
+ readonly isApplicable: (data: Structure) => boolean
|
|
|
+ readonly compute: (ctx: RuntimeContext, data: Structure, props: PD.Values<Params>) => Promise<Value>
|
|
|
}
|
|
|
|
|
|
// TODO currently this always uses .inheritedPropertyData
|
|
@@ -131,7 +131,7 @@ namespace CustomStructureProperty {
|
|
|
// this invalidates property.value
|
|
|
set(data, p, undefined)
|
|
|
}
|
|
|
- },
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -141,33 +141,44 @@ namespace CustomStructureProperty {
|
|
|
}
|
|
|
|
|
|
export class Registry {
|
|
|
- private providers = OrderedMap<string, Provider<any, any>>().asMutable();
|
|
|
+ private providers = OrderedMap<string, Provider<any, any>>().asMutable()
|
|
|
+ private defaultAutoAttachValues = new Map<string, boolean>()
|
|
|
|
|
|
/** Get params for all applicable property providers */
|
|
|
getParams(data: Structure) {
|
|
|
const values = this.providers.values();
|
|
|
- const params: PD.Params = {};
|
|
|
+ const params: PD.Params = {}
|
|
|
while (true) {
|
|
|
- const v = values.next();
|
|
|
- if (v.done) break;
|
|
|
- if (!v.value.isApplicable(data)) continue;
|
|
|
- params[v.value.descriptor.name] = PD.Group(v.value.getParams(data), { label: v.value.label })
|
|
|
+ const v = values.next()
|
|
|
+ if (v.done) break
|
|
|
+ const provider = v.value
|
|
|
+ if (!provider.isApplicable(data)) continue
|
|
|
+ params[provider.descriptor.name] = PD.Group({
|
|
|
+ autoAttach: PD.Boolean(this.defaultAutoAttachValues.get(provider.descriptor.name)!),
|
|
|
+ ...provider.getParams(data),
|
|
|
+ }, { label: v.value.label })
|
|
|
}
|
|
|
return params
|
|
|
}
|
|
|
|
|
|
+ setDefaultAutoAttach(name: string, value: boolean) {
|
|
|
+ this.defaultAutoAttachValues.set(name, value)
|
|
|
+ }
|
|
|
+
|
|
|
get(name: string) {
|
|
|
const prop = this.providers.get(name);
|
|
|
- if (!prop) throw new Error(`Custom property '${name}' is not registered.`);
|
|
|
- return this.providers.get(name);
|
|
|
+ if (!prop) throw new Error(`Custom property '${name}' is not registered.`)
|
|
|
+ return this.providers.get(name)
|
|
|
}
|
|
|
|
|
|
- register(provider: Provider<any, any>) {
|
|
|
- this.providers.set(provider.descriptor.name, provider);
|
|
|
+ register(provider: Provider<any, any>, defaultAutoAttach: boolean) {
|
|
|
+ this.providers.set(provider.descriptor.name, provider)
|
|
|
+ this.defaultAutoAttachValues.set(provider.descriptor.name, defaultAutoAttach)
|
|
|
}
|
|
|
|
|
|
unregister(name: string) {
|
|
|
- this.providers.delete(name);
|
|
|
+ this.providers.delete(name)
|
|
|
+ this.defaultAutoAttachValues.delete(name)
|
|
|
}
|
|
|
}
|
|
|
}
|