refactor(core): ts’ify core

This commit is contained in:
Tobias Bosch
2015-05-20 09:48:15 -07:00
parent aabc898f3b
commit e61d82b9be
56 changed files with 2128 additions and 1922 deletions

View File

@ -18,12 +18,15 @@ import {CompileControl} from './compile_control';
import {DirectiveMetadata} from '../../api';
import {dashCaseToCamelCase, camelCaseToDashCase, EVENT_TARGET_SEPARATOR} from '../util';
import {
DirectiveBuilder
} from '../view/proto_view_builder'
/**
* Parses the directives on a single element. Assumes ViewSplitter has already created
* <template> elements for template directives.
*/
export class DirectiveParser implements CompileStep {
/**
* Parses the directives on a single element. Assumes ViewSplitter has already created
* <template> elements for template directives.
*/
export class DirectiveParser implements CompileStep {
_selectorMatcher: SelectorMatcher;
_directives: List<DirectiveMetadata>;
_parser: Parser;
@ -118,7 +121,8 @@ export class DirectiveParser implements CompileStep {
});
}
_bindDirectiveProperty(dirProperty, bindConfig, compileElement, directiveBinderBuilder) {
_bindDirectiveProperty(dirProperty: string, bindConfig: string, compileElement: CompileElement,
directiveBinderBuilder: DirectiveBuilder) {
var pipes = this._splitBindConfig(bindConfig);
var elProp = ListWrapper.removeAt(pipes, 0);

View File

@ -140,12 +140,12 @@ export class SelectorMatcher {
return notMatcher;
}
private _elementMap: Map<string, string>;
private _elementPartialMap: Map<string, string>;
private _classMap: Map<string, string>;
private _classPartialMap: Map<string, string>;
private _attrValueMap: Map<string, string>;
private _attrValuePartialMap: Map<string, string>;
private _elementMap: Map<string, List<string>>;
private _elementPartialMap: Map<string, SelectorMatcher>;
private _classMap: Map<string, List<string>>;
private _classPartialMap: Map<string, SelectorMatcher>;
private _attrValueMap: Map<string, Map<string, List<string>>>;
private _attrValuePartialMap: Map<string, Map<string, SelectorMatcher>>;
private _listContexts: List<SelectorListContext>;
constructor() {
@ -212,22 +212,28 @@ export class SelectorMatcher {
var isTerminal = index === attrs.length - 2;
var attrName = attrs[index++];
var attrValue = attrs[index++];
var map = isTerminal ? matcher._attrValueMap : matcher._attrValuePartialMap;
var valuesMap = MapWrapper.get(map, attrName);
if (isBlank(valuesMap)) {
valuesMap = MapWrapper.create();
MapWrapper.set(map, attrName, valuesMap);
}
if (isTerminal) {
this._addTerminal(valuesMap, attrValue, selectable);
var terminalMap = matcher._attrValueMap;
var terminalValuesMap = MapWrapper.get(terminalMap, attrName);
if (isBlank(terminalValuesMap)) {
terminalValuesMap = MapWrapper.create();
MapWrapper.set(terminalMap, attrName, terminalValuesMap);
}
this._addTerminal(terminalValuesMap, attrValue, selectable);
} else {
matcher = this._addPartial(valuesMap, attrValue);
var parttialMap = matcher._attrValuePartialMap;
var partialValuesMap = MapWrapper.get(parttialMap, attrName);
if (isBlank(partialValuesMap)) {
partialValuesMap = MapWrapper.create();
MapWrapper.set(parttialMap, attrName, partialValuesMap);
}
matcher = this._addPartial(partialValuesMap, attrValue);
}
}
}
}
private _addTerminal(map: Map<string, string>, name: string, selectable: SelectorContext) {
private _addTerminal(map: Map<string, List<string>>, name: string, selectable: SelectorContext) {
var terminalList = MapWrapper.get(map, name);
if (isBlank(terminalList)) {
terminalList = ListWrapper.create();
@ -236,7 +242,7 @@ export class SelectorMatcher {
ListWrapper.push(terminalList, selectable);
}
private _addPartial(map: Map<string, string>, name: string) {
private _addPartial(map: Map<string, SelectorMatcher>, name: string): SelectorMatcher {
var matcher = MapWrapper.get(map, name);
if (isBlank(matcher)) {
matcher = new SelectorMatcher();
@ -282,22 +288,24 @@ export class SelectorMatcher {
var attrName = attrs[index++];
var attrValue = attrs[index++];
var valuesMap = MapWrapper.get(this._attrValueMap, attrName);
var terminalValuesMap = MapWrapper.get(this._attrValueMap, attrName);
if (!StringWrapper.equals(attrValue, _EMPTY_ATTR_VALUE)) {
result =
this._matchTerminal(valuesMap, _EMPTY_ATTR_VALUE, cssSelector, matchedCallback) ||
result;
result = this._matchTerminal(terminalValuesMap, _EMPTY_ATTR_VALUE, cssSelector,
matchedCallback) ||
result;
}
result = this._matchTerminal(valuesMap, attrValue, cssSelector, matchedCallback) || result;
result = this._matchTerminal(terminalValuesMap, attrValue, cssSelector, matchedCallback) ||
result;
valuesMap = MapWrapper.get(this._attrValuePartialMap, attrName);
result = this._matchPartial(valuesMap, attrValue, cssSelector, matchedCallback) || result;
var partialValuesMap = MapWrapper.get(this._attrValuePartialMap, attrName);
result =
this._matchPartial(partialValuesMap, attrValue, cssSelector, matchedCallback) || result;
}
}
return result;
}
_matchTerminal(map: Map<string, string>, name, cssSelector: CssSelector,
_matchTerminal(map: Map<string, List<string>>, name, cssSelector: CssSelector,
matchedCallback /*: (CssSelector, any) => void*/): boolean {
if (isBlank(map) || isBlank(name)) {
return false;
@ -320,7 +328,7 @@ export class SelectorMatcher {
return result;
}
_matchPartial(map: Map<string, string>, name, cssSelector: CssSelector,
_matchPartial(map: Map<string, SelectorMatcher>, name, cssSelector: CssSelector,
matchedCallback /*: (CssSelector, any) => void*/): boolean {
if (isBlank(map) || isBlank(name)) {
return false;

View File

@ -11,7 +11,7 @@ import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {EventManagerPlugin} from './event_manager';
var modifierKeys = ['alt', 'control', 'meta', 'shift'];
var modifierKeyGetters =
var modifierKeyGetters: StringMap<string, Function> =
{
'alt': (event) => event.altKey,
'control': (event) => event.ctrlKey,

View File

@ -40,14 +40,15 @@ export class EmulatedScopedShadowDomStrategy extends EmulatedUnscopedShadowDomSt
cssText = this.styleUrlResolver.resolveUrls(cssText, templateUrl);
var inlinedCss = this.styleInliner.inlineImports(cssText, templateUrl);
if (isPresent(inlinedCss.asyncResult)) {
if (PromiseWrapper.isPromise(inlinedCss)) {
DOM.setText(styleEl, '');
return inlinedCss.asyncResult.then((css) => {
css = shimCssForComponent(css, hostComponentId);
DOM.setText(styleEl, css);
});
return (<Promise<string>>inlinedCss)
.then((css) => {
css = shimCssForComponent(css, hostComponentId);
DOM.setText(styleEl, css);
});
} else {
var css = shimCssForComponent(inlinedCss.syncResult, hostComponentId);
var css = shimCssForComponent(<string>inlinedCss, hostComponentId);
DOM.setText(styleEl, css);
DOM.remove(styleEl);
insertStyleElement(this.styleHost, styleEl);

View File

@ -18,10 +18,6 @@ import {
PromiseWrapper,
} from 'angular2/src/facade/async';
export class SyncAsyncResult<T> {
constructor(public syncResult: T, public asyncResult: Promise<T>) {}
}
/**
* Inline @import rules in the given CSS.
*
@ -48,18 +44,18 @@ export class StyleInliner {
* @param {string} baseUrl
* @returns {*} a Promise<string> when @import rules are present, a string otherwise
*/
inlineImports(cssText: string, baseUrl: string): SyncAsyncResult<string> {
inlineImports(cssText: string, baseUrl: string): Promise<string>| string {
return this._inlineImports(cssText, baseUrl, []);
}
_inlineImports(cssText: string, baseUrl: string,
inlinedUrls: List<string>): SyncAsyncResult<string> {
_inlineImports(cssText: string, baseUrl: string, inlinedUrls: List<string>): Promise<string>|
string {
var partIndex = 0;
var parts = StringWrapper.split(cssText, _importRe);
if (parts.length === 1) {
// no @import rule found, return the original css
return new SyncAsyncResult(cssText, null);
return cssText;
}
var promises = [];
@ -87,14 +83,14 @@ export class StyleInliner {
promise = PromiseWrapper.then(this._xhr.get(url), (rawCss) => {
// resolve nested @import rules
var inlinedCss = this._inlineImports(rawCss, url, inlinedUrls);
if (isPresent(inlinedCss.asyncResult)) {
if (PromiseWrapper.isPromise(inlinedCss)) {
// wait until nested @import are inlined
return inlinedCss.asyncResult.then(
(css) => {return prefix + this._transformImportedCss(css, mediaQuery, url) + '\n'});
return (<Promise<string>>inlinedCss)
.then((css) => {return prefix + this._transformImportedCss(css, mediaQuery, url) +
'\n'});
} else {
// there are no nested @import, return the css
return prefix + this._transformImportedCss(inlinedCss.syncResult, mediaQuery, url) +
'\n';
return prefix + this._transformImportedCss(<string>inlinedCss, mediaQuery, url) + '\n';
}
}, (error) => `/* failed to import ${url} */\n`);
}
@ -102,14 +98,14 @@ export class StyleInliner {
partIndex += 2;
}
return new SyncAsyncResult(null, PromiseWrapper.all(promises).then(function(cssParts) {
return PromiseWrapper.all(promises).then(function(cssParts) {
var cssText = cssParts.join('');
if (partIndex < parts.length) {
// append then content located after the last @import rule
cssText += parts[partIndex];
}
return cssText;
}));
});
}
_transformImportedCss(css: string, mediaQuery: string, url: string): string {