OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:html'; |
| 6 import 'dart:async'; |
| 7 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 8 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 9 import 'package:observatory/src/elements/helpers/uris.dart'; |
| 10 import 'package:observatory/models.dart' show ConnectionException; |
| 11 |
| 12 |
| 13 class ExceptionDeleteEvent{ |
| 14 final Exception exception; |
| 15 final StackTrace stacktrace; |
| 16 |
| 17 ExceptionDeleteEvent(this.exception, {this.stacktrace}); |
| 18 } |
| 19 |
| 20 class NavNotifyExceptionElement extends HtmlElement implements Renderable { |
| 21 static const tag = const Tag<NavNotifyExceptionElement>('nav-exception'); |
| 22 |
| 23 RenderingScheduler _r; |
| 24 |
| 25 Stream<RenderedEvent<NavNotifyExceptionElement>> get onRendered => |
| 26 _r.onRendered; |
| 27 |
| 28 final StreamController<ExceptionDeleteEvent> _onDelete = |
| 29 new StreamController<ExceptionDeleteEvent>.broadcast(); |
| 30 Stream<ExceptionDeleteEvent> get onDelete => _onDelete.stream; |
| 31 |
| 32 Exception _exception; |
| 33 StackTrace _stacktrace; |
| 34 Exception get exception => _exception; |
| 35 StackTrace get stacktrace => _stacktrace; |
| 36 |
| 37 factory NavNotifyExceptionElement(Exception exception, |
| 38 {StackTrace stacktrace: null, RenderingQueue queue}) { |
| 39 assert(exception != null); |
| 40 NavNotifyExceptionElement e = document.createElement(tag.name); |
| 41 e._r = new RenderingScheduler(e, queue: queue); |
| 42 e._exception = exception; |
| 43 e._stacktrace = stacktrace; |
| 44 return e; |
| 45 } |
| 46 |
| 47 NavNotifyExceptionElement.created() : super.created(); |
| 48 |
| 49 @override |
| 50 void attached() { |
| 51 super.attached(); |
| 52 _r.enable(); |
| 53 } |
| 54 |
| 55 @override |
| 56 void detached() { |
| 57 super.detached(); |
| 58 children = []; |
| 59 _r.disable(notify: true); |
| 60 } |
| 61 |
| 62 void render() { |
| 63 if (exception is ConnectionException) { |
| 64 renderConnectionException(); |
| 65 } else { |
| 66 renderGenericException(); |
| 67 } |
| 68 } |
| 69 |
| 70 void renderConnectionException() { |
| 71 children = [ |
| 72 new DivElement() |
| 73 ..children = [ |
| 74 new SpanElement()..text = 'The request cannot be completed because the ' |
| 75 'VM is currently disconnected', |
| 76 new BRElement(), new BRElement(), |
| 77 new SpanElement()..text = '[', |
| 78 new AnchorElement(href: Uris.vmConnect()) |
| 79 ..text = 'Connect to a different VM', |
| 80 new SpanElement()..text = ']', |
| 81 new ButtonElement()..innerHtml = '×' |
| 82 ..onClick.map(_toEvent).listen(_delete) |
| 83 ] |
| 84 ]; |
| 85 } |
| 86 |
| 87 void renderGenericException() { |
| 88 List<Node> content; |
| 89 content = [ |
| 90 new SpanElement()..text = 'Unexpected exception:', |
| 91 new BRElement(), new BRElement(), |
| 92 new DivElement()..text = exception.toString(), |
| 93 new BRElement() |
| 94 ]; |
| 95 if (stacktrace != null) { |
| 96 content.addAll([ |
| 97 new SpanElement()..text = 'Stacktrace:', |
| 98 new BRElement(), new BRElement(), |
| 99 new DivElement()..text = stacktrace.toString(), |
| 100 new BRElement() |
| 101 ]); |
| 102 } |
| 103 content.addAll([ |
| 104 new SpanElement()..text = '[', |
| 105 new AnchorElement(href: Uris.vmConnect()) |
| 106 ..text = 'Connect to a different VM', |
| 107 new SpanElement()..text = ']', |
| 108 new ButtonElement()..innerHtml = '×' |
| 109 ..onClick.map(_toEvent).listen(_delete) |
| 110 ]); |
| 111 children = [ |
| 112 new DivElement() |
| 113 ..children = content |
| 114 ]; |
| 115 } |
| 116 |
| 117 ExceptionDeleteEvent _toEvent(_) { |
| 118 return new ExceptionDeleteEvent(exception, stacktrace: stacktrace); |
| 119 } |
| 120 |
| 121 void _delete(ExceptionDeleteEvent e) { |
| 122 _onDelete.add(e); |
| 123 } |
| 124 |
| 125 void delete() { |
| 126 _onDelete.add(new ExceptionDeleteEvent(exception, stacktrace: stacktrace)); |
| 127 } |
| 128 } |
OLD | NEW |