2017-12-20 10:47:22 -08:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {RendererType2} from '../render/api';
|
|
|
|
import {Type} from '../type';
|
|
|
|
import {resolveRendererType2} from '../view/util';
|
|
|
|
|
2018-01-17 09:45:40 -08:00
|
|
|
import {diPublic} from './di';
|
|
|
|
import {componentRefresh} from './instructions';
|
2018-01-09 18:38:17 -08:00
|
|
|
import {ComponentDef, ComponentDefArgs, DirectiveDef, DirectiveDefArgs} from './interfaces/definition';
|
2017-12-20 10:47:22 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a component definition object.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
* ```
|
|
|
|
* class MyDirective {
|
|
|
|
* // Generated by Angular Template Compiler
|
|
|
|
* // [Symbol] syntax will not be supported by TypeScript until v2.7
|
|
|
|
* static [COMPONENT_DEF_SYMBOL] = defineComponent({
|
|
|
|
* ...
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function defineComponent<T>(componentDefinition: ComponentDefArgs<T>): ComponentDef<T> {
|
|
|
|
const def = <ComponentDef<any>>{
|
|
|
|
diPublic: null,
|
|
|
|
n: componentDefinition.factory,
|
|
|
|
tag: (componentDefinition as ComponentDefArgs<T>).tag || null !,
|
|
|
|
template: (componentDefinition as ComponentDefArgs<T>).template || null !,
|
2018-01-18 16:27:43 -08:00
|
|
|
r: componentDefinition.refresh || (componentDefinition.template ?
|
|
|
|
function(d: number, e: number) {
|
|
|
|
componentRefresh(d, e, componentDefinition.template);
|
|
|
|
} :
|
|
|
|
noop),
|
2017-12-20 16:26:07 -08:00
|
|
|
h: componentDefinition.hostBindings || noop,
|
2017-12-20 10:47:22 -08:00
|
|
|
inputs: invertObject(componentDefinition.inputs),
|
|
|
|
outputs: invertObject(componentDefinition.outputs),
|
|
|
|
methods: invertObject(componentDefinition.methods),
|
|
|
|
rendererType: resolveRendererType2(componentDefinition.rendererType) || null,
|
2018-01-08 21:57:50 -08:00
|
|
|
exportAs: componentDefinition.exportAs,
|
2017-12-20 10:47:22 -08:00
|
|
|
};
|
|
|
|
const feature = componentDefinition.features;
|
|
|
|
feature && feature.forEach((fn) => fn(def));
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function NgOnChangesFeature<T>(definition: DirectiveDef<T>) {
|
|
|
|
// TODO: implement. See: https://app.asana.com/0/443577627818617/465170715764659
|
|
|
|
}
|
|
|
|
|
|
|
|
export function PublicFeature<T>(definition: DirectiveDef<T>) {
|
|
|
|
definition.diPublic = diPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EMPTY = {};
|
|
|
|
|
2017-12-20 16:26:07 -08:00
|
|
|
function noop() {}
|
|
|
|
|
2017-12-20 10:47:22 -08:00
|
|
|
/** Swaps the keys and values of an object. */
|
|
|
|
function invertObject(obj: any): any {
|
|
|
|
if (obj == null) return EMPTY;
|
|
|
|
const newObj: any = {};
|
|
|
|
for (let minifiedKey in obj) {
|
|
|
|
newObj[obj[minifiedKey]] = minifiedKey;
|
|
|
|
}
|
|
|
|
return newObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a directive definition object.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
* ```
|
|
|
|
* class MyDirective {
|
|
|
|
* // Generated by Angular Template Compiler
|
|
|
|
* // [Symbol] syntax will not be supported by TypeScript until v2.7
|
|
|
|
* static [DIRECTIVE_DEF_SYMBOL] = defineDirective({
|
|
|
|
* ...
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export const defineDirective = defineComponent as<T>(directiveDefinition: DirectiveDefArgs<T>) =>
|
|
|
|
DirectiveDef<T>;
|