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 M.NotificationRepository _repository; |
| 23 StreamSubscription _subscription; |
| 24 |
| 25 bool _notifyOnPause; |
| 26 bool get notifyOnPause => _notifyOnPause; |
| 27 set notifyOnPause(bool value) => |
| 28 _notifyOnPause = _r.checkAndReact(_notifyOnPause, value); |
| 29 |
| 30 factory NavNotifyElement(M.NotificationRepository repository, |
| 31 {bool notifyOnPause: true, RenderingQueue queue}) { |
| 32 assert(repository != null); |
| 33 assert(notifyOnPause != null); |
| 34 NavNotifyElement e = document.createElement(tag.name); |
| 35 e._r = new RenderingScheduler(e, queue: queue); |
| 36 e._repository = repository; |
| 37 e._notifyOnPause = notifyOnPause; |
| 38 return e; |
| 39 } |
| 40 |
| 41 NavNotifyElement.created() : super.created(); |
| 42 |
| 43 @override |
| 44 void attached() { |
| 45 super.attached(); |
| 46 _r.enable(); |
| 47 _subscription = _repository.onChange.listen((_) => _r.dirty()); |
| 48 } |
| 49 |
| 50 @override |
| 51 void detached() { |
| 52 super.detached(); |
| 53 children = []; |
| 54 _r.disable(notify: true); |
| 55 _subscription.cancel(); |
| 56 } |
| 57 |
| 58 void render() { |
| 59 children = [ |
| 60 new DivElement() |
| 61 ..children = [ |
| 62 new DivElement() |
| 63 ..children = _repository.list() |
| 64 .where(_filter).map(_toElement).toList() |
| 65 ] |
| 66 ]; |
| 67 } |
| 68 |
| 69 bool _filter(M.Notification notification) { |
| 70 if (!_notifyOnPause && notification is M.EventNotification) { |
| 71 return !M.Event.isPauseEvent(notification.event); |
| 72 } |
| 73 return true; |
| 74 } |
| 75 |
| 76 HtmlElement _toElement(M.Notification notification) { |
| 77 if (notification is M.EventNotification) { |
| 78 return new NavNotifyEventElement(notification.event, queue: _r.queue) |
| 79 ..onDelete.listen((_) => _repository.delete(notification)); |
| 80 } else if (notification is M.ExceptionNotification) { |
| 81 return new NavNotifyExceptionElement( |
| 82 notification.exception, stacktrace: notification.stacktrace, |
| 83 queue: _r.queue) |
| 84 ..onDelete.listen((_) => _repository.delete(notification)); |
| 85 } else { |
| 86 assert(false); |
| 87 return new DivElement()..text = 'Invalid Notification Type'; |
| 88 } |
| 89 } |
| 90 } |
OLD | NEW |