Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:html'; | 6 import 'dart:html'; |
| 7 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; | 7 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 8 import 'package:observatory/src/elements/helpers/tag.dart'; | 8 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 9 | 9 |
| 10 typedef HtmlElement VirtualCollectionCreateCallback(); | 10 typedef HtmlElement VirtualCollectionCreateCallback(); |
| 11 typedef void VirtualCollectionUpdateCallback(HtmlElement el, dynamic item, | 11 typedef void VirtualCollectionUpdateCallback(HtmlElement el, dynamic item, |
| 12 int index); | 12 int index); |
| 13 | 13 |
| 14 class VirtualCollectionElement extends HtmlElement implements Renderable { | 14 class VirtualCollectionElement extends HtmlElement implements Renderable { |
| 15 static const tag = | 15 static const tag = |
| 16 const Tag<VirtualCollectionElement>('virtual-collection'); | 16 const Tag<VirtualCollectionElement>('virtual-collection'); |
| 17 | 17 |
| 18 RenderingScheduler<VirtualCollectionElement> _r; | 18 RenderingScheduler<VirtualCollectionElement> _r; |
| 19 | 19 |
| 20 Stream<RenderedEvent<VirtualCollectionElement>> get onRendered => | 20 Stream<RenderedEvent<VirtualCollectionElement>> get onRendered => |
| 21 _r.onRendered; | 21 _r.onRendered; |
| 22 | 22 |
| 23 VirtualCollectionCreateCallback _create; | 23 VirtualCollectionCreateCallback _create; |
| 24 VirtualCollectionCreateCallback _createHeader; | |
| 24 VirtualCollectionUpdateCallback _update; | 25 VirtualCollectionUpdateCallback _update; |
| 25 double _itemHeight; | 26 double _itemHeight; |
| 26 int _top; | 27 int _top; |
| 27 double _height; | 28 double _height; |
| 28 List _items; | 29 List _items; |
| 29 StreamSubscription _onScrollSubscription; | 30 StreamSubscription _onScrollSubscription; |
| 30 StreamSubscription _onResizeSubscription; | 31 StreamSubscription _onResizeSubscription; |
| 31 | 32 |
| 32 List get items => _items; | 33 List get items => _items; |
| 33 | 34 |
| 34 set items(Iterable value) { | 35 set items(Iterable value) { |
| 35 _items = new List.unmodifiable(value); | 36 _items = new List.unmodifiable(value); |
| 36 _r.dirty(); | 37 _r.dirty(); |
| 37 } | 38 } |
| 38 | 39 |
| 39 | 40 |
| 40 factory VirtualCollectionElement(VirtualCollectionCreateCallback create, | 41 factory VirtualCollectionElement(VirtualCollectionCreateCallback create, |
| 41 VirtualCollectionUpdateCallback update, {Iterable items: const [], | 42 VirtualCollectionUpdateCallback update, {Iterable items: const [], |
| 43 VirtualCollectionCreateCallback createHeader, | |
| 42 RenderingQueue queue}) { | 44 RenderingQueue queue}) { |
| 43 assert(create != null); | 45 assert(create != null); |
| 44 assert(update != null); | 46 assert(update != null); |
| 45 assert(items != null); | 47 assert(items != null); |
| 46 VirtualCollectionElement e = document.createElement(tag.name); | 48 VirtualCollectionElement e = document.createElement(tag.name); |
| 47 e._r = new RenderingScheduler(e, queue: queue); | 49 e._r = new RenderingScheduler(e, queue: queue); |
| 48 e._create = create; | 50 e._create = create; |
| 51 e._createHeader = createHeader; | |
| 49 e._update = update; | 52 e._update = update; |
| 50 e._items = new List.unmodifiable(items); | 53 e._items = new List.unmodifiable(items); |
| 51 return e; | 54 return e; |
| 52 } | 55 } |
| 53 | 56 |
| 54 VirtualCollectionElement.created() : super.created(); | 57 VirtualCollectionElement.created() : super.created(); |
| 55 | 58 |
| 56 @override | 59 @override |
| 57 attached() { | 60 attached() { |
| 58 super.attached(); | 61 super.attached(); |
| 59 _r.enable(); | 62 _r.enable(); |
| 60 _top = 0; | 63 _top = null; |
| 61 _height = getBoundingClientRect().height; | 64 _itemHeight = null; |
| 62 _itemHeight = _computeItemHeight(); | |
| 63 _onScrollSubscription = onScroll.listen(_onScroll); | 65 _onScrollSubscription = onScroll.listen(_onScroll); |
| 64 _onResizeSubscription = window.onResize.listen(_onResize); | 66 _onResizeSubscription = window.onResize.listen(_onResize); |
| 65 } | 67 } |
| 66 | 68 |
| 67 @override | 69 @override |
| 68 detached() { | 70 detached() { |
| 69 super.detached(); | 71 super.detached(); |
| 70 _r.disable(notify: true); | 72 _r.disable(notify: true); |
| 71 children = const []; | 73 children = const []; |
| 72 _onScrollSubscription.cancel(); | 74 _onScrollSubscription.cancel(); |
| 73 _onResizeSubscription.cancel(); | 75 _onResizeSubscription.cancel(); |
| 74 } | 76 } |
| 75 | 77 |
| 78 final DivElement _header = new DivElement()..classes = const ['header']; | |
| 76 final DivElement _scroller = new DivElement()..classes = const ['scroller']; | 79 final DivElement _scroller = new DivElement()..classes = const ['scroller']; |
| 77 final DivElement _shifter = new DivElement()..classes = const ['shifter']; | 80 final DivElement _shifter = new DivElement()..classes = const ['shifter']; |
| 78 | 81 |
| 79 dynamic getItemFromElement(HtmlElement element) { | 82 dynamic getItemFromElement(HtmlElement element) { |
| 80 final el_index = _shifter.children.indexOf(element); | 83 final el_index = _shifter.children.indexOf(element); |
| 81 if (el_index < 0) { | 84 if (el_index < 0) { |
| 82 return null; | 85 return null; |
| 83 } | 86 } |
| 84 final item_index = | 87 final item_index = |
| 85 _top + el_index - (_shifter.children.length * _inverse_preload).floor(); | 88 _top + el_index - (_shifter.children.length * _inverse_preload).floor(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 96 /// L = length of all the elements loaded | 99 /// L = length of all the elements loaded |
| 97 /// l = length of the visible area | 100 /// l = length of the visible area |
| 98 /// | 101 /// |
| 99 /// L = l + 2 * l / _preload | 102 /// L = l + 2 * l / _preload |
| 100 /// l = L * _preload / (_preload + 2) | 103 /// l = L * _preload / (_preload + 2) |
| 101 /// | 104 /// |
| 102 /// tail = l / _preload = L * 1 / (_preload + 2) = L * _inverse_preload | 105 /// tail = l / _preload = L * 1 / (_preload + 2) = L * _inverse_preload |
| 103 static const double _inverse_preload = 1 / (_preload + 2); | 106 static const double _inverse_preload = 1 / (_preload + 2); |
| 104 | 107 |
| 105 void render() { | 108 void render() { |
| 106 _top = (scrollTop / _itemHeight).floor(); | 109 if (children.isEmpty) { |
| 110 children = [ | |
| 111 _scroller | |
| 112 ..children = [ | |
| 113 _header, | |
| 114 _shifter | |
| 115 ..children = [_create()] | |
| 116 ], | |
| 117 ]; | |
| 118 _itemHeight = _shifter.children[0].getBoundingClientRect().height; | |
| 119 _height = getBoundingClientRect().height; | |
| 120 } | |
| 107 | 121 |
| 122 final top = (scrollTop / _itemHeight).floor(); | |
| 123 | |
| 124 _header.style.top = '${scrollTop}px'; | |
| 108 _scroller.style.height = '${_itemHeight*(_items.length)}px'; | 125 _scroller.style.height = '${_itemHeight*(_items.length)}px'; |
| 109 final tail_length = (_height / _itemHeight / _preload).ceil(); | 126 final tail_length = (_height / _itemHeight / _preload).ceil(); |
| 110 _shifter.style.top = '${_itemHeight*(_top - tail_length)}px'; | |
| 111 final length = tail_length * 2 + tail_length * _preload; | 127 final length = tail_length * 2 + tail_length * _preload; |
| 112 | 128 |
| 113 if (_shifter.children.length < length) { | 129 if (_shifter.children.length < length) { |
| 130 if (_createHeader != null) { | |
| 131 _header.children = [_createHeader()]; | |
| 132 } | |
| 114 while (_shifter.children.length != length) { | 133 while (_shifter.children.length != length) { |
| 115 var e = _create(); | 134 var e = _create(); |
| 116 e..style.display = 'hidden'; | 135 e..style.display = 'hidden'; |
| 117 _shifter.children.add(e); | 136 _shifter.children.add(e); |
| 118 } | 137 } |
| 119 children = [ | 138 children = [ |
| 120 _scroller | 139 _scroller |
| 121 ..children = [_shifter] | 140 ..children = [_header, _shifter], |
| 122 ]; | 141 ]; |
| 142 | |
| 143 _top = null; // force update; | |
| 123 } | 144 } |
| 124 | 145 if (_top == null || (top - _top).abs() >= tail_length) { |
|
Cutch
2016/08/17 14:28:44
Our style is to always wrap multiple if expression
cbernaschina
2016/08/17 17:01:27
Done.
| |
| 125 int i = _top - tail_length; | 146 _shifter.style.top = '${_itemHeight*(top-tail_length)}px'; |
| 126 for (final HtmlElement e in _shifter.children) { | 147 int i = top - tail_length; |
| 127 if (0 <= i && i < _items.length) { | 148 for (final HtmlElement e in _shifter.children) { |
| 128 e..style.display = null; | 149 if (0 <= i && i < _items.length) { |
| 129 _update(e, _items[i], i); | 150 e..style.display = null; |
| 130 } else { | 151 _update(e, _items[i], i); |
| 131 e.style.display = 'hidden'; | 152 } else { |
| 153 e.style.display = 'hidden'; | |
| 154 } | |
| 155 i++; | |
| 132 } | 156 } |
| 133 i++; | 157 _top = top; |
| 134 } | 158 } |
| 135 } | 159 } |
| 136 | 160 |
| 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(_) { | 161 void _onScroll(_) { |
| 146 if(_r.isDirty) return; | 162 _r.dirty(); |
| 147 if ((scrollTop - _top * _itemHeight).abs() >= | |
| 148 _shifter.children.length * _inverse_preload * _itemHeight) { | |
| 149 _r.dirty(); | |
| 150 } | |
| 151 } | 163 } |
| 152 | 164 |
| 153 void _onResize(_) { | 165 void _onResize(_) { |
| 154 final newHeight = getBoundingClientRect().height; | 166 final newHeight = getBoundingClientRect().height; |
| 155 if (newHeight > _height) { | 167 if (newHeight > _height) { |
| 156 _height = newHeight; | 168 _height = newHeight; |
| 157 _r.dirty(); | 169 _r.dirty(); |
| 158 } | 170 } |
| 159 } | 171 } |
| 160 } | 172 } |
| OLD | NEW |