OLD | NEW |
1 part of angular; | 1 /** |
| 2 * Introspection of Elements for debugging and tests. |
| 3 */ |
| 4 library angular.introspection; |
2 | 5 |
3 /** | 6 import 'dart:html' as dom; |
4 * A global write only variable which keeps track of objects attached to the | 7 import 'package:di/di.dart'; |
5 * elements. This is useful for debugging AngularDart application from the | 8 import 'package:angular/introspection_js.dart'; |
6 * browser's REPL. | 9 import 'package:angular/core/module_internal.dart'; |
7 */ | 10 import 'package:angular/core_dom/module_internal.dart'; |
8 var _elementExpando = new Expando('element'); | |
9 | 11 |
10 /** | 12 /** |
11 * Return the closest [ElementProbe] object for a given [Element]. | 13 * Return the closest [ElementProbe] object for a given [Element]. |
12 * | 14 * |
13 * NOTE: This global method is here to make it easier to debug Angular | 15 * **NOTE:** This global method is here to make it easier to debug Angular |
14 * application from the browser's REPL, unit or end-to-end tests. The | 16 * application from the browser's REPL, unit or end-to-end tests. The |
15 * function is not intended to be called from Angular application. | 17 * function is not intended to be called from Angular application. |
16 */ | 18 */ |
17 ElementProbe ngProbe(dom.Node node) { | 19 ElementProbe ngProbe(dom.Node node) { |
18 while(node != null) { | 20 if (node == null) { |
19 var probe = _elementExpando[node]; | 21 throw "ngProbe called without node"; |
| 22 } |
| 23 var origNode = node; |
| 24 while (node != null) { |
| 25 var probe = elementExpando[node]; |
20 if (probe != null) return probe; | 26 if (probe != null) return probe; |
21 node = node.parent; | 27 node = node.parent; |
22 } | 28 } |
23 return null; | 29 throw "Could not find a probe for [$origNode]"; |
24 } | 30 } |
25 | 31 |
26 | 32 |
27 /** | 33 /** |
28 * Return the [Injector] associated with a current [Element]. | 34 * Return the [Injector] associated with a current [Element]. |
29 * | 35 * |
30 * **NOTE**: This global method is here to make it easier to debug Angular | 36 * **NOTE**: This global method is here to make it easier to debug Angular |
31 * application from the browser's REPL, unit or end-to-end tests. The function | 37 * application from the browser's REPL, unit or end-to-end tests. The function |
32 * is not intended to be called from Angular application. | 38 * is not intended to be called from Angular application. |
33 */ | 39 */ |
(...skipping 30 matching lines...) Expand all Loading... |
64 } | 70 } |
65 | 71 |
66 /** | 72 /** |
67 * Return a List of directive controllers associated with a current [Element]. | 73 * Return a List of directive controllers associated with a current [Element]. |
68 * | 74 * |
69 * **NOTE**: This global method is here to make it easier to debug Angular | 75 * **NOTE**: This global method is here to make it easier to debug Angular |
70 * application from the browser's REPL, unit or end-to-end tests. The function | 76 * application from the browser's REPL, unit or end-to-end tests. The function |
71 * is not intended to be called from Angular application. | 77 * is not intended to be called from Angular application. |
72 */ | 78 */ |
73 List<Object> ngDirectives(dom.Node node) { | 79 List<Object> ngDirectives(dom.Node node) { |
74 ElementProbe probe = _elementExpando[node]; | 80 ElementProbe probe = elementExpando[node]; |
75 return probe == null ? [] : probe.directives; | 81 return probe == null ? [] : probe.directives; |
76 } | 82 } |
77 | 83 |
78 _publishToJavaScript() { | |
79 js.context | |
80 ..['ngProbe'] = (dom.Node node) => _jsProbe(ngProbe(node)) | |
81 ..['ngInjector'] = (dom.Node node) => _jsInjector(ngInjector(node)) | |
82 ..['ngScope'] = (dom.Node node) => _jsScope(ngScope(node)) | |
83 ..['ngQuery'] = (dom.Node node, String selector, [String containsText]) => | |
84 new js.JsArray.from(ngQuery(node, selector, containsText)); | |
85 } | |
86 | |
87 js.JsObject _jsProbe(ElementProbe probe) { | |
88 return new js.JsObject.jsify({ | |
89 "element": probe.element, | |
90 "injector": _jsInjector(probe.injector), | |
91 "scope": _jsScope(probe.scope), | |
92 "directives": probe.directives.map((directive) => _jsDirective(directive)) | |
93 })..['_dart_'] = probe; | |
94 } | |
95 | |
96 js.JsObject _jsInjector(Injector injector) => | |
97 new js.JsObject.jsify({"get": injector.get})..['_dart_'] = injector; | |
98 | |
99 js.JsObject _jsScope(Scope scope) { | |
100 return new js.JsObject.jsify({ | |
101 "apply": scope.apply, | |
102 "digest": scope.rootScope.digest, | |
103 "flush": scope.rootScope.flush, | |
104 "context": scope.context, | |
105 "get": (name) => scope.context[name], | |
106 "set": (name, value) => scope.context[name] = value | |
107 })..['_dart_'] = scope; | |
108 } | |
109 | |
110 _jsDirective(directive) => directive; | |
OLD | NEW |