OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library flag_list_element; | 5 library flag_list_element; |
6 | 6 |
| 7 import 'dart:html'; |
7 import 'dart:async'; | 8 import 'dart:async'; |
8 import 'package:polymer/polymer.dart'; | 9 import 'package:observatory/models.dart' as M; |
9 import 'observatory_element.dart'; | 10 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
10 import 'package:observatory/service.dart'; | 11 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 12 import 'package:observatory/src/elements/helpers/uris.dart'; |
| 13 import 'package:observatory/src/elements/nav/bar.dart'; |
| 14 import 'package:observatory/src/elements/nav/menu.dart'; |
| 15 import 'package:observatory/src/elements/nav/notify.dart'; |
| 16 import 'package:observatory/src/elements/nav/refresh.dart'; |
| 17 import 'package:observatory/src/elements/nav/top_menu.dart'; |
| 18 import 'package:observatory/src/elements/nav/vm_menu.dart'; |
| 19 import 'package:observatory/src/elements/view_footer.dart'; |
11 | 20 |
12 @CustomTag('flag-list') | 21 class FlagListElement extends HtmlElement implements Renderable { |
13 class FlagListElement extends ObservatoryElement { | 22 static const tag = const Tag<FlagListElement>('flag-list', |
14 @published ServiceMap flagList; | 23 dependencies: const [NavBarElement.tag, |
| 24 NavMenuElement.tag, |
| 25 NavNotifyElement.tag, |
| 26 NavRefreshElement.tag, |
| 27 NavTopMenuElement.tag, |
| 28 NavVMMenuElement.tag, |
| 29 ViewFooterElement.tag,]); |
15 | 30 |
16 void flagListChanged(oldValue) { | 31 RenderingScheduler _r; |
17 modifiedFlags = | 32 |
18 flagList['flags'].where((flag) => flag['modified']).toList(); | 33 Stream<RenderedEvent<FlagListElement>> get onRendered => _r.onRendered; |
19 unmodifiedFlags = | 34 |
20 flagList['flags'].where((flag) => !flag['modified']).toList(); | 35 M.VMRef _vm; |
| 36 Stream<M.VMUpdateEvent> _vmUpdates; |
| 37 M.FlagsRepository _repository; |
| 38 M.NotificationRepository _notifications; |
| 39 Iterable<M.Flag> _flags; |
| 40 |
| 41 M.VMRef get vm => _vm; |
| 42 |
| 43 factory FlagListElement(M.VMRef vm, |
| 44 Stream<M.VMUpdateEvent> vmUpdates, |
| 45 M.FlagsRepository repository, |
| 46 M.NotificationRepository notifications, |
| 47 {RenderingQueue queue}) { |
| 48 assert(vm != null); |
| 49 assert(vmUpdates != null); |
| 50 assert(repository != null); |
| 51 assert(notifications != null); |
| 52 FlagListElement e = document.createElement(tag.name); |
| 53 e._r = new RenderingScheduler(e, queue: queue); |
| 54 e._vm = vm; |
| 55 e._vmUpdates = vmUpdates; |
| 56 e._repository = repository; |
| 57 e._notifications = notifications; |
| 58 return e; |
21 } | 59 } |
22 | 60 |
23 @observable List<ServiceMap> modifiedFlags = new List<ServiceMap>(); | |
24 @observable List<ServiceMap> unmodifiedFlags = new List<ServiceMap>(); | |
25 | |
26 FlagListElement.created() : super.created(); | 61 FlagListElement.created() : super.created(); |
27 | 62 |
28 Future refresh() { | 63 @override |
29 return flagList.reload(); | 64 void attached() { |
| 65 super.attached(); |
| 66 _r.enable(); |
| 67 _refresh(); |
| 68 } |
| 69 |
| 70 @override |
| 71 void detached() { |
| 72 super.detached(); |
| 73 children = []; |
| 74 _r.disable(notify: true); |
| 75 } |
| 76 |
| 77 void render() { |
| 78 final content = <Element>[]; |
| 79 if (_flags == null) { |
| 80 content.add(new HeadingElement.h1()..text = 'Loading Flags...'); |
| 81 } else { |
| 82 final modified = _flags.where(_isModified); |
| 83 final unmodified = _flags.where(_isUnmodified); |
| 84 |
| 85 if (modified.isNotEmpty) { |
| 86 content.add(new HeadingElement.h1()..text = 'Modified Flags'); |
| 87 content.add(new BRElement()); |
| 88 content.addAll(modified.expand(_renderFlag)); |
| 89 content.add(new HRElement()); |
| 90 } |
| 91 |
| 92 content.add(new HeadingElement.h1()..text = 'Unmodified Flags'); |
| 93 content.add(new BRElement()); |
| 94 |
| 95 if (unmodified.isEmpty) { |
| 96 content.add(new HeadingElement.h2()..text = 'None'); |
| 97 } else { |
| 98 content.addAll(unmodified.expand(_renderFlag)); |
| 99 } |
| 100 } |
| 101 |
| 102 children = [ |
| 103 new NavBarElement(queue: _r.queue) |
| 104 ..children = [ |
| 105 new NavTopMenuElement(queue: _r.queue), |
| 106 new NavVMMenuElement(_vm, _vmUpdates, queue: _r.queue), |
| 107 new NavMenuElement('flags', link: Uris.flags(), last: true, |
| 108 queue: _r.queue), |
| 109 new NavRefreshElement(queue: _r.queue) |
| 110 ..onRefresh.listen((e) async { |
| 111 e.element.disabled = true; |
| 112 try { |
| 113 await _refresh(); |
| 114 } finally { |
| 115 e.element.disabled = false; |
| 116 } |
| 117 }), |
| 118 new NavNotifyElement(_notifications, queue: _r.queue) |
| 119 ], |
| 120 new DivElement() |
| 121 ..classes = ['content-centered'] |
| 122 ..children = content, |
| 123 new ViewFooterElement(queue: _r.queue) |
| 124 ]; |
| 125 } |
| 126 |
| 127 Future _refresh() async { |
| 128 _flags = await _repository.list(_vm); |
| 129 _r.dirty(); |
| 130 } |
| 131 |
| 132 static bool _isModified(M.Flag flag) => flag.modified; |
| 133 static bool _isUnmodified(M.Flag flag) => !flag.modified; |
| 134 |
| 135 static List<Element> _renderFlag(M.Flag flag) { |
| 136 return [ |
| 137 new SpanElement()..classes = const ['comment'] |
| 138 ..text = '// ${flag.comment}', |
| 139 new DivElement()..classes = flag.modified ? const ['flag', 'modified'] |
| 140 : const ['flag', 'unmodified'] |
| 141 ..children = [ |
| 142 new SpanElement()..classes = const ['name'] |
| 143 ..text = flag.name, |
| 144 new SpanElement()..text = '=', |
| 145 new SpanElement()..classes = const ['value'] |
| 146 ..text = flag.valueAsString ?? 'NULL' |
| 147 ], |
| 148 new BRElement(), |
| 149 ]; |
30 } | 150 } |
31 } | 151 } |
32 | |
33 @CustomTag('flag-item') | |
34 class FlagItemElement extends ObservatoryElement { | |
35 @published ObservableMap flag; | |
36 | |
37 FlagItemElement.created() : super.created(); | |
38 } | |
OLD | NEW |