@plcmp/pl-virtual-scroll 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/package.json +29 -0
- package/pl-virtual-scroll.js +250 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2021 Limited liability company «OST»
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plcmp/pl-virtual-scroll",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Component for lazy list render.",
|
|
5
|
+
"main": "pl-virtual-scroll.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "https://github.com/plcmp/pl-virtual-scroll.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Dmitry Boikov",
|
|
13
|
+
"email": "boikovdv@gmail.com"
|
|
14
|
+
},
|
|
15
|
+
"contributors": [
|
|
16
|
+
{
|
|
17
|
+
"name": "Azat Yamalov",
|
|
18
|
+
"email": "yamalov.azat@live.com"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"name": "Andrey Maksimov",
|
|
22
|
+
"email": "andrmaxmail@gmail.com"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"polylib": "^1.0.0",
|
|
27
|
+
"@plcmp/utils": "^0.0.1"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { html, PlElement, TemplateInstance, createContext } from "polylib";
|
|
2
|
+
import { PlaceHolder } from "@plcmp/utils";
|
|
3
|
+
|
|
4
|
+
/** @typedef VirtualScrollItem
|
|
5
|
+
* @property {LightDataContext} ctx
|
|
6
|
+
* @property {TemplateInstance} ti
|
|
7
|
+
* @property {Number} index
|
|
8
|
+
* @property {Number} h - height of rendered item
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
class PlVirtualScroll extends PlElement {
|
|
12
|
+
/** @type VirtualScrollItem[]*/
|
|
13
|
+
phyPool = [];
|
|
14
|
+
constructor() {
|
|
15
|
+
super({ lightDom: true });
|
|
16
|
+
}
|
|
17
|
+
static properties = {
|
|
18
|
+
as: { value: 'item' },
|
|
19
|
+
items: { type: Array, observer: '_dataChanged' },
|
|
20
|
+
renderedStart: { type: Number, value: 0 },
|
|
21
|
+
renderedCount: { type: Number, value: 0 },
|
|
22
|
+
phyItems: { type: Array, value: () => [] },
|
|
23
|
+
canvas: { type: Object }
|
|
24
|
+
}
|
|
25
|
+
static template = html`
|
|
26
|
+
<style>
|
|
27
|
+
pl-virtual-scroll {
|
|
28
|
+
height: 100%;
|
|
29
|
+
overflow: auto;
|
|
30
|
+
display: block;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
pl-virtual-scroll #vsCanvas {
|
|
34
|
+
position: relative;
|
|
35
|
+
/*noinspection CssUnresolvedCustomProperty*/
|
|
36
|
+
contain: strict;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.vsItem {
|
|
40
|
+
position: absolute;
|
|
41
|
+
left: 0;
|
|
42
|
+
top: 0;
|
|
43
|
+
}
|
|
44
|
+
</style>
|
|
45
|
+
<div id="vsCanvas">
|
|
46
|
+
</div>
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
connectedCallback() {
|
|
50
|
+
super.connectedCallback();
|
|
51
|
+
let canvas = this.canvas ?? this.$.vsCanvas;
|
|
52
|
+
canvas.parentNode.addEventListener('scroll', this.onScroll.bind(this));
|
|
53
|
+
let tpl = this.querySelector('template');
|
|
54
|
+
this.oTpl = tpl;
|
|
55
|
+
this.rTpl = tpl.tpl;//new Template(`<div class="vsItem">${tpl.innerHTML}</div>`);
|
|
56
|
+
this._pti = tpl._pti;
|
|
57
|
+
this._hti = tpl._hti;
|
|
58
|
+
this.pctx = tpl._pti?.ctx;
|
|
59
|
+
/* render items if them already assigned */
|
|
60
|
+
if (Array.isArray(this.items) && this.items.length > 0) {
|
|
61
|
+
this.render();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
_dataChanged(data, old, mutation) {
|
|
65
|
+
// set microtask, element may be not inserted in dom tree yet,
|
|
66
|
+
// but we need to know viewport height to render
|
|
67
|
+
let [, index, ...rest] = mutation.path.split('.');
|
|
68
|
+
switch (mutation.action) {
|
|
69
|
+
case 'upd':
|
|
70
|
+
if (index !== undefined && +index >= 0) {
|
|
71
|
+
let el = this.phyPool.find(i => i.index === +index);
|
|
72
|
+
if (el && rest.length > 0) {
|
|
73
|
+
let path = [this.as, ...rest].join('.');
|
|
74
|
+
el.ti.applyEffects({ ...mutation, path });
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
setTimeout(() => this.render(), 0);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case 'splice':
|
|
81
|
+
let { index: spliceIndex } = mutation;
|
|
82
|
+
|
|
83
|
+
//TODO: add more Heuristic to scroll list if visible elements that not changed? like insert rows before
|
|
84
|
+
// visible area
|
|
85
|
+
|
|
86
|
+
//refresh all PHY if they can be affected
|
|
87
|
+
this.phyPool.forEach(i => {
|
|
88
|
+
if (i.index !== null && i.index >= spliceIndex && i.index < this.items.length) {
|
|
89
|
+
i.ctx.replace(this.items[i.index]);
|
|
90
|
+
i.ti.ctx = i.ctx;
|
|
91
|
+
i.ti.applyEffects();
|
|
92
|
+
i.ti.applyBinds();
|
|
93
|
+
} else if (i.index >= this.items.length) {
|
|
94
|
+
i.index = null;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
this.render();
|
|
98
|
+
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* @param {Boolean} [scroll] - render for new scroll position
|
|
106
|
+
*/
|
|
107
|
+
render(scroll) {
|
|
108
|
+
// detect window height
|
|
109
|
+
// detect new position,
|
|
110
|
+
let canvas = this.canvas ?? this.$.vsCanvas;
|
|
111
|
+
let offset = canvas.parentNode.scrollTop;
|
|
112
|
+
let height = canvas.parentNode.offsetHeight;
|
|
113
|
+
if (height === 0 || !this.items) return;
|
|
114
|
+
|
|
115
|
+
if (!this.elementHeight && this.items.length > 0) {
|
|
116
|
+
let el = this.renderItem(0, undefined, 0);
|
|
117
|
+
this.elementHeight = el.h;
|
|
118
|
+
}
|
|
119
|
+
/*let first = Math.floor(offset / this.elementHeight);
|
|
120
|
+
let last = Math.ceil((offset+height) / this.elementHeight);*/
|
|
121
|
+
// Reset scroll position if update data smaller than current visible index
|
|
122
|
+
if (!scroll && this.items.length < (offset + height * 1.5) / this.elementHeight) {
|
|
123
|
+
canvas.parentNode.scrollTop = 0;
|
|
124
|
+
}
|
|
125
|
+
let shadowEnd = Math.min(this.items.length, Math.ceil((offset + height * 1.5) / this.elementHeight));
|
|
126
|
+
let shadowBegin = Math.max(0, Math.floor(Math.min((offset - height / 2) / this.elementHeight, shadowEnd - height * 2 / this.elementHeight)));
|
|
127
|
+
|
|
128
|
+
let used = [], unused = [];
|
|
129
|
+
this.phyPool.forEach(x => {
|
|
130
|
+
if (x.index !== null && shadowBegin <= x.index && x.index <= shadowEnd && x.index < this.items.length) {
|
|
131
|
+
if (x.ctx.model !== this.items[x.index]) {
|
|
132
|
+
x.ctx.replace(this.items[x.index]);
|
|
133
|
+
x.ti.ctx = x.ctx;
|
|
134
|
+
x.ti.applyBinds();
|
|
135
|
+
x.ti.applyEffects();
|
|
136
|
+
}
|
|
137
|
+
used.push(x);
|
|
138
|
+
} else {
|
|
139
|
+
unused.push(x);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
if (used.length > 0) {
|
|
144
|
+
used = used.sort((a, b) => a.index - b.index);
|
|
145
|
+
if (used[used.length - 1].index < shadowEnd) {
|
|
146
|
+
let prev = used[used.length - 1];
|
|
147
|
+
let lastUsedIndex = prev.index;
|
|
148
|
+
for (let i = lastUsedIndex + 1; i <= Math.min(shadowEnd, this.items.length - 1); i++) {
|
|
149
|
+
prev = this.renderItem(i, unused.pop(), prev);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (used[0].index > shadowBegin) {
|
|
153
|
+
let prev = used[0];
|
|
154
|
+
let lastUsedIndex = prev.index;
|
|
155
|
+
for (let i = lastUsedIndex - 1; i >= shadowBegin; i--) {
|
|
156
|
+
prev = this.renderItem(i, unused.pop(), prev, true);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} else if (shadowBegin >= 0 && this.items.length > 0) {
|
|
160
|
+
let prev = this.renderItem(shadowBegin, unused.pop(), shadowBegin * this.elementHeight);
|
|
161
|
+
for (let i = shadowBegin + 1; i <= shadowEnd; i++) {
|
|
162
|
+
prev = this.renderItem(i, unused.pop(), prev);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
unused.forEach(u => {
|
|
167
|
+
u.index = null;
|
|
168
|
+
u.ti._nodes.forEach(i => { if (i.style) i.style.transform = `translateY(-100%)`; });
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// fill .5 height window in background
|
|
172
|
+
// while phy window not filed, expect +-.5 screen
|
|
173
|
+
// render must begin from visible start forward, then backward to .5 s/h
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
if (this.elementHeight && this.items.length)
|
|
177
|
+
canvas.style.setProperty('height', this.elementHeight * this.items.length + 'px')
|
|
178
|
+
else
|
|
179
|
+
canvas.style.setProperty('height', 0)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
*
|
|
184
|
+
* @param index
|
|
185
|
+
* @param {VirtualScrollItem} p_item
|
|
186
|
+
* @param prev
|
|
187
|
+
* @param [backward]
|
|
188
|
+
* @return {VirtualScrollItem}
|
|
189
|
+
*/
|
|
190
|
+
renderItem(index, p_item, prev, backward) {
|
|
191
|
+
// get from pull of phy
|
|
192
|
+
// on need more create one
|
|
193
|
+
if (index < 0 || index >= this.items.length) {
|
|
194
|
+
if (p_item) p_item.index = null;
|
|
195
|
+
return p_item;
|
|
196
|
+
}
|
|
197
|
+
if (this.items[index] instanceof PlaceHolder) this.items.load?.(this.items[index])
|
|
198
|
+
let target = p_item ?? this.createNewItem(this.items[index]);
|
|
199
|
+
|
|
200
|
+
target.index = index;
|
|
201
|
+
if (p_item) {
|
|
202
|
+
p_item.ctx.replace(this.items[index])
|
|
203
|
+
p_item.ti.ctx = p_item.ctx;
|
|
204
|
+
p_item.ti.applyBinds();
|
|
205
|
+
p_item.ti.applyEffects();
|
|
206
|
+
} else {
|
|
207
|
+
this.phyPool.push(target);
|
|
208
|
+
}
|
|
209
|
+
target.offset = typeof (prev) == 'number' ? prev : (backward ? prev.offset - target.h : prev.offset + prev.h);
|
|
210
|
+
target.ti._nodes.forEach(n => {
|
|
211
|
+
if (n.style) {
|
|
212
|
+
n.style.transform = `translateY(${target.offset}px)`;
|
|
213
|
+
n.style.position = 'absolute';
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
return target;
|
|
217
|
+
}
|
|
218
|
+
createNewItem(v) {
|
|
219
|
+
let ctx = createContext(this, v, this.as)
|
|
220
|
+
let ti = new TemplateInstance(this.rTpl);
|
|
221
|
+
ti._hti = this._hti;
|
|
222
|
+
ctx._ti = ti;
|
|
223
|
+
ti.attach({ ...ctx, root: this.canvas ?? this.$.vsCanvas }, undefined, this._pti);
|
|
224
|
+
let rect = calcNodesRect(ti._nodes);
|
|
225
|
+
let h = rect.height;
|
|
226
|
+
|
|
227
|
+
return { ctx, ti, h };
|
|
228
|
+
}
|
|
229
|
+
onScroll() {
|
|
230
|
+
this.render(true);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function calcNodesRect(nodes) {
|
|
235
|
+
nodes = nodes.filter(n => n.getBoundingClientRect);
|
|
236
|
+
let rect = nodes[0].getBoundingClientRect();
|
|
237
|
+
let { top, bottom, left, right } = rect;
|
|
238
|
+
({ top, bottom, left, right } = nodes.map(n => n.getBoundingClientRect()).filter(i => i).reduce((a, c) => (
|
|
239
|
+
{
|
|
240
|
+
top: Math.min(a.top, c.top),
|
|
241
|
+
bottom: Math.max(a.bottom, c.bottom),
|
|
242
|
+
left: Math.min(a.left, c.left),
|
|
243
|
+
right: Math.max(a.right, c.right)
|
|
244
|
+
})
|
|
245
|
+
, { top, bottom, left, right }));
|
|
246
|
+
let { x, y, height, width } = { x: left, y: top, width: right - left, height: bottom - top };
|
|
247
|
+
return { x, y, height, width };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
customElements.define('pl-virtual-scroll', PlVirtualScroll);
|