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/helpers/rendering_scheduler.dart'; | |
| 8 import 'package:observatory/src/elements/helpers/tag.dart'; | |
| 9 | |
| 10 typedef HtmlElement _CreateElementCallback(); | |
| 11 typedef void _UpdateElementCallback(HtmlElement el, dynamic item, int index); | |
| 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 => _r.onRendere d; | |
| 20 | |
| 21 _CreateElementCallback _create; | |
| 22 _UpdateElementCallback _update; | |
| 23 double _itemHeight; | |
| 24 int _top; | |
| 25 int _height; | |
| 26 List _items; | |
| 27 StreamSubscription _onScroollSubscription; | |
|
turnidge
2016/08/01 17:32:39
Typo: Scrooll -> Scroll.
cbernaschina
2016/08/01 17:44:45
Done.
| |
| 28 StreamSubscription _onResizeSubscription; | |
| 29 | |
| 30 List get items => _items; | |
| 31 | |
| 32 set items(Iterable value) { | |
| 33 _items = new List.unmodifiable(value); | |
| 34 _r.dirty(); | |
| 35 } | |
| 36 | |
| 37 | |
| 38 factory VirtualCollectionElement(_CreateElementCallback create, | |
| 39 _UpdateElementCallback update, {Iterable items: const [], | |
| 40 RenderingQueue queue}) { | |
| 41 assert(create != null); | |
| 42 assert(update != null); | |
| 43 assert(items != null); | |
| 44 VirtualCollectionElement e = document.createElement(tag.name); | |
| 45 e._r = new RenderingScheduler(e, queue: queue); | |
| 46 e._create = create; | |
| 47 e._update = update; | |
| 48 e._items = new List.unmodifiable(items); | |
| 49 return e; | |
| 50 } | |
| 51 | |
| 52 VirtualCollectionElement.created() : super.created(); | |
| 53 | |
| 54 @override | |
| 55 attached() { | |
| 56 super.attached(); | |
| 57 _r.enable(); | |
| 58 _top = 0; | |
| 59 _height = getBoundingClientRect().height; | |
| 60 _itemHeight = _computeItemHeight(); | |
| 61 _onScroollSubscription = onScroll.listen(_onScroll); | |
| 62 _onResizeSubscription = window.onResize.listen(_onResize); | |
| 63 } | |
| 64 | |
| 65 @override | |
| 66 detached() { | |
| 67 super.detached(); _r.disable(notify: true); | |
|
Cutch
2016/08/01 17:02:35
new line
cbernaschina
2016/08/01 17:44:45
Done.
| |
| 68 children = const []; | |
| 69 _onScroollSubscription.cancel(); | |
| 70 _onResizeSubscription.cancel(); | |
| 71 } | |
| 72 | |
| 73 final DivElement _scroller = new DivElement()..classes = const ['scroller']; | |
| 74 final DivElement _shifter = new DivElement()..classes = const ['shifter']; | |
| 75 | |
| 76 dynamic getItemFromElement(HtmlElement element) { | |
| 77 final e_i = _shifter.children.indexOf(element); | |
| 78 if (e_i < 0) return null; | |
|
turnidge
2016/08/01 17:32:39
Use {}'s here and everywhere for if statements in
cbernaschina
2016/08/01 17:44:44
Done.
| |
| 79 final i_i = _top + e_i - (_shifter.children.length / 4).floor(); | |
|
turnidge
2016/08/01 17:32:40
Can you think of a better name than i_i? What is
cbernaschina
2016/08/01 17:44:45
item_index
done
| |
| 80 if (0 <= i_i && i_i < items.length) return _items[i_i]; | |
|
turnidge
2016/08/01 17:32:39
Here too, for example.
cbernaschina
2016/08/01 17:44:45
Done.
| |
| 81 return null; | |
| 82 } | |
| 83 | |
| 84 void render() { | |
| 85 _top = (scrollTop / _itemHeight).floor(); | |
| 86 | |
| 87 _scroller.style.height = '${_itemHeight*(_items.length)}px'; | |
| 88 _shifter.style.top = '${_itemHeight*_top}px'; | |
| 89 final length_4 = (_height / _itemHeight / 2).ceil(); | |
| 90 final length = length_4 * 4; | |
| 91 | |
| 92 if (_shifter.children.length < length) { | |
| 93 while (_shifter.children.length != length) { | |
| 94 _shifter.children.add(_create()..style.display = 'hidden'); | |
| 95 } | |
| 96 _shifter.style.height = '${_itemHeight*length}px'; | |
| 97 children = [_scroller..children = [_shifter]]; | |
|
turnidge
2016/08/01 17:32:39
This is kind of a complex little line. What is it
cbernaschina
2016/08/01 17:44:45
Changed to:
children = [
_scroller
..childre
| |
| 98 } | |
| 99 | |
| 100 int i = _top - length_4; | |
| 101 for (final HtmlElement e in _shifter.children) { | |
| 102 if (0 <= i && i < _items.length) { | |
| 103 _update(e..style.display = null, _items[i], i); | |
|
Cutch
2016/08/01 17:02:35
don't assign in a function call.
cbernaschina
2016/08/01 17:44:45
Done.
| |
| 104 } else { | |
| 105 e.style.display = 'hidden'; | |
| 106 } | |
| 107 i++; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 double _computeItemHeight() { | |
| 112 final c = children; | |
| 113 children = [_create()]; | |
| 114 final height = children[0].getBoundingClientRect().height; | |
| 115 children = c; | |
| 116 return height; | |
| 117 } | |
| 118 | |
| 119 void _onScroll(_) { | |
| 120 if(_r.isDirty) return; | |
| 121 if ((scrollTop - _top * _itemHeight).abs() >= | |
| 122 _shifter.children.length / 4 * _itemHeight) { | |
|
Cutch
2016/08/01 17:02:35
please give "4" (here and elsewhere) a symbolic na
cbernaschina
2016/08/01 17:44:44
Done.
| |
| 123 _r.dirty(); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void _onResize(_) { | |
| 128 _height = getBoundingClientRect().height; | |
| 129 _r.dirty(); | |
| 130 } | |
| 131 } | |
| OLD | NEW |