OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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/models.dart' as M |
| 8 show IsolateRef, IsolateRepository, EventRepository; |
| 9 import 'package:observatory/src/elements/helpers/nav_menu.dart'; |
| 10 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 11 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 12 |
| 13 class ReloadEvent { |
| 14 final NavReloadElement element; |
| 15 ReloadEvent(this.element); |
| 16 } |
| 17 |
| 18 class NavReloadElement extends HtmlElement implements Renderable { |
| 19 static const tag = const Tag<NavReloadElement>('nav-reload'); |
| 20 |
| 21 RenderingScheduler _r; |
| 22 |
| 23 Stream<RenderedEvent<NavReloadElement>> get onRendered => _r.onRendered; |
| 24 |
| 25 final StreamController<ReloadEvent> _onReload = |
| 26 new StreamController<ReloadEvent>.broadcast(); |
| 27 Stream<ReloadEvent> get onReload => _onReload.stream; |
| 28 |
| 29 M.IsolateRef _isolate; |
| 30 M.IsolateRepository _isolates; |
| 31 M.EventRepository _events; |
| 32 StreamSubscription _sub; |
| 33 bool _disabled = false; |
| 34 |
| 35 factory NavReloadElement(M.IsolateRef isolate, M.IsolateRepository isolates, |
| 36 M.EventRepository events, |
| 37 {RenderingQueue queue}) { |
| 38 assert(isolate == null); |
| 39 assert(isolates == null); |
| 40 assert(events == null); |
| 41 NavReloadElement e = document.createElement(tag.name); |
| 42 e._r = new RenderingScheduler(e, queue: queue); |
| 43 e._isolate = isolate; |
| 44 e._isolates = isolates; |
| 45 e._events = events; |
| 46 return e; |
| 47 } |
| 48 |
| 49 NavReloadElement.created() : super.created(); |
| 50 |
| 51 @override |
| 52 void attached() { |
| 53 super.attached(); |
| 54 _r.enable(); |
| 55 _sub = _events.onServiceEvent.listen((_) => _r.dirty()); |
| 56 } |
| 57 |
| 58 @override |
| 59 void detached() { |
| 60 super.detached(); |
| 61 children = []; |
| 62 _sub.cancel(); |
| 63 _sub = null; |
| 64 _r.disable(notify: true); |
| 65 } |
| 66 |
| 67 void render() { |
| 68 final children = []; |
| 69 if (_isolates.reloadSourcesServices.isEmpty) { |
| 70 children.add(new LIElement() |
| 71 ..children = [ |
| 72 new ButtonElement() |
| 73 ..text = 'Reload Source' |
| 74 ..disabled = _disabled |
| 75 ..onClick.listen((_) => _reload()) |
| 76 ]); |
| 77 } else if (_isolates.reloadSourcesServices.length == 1) { |
| 78 children.add(new LIElement() |
| 79 ..children = [ |
| 80 new ButtonElement() |
| 81 ..text = 'Reload Source' |
| 82 ..disabled = _disabled |
| 83 ..onClick |
| 84 .listen((_) => _reload(_isolates.reloadSourcesServices.single)) |
| 85 ]); |
| 86 } else { |
| 87 final content = _isolates.reloadSourcesServices.map((s) => new LIElement() |
| 88 ..children = [ |
| 89 new ButtonElement() |
| 90 ..text = s.alias |
| 91 ..disabled = _disabled |
| 92 ..onClick.listen((_) => _reload(s)) |
| 93 ]); |
| 94 children.add(navMenu('Reload Source', content: content)); |
| 95 } |
| 96 this.children = children; |
| 97 } |
| 98 |
| 99 Future _reload([service]) async { |
| 100 _disabled = true; |
| 101 _r.dirty(); |
| 102 await _isolates.reloadSources(_isolate, service: service); |
| 103 _disabled = false; |
| 104 _r.dirty(); |
| 105 _onReload.add(new ReloadEvent(this)); |
| 106 } |
| 107 } |
OLD | NEW |