1 | import Grid, { GridItem, GridOutlines } from "@egjs/grid";
|
2 | import { ITEM_TYPE } from "./consts";
|
3 | import { InfiniteGridItem, InfiniteGridItemStatus } from "./InfiniteGridItem";
|
4 |
|
5 | export const LOADING_GROUP_KEY = "__INFINITEGRID__LOADING_GRID";
|
6 | export const LOADING_ITEM_KEY = "__INFINITEGRID__LOADING_ITEM";
|
7 |
|
8 | export class LoadingGrid extends Grid {
|
9 | public type: "start" | "end" | "" = "";
|
10 | public getLoadingItem(): InfiniteGridItem | null {
|
11 | return this.items[0] as InfiniteGridItem || null;
|
12 | }
|
13 | public setLoadingItem(item: Partial<InfiniteGridItemStatus> | null) {
|
14 | if (item) {
|
15 | const loadingItem = this.getLoadingItem();
|
16 |
|
17 | if (!loadingItem) {
|
18 | this.items = [new InfiniteGridItem(this.options.horizontal, {
|
19 | ...item,
|
20 | type: ITEM_TYPE.LOADING,
|
21 | key: LOADING_ITEM_KEY,
|
22 | })];
|
23 | } else {
|
24 | for (const name in item) {
|
25 | loadingItem[name] = item[name];
|
26 | }
|
27 | }
|
28 | } else {
|
29 | this.items = [];
|
30 | }
|
31 | }
|
32 | public applyGrid(items: GridItem[], direction: "start" | "end", outline: number[]): GridOutlines {
|
33 | if (!items.length) {
|
34 | return {
|
35 | start: outline,
|
36 | end: outline,
|
37 | };
|
38 | }
|
39 | const nextOutline = outline.length ? [...outline] : [0];
|
40 | const item = items[0];
|
41 | const offset = item.contentSize + this.gap;
|
42 |
|
43 | item.cssInlinePos = this.getContainerInlineSize() / 2 - item.inlineSize / 2;
|
44 |
|
45 | if (direction === "end") {
|
46 | const maxPos = Math.max(...nextOutline);
|
47 |
|
48 | item.cssContentPos = maxPos;
|
49 |
|
50 | return {
|
51 | start: nextOutline,
|
52 | end: nextOutline.map((pos) => pos + offset),
|
53 | };
|
54 | } else {
|
55 | const minPos = Math.min(...nextOutline);
|
56 |
|
57 | item.cssContentPos = minPos - offset;
|
58 |
|
59 | return {
|
60 | start: nextOutline.map((pos) => pos - offset),
|
61 | end: nextOutline,
|
62 | };
|
63 | }
|
64 | }
|
65 | }
|