|
| 1 | +import { Unsubscribe } from 'redux'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Component definition, serving as a blueprint to create new instances, as well |
| 5 | + * as providing access to existing instances. |
| 6 | + * |
| 7 | + * Use the `create` method to create new instance and (optionally) mount it to |
| 8 | + * the given DOM element, depending on whether the component requires DOM context. |
| 9 | + * |
| 10 | + * Note that the `create` method itself is optional - components may expose their |
| 11 | + * existing instances without allowing further instantiation. |
| 12 | + */ |
| 13 | +export interface ComponentDefinition { |
| 14 | + |
| 15 | + /** |
| 16 | + * Name that reflects the component type (must be unique). |
| 17 | + */ |
| 18 | + name: string; |
| 19 | + |
| 20 | + /** |
| 21 | + * Create new instance and (optionally) mount it to the given DOM element. |
| 22 | + * |
| 23 | + * @param props Used to initialize the component. |
| 24 | + * @param mountTo DOM element to mount the component to. |
| 25 | + * @param callback Callback for providing component instance. |
| 26 | + * |
| 27 | + * @returns Function to destroy (and unmount) the component. |
| 28 | + */ |
| 29 | + create?(props?: any, mountTo?: HTMLElement, callback?: ComponentInstanceCallback): Unsubscribe; |
| 30 | + |
| 31 | + /** |
| 32 | + * Get instance of this component, as indicated by its `id`. |
| 33 | + * |
| 34 | + * @param id Instance `id` to lookup. |
| 35 | + * |
| 36 | + * @returns Component instance matching the `id` or `undefined` if there is |
| 37 | + * no such instance. |
| 38 | + */ |
| 39 | + getInstance(id: string): ComponentInstance | undefined; |
| 40 | + |
| 41 | + /** |
| 42 | + * Get all component instances. |
| 43 | + * |
| 44 | + * @returns Current snapshot of instances associated with this definition. |
| 45 | + */ |
| 46 | + getAllInstances(): ComponentInstance[]; |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Component instance, identified (and lookup-able) by its `id` property. |
| 52 | + * |
| 53 | + * Each instance may provide an interface for interaction. For example, |
| 54 | + * a navigational component might expose `addMenuItem` or similar methods. |
| 55 | + * By convention, such interface should be represented by the `interact` |
| 56 | + * property. |
| 57 | + */ |
| 58 | +export interface ComponentInstance { |
| 59 | + |
| 60 | + /** |
| 61 | + * Instance `id` is merely a way to distinguish individual component instances |
| 62 | + * for better lookup - it's not guaranteed to be unique or follow any specific |
| 63 | + * format. |
| 64 | + * |
| 65 | + * If not defined or not a string, its value will be auto-generated. |
| 66 | + */ |
| 67 | + id: string; |
| 68 | + |
| 69 | + /** |
| 70 | + * Interface for component interaction (optional). |
| 71 | + * |
| 72 | + * The value is entirely component specific. |
| 73 | + */ |
| 74 | + interact?: any; |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Blueprint used to create and destroy component instances. |
| 80 | + */ |
| 81 | +export interface ComponentBlueprint { |
| 82 | + |
| 83 | + /** |
| 84 | + * Just like the `ComponentDefinition.create` method, minus the cleanup part. |
| 85 | + * |
| 86 | + * This method *must* return an object that satisfies the `ComponentInstance` |
| 87 | + * interface. Otherwise, the `ComponentDefinition.create` method will throw |
| 88 | + * an error that indicates a problem with the blueprint. |
| 89 | + * |
| 90 | + * Note that the returned instance will be sanitized (e.g. ensure proper `id` |
| 91 | + * value) prior to adding it to the instance collection. |
| 92 | + */ |
| 93 | + create?(props?: any, mountTo?: HTMLElement): ComponentInstance; |
| 94 | + |
| 95 | + /** |
| 96 | + * Destroy `instance` and unmount it from the given DOM element as necessary. |
| 97 | + */ |
| 98 | + destroy?(instance?: ComponentInstance, mountFrom?: HTMLElement): void; |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Callback used to provide the component instance. |
| 104 | + */ |
| 105 | +export type ComponentInstanceCallback = (instance: ComponentInstance) => void; |
| 106 | + |
| 107 | +/** |
| 108 | + * `ManageIQ.component` API. |
| 109 | + */ |
| 110 | +export interface ComponentApi { |
| 111 | + |
| 112 | + /** |
| 113 | + * Define new component. |
| 114 | + * |
| 115 | + * Each component has a unique `name`. Attempts to define new component with |
| 116 | + * an already taken `name` will have no effect. |
| 117 | + * |
| 118 | + * @param name Component name (must be unique). |
| 119 | + * @param blueprint Blueprint used to create and destroy component instances. |
| 120 | + * @param instances Existing instances to associate with this definition. |
| 121 | + * |
| 122 | + * @returns Definition of the new component or `undefined` if the provided |
| 123 | + * `name` is already taken. |
| 124 | + */ |
| 125 | + define(name: string, blueprint: ComponentBlueprint, instances?: ComponentInstance[]): ComponentDefinition | undefined; |
| 126 | + |
| 127 | + /** |
| 128 | + * Get definition of a component, as indicated by its `name`. |
| 129 | + * |
| 130 | + * @param name Component `name` to lookup. |
| 131 | + * |
| 132 | + * @returns Component definition matching the `name` or `undefined` if there |
| 133 | + * is no such definition. |
| 134 | + */ |
| 135 | + getDefinition(name: string): ComponentDefinition | undefined; |
| 136 | + |
| 137 | + /** |
| 138 | + * Get all component definitions. |
| 139 | + * |
| 140 | + * @returns Current snapshot of all component definitions. |
| 141 | + */ |
| 142 | + getAllDefinitions(): ComponentDefinition[]; |
| 143 | + |
| 144 | +} |
0 commit comments