Chromium Code Reviews| 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:async'; | |
| 6 import 'dart:html'; | |
| 7 import 'package:observatory/src/elements/containers/virtual_collection.dart'; | |
| 8 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; | |
| 9 import 'package:observatory/src/elements/helpers/tag.dart'; | |
| 10 | |
| 11 typedef HtmlElement CreateElementCallback(toggle({bool forward})); | |
|
Cutch
2016/08/01 23:05:54
s/forward/autoExpandChilNodesWithOnlyOneChild
cbernaschina
2016/08/01 23:17:34
Done.
| |
| 12 typedef void UpdateElementCallback(HtmlElement el, dynamic item, int depth); | |
| 13 typedef Iterable<dynamic> GetChildrenCallback(dynamic value); | |
| 14 | |
| 15 class VirtualTreeElement extends HtmlElement implements Renderable { | |
| 16 static const tag = | |
| 17 const Tag<VirtualTreeElement>('virtual-tree', dependencies: const [ | |
| 18 VirtualCollectionElement.tag | |
| 19 ]); | |
| 20 | |
| 21 RenderingScheduler<VirtualTreeElement> _r; | |
| 22 | |
| 23 Stream<RenderedEvent<VirtualTreeElement>> get onRendered => _r.onRendered; | |
| 24 | |
| 25 GetChildrenCallback _children; | |
| 26 List _items; | |
| 27 List _depths; | |
| 28 final Set _expanded = new Set(); | |
| 29 | |
| 30 List get items => _items; | |
| 31 | |
| 32 set items(Iterable value) { | |
| 33 _items = new List.unmodifiable(value); | |
| 34 _expanded.clear(); | |
| 35 _r.dirty(); | |
| 36 } | |
| 37 | |
| 38 factory VirtualTreeElement(CreateElementCallback create, | |
| 39 UpdateElementCallback update, GetChildrenCallback children, | |
| 40 {Iterable items: const [], RenderingQueue queue}) { | |
| 41 assert(create != null); | |
| 42 assert(update != null); | |
| 43 assert(children != null); | |
| 44 assert(items != null); | |
| 45 VirtualTreeElement e = document.createElement(tag.name); | |
| 46 e._r = new RenderingScheduler(e, queue: queue); | |
| 47 e._children = children; | |
| 48 e._collection = new VirtualCollectionElement(() { | |
| 49 var element; | |
| 50 return element = create(({bool forward: false}) { | |
| 51 var item = e._collection.getItemFromElement(element); | |
| 52 if (e.isExpanded(item)) { | |
| 53 e.collapse(item, forward: forward); | |
| 54 } else { | |
| 55 e.expand(item, forward: forward); | |
| 56 } | |
| 57 }); | |
| 58 }, (HtmlElement el, dynamic item, int index) { | |
| 59 update(el, item, e._depths[index]); | |
| 60 }, queue: queue); | |
| 61 e._items = new List.unmodifiable(items); | |
| 62 return e; | |
| 63 } | |
| 64 | |
| 65 VirtualTreeElement.created() : super.created(); | |
| 66 | |
| 67 bool isExpanded(item) { | |
| 68 return _expanded.contains(item); | |
| 69 } | |
| 70 | |
| 71 void expand(item, {bool forward: false}) { | |
| 72 if (_expanded.add(item)) _r.dirty(); | |
| 73 if (forward) { | |
| 74 var children = _children(item); | |
| 75 while (children.length == 1) { | |
| 76 _expanded.add(children.first); | |
| 77 children = _children(children.first); | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void collapse(item, {bool forward: false}) { | |
| 83 if (_expanded.remove(item)) _r.dirty(); | |
| 84 if (forward) { | |
| 85 var children = _children(item); | |
| 86 while (children.length == 1) { | |
| 87 _expanded.remove(children.first); | |
| 88 children = _children(children.first); | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 @override | |
| 94 attached() { | |
| 95 super.attached(); | |
| 96 _r.enable(); | |
| 97 } | |
| 98 | |
| 99 @override | |
| 100 detached() { | |
| 101 super.detached(); _r.disable(notify: true); | |
| 102 children = const []; | |
| 103 } | |
| 104 | |
| 105 VirtualCollectionElement _collection; | |
| 106 | |
| 107 void render() { | |
| 108 if (children.length == 0) { | |
| 109 children = [_collection]; | |
| 110 } | |
| 111 Iterable _toList(item) { | |
| 112 if (isExpanded(item)) { | |
| 113 Iterable children = _children(item); | |
| 114 if (children.isNotEmpty) { | |
| 115 return [item]..addAll(children.expand(_toList)); | |
| 116 } | |
| 117 } | |
| 118 return [item]; | |
| 119 } | |
| 120 _collection.items = _items.expand(_toList); | |
| 121 var depth = 0; | |
| 122 Iterable _toDepth(item) { | |
| 123 if (isExpanded(item)) { | |
| 124 Iterable children = _children(item); | |
| 125 if (children.isNotEmpty) { | |
| 126 depth++; | |
| 127 return children.expand(_toDepth).toList() | |
| 128 ..insert(0, --depth); | |
| 129 } | |
| 130 } | |
| 131 return [depth]; | |
| 132 } | |
| 133 _depths = _items.expand(_toDepth).toList(); | |
| 134 } | |
| 135 } | |
| OLD | NEW |