2016-06-23 12:47:54 -04: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
|
|
|
|
*/
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {DirectiveResolver} from '@angular/compiler';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {DirectiveMetadata, Type} from '@angular/core';
|
2015-08-06 16:19:29 -04:00
|
|
|
|
|
|
|
var COMPONENT_SELECTOR = /^[\w|-]*$/;
|
|
|
|
var SKEWER_CASE = /-(\w)/g;
|
|
|
|
var directiveResolver = new DirectiveResolver();
|
|
|
|
|
2015-10-01 16:14:59 -04:00
|
|
|
export interface AttrProp {
|
|
|
|
prop: string;
|
|
|
|
attr: string;
|
|
|
|
bracketAttr: string;
|
|
|
|
bracketParenAttr: string;
|
|
|
|
parenAttr: string;
|
|
|
|
onAttr: string;
|
|
|
|
bindAttr: string;
|
|
|
|
bindonAttr: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ComponentInfo {
|
2015-10-05 19:02:21 -04:00
|
|
|
type: Type;
|
2015-10-01 16:14:59 -04:00
|
|
|
selector: string;
|
|
|
|
inputs: AttrProp[];
|
|
|
|
outputs: AttrProp[];
|
|
|
|
}
|
|
|
|
|
2015-10-05 19:02:21 -04:00
|
|
|
export function getComponentInfo(type: Type): ComponentInfo {
|
2015-08-06 16:19:29 -04:00
|
|
|
var resolvedMetadata: DirectiveMetadata = directiveResolver.resolve(type);
|
|
|
|
var selector = resolvedMetadata.selector;
|
|
|
|
if (!selector.match(COMPONENT_SELECTOR)) {
|
|
|
|
throw new Error('Only selectors matching element names are supported, got: ' + selector);
|
|
|
|
}
|
2016-06-08 19:38:52 -04:00
|
|
|
var selector = selector.replace(
|
|
|
|
SKEWER_CASE, (all: any /** TODO #9100 */, letter: string) => letter.toUpperCase());
|
2015-10-01 16:14:59 -04:00
|
|
|
return {
|
|
|
|
type: type,
|
|
|
|
selector: selector,
|
|
|
|
inputs: parseFields(resolvedMetadata.inputs),
|
|
|
|
outputs: parseFields(resolvedMetadata.outputs)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseFields(names: string[]): AttrProp[] {
|
|
|
|
var attrProps: AttrProp[] = [];
|
|
|
|
if (names) {
|
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var parts = names[i].split(':');
|
|
|
|
var prop = parts[0].trim();
|
|
|
|
var attr = (parts[1] || parts[0]).trim();
|
|
|
|
var capitalAttr = attr.charAt(0).toUpperCase() + attr.substr(1);
|
|
|
|
attrProps.push(<AttrProp>{
|
|
|
|
prop: prop,
|
|
|
|
attr: attr,
|
|
|
|
bracketAttr: `[${attr}]`,
|
|
|
|
parenAttr: `(${attr})`,
|
2015-10-05 19:02:21 -04:00
|
|
|
bracketParenAttr: `[(${attr})]`,
|
2015-10-01 16:14:59 -04:00
|
|
|
onAttr: `on${capitalAttr}`,
|
|
|
|
bindAttr: `bind${capitalAttr}`,
|
|
|
|
bindonAttr: `bindon${capitalAttr}`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attrProps;
|
2015-08-06 16:19:29 -04:00
|
|
|
}
|