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AC;IAED,mBAAmB;QACf,KAAK,MAAM,SAAS,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,EAAE;YACvD,SAAS,CAAC,aAAa,EAAE,CAAC;SAC7B;QAED,IAAI,CAAC,kBAAkB,GAAG,EAAE,CAAC;IACjC,CAAC;CACJ;AA5ED,gDA4EC","sourcesContent":["import * as _ from 'lodash';\n\nimport { Application } from '../application';\nimport { EventDispatcher, Event, EventMap } from './events';\nimport { DeclarationOption } from './options/declaration';\n\n/**\n * Exposes a reference to the root Application component.\n */\nexport interface ComponentHost {\n readonly application: Application;\n}\n\nexport interface Component extends AbstractComponent<ComponentHost> {\n\n}\n\nexport interface ComponentClass<T extends Component, O extends ComponentHost = ComponentHost> extends Function {\n new(owner: O): T;\n}\n\n/**\n * Option-bag passed to Component decorator.\n */\nexport interface ComponentOptions {\n name?: string;\n /** Specify valid child component class. Used to prove that children are valid via `instanceof` checks */\n childClass?: Function;\n internal?: boolean;\n}\n\nconst childMappings: {host: ChildableComponent<any, any>, child: Function}[] = [];\n\n/**\n * Class decorator applied to Components\n */\nexport function Component(options: ComponentOptions): ClassDecorator {\n return (target: Function) => {\n const proto = target.prototype;\n if (!(proto instanceof AbstractComponent)) {\n throw new Error('The `Component` decorator can only be used with a subclass of `AbstractComponent`.');\n }\n\n if (options.childClass) {\n if (!(proto instanceof ChildableComponent)) {\n throw new Error('The `Component` decorator accepts the parameter `childClass` only when used with a subclass of `ChildableComponent`.');\n }\n\n childMappings.push({\n host: proto,\n child: options.childClass\n });\n }\n\n const name = options.name;\n if (name) {\n proto.componentName = name;\n }\n\n const internal = !!options.internal;\n if (name && !internal) {\n for (const childMapping of childMappings) {\n if (!(proto instanceof childMapping.child)) {\n continue;\n }\n\n const host = childMapping.host;\n host['_defaultComponents'] = host['_defaultComponents'] || {};\n host['_defaultComponents'][name] = target as any;\n break;\n }\n }\n };\n}\n\n/**\n * Decorator that declares a configuration option.\n *\n * Use it on an instance property of a Component class.\n */\nexport function Option(options: DeclarationOption): PropertyDecorator {\n return function(target: object, propertyKey: string | symbol) {\n if (!(target instanceof AbstractComponent)) {\n throw new Error('The `Option` decorator can only be used on properties within an `AbstractComponent` subclass.');\n }\n\n options.component = target['_componentName'];\n target['_componentOptions'] = target['_componentOptions'] || [];\n target['_componentOptions'].push(options);\n\n Object.defineProperty(target, propertyKey, {\n get: function (this: AbstractComponent<ComponentHost>) {\n return this.application.options.getValue(options.name);\n },\n enumerable: true,\n configurable: true\n });\n };\n}\n\nexport class ComponentEvent extends Event {\n owner: ComponentHost;\n\n component: AbstractComponent<ComponentHost>;\n\n static ADDED = 'componentAdded';\n\n static REMOVED = 'componentRemoved';\n\n constructor(name: string, owner: ComponentHost, component: AbstractComponent<ComponentHost>) {\n super(name);\n this.owner = owner;\n this.component = component;\n }\n}\n\n/**\n * Dummy owner to be passed in to AbstractComponent / ChildableComponents if the class being constructed is\n * the application. The application does not have an owner and will return itself for component.application\n * and component.owner.\n */\nexport const DUMMY_APPLICATION_OWNER = Symbol();\n\n/**\n * Component base class. Has an owner (unless it's the application root component),\n * can dispatch events to its children, and has access to the root Application component.\n *\n * @template O type of component's owner.\n */\nexport abstract class AbstractComponent<O extends ComponentHost> extends EventDispatcher implements ComponentHost {\n /**\n * The owner of this component instance.\n */\n private _componentOwner: O | typeof DUMMY_APPLICATION_OWNER;\n\n /**\n * The name of this component as set by the @Component decorator.\n */\n public componentName!: string;\n\n /**\n * A list of options defined by this component.\n */\n private _componentOptions?: DeclarationOption[];\n\n /**\n * Create new Component instance.\n */\n constructor(owner: O | typeof DUMMY_APPLICATION_OWNER) {\n super();\n this._componentOwner = owner;\n this.initialize();\n }\n\n /**\n * Initialize this component.\n */\n protected initialize() {}\n\n protected bubble(name: Event|EventMap|string, ...args: any[]) {\n super.trigger(name, ...args);\n\n if (this.owner instanceof AbstractComponent && this._componentOwner !== DUMMY_APPLICATION_OWNER) {\n this.owner.bubble(name, ...args);\n }\n\n return this;\n }\n\n /**\n * Return all option declarations emitted by this component.\n */\n getOptionDeclarations(): DeclarationOption[] {\n return (this._componentOptions || []).slice();\n }\n\n /**\n * Return the application / root component instance.\n */\n get application(): Application {\n return this._componentOwner === DUMMY_APPLICATION_OWNER\n ? this as any as Application\n : this._componentOwner.application;\n }\n\n /**\n * Return the owner of this component.\n */\n get owner(): O {\n return this._componentOwner === DUMMY_APPLICATION_OWNER\n ? this as any\n : this._componentOwner;\n }\n}\n\n/**\n * Component that can have child components.\n *\n * @template O type of Component's owner\n * @template C type of Component's children\n */\nexport abstract class ChildableComponent<O extends ComponentHost, C extends Component> extends AbstractComponent<O> {\n /**\n *\n */\n private _componentChildren?: {[name: string]: C};\n\n private _defaultComponents?: {[name: string]: ComponentClass<C>};\n\n /**\n * Create new Component instance.\n */\n constructor(owner: O | typeof DUMMY_APPLICATION_OWNER) {\n super(owner);\n\n _.entries(this._defaultComponents || {}).forEach(([name, component]) => {\n this.addComponent(name, component);\n });\n }\n\n /**\n * Retrieve a plugin instance.\n *\n * @returns The instance of the plugin or undefined if no plugin with the given class is attached.\n */\n getComponent(name: string): C | undefined {\n return (this._componentChildren || {})[name];\n }\n\n getComponents(): C[] {\n return _.values(this._componentChildren);\n }\n\n hasComponent(name: string): boolean {\n return !!(this._componentChildren || {})[name];\n }\n\n addComponent<T extends C>(name: string, componentClass: T|ComponentClass<T, O>): T {\n if (!this._componentChildren) {\n this._componentChildren = {};\n }\n\n if (this._componentChildren[name]) {\n // Component already added so we will return the existing component\n // TODO: add better logging around this because it could be unexpected but shouldn't be fatal\n // See https://github.com/TypeStrong/typedoc/issues/846\n return <T> this._componentChildren[name];\n } else {\n const component: T = typeof componentClass === 'function'\n ? new (<ComponentClass<T>> componentClass)(this)\n : componentClass;\n const event = new ComponentEvent(ComponentEvent.ADDED, this, component);\n\n this.bubble(event);\n this._componentChildren[name] = component;\n\n return component;\n }\n }\n\n removeComponent(name: string): C | undefined {\n const component = (this._componentChildren || {})[name];\n if (component) {\n delete this._componentChildren![name];\n component.stopListening();\n this.bubble(new ComponentEvent(ComponentEvent.REMOVED, this, component));\n return component;\n }\n }\n\n removeAllComponents() {\n for (const component of _.values(this._componentChildren)) {\n component.stopListening();\n }\n\n this._componentChildren = {};\n }\n}\n"]}
\No newline at end of file