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/models.dart' as M; |
| 8 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 9 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 10 import 'package:observatory/src/elements/nav/notify_event.dart'; |
| 11 import 'package:observatory/src/elements/nav/notify_exception.dart'; |
| 12 |
| 13 class NavNotifyElement extends HtmlElement implements Renderable { |
| 14 static const tag = const Tag<NavNotifyElement>('nav-notify-wrapped', |
| 15 dependencies: const [ NavNotifyEventElement.tag, |
| 16 NavNotifyExceptionElement.tag ]); |
| 17 |
| 18 RenderingScheduler _r; |
| 19 |
| 20 Stream<RenderedEvent<NavNotifyElement>> get onRendered => _r.onRendered; |
| 21 |
| 22 bool _notifyOnPause; |
| 23 M.NotificationRepository _repository; |
| 24 StreamSubscription _subscription; |
| 25 |
| 26 factory NavNotifyElement(M.NotificationRepository repository, |
| 27 {bool notifyOnPause: true, RenderingQueue queue}) { |
| 28 assert(repository != null); |
| 29 assert(notifyOnPause != null); |
| 30 NavNotifyElement e = document.createElement(tag.name); |
| 31 e._r = new RenderingScheduler(e, queue: queue); |
| 32 e._repository = repository; |
| 33 e._notifyOnPause = notifyOnPause; |
| 34 return e; |
| 35 } |
| 36 |
| 37 NavNotifyElement.created() : super.created(); |
| 38 |
| 39 @override |
| 40 void attached() { |
| 41 super.attached(); |
| 42 _r.enable(); |
| 43 _subscription = _repository.onChange.listen((_) => _r.dirty()); |
| 44 } |
| 45 |
| 46 @override |
| 47 void detached() { |
| 48 super.detached(); children = []; _r.disable(); |
| 49 _subscription.cancel(); |
| 50 } |
| 51 |
| 52 void render() { |
| 53 children = [ |
| 54 new DivElement() |
| 55 ..children = [ |
| 56 new DivElement() |
| 57 ..children = _repository.list().where(_filter).map(_toElement) |
| 58 ] |
| 59 ]; |
| 60 } |
| 61 |
| 62 bool _filter(M.Notification notification) { |
| 63 if (!_notifyOnPause && notification is M.EventNotification) { |
| 64 return !M.Event.isPauseEvent(notification.event); |
| 65 } |
| 66 return true; |
| 67 } |
| 68 |
| 69 HtmlElement _toElement(M.Notification notification) { |
| 70 if (notification is M.EventNotification) { |
| 71 return new NavNotifyEventElement(notification.event, queue: _r.queue) |
| 72 ..onRemove.listen((_) => _repository.delete(notification)); |
| 73 } else if (notification is M.ExceptionNotification) { |
| 74 return new NavNotifyExceptionElement( |
| 75 notification.exception, stacktrace: notification.stacktrace, |
| 76 queue: _r.queue) |
| 77 ..onRemove.listen((_) => _repository.delete(notification)); |
| 78 } else { |
| 79 assert(false); |
| 80 return new DivElement()..text = 'Invalid Notification Type'; |
| 81 } |
| 82 } |
| 83 } |
OLD | NEW |