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 curly_block_element; | 5 library curly_block_element; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'package:observatory/src/elements/helpers/tag.dart'; | 9 import 'package:observatory/src/elements/helpers/tag.dart'; |
10 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; | 10 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
11 | 11 |
12 class CurlyBlockToggleEvent { | 12 class CurlyBlockToggleEvent { |
13 final CurlyBlockElement control; | 13 final CurlyBlockElement control; |
14 | 14 |
15 CurlyBlockToggleEvent(this.control); | 15 CurlyBlockToggleEvent(this.control); |
16 } | 16 } |
17 | 17 |
18 class CurlyBlockElement extends HtmlElement implements Renderable { | 18 class CurlyBlockElement extends HtmlElement implements Renderable { |
19 static final StyleElement _style = () { | 19 static const tag = const Tag<CurlyBlockElement>('curly-block'); |
20 var style = new StyleElement(); | |
21 style.text = '''span.curly-block { | |
22 color: #0489c3; | |
23 cursor: pointer; | |
24 } | |
25 span.curly-block.disabled { | |
26 color: white; | |
27 cursor: wait; | |
28 }'''; | |
29 return style; | |
30 }(); | |
31 | |
32 static const tag = const Tag<CurlyBlockElement>('curly-block-wrapped'); | |
33 | 20 |
34 RenderingScheduler<CurlyBlockElement> _r; | 21 RenderingScheduler<CurlyBlockElement> _r; |
35 | 22 |
36 final StreamController<CurlyBlockToggleEvent> _onToggle = | 23 final StreamController<CurlyBlockToggleEvent> _onToggle = |
37 new StreamController<CurlyBlockToggleEvent>.broadcast(); | 24 new StreamController<CurlyBlockToggleEvent>.broadcast(); |
38 Stream<CurlyBlockToggleEvent> get onToggle => _onToggle.stream; | 25 Stream<CurlyBlockToggleEvent> get onToggle => _onToggle.stream; |
39 Stream<RenderedEvent<CurlyBlockElement>> get onRendered => _r.onRendered; | 26 Stream<RenderedEvent<CurlyBlockElement>> get onRendered => _r.onRendered; |
40 | 27 |
41 bool _expanded; | 28 bool _expanded; |
42 bool _disabled; | 29 bool _disabled; |
| 30 Iterable<Element> _content = const []; |
43 | 31 |
44 bool get expanded => _expanded; | 32 bool get expanded => _expanded; |
| 33 bool get disabled => _disabled; |
| 34 Iterable<Element> get content => _content; |
| 35 |
45 set expanded(bool value) { | 36 set expanded(bool value) { |
46 if (_expanded != value) _onToggle.add(new CurlyBlockToggleEvent(this)); | 37 if (_expanded != value) _onToggle.add(new CurlyBlockToggleEvent(this)); |
47 _expanded = _r.checkAndReact(_expanded, value); | 38 _expanded = _r.checkAndReact(_expanded, value); |
48 } | 39 } |
49 bool get disabled => _disabled; | |
50 set disabled(bool value) => _disabled = _r.checkAndReact(_disabled, value); | 40 set disabled(bool value) => _disabled = _r.checkAndReact(_disabled, value); |
| 41 set content(Iterable<Element> value) { |
| 42 _content = value.toList(); |
| 43 _r.dirty(); |
| 44 } |
51 | 45 |
52 factory CurlyBlockElement({bool expanded: false, bool disabled: false, | 46 factory CurlyBlockElement({bool expanded: false, bool disabled: false, |
53 RenderingQueue queue}) { | 47 RenderingQueue queue}) { |
54 assert(expanded != null); | 48 assert(expanded != null); |
55 assert(disabled != null); | 49 assert(disabled != null); |
56 CurlyBlockElement e = document.createElement(tag.name); | 50 CurlyBlockElement e = document.createElement(tag.name); |
57 e._r = new RenderingScheduler(e, queue: queue); | 51 e._r = new RenderingScheduler(e, queue: queue); |
58 e._expanded = expanded; | 52 e._expanded = expanded; |
59 e._disabled = disabled; | 53 e._disabled = disabled; |
| 54 e._r.enable(); |
60 return e; | 55 return e; |
61 } | 56 } |
62 | 57 |
63 CurlyBlockElement.created() : super.created() { createShadowRoot(); } | 58 CurlyBlockElement.created() : super.created(); |
64 | 59 |
65 @override | 60 @override |
66 void attached() { super.attached(); _r.enable(); } | 61 void attached() { super.attached(); _r.enable(); } |
67 | 62 |
68 @override | 63 @override |
69 void detached() { | 64 void detached() { |
70 super.detached(); _r.disable(notify: true); | 65 super.detached(); _r.disable(notify: true); |
71 shadowRoot.children = []; | 66 children = []; |
72 } | 67 } |
73 | 68 |
74 void toggle() { | 69 void toggle() { |
75 if (disabled) { | 70 if (disabled) { |
76 _r.scheduleNotification(); | 71 _r.scheduleNotification(); |
77 return; | 72 return; |
78 } | 73 } |
79 expanded = !expanded; | 74 expanded = !expanded; |
80 } | 75 } |
81 | 76 |
82 void render() { | 77 void render() { |
83 List<Element> children = [ | 78 List<Element> content = [ |
84 _style.clone(true), | |
85 new SpanElement()..text = '{' | 79 new SpanElement()..text = '{' |
86 ]; | 80 ]; |
87 SpanElement label = new SpanElement() | 81 SpanElement label = new SpanElement() |
88 ..classes = disabled ? ['curly-block', 'disabled'] : ['curly-block'] | 82 ..classes = disabled ? ['curly-block', 'disabled'] : ['curly-block'] |
89 ..innerHtml = expanded ? | 83 ..innerHtml = expanded ? |
90 ' ⊟ ' : ' ⊞ '; | 84 ' ⊟ ' : ' ⊞ '; |
91 if (disabled) { | 85 if (disabled) { |
92 children.add(label); | 86 content.add(label); |
93 } else { | 87 } else { |
94 children.add(new AnchorElement() | 88 content.add(new AnchorElement() |
95 ..onClick.listen((_) { toggle(); }) | 89 ..onClick.listen((_) { toggle(); }) |
96 ..children = [label]); | 90 ..children = [label]); |
97 } | 91 } |
98 if (expanded) { | 92 if (expanded) { |
99 children.addAll([ | 93 content.add(new BRElement()); |
100 new BRElement(), | 94 content.addAll(_content); |
101 new ContentElement() | |
102 ]); | |
103 } | 95 } |
104 children.add(new SpanElement()..text = '}'); | 96 content.add(new SpanElement()..text = '}'); |
105 shadowRoot.children = children; | 97 children = content; |
106 } | 98 } |
107 } | 99 } |
OLD | NEW |