|
1 | | -import {ListWrapper, MapWrapper, Map, StringMapWrapper, List} from 'angular2/src/facade/collection'; |
| 1 | +import { |
| 2 | + ListWrapper, |
| 3 | + MapWrapper, |
| 4 | + Map, |
| 5 | + StringMapWrapper, |
| 6 | + List, |
| 7 | + StringMap |
| 8 | +} from 'angular2/src/facade/collection'; |
2 | 9 | import { |
3 | 10 | AST, |
4 | 11 | Locals, |
@@ -202,7 +209,36 @@ export class AppView implements ChangeDispatcher, RenderEventDispatcher { |
202 | 209 |
|
203 | 210 | getHostElement(): ElementRef { |
204 | 211 | var boundElementIndex = this.mainMergeMapping.hostElementIndicesByViewIndex[this.viewOffset]; |
205 | | - return this.elementRefs[boundElementIndex]; |
| 212 | + return isPresent(boundElementIndex) ? this.elementRefs[boundElementIndex] : null; |
| 213 | + } |
| 214 | + |
| 215 | + getDebugContext(elementIndex: number, directiveIndex: DirectiveIndex): StringMap<string, any> { |
| 216 | + try { |
| 217 | + var offsettedIndex = this.elementOffset + elementIndex; |
| 218 | + var hasRefForIndex = offsettedIndex < this.elementRefs.length; |
| 219 | + |
| 220 | + var elementRef = hasRefForIndex ? this.elementRefs[this.elementOffset + elementIndex] : null; |
| 221 | + var host = this.getHostElement(); |
| 222 | + var ei = hasRefForIndex ? this.elementInjectors[this.elementOffset + elementIndex] : null; |
| 223 | + |
| 224 | + var element = isPresent(elementRef) ? elementRef.nativeElement : null; |
| 225 | + var componentElement = isPresent(host) ? host.nativeElement : null; |
| 226 | + var directive = isPresent(directiveIndex) ? this.getDirectiveFor(directiveIndex) : null; |
| 227 | + var injector = isPresent(ei) ? ei.getInjector() : null; |
| 228 | + |
| 229 | + return { |
| 230 | + element: element, |
| 231 | + componentElement: componentElement, |
| 232 | + directive: directive, |
| 233 | + context: this.context, |
| 234 | + locals: _localsToStringMap(this.locals), |
| 235 | + injector: injector |
| 236 | + }; |
| 237 | + } catch (e) { |
| 238 | + // TODO: vsavkin log the exception once we have a good way to log errors and warnings |
| 239 | + // if an error happens during getting the debug context, we return an empty map. |
| 240 | + return {}; |
| 241 | + } |
206 | 242 | } |
207 | 243 |
|
208 | 244 | getDetectorFor(directive: DirectiveIndex): any { |
@@ -252,6 +288,16 @@ export class AppView implements ChangeDispatcher, RenderEventDispatcher { |
252 | 288 | } |
253 | 289 | } |
254 | 290 |
|
| 291 | +function _localsToStringMap(locals: Locals): StringMap<string, any> { |
| 292 | + var res = {}; |
| 293 | + var c = locals; |
| 294 | + while (isPresent(c)) { |
| 295 | + res = StringMapWrapper.merge(res, MapWrapper.toStringMap(c.current)); |
| 296 | + c = c.parent; |
| 297 | + } |
| 298 | + return res; |
| 299 | +} |
| 300 | + |
255 | 301 | /** |
256 | 302 | * |
257 | 303 | */ |
|
0 commit comments