forked from mathiasbynens/virtual-scroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual-scroller-element.js
213 lines (194 loc) · 5.73 KB
/
virtual-scroller-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {_item, _key, ItemSource} from './item-source.js';
import {default as Layout1dGrid} from './layouts/layout-1d-grid.js';
import {default as Layout1d} from './layouts/layout-1d.js';
import {VirtualScroller} from './virtual-scroller.js';
export {ItemSource};
/** Properties */
const _scroller = Symbol();
const _createElement = Symbol();
const _updateElement = Symbol();
const _recycleElement = Symbol();
const _nodePool = Symbol();
const _rawItemSource = Symbol();
const _itemSource = Symbol();
const _elementSource = Symbol();
const _firstConnected = Symbol();
/** Functions */
const _render = Symbol();
export class VirtualScrollerElement extends HTMLElement {
constructor() {
super();
this[_scroller] = null;
// Default create/update/recycleElement.
this[_nodePool] = [];
let childTemplate = null;
this[_createElement] = () => {
if (this[_nodePool] && this[_nodePool].length) {
return this[_nodePool].pop();
}
if (!childTemplate) {
const template = this.querySelector('template');
childTemplate = template && template.content.firstElementChild ?
template.content.firstElementChild :
document.createElement('div');
}
return childTemplate.cloneNode(true);
};
this[_updateElement] = (element, item) => element.textContent =
item.toString();
this[_recycleElement] = (element) => this[_nodePool].push(element);
this[_itemSource] = this[_rawItemSource] = null;
this[_elementSource] = {};
this[_firstConnected] = false;
}
connectedCallback() {
if (!this[_firstConnected]) {
this.attachShadow({mode: 'open'}).innerHTML = `
<style>
:host {
display: block;
position: relative;
contain: strict;
height: 150px;
overflow: auto;
}
:host([hidden]) {
display: none;
}
::slotted(*) {
box-sizing: border-box;
}
:host([layout=vertical]) ::slotted(*) {
width: 100%;
}
:host([layout=horizontal]) ::slotted(*) {
height: 100%;
}
</style>
<slot></slot>`;
// Set default values.
if (!this.layout) {
this.layout = 'vertical';
}
// Enables rendering.
this[_firstConnected] = true;
}
this[_render]();
}
static get observedAttributes() {
return ['layout'];
}
attributeChangedCallback(name, oldVal, newVal) {
this[_render]();
}
get layout() {
return this.getAttribute('layout');
}
set layout(layout) {
this.setAttribute('layout', layout);
}
get itemSource() {
return this[_itemSource];
}
set itemSource(itemSource) {
// No Change.
if (this[_rawItemSource] === itemSource) {
return;
}
this[_rawItemSource] = itemSource;
this[_itemSource] = Array.isArray(itemSource) ?
ItemSource.fromArray(itemSource) :
itemSource;
this[_render]();
}
get createElement() {
return this[_createElement];
}
set createElement(fn) {
// Resets default recycling.
if (this[_nodePool]) {
this.recycleElement = null;
}
this[_createElement] = fn;
// Invalidate wrapped function.
this[_elementSource].createElement = null;
this[_render]();
}
get updateElement() {
return this[_updateElement];
}
set updateElement(fn) {
this[_updateElement] = fn;
// Invalidate wrapped function.
this[_elementSource].updateElement = null;
this[_render]();
}
get recycleElement() {
return this[_recycleElement];
}
set recycleElement(fn) {
// Marks default recycling changed.
this[_nodePool] = null;
this[_recycleElement] = fn;
// Invalidate wrapped function.
this[_elementSource].recycleElement = null;
this[_render]();
}
itemsChanged() {
if (this[_scroller]) {
// Render because length might have changed.
this[_render]();
// Request reset because items might have changed.
this[_scroller].requestReset();
}
}
scrollToIndex(index, { position = 'start' } = {}) {
if (this[_scroller]) {
this[_scroller].layout.scrollToIndex(index, position);
}
}
[_render]() {
// Wait first connected as scroller needs to measure
// sizes of container and children.
if (!this[_firstConnected] || !this.createElement) {
return;
}
if (!this[_scroller]) {
this[_scroller] =
new VirtualScroller({container: this, scrollTarget: this});
}
const scroller = this[_scroller];
const layoutAttr = this.layout;
const Layout = layoutAttr.endsWith('-grid') ? Layout1dGrid : Layout1d;
const direction =
layoutAttr.startsWith('horizontal') ? 'horizontal' : 'vertical';
const layout = scroller.layout instanceof Layout &&
scroller.layout.direction === direction ?
scroller.layout :
new Layout({direction});
let {createElement, updateElement, recycleElement} = this[_elementSource];
if (!createElement) {
createElement = this[_elementSource].createElement = (index) =>
this.createElement(this.itemSource[_item](index), index);
}
if (this.updateElement && !updateElement) {
updateElement = this[_elementSource].updateElement = (element, index) =>
this.updateElement(element, this.itemSource[_item](index), index);
}
if (this.recycleElement && !recycleElement) {
recycleElement = this[_elementSource].recycleElement = (element, index) =>
this.recycleElement(element, this.itemSource[_item](index), index);
}
const elementKey = this.itemSource ? this.itemSource[_key] : null;
const totalItems = this.itemSource ? this.itemSource.length : 0;
Object.assign(scroller, {
layout,
createElement,
updateElement,
recycleElement,
elementKey,
totalItems
});
}
}
customElements.define('virtual-scroller', VirtualScrollerElement);