diff --git a/lib/bootstrap.dart b/lib/bootstrap.dart index 2757135d0..c4d0bf117 100644 --- a/lib/bootstrap.dart +++ b/lib/bootstrap.dart @@ -61,11 +61,11 @@ Injector _defaultInjectorFactory(List modules) => * Injector injector = ngBootstrap(module: myAppModule); */ Injector ngBootstrap({ - Module module: null, - List modules: null, - dom.Element element: null, - String selector: '[ng-app]', - Injector injectorFactory(List modules): _defaultInjectorFactory}) { + Module module: null, + List modules: null, + dom.Element element: null, + String selector: '[ng-app]', + Injector injectorFactory(List modules): _defaultInjectorFactory}) { _publishToJavaScript(); var ngModules = [new AngularModule()]; @@ -74,7 +74,9 @@ Injector ngBootstrap({ if (element == null) { element = dom.querySelector(selector); var document = dom.window.document; - if (element == null) element = document.childNodes.firstWhere((e) => e is dom.Element); + if (element == null) { + element = document.childNodes.firstWhere((e) => e is dom.Element); + } } // The injector must be created inside the zone, so we create the @@ -87,7 +89,8 @@ Injector ngBootstrap({ return zone.run(() { var rootElements = [element]; Injector injector = injectorFactory(ngModules); - injector.get(Compiler)(rootElements, injector.get(DirectiveMap))(injector, rootElements); + injector.get(Compiler)(rootElements, injector.get(DirectiveMap)) + (injector, rootElements); return injector; }); } diff --git a/lib/core_dom/selector.dart b/lib/core_dom/selector.dart index 14579d9b6..83c69ac66 100644 --- a/lib/core_dom/selector.dart +++ b/lib/core_dom/selector.dart @@ -32,7 +32,7 @@ class _Directive { final Type type; final NgAnnotation annotation; - _Directive(Type this.type, NgAnnotation this.annotation); + _Directive(this.type, this.annotation); toString() => annotation.selector; } @@ -42,13 +42,15 @@ class _ContainsSelector { final NgAnnotation annotation; final RegExp regexp; - _ContainsSelector(this.annotation, String regexp) : regexp = new RegExp(regexp); + _ContainsSelector(this.annotation, String regexp) + : regexp = new RegExp(regexp); } -RegExp _SELECTOR_REGEXP = new RegExp(r'^(?:([\w\-]+)|(?:\.([\w\-]+))|(?:\[([\w\-\*]+)(?:=([^\]]*))?\]))'); -RegExp _COMMENT_COMPONENT_REGEXP = new RegExp(r'^\[([\w\-]+)(?:\=(.*))?\]$'); -RegExp _CONTAINS_REGEXP = new RegExp(r'^:contains\(\/(.+)\/\)$'); // -RegExp _ATTR_CONTAINS_REGEXP = new RegExp(r'^\[\*=\/(.+)\/\]$'); // +var _SELECTOR_REGEXP = new RegExp(r'^(?:([\w\-]+)|(?:\.([\w\-]+))|' + r'(?:\[([\w\-\*]+)(?:=([^\]]*))?\]))'); +var _COMMENT_COMPONENT_REGEXP = new RegExp(r'^\[([\w\-]+)(?:\=(.*))?\]$'); +var _CONTAINS_REGEXP = new RegExp(r'^:contains\(\/(.+)\/\)$'); // +var _ATTR_CONTAINS_REGEXP = new RegExp(r'^\[\*=\/(.+)\/\]$'); // class _SelectorPart { final String element; @@ -56,14 +58,14 @@ class _SelectorPart { final String attrName; final String attrValue; - const _SelectorPart.fromElement(String this.element) + const _SelectorPart.fromElement(this.element) : className = null, attrName = null, attrValue = null; - const _SelectorPart.fromClass(String this.className) + const _SelectorPart.fromClass(this.className) : element = null, attrName = null, attrValue = null; - const _SelectorPart.fromAttribute(String this.attrName, String this.attrValue) + const _SelectorPart.fromAttribute(this.attrName, this.attrValue) : element = null, className = null; toString() => @@ -76,16 +78,16 @@ class _SelectorPart { class _ElementSelector { - String name; + final String name; - Map> elementMap = new Map>(); - Map elementPartialMap = new Map(); + var elementMap = >{}; + var elementPartialMap = {}; - Map> classMap = new Map>(); - Map classPartialMap = new Map(); + var classMap = >{}; + var classPartialMap = {}; - Map>> attrValueMap = new Map>>(); - Map> attrValuePartialMap = new Map>(); + var attrValueMap = >>{}; + var attrValuePartialMap = >{}; _ElementSelector(this.name); @@ -96,8 +98,8 @@ class _ElementSelector { if ((name = selectorPart.element) != null) { if (terminal) { elementMap - .putIfAbsent(name, () => []) - .add(directive); + .putIfAbsent(name, () => []) + .add(directive); } else { elementPartialMap .putIfAbsent(name, () => new _ElementSelector(name)) @@ -106,8 +108,8 @@ class _ElementSelector { } else if ((name = selectorPart.className) != null) { if (terminal) { classMap - .putIfAbsent(name, () => []) - .add(directive); + .putIfAbsent(name, () => []) + .add(directive); } else { classPartialMap .putIfAbsent(name, () => new _ElementSelector(name)) @@ -116,13 +118,14 @@ class _ElementSelector { } else if ((name = selectorPart.attrName) != null) { if (terminal) { attrValueMap - .putIfAbsent(name, () => new Map>()) + .putIfAbsent(name, () => >{}) .putIfAbsent(selectorPart.attrValue, () => []) .add(directive); } else { attrValuePartialMap - .putIfAbsent(name, () => new Map()) - .putIfAbsent(selectorPart.attrValue, () => new _ElementSelector(name)) + .putIfAbsent(name, () => {}) + .putIfAbsent(selectorPart.attrValue, () => + new _ElementSelector(name)) .addDirective(selectorParts, directive); } } else { @@ -130,37 +133,47 @@ class _ElementSelector { } } - _addRefs(List refs, List<_Directive> directives, dom.Node node, [String attrValue]) { + _addRefs(List refs, List<_Directive> directives, dom.Node node, + [String attrValue]) { directives.forEach((directive) => - refs.add(new DirectiveRef(node, directive.type, directive.annotation, attrValue))); + refs.add(new DirectiveRef(node, directive.type, directive.annotation, + attrValue))); } - List<_ElementSelector> selectNode(List refs, List<_ElementSelector> partialSelection, - dom.Node node, String nodeName) { + List<_ElementSelector> selectNode(List refs, + List<_ElementSelector> partialSelection, + dom.Node node, String nodeName) { if (elementMap.containsKey(nodeName)) { _addRefs(refs, elementMap[nodeName], node); } if (elementPartialMap.containsKey(nodeName)) { - if (partialSelection == null) partialSelection = new List<_ElementSelector>(); + if (partialSelection == null) { + partialSelection = new List<_ElementSelector>(); + } partialSelection.add(elementPartialMap[nodeName]); } return partialSelection; } - List<_ElementSelector> selectClass(List refs, List<_ElementSelector> partialSelection, - dom.Node node, String className) { + List<_ElementSelector> selectClass(List refs, + List<_ElementSelector> partialSelection, + dom.Node node, String className) { if (classMap.containsKey(className)) { _addRefs(refs, classMap[className], node); } if (classPartialMap.containsKey(className)) { - if (partialSelection == null) partialSelection = new List<_ElementSelector>(); + if (partialSelection == null) { + partialSelection = new List<_ElementSelector>(); + } partialSelection.add(classPartialMap[className]); } return partialSelection; } - List<_ElementSelector> selectAttr(List refs, List<_ElementSelector> partialSelection, - dom.Node node, String attrName, String attrValue) { + List<_ElementSelector> selectAttr(List refs, + List<_ElementSelector> partialSelection, + dom.Node node, String attrName, + String attrValue) { String matchingKey = _matchingKey(attrValueMap.keys, attrName); @@ -174,30 +187,34 @@ class _ElementSelector { } } if (attrValuePartialMap.containsKey(attrName)) { - Map valuesPartialMap = attrValuePartialMap[attrName]; + Map valuesPartialMap = + attrValuePartialMap[attrName]; if (valuesPartialMap.containsKey('')) { - if (partialSelection == null) partialSelection = new List<_ElementSelector>(); + if (partialSelection == null) { + partialSelection = new List<_ElementSelector>(); + } partialSelection.add(valuesPartialMap['']); } if (attrValue != '' && valuesPartialMap.containsKey(attrValue)) { - if (partialSelection == null) partialSelection = new List<_ElementSelector>(); + if (partialSelection == null) { + partialSelection = new List<_ElementSelector>(); + } partialSelection.add(valuesPartialMap[attrValue]); } } return partialSelection; } - String _matchingKey(Iterable keys, String attrName) { - return keys.firstWhere( - (key) => new RegExp('^${key.replaceAll('*', r'[\w\-]+')}\$').hasMatch(attrName), - orElse: () => null); - } + String _matchingKey(Iterable keys, String attrName) => + keys.firstWhere((key) => + new RegExp('^${key.replaceAll('*', r'[\w\-]+')}\$') + .hasMatch(attrName), orElse: () => null); toString() => 'ElementSelector($name)'; } List<_SelectorPart> _splitCss(String selector, Type type) { - List<_SelectorPart> parts = []; + var parts = <_SelectorPart>[]; var remainder = selector; var match; while (!remainder.isEmpty) { @@ -226,9 +243,9 @@ List<_SelectorPart> _splitCss(String selector, Type type) { */ DirectiveSelector directiveSelectorFactory(DirectiveMap directives) { - _ElementSelector elementSelector = new _ElementSelector(''); - List<_ContainsSelector> attrSelector = []; - List<_ContainsSelector> textSelector = []; + var elementSelector = new _ElementSelector(''); + var attrSelector = <_ContainsSelector>[]; + var textSelector = <_ContainsSelector>[]; directives.forEach((NgAnnotation annotation, Type type) { var match; var selector = annotation.selector; @@ -242,17 +259,18 @@ DirectiveSelector directiveSelectorFactory(DirectiveMap directives) { } else if ((match = _ATTR_CONTAINS_REGEXP.firstMatch(selector)) != null) { attrSelector.add(new _ContainsSelector(annotation, match[1])); } else if ((selectorParts = _splitCss(selector, type)) != null){ - elementSelector.addDirective(selectorParts, new _Directive(type, annotation)); + elementSelector.addDirective(selectorParts, + new _Directive(type, annotation)); } else { throw new ArgumentError('Unsupported Selector: $selector'); } }); return (dom.Node node) { - List directiveRefs = []; - List<_ElementSelector> partialSelection = null; - Map classes = new Map(); - Map attrs = new Map(); + var directiveRefs = []; + List<_ElementSelector> partialSelection; + var classes = {}; + var attrs = {}; switch(node.nodeType) { case 1: // Element @@ -266,25 +284,27 @@ DirectiveSelector directiveSelectorFactory(DirectiveMap directives) { } // Select node - partialSelection = elementSelector.selectNode(directiveRefs, partialSelection, element, nodeName); + partialSelection = elementSelector.selectNode(directiveRefs, + partialSelection, element, nodeName); // Select .name if ((element.classes) != null) { for(var name in element.classes) { classes[name] = true; - partialSelection = elementSelector.selectClass(directiveRefs, partialSelection, element, name); + partialSelection = elementSelector.selectClass(directiveRefs, + partialSelection, element, name); } } // Select [attributes] - element.attributes.forEach((attrName, value){ + element.attributes.forEach((attrName, value) { attrs[attrName] = value; - for(var k = 0, kk = attrSelector.length; k < kk; k++) { + for(var k = 0; k < attrSelector.length; k++) { _ContainsSelector selectorRegExp = attrSelector[k]; if (selectorRegExp.regexp.hasMatch(value)) { // this directive is matched on any attribute name, and so - // we need to pass the name to the directive by prefixing it to the - // value. Yes it is a bit of a hack. + // we need to pass the name to the directive by prefixing it to + // the value. Yes it is a bit of a hack. directives[selectorRegExp.annotation].forEach((type) { directiveRefs.add(new DirectiveRef( node, type, selectorRegExp.annotation, '$attrName=$value')); @@ -292,7 +312,8 @@ DirectiveSelector directiveSelectorFactory(DirectiveMap directives) { } } - partialSelection = elementSelector.selectAttr(directiveRefs, partialSelection, node, attrName, value); + partialSelection = elementSelector.selectAttr(directiveRefs, + partialSelection, node, attrName, value); }); while(partialSelection != null) { @@ -300,21 +321,25 @@ DirectiveSelector directiveSelectorFactory(DirectiveMap directives) { partialSelection = null; elementSelectors.forEach((_ElementSelector elementSelector) { classes.forEach((className, _) { - partialSelection = elementSelector.selectClass(directiveRefs, partialSelection, node, className); + partialSelection = elementSelector.selectClass(directiveRefs, + partialSelection, node, className); }); attrs.forEach((attrName, value) { - partialSelection = elementSelector.selectAttr(directiveRefs, partialSelection, node, attrName, value); + partialSelection = elementSelector.selectAttr(directiveRefs, + partialSelection, node, attrName, value); }); }); } break; case 3: // Text Node - for(var value = node.nodeValue, k = 0, kk = textSelector.length; k < kk; k++) { - var selectorRegExp = textSelector[k]; + var value = node.nodeValue; + for(var k = 0; k < textSelector.length; k++) { + var selectorRegExp = textSelector[k]; if (selectorRegExp.regexp.hasMatch(value)) { directives[selectorRegExp.annotation].forEach((type) { - directiveRefs.add(new DirectiveRef(node, type, selectorRegExp.annotation, value)); + directiveRefs.add(new DirectiveRef(node, type, + selectorRegExp.annotation, value)); }); } } diff --git a/lib/introspection.dart b/lib/introspection.dart index 04a20246f..e2d27235a 100644 --- a/lib/introspection.dart +++ b/lib/introspection.dart @@ -4,7 +4,7 @@ part of angular; * A global write only variable which keeps track of objects attached to the elements. * This is usefull for debugging AngularDart application from the browser's REPL. */ -Expando _elementExpando = new Expando('element'); +var _elementExpando = new Expando('element'); /** * Return the closest [ElementProbe] object for a given [Element]. @@ -74,11 +74,12 @@ List ngDirectives(dom.Node node) { } _publishToJavaScript() { - js.context['ngProbe'] = (dom.Node node) => _jsProbe(ngProbe(node)); - js.context['ngInjector'] = (dom.Node node) => _jsInjector(ngInjector(node)); - js.context['ngScope'] = (dom.Node node) => _jsScope(ngScope(node)); - js.context['ngQuery'] = (dom.Node node, String selector, [String containsText]) => - new js.JsArray.from(ngQuery(node, selector, containsText)); + js.context + ..['ngProbe'] = (dom.Node node) => _jsProbe(ngProbe(node)) + ..['ngInjector'] = (dom.Node node) => _jsInjector(ngInjector(node)) + ..['ngScope'] = (dom.Node node) => _jsScope(ngScope(node)) + ..['ngQuery'] = (dom.Node node, String selector, [String containsText]) => + new js.JsArray.from(ngQuery(node, selector, containsText)); } js.JsObject _jsProbe(ElementProbe probe) { @@ -90,11 +91,8 @@ js.JsObject _jsProbe(ElementProbe probe) { })..['_dart_'] = probe; } -js.JsObject _jsInjector(Injector injector) { - return new js.JsObject.jsify({ - "get": injector.get - })..['_dart_'] = injector; -} +js.JsObject _jsInjector(Injector injector) => + new js.JsObject.jsify({ "get": injector.get})..['_dart_'] = injector; js.JsObject _jsScope(Scope scope) { return new js.JsObject.jsify({ diff --git a/lib/playback/playback_data.dart b/lib/playback/playback_data.dart index 5f25f9ce4..f37678abf 100644 --- a/lib/playback/playback_data.dart +++ b/lib/playback/playback_data.dart @@ -3,4 +3,4 @@ library angular.playback.playback_data; // During HTTP playback, this file will be replaced with a file // that has playback data. -Map playbackData = { }; +var playbackData = {}; diff --git a/lib/playback/playback_http.dart b/lib/playback/playback_http.dart index df57b95a2..f6253a3d9 100644 --- a/lib/playback/playback_http.dart +++ b/lib/playback/playback_http.dart @@ -13,9 +13,9 @@ import 'package:angular/playback/playback_data.dart' as playback_data; @NgInjectableService() class PlaybackHttpBackendConfig { requestKey(String url, - {String method, bool withCredentials, String responseType, - String mimeType, Map requestHeaders, sendData, - void onProgress(ProgressEvent e)}) { + {String method, bool withCredentials, String responseType, + String mimeType, Map requestHeaders, sendData, + void onProgress(ProgressEvent e)}) { return JSON.encode({ "url": url, "method": method, @@ -29,7 +29,7 @@ class PlaybackHttpBackendConfig { // the HttpBackend, but it will be implemented by ourselves. class HttpBackendWrapper { HttpBackend backend; - HttpBackendWrapper(HttpBackend this.backend); + HttpBackendWrapper(this.backend); } class RecordingHttpBackend implements HttpBackend { @@ -37,9 +37,8 @@ class RecordingHttpBackend implements HttpBackend { HttpBackend _prodBackend; PlaybackHttpBackendConfig _config; - RecordingHttpBackend(HttpBackendWrapper wrapper, this._config) { - this._prodBackend = wrapper.backend; - } + RecordingHttpBackend(HttpBackendWrapper wrapper, this._config) + : _prodBackend = wrapper.backend; Future request(String url, {String method, bool withCredentials, String responseType, @@ -97,9 +96,7 @@ class PlaybackHttpBackend implements HttpBackend { sendData: sendData, onProgress: onProgress); - if (!data.containsKey(key)) { - throw ["Request is not recorded $key"]; - } + if (!data.containsKey(key)) throw ["Request is not recorded $key"]; var playback = data[key]; return new Future.value( new mock.MockHttpRequest( diff --git a/lib/routing/ng_bind_route.dart b/lib/routing/ng_bind_route.dart index 8ffdef3d4..7305401cf 100644 --- a/lib/routing/ng_bind_route.dart +++ b/lib/routing/ng_bind_route.dart @@ -28,8 +28,7 @@ part of angular.routing; selector: '[ng-bind-route]', map: const { 'ng-bind-route': '@routeName' - } -) + }) class NgBindRouteDirective implements RouteProvider { Router _router; String routeName; @@ -41,13 +40,9 @@ class NgBindRouteDirective implements RouteProvider { /// Returns the parent [RouteProvider]. RouteProvider get _parent => _injector.parent.get(RouteProvider); - Route get route { - if (routeName.startsWith('.')) { - return _parent.route.getRoute(routeName.substring(1)); - } else { - return _router.root.getRoute(routeName); - } - } + Route get route => routeName.startsWith('.') ? + _parent.route.getRoute(routeName.substring(1)) : + _router.root.getRoute(routeName); Map get parameters { var res = {}; diff --git a/lib/routing/ng_view.dart b/lib/routing/ng_view.dart index fb8da9988..8b4db4b3a 100644 --- a/lib/routing/ng_view.dart +++ b/lib/routing/ng_view.dart @@ -59,8 +59,7 @@ part of angular.routing; @NgDirective( selector: 'ng-view', publishTypes: const [RouteProvider], - visibility: NgDirective.CHILDREN_VISIBILITY -) + visibility: NgDirective.CHILDREN_VISIBILITY) class NgViewDirective implements NgDetachAware, RouteProvider { final NgRoutingHelper locationService; final BlockCache blockCache; @@ -78,19 +77,15 @@ class NgViewDirective implements NgDetachAware, RouteProvider { Router router, this.directives) : injector = injector, locationService = injector.get(NgRoutingHelper) { RouteProvider routeProvider = injector.parent.get(NgViewDirective); - if (routeProvider != null) { - _route = routeProvider.route.newHandle(); - } else { - _route = router.root.newHandle(); - } + _route = routeProvider != null ? + routeProvider.route.newHandle() : + router.root.newHandle(); locationService._registerPortal(this); _maybeReloadViews(); } void _maybeReloadViews() { - if (_route.isActive) { - locationService._reloadViews(startingFrom: _route); - } + if (_route.isActive) locationService._reloadViews(startingFrom: _route); } detach() { @@ -115,17 +110,15 @@ class NgViewDirective implements NgDetachAware, RouteProvider { blockCache.fromUrl(templateUrl, directives).then((blockFactory) { _cleanUp(); _previousScope = scope.$new(); - _previousBlock = blockFactory( - injector.createChild([new Module()..value(Scope, _previousScope)])); + _previousBlock = blockFactory(injector.createChild( + [new Module()..value(Scope, _previousScope)])); _previousBlock.elements.forEach((elm) => element.append(elm)); }); } _cleanUp() { - if (_previousBlock == null) { - return; - } + if (_previousBlock == null) return; _previousBlock.remove(); _previousScope.$destroy(); diff --git a/lib/routing/routing.dart b/lib/routing/routing.dart index 098cb2e13..60fbf2757 100644 --- a/lib/routing/routing.dart +++ b/lib/routing/routing.dart @@ -32,8 +32,8 @@ abstract class RouteInitializer { class NgRoutingHelper { final Router router; final NgApp _ngApp; - List portals = []; - Map _templates = new Map(); + var portals = []; + var _templates = {}; NgRoutingHelper(RouteInitializer initializer, this.router, this._ngApp) { if (initializer == null) {