Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: runtime/observatory/lib/src/elements/containers/virtual_collection.dart

Issue 2203433002: Added new implementations for virtual containers (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Removed unnecessary hides Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/helpers/rendering_scheduler.dart';
8 import 'package:observatory/src/elements/helpers/tag.dart';
9
10 typedef HtmlElement VirtualCollectionCreateCallback();
11 typedef void VirtualCollectionUpdateCallback(HtmlElement el, dynamic item, int i ndex);
Cutch 2016/08/01 23:45:11 long line
cbernaschina 2016/08/01 23:50:41 Done.
12
13 class VirtualCollectionElement extends HtmlElement implements Renderable {
14 static const tag =
15 const Tag<VirtualCollectionElement>('virtual-collection');
16
17 RenderingScheduler<VirtualCollectionElement> _r;
18
19 Stream<RenderedEvent<VirtualCollectionElement>> get onRendered =>
20 _r.onRendered;
21
22 VirtualCollectionCreateCallback _create;
23 VirtualCollectionUpdateCallback _update;
24 double _itemHeight;
25 int _top;
26 int _height;
27 List _items;
28 StreamSubscription _onScrollSubscription;
29 StreamSubscription _onResizeSubscription;
30
31 List get items => _items;
32
33 set items(Iterable value) {
34 _items = new List.unmodifiable(value);
35 _r.dirty();
36 }
37
38
39 factory VirtualCollectionElement(VirtualCollectionCreateCallback create,
40 VirtualCollectionUpdateCallback update, {Iterable items: const [],
41 RenderingQueue queue}) {
42 assert(create != null);
43 assert(update != null);
44 assert(items != null);
45 VirtualCollectionElement e = document.createElement(tag.name);
46 e._r = new RenderingScheduler(e, queue: queue);
47 e._create = create;
48 e._update = update;
49 e._items = new List.unmodifiable(items);
50 return e;
51 }
52
53 VirtualCollectionElement.created() : super.created();
54
55 @override
56 attached() {
57 super.attached();
58 _r.enable();
59 _top = 0;
60 _height = getBoundingClientRect().height;
61 _itemHeight = _computeItemHeight();
62 _onScrollSubscription = onScroll.listen(_onScroll);
63 _onResizeSubscription = window.onResize.listen(_onResize);
64 }
65
66 @override
67 detached() {
68 super.detached();
69 _r.disable(notify: true);
70 children = const [];
71 _onScrollSubscription.cancel();
72 _onResizeSubscription.cancel();
73 }
74
75 final DivElement _scroller = new DivElement()..classes = const ['scroller'];
76 final DivElement _shifter = new DivElement()..classes = const ['shifter'];
77
78 dynamic getItemFromElement(HtmlElement element) {
79 final el_index = _shifter.children.indexOf(element);
80 if (el_index < 0) {
81 return null;
82 }
83 final item_index =
84 _top + el_index - (_shifter.children.length * _inverse_preload).floor();
85 if (0 <= item_index && item_index < items.length) {
86 return _items[item_index];
87 }
88 return null;
89 }
90
91 /// The preloaded element before and after the visible area are:
92 /// 1/preload_size of the number of items in the visble area.
93 /// See shared.css for the "top:-25%;".
94 static const int _preload = 2;
95 /// L = length of all the elements loaded
96 /// l = length of the visible area
97 ///
98 /// L = l + 2 * l / _preload
99 /// l = L * _preload / (_preload + 2)
100 ///
101 /// tail = l / _preload = L * 1 / (_preload + 2) = L * _inverse_preload
102 static const double _inverse_preload = 1 / (_preload + 2);
103
104 void render() {
105 _top = (scrollTop / _itemHeight).floor();
106
107 _scroller.style.height = '${_itemHeight*(_items.length)}px';
108 _shifter.style.top = '${_itemHeight*_top}px';
109 final tail_length = (_height / _itemHeight / _preload).ceil();
110 final length = tail_length * 2 + tail_length * _preload;
111
112 if (_shifter.children.length < length) {
113 while (_shifter.children.length != length) {
114 var e = _create();
115 e..style.display = 'hidden';
116 _shifter.children.add(e);
117 }
118 _shifter.style.height = '${_itemHeight*length}px';
119 children = [
120 _scroller
121 ..children = [_shifter]
122 ];
123 }
124
125 int i = _top - tail_length;
126 for (final HtmlElement e in _shifter.children) {
127 if (0 <= i && i < _items.length) {
128 e..style.display = null;
129 _update(e, _items[i], i);
130 } else {
131 e.style.display = 'hidden';
132 }
133 i++;
134 }
135 }
136
137 double _computeItemHeight() {
138 final c = children;
139 children = [_create()];
140 final height = children[0].getBoundingClientRect().height;
141 children = c;
142 return height;
143 }
144
145 void _onScroll(_) {
146 if(_r.isDirty) return;
147 if ((scrollTop - _top * _itemHeight).abs() >=
148 _shifter.children.length * _inverse_preload * _itemHeight) {
149 _r.dirty();
150 }
151 }
152
153 void _onResize(_) {
154 _height = getBoundingClientRect().height;
155 _r.dirty();
156 }
157 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698