-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.ts
68 lines (63 loc) · 2.24 KB
/
render.ts
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
import { BaseProcessFactory, CommonProcess, ProcessStatus } from './misc/index';
import { Scroller } from '../scroller';
import { Item } from '../classes/item';
export default class Render extends BaseProcessFactory(CommonProcess.render) {
static run(scroller: Scroller): void {
const { workflow, state: { cycle, render, scrollState }, viewport } = scroller;
scroller.logger.stat('before new items render');
if (scrollState.positionBeforeAsync === null) {
scrollState.positionBeforeAsync = viewport.scrollPosition;
}
render.renderTimer = setTimeout(() => {
render.renderTimer = null;
if (Render.doRender(scroller)) {
workflow.call({
process: Render.process,
status: render.noSize ? ProcessStatus.done : ProcessStatus.next,
payload: { process: cycle.initiator }
});
} else {
workflow.call({
process: Render.process,
status: ProcessStatus.error,
payload: { error: 'Can\'t associate item with element' }
});
}
}, 0);
}
static doRender(scroller: Scroller): boolean {
const { state: { fetch, render }, viewport, buffer, logger } = scroller;
render.positionBefore = viewport.scrollPosition;
if (!fetch.isCheck) {
render.sizeBefore = viewport.getScrollableSize();
if (
fetch.items.map(item => Render.processElement(scroller, item)).some(x => !x)
) {
return false;
}
}
buffer.checkAverageSize();
render.sizeAfter = viewport.getScrollableSize();
logger.stat('after new items render');
logger.log(() => render.noSize ? 'viewport size has not been changed' : void 0);
return true;
}
static processElement(scroller: Scroller, item: Item): boolean {
const { state: { fetch }, viewport, buffer } = scroller;
const element = viewport.element.querySelector(`[data-sid="${item.nodeId}"]`);
if (!element) {
return false;
}
item.element = element as HTMLElement;
item.element.style.left = '';
item.element.style.top = '';
item.element.style.position = '';
item.invisible = false;
item.setSize();
buffer.cacheItem(item);
if (item.$index < fetch.minIndex) {
fetch.negativeSize += item.size;
}
return true;
}
}