@vnejs/module.controller.arrows.grid 0.2.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/dist/index.d.ts +59 -0
- package/dist/index.js +199 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/package.json +40 -0
- package/src/index.ts +266 -0
- package/src/types.ts +18 -0
- package/tsconfig.json +9 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { ModuleControllerArrowsGridConstants, ModuleControllerArrowsGridEvents, ModuleControllerArrowsGridParams, ModuleControllerArrowsGridSettings, ModuleControllerArrowsGridState } from "./types.js";
|
|
3
|
+
export type { ModuleControllerArrowsGridConstants, ModuleControllerArrowsGridEvents, ModuleControllerArrowsGridParams, ModuleControllerArrowsGridSettings, ModuleControllerArrowsGridState, } from "./types.js";
|
|
4
|
+
export declare abstract class ModuleControllerArrowsGrid<TEvents extends ModuleControllerArrowsGridEvents = ModuleControllerArrowsGridEvents, TConstants extends ModuleControllerArrowsGridConstants = ModuleControllerArrowsGridConstants, TSettings extends ModuleControllerArrowsGridSettings = ModuleControllerArrowsGridSettings, TParams extends ModuleControllerArrowsGridParams = ModuleControllerArrowsGridParams, TState extends ModuleControllerArrowsGridState = ModuleControllerArrowsGridState> extends ModuleCore<TEvents, TConstants, TSettings, TParams, TState> {
|
|
5
|
+
abstract name: string;
|
|
6
|
+
abstract EVENT_SHOW: string;
|
|
7
|
+
abstract EVENT_HIDE: string;
|
|
8
|
+
abstract EVENT_STATE_UPDATE: string;
|
|
9
|
+
abstract EVENT_UPDATE_FAST: string;
|
|
10
|
+
abstract EVENT_GRID_NEXT: string;
|
|
11
|
+
abstract EVENT_GRID_PREV: string;
|
|
12
|
+
abstract EVENT_GRID_CLEAR: string;
|
|
13
|
+
EVENT_GRID_UPDATE_ITEMS?: string;
|
|
14
|
+
abstract CONTROLS_ZINDEX: number;
|
|
15
|
+
CONTROLS: {
|
|
16
|
+
abstract_arrow_up: () => undefined;
|
|
17
|
+
abstract_arrow_right: () => undefined;
|
|
18
|
+
abstract_arrow_bottom: () => undefined;
|
|
19
|
+
abstract_arrow_left: () => undefined;
|
|
20
|
+
};
|
|
21
|
+
gridRow: number | null;
|
|
22
|
+
gridColumn: number | null;
|
|
23
|
+
isDisableGridChangeHorizontal: boolean;
|
|
24
|
+
isDisableGridChangeVertical: boolean;
|
|
25
|
+
cachedGridIndex: number;
|
|
26
|
+
cachedGridsCount: number;
|
|
27
|
+
cachedItemsCount: number | null;
|
|
28
|
+
abstract getMaxRows: () => number;
|
|
29
|
+
abstract getMaxColumns: () => number;
|
|
30
|
+
getItemsCount: () => number;
|
|
31
|
+
getCurrentGridIndex: () => number;
|
|
32
|
+
getGridsCount: () => number;
|
|
33
|
+
getGridItemsCount: (gridItems?: unknown[]) => number;
|
|
34
|
+
subscribe: () => void;
|
|
35
|
+
getDefaultState: () => TState;
|
|
36
|
+
getGridItems: () => Promise<unknown[]>;
|
|
37
|
+
onGridClear: () => Promise<unknown[]> | undefined;
|
|
38
|
+
onGridUpdateItems: () => Promise<void>;
|
|
39
|
+
findNearestValidCell: (row: number, column: number) => {
|
|
40
|
+
row: number;
|
|
41
|
+
column: number;
|
|
42
|
+
} | null;
|
|
43
|
+
snapSelectionToGrid: () => void;
|
|
44
|
+
onShow: () => Promise<unknown[]> | undefined;
|
|
45
|
+
onHide: () => Promise<unknown[]> | undefined;
|
|
46
|
+
onStateUpdate: (payload?: Partial<TState>) => void;
|
|
47
|
+
initRowAndColumns: () => void;
|
|
48
|
+
isCellInGrid: (row: number, column: number) => boolean;
|
|
49
|
+
hasNextGrid: () => boolean;
|
|
50
|
+
hasPrevGrid: () => boolean;
|
|
51
|
+
emitPosition: () => void;
|
|
52
|
+
goNextGrid: (nextRow: number, nextColumn: number) => void;
|
|
53
|
+
goPrevGrid: (nextRow: number, nextColumn: number) => void;
|
|
54
|
+
setCell: (row: number, column: number) => void;
|
|
55
|
+
incRow: () => void;
|
|
56
|
+
decRow: () => void;
|
|
57
|
+
incColumn: () => void;
|
|
58
|
+
decColumn: () => void;
|
|
59
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
export class ModuleControllerArrowsGrid extends ModuleCore {
|
|
3
|
+
EVENT_GRID_UPDATE_ITEMS;
|
|
4
|
+
CONTROLS = {
|
|
5
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_UP]: () => void this.decRow(),
|
|
6
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_RIGHT]: () => void this.incColumn(),
|
|
7
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_BOTTOM]: () => void this.incRow(),
|
|
8
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_LEFT]: () => void this.decColumn(),
|
|
9
|
+
};
|
|
10
|
+
gridRow = null;
|
|
11
|
+
gridColumn = null;
|
|
12
|
+
isDisableGridChangeHorizontal = false;
|
|
13
|
+
isDisableGridChangeVertical = false;
|
|
14
|
+
cachedGridIndex = 0;
|
|
15
|
+
cachedGridsCount = 1;
|
|
16
|
+
cachedItemsCount = null;
|
|
17
|
+
getItemsCount = () => this.cachedItemsCount ?? this.getMaxRows() * this.getMaxColumns();
|
|
18
|
+
getCurrentGridIndex = () => this.cachedGridIndex;
|
|
19
|
+
getGridsCount = () => this.cachedGridsCount;
|
|
20
|
+
getGridItemsCount = (gridItems = []) => gridItems.filter((item) => !item?.isStub).length;
|
|
21
|
+
subscribe = () => {
|
|
22
|
+
this.on(this.EVENT_SHOW, this.onShow);
|
|
23
|
+
this.on(this.EVENT_HIDE, this.onHide);
|
|
24
|
+
this.on(this.EVENT_STATE_UPDATE, this.onStateUpdate);
|
|
25
|
+
this.on(this.EVENT_GRID_CLEAR, this.onGridClear);
|
|
26
|
+
if (this.EVENT_GRID_UPDATE_ITEMS)
|
|
27
|
+
this.on(this.EVENT_GRID_UPDATE_ITEMS, this.onGridUpdateItems);
|
|
28
|
+
};
|
|
29
|
+
getDefaultState = () => ({});
|
|
30
|
+
getGridItems = async () => [];
|
|
31
|
+
onGridClear = () => {
|
|
32
|
+
this.gridRow = null;
|
|
33
|
+
this.gridColumn = null;
|
|
34
|
+
return this.emit(this.EVENT_STATE_UPDATE, {
|
|
35
|
+
gridRow: null,
|
|
36
|
+
gridColumn: null,
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
onGridUpdateItems = async () => {
|
|
40
|
+
const gridItems = await this.getGridItems();
|
|
41
|
+
this.cachedItemsCount = this.getGridItemsCount(gridItems);
|
|
42
|
+
await this.emit(this.EVENT_STATE_UPDATE, { gridItems });
|
|
43
|
+
this.snapSelectionToGrid();
|
|
44
|
+
};
|
|
45
|
+
findNearestValidCell = (row, column) => {
|
|
46
|
+
const maxColumns = this.getMaxColumns();
|
|
47
|
+
const itemsCount = this.getItemsCount();
|
|
48
|
+
if (itemsCount <= 0)
|
|
49
|
+
return null;
|
|
50
|
+
let bestRow = 0;
|
|
51
|
+
let bestColumn = 0;
|
|
52
|
+
let bestDist = Number.POSITIVE_INFINITY;
|
|
53
|
+
for (let index = 0; index < itemsCount; index++) {
|
|
54
|
+
const nextRow = Math.floor(index / maxColumns);
|
|
55
|
+
const nextColumn = index % maxColumns;
|
|
56
|
+
const dist = Math.abs(nextRow - row) + Math.abs(nextColumn - column);
|
|
57
|
+
if (dist >= bestDist)
|
|
58
|
+
continue;
|
|
59
|
+
bestDist = dist;
|
|
60
|
+
bestRow = nextRow;
|
|
61
|
+
bestColumn = nextColumn;
|
|
62
|
+
}
|
|
63
|
+
return { row: bestRow, column: bestColumn };
|
|
64
|
+
};
|
|
65
|
+
snapSelectionToGrid = () => {
|
|
66
|
+
if (this.gridRow === null || this.gridColumn === null)
|
|
67
|
+
return;
|
|
68
|
+
if (this.isCellInGrid(this.gridRow, this.gridColumn))
|
|
69
|
+
return;
|
|
70
|
+
const nearest = this.findNearestValidCell(this.gridRow, this.gridColumn);
|
|
71
|
+
if (!nearest) {
|
|
72
|
+
this.gridRow = null;
|
|
73
|
+
this.gridColumn = null;
|
|
74
|
+
this.emitPosition();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.gridRow = nearest.row;
|
|
78
|
+
this.gridColumn = nearest.column;
|
|
79
|
+
this.emitPosition();
|
|
80
|
+
};
|
|
81
|
+
onShow = () => {
|
|
82
|
+
this.gridRow = null;
|
|
83
|
+
this.gridColumn = null;
|
|
84
|
+
return this.emit(this.EVENTS.CONTROLS.PUSH, {
|
|
85
|
+
key: this.name,
|
|
86
|
+
controls: this.CONTROLS,
|
|
87
|
+
index: this.CONTROLS_ZINDEX + 1,
|
|
88
|
+
checkNext: true,
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
onHide = () => {
|
|
92
|
+
this.gridRow = null;
|
|
93
|
+
this.gridColumn = null;
|
|
94
|
+
return this.emit(this.EVENTS.CONTROLS.POP, { key: this.name });
|
|
95
|
+
};
|
|
96
|
+
onStateUpdate = (payload = {}) => {
|
|
97
|
+
this.updateState(payload);
|
|
98
|
+
if (payload.gridIndex !== undefined)
|
|
99
|
+
this.cachedGridIndex = payload.gridIndex;
|
|
100
|
+
if (payload.gridsCount !== undefined)
|
|
101
|
+
this.cachedGridsCount = payload.gridsCount;
|
|
102
|
+
if (payload.gridItems !== undefined)
|
|
103
|
+
this.cachedItemsCount = this.getGridItemsCount(payload.gridItems);
|
|
104
|
+
if ("gridRow" in payload)
|
|
105
|
+
this.gridRow = payload.gridRow ?? null;
|
|
106
|
+
if ("gridColumn" in payload)
|
|
107
|
+
this.gridColumn = payload.gridColumn ?? null;
|
|
108
|
+
};
|
|
109
|
+
initRowAndColumns = () => {
|
|
110
|
+
this.gridRow = 0;
|
|
111
|
+
this.gridColumn = 0;
|
|
112
|
+
this.emitPosition();
|
|
113
|
+
};
|
|
114
|
+
isCellInGrid = (row, column) => {
|
|
115
|
+
const maxRows = this.getMaxRows();
|
|
116
|
+
const maxColumns = this.getMaxColumns();
|
|
117
|
+
if (row < 0 || column < 0 || row >= maxRows || column >= maxColumns)
|
|
118
|
+
return false;
|
|
119
|
+
return row * maxColumns + column < this.getItemsCount();
|
|
120
|
+
};
|
|
121
|
+
hasNextGrid = () => this.getCurrentGridIndex() + 1 < this.getGridsCount();
|
|
122
|
+
hasPrevGrid = () => this.getCurrentGridIndex() > 0;
|
|
123
|
+
emitPosition = () => {
|
|
124
|
+
this.emit(this.EVENT_STATE_UPDATE, {
|
|
125
|
+
gridRow: this.gridRow,
|
|
126
|
+
gridColumn: this.gridColumn,
|
|
127
|
+
});
|
|
128
|
+
this.emit(this.EVENT_UPDATE_FAST);
|
|
129
|
+
};
|
|
130
|
+
goNextGrid = (nextRow, nextColumn) => {
|
|
131
|
+
if (!this.hasNextGrid())
|
|
132
|
+
return;
|
|
133
|
+
this.gridRow = nextRow;
|
|
134
|
+
this.gridColumn = nextColumn;
|
|
135
|
+
this.emit(this.EVENT_STATE_UPDATE, {
|
|
136
|
+
gridRow: this.gridRow,
|
|
137
|
+
gridColumn: this.gridColumn,
|
|
138
|
+
});
|
|
139
|
+
this.emit(this.EVENT_GRID_NEXT);
|
|
140
|
+
};
|
|
141
|
+
goPrevGrid = (nextRow, nextColumn) => {
|
|
142
|
+
if (!this.hasPrevGrid())
|
|
143
|
+
return;
|
|
144
|
+
this.gridRow = nextRow;
|
|
145
|
+
this.gridColumn = nextColumn;
|
|
146
|
+
this.emit(this.EVENT_STATE_UPDATE, {
|
|
147
|
+
gridRow: this.gridRow,
|
|
148
|
+
gridColumn: this.gridColumn,
|
|
149
|
+
});
|
|
150
|
+
this.emit(this.EVENT_GRID_PREV);
|
|
151
|
+
};
|
|
152
|
+
setCell = (row, column) => {
|
|
153
|
+
if (!this.isCellInGrid(row, column))
|
|
154
|
+
return;
|
|
155
|
+
this.gridRow = row;
|
|
156
|
+
this.gridColumn = column;
|
|
157
|
+
this.emitPosition();
|
|
158
|
+
};
|
|
159
|
+
incRow = () => {
|
|
160
|
+
if (this.gridRow === null || this.gridColumn === null)
|
|
161
|
+
return this.initRowAndColumns();
|
|
162
|
+
const nextRow = this.gridRow + 1;
|
|
163
|
+
if (this.isCellInGrid(nextRow, this.gridColumn))
|
|
164
|
+
return this.setCell(nextRow, this.gridColumn);
|
|
165
|
+
if (this.isDisableGridChangeVertical)
|
|
166
|
+
return;
|
|
167
|
+
return this.goNextGrid(0, this.gridColumn);
|
|
168
|
+
};
|
|
169
|
+
decRow = () => {
|
|
170
|
+
if (this.gridRow === null || this.gridColumn === null)
|
|
171
|
+
return this.initRowAndColumns();
|
|
172
|
+
const nextRow = this.gridRow - 1;
|
|
173
|
+
if (this.isCellInGrid(nextRow, this.gridColumn))
|
|
174
|
+
return this.setCell(nextRow, this.gridColumn);
|
|
175
|
+
if (this.isDisableGridChangeVertical)
|
|
176
|
+
return;
|
|
177
|
+
return this.goPrevGrid(this.getMaxRows() - 1, this.gridColumn);
|
|
178
|
+
};
|
|
179
|
+
incColumn = () => {
|
|
180
|
+
if (this.gridRow === null || this.gridColumn === null)
|
|
181
|
+
return this.initRowAndColumns();
|
|
182
|
+
const nextColumn = this.gridColumn + 1;
|
|
183
|
+
if (this.isCellInGrid(this.gridRow, nextColumn))
|
|
184
|
+
return this.setCell(this.gridRow, nextColumn);
|
|
185
|
+
if (this.isDisableGridChangeHorizontal)
|
|
186
|
+
return;
|
|
187
|
+
return this.goNextGrid(this.gridRow, 0);
|
|
188
|
+
};
|
|
189
|
+
decColumn = () => {
|
|
190
|
+
if (this.gridRow === null || this.gridColumn === null)
|
|
191
|
+
return this.initRowAndColumns();
|
|
192
|
+
const nextColumn = this.gridColumn - 1;
|
|
193
|
+
if (this.isCellInGrid(this.gridRow, nextColumn))
|
|
194
|
+
return this.setCell(this.gridRow, nextColumn);
|
|
195
|
+
if (this.isDisableGridChangeHorizontal)
|
|
196
|
+
return;
|
|
197
|
+
return this.goPrevGrid(this.gridRow, this.getMaxColumns() - 1);
|
|
198
|
+
};
|
|
199
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { Constants as ControlsConstants, PluginName as ControlsPluginName, SubscribeEvents as ControlsSubscribeEvents } from "@vnejs/contracts.controls";
|
|
3
|
+
export type ModuleControllerArrowsGridEvents = ModuleCoreEvents & Record<ControlsPluginName, ControlsSubscribeEvents>;
|
|
4
|
+
export type ModuleControllerArrowsGridConstants = ModuleCoreConstants & Record<ControlsPluginName, ControlsConstants>;
|
|
5
|
+
export type ModuleControllerArrowsGridSettings = ModuleCoreSettings;
|
|
6
|
+
export type ModuleControllerArrowsGridParams = ModuleCoreParams;
|
|
7
|
+
export type ModuleControllerArrowsGridState = {
|
|
8
|
+
gridRow?: number | null;
|
|
9
|
+
gridColumn?: number | null;
|
|
10
|
+
gridIndex?: number;
|
|
11
|
+
gridsCount?: number;
|
|
12
|
+
gridItems?: unknown[];
|
|
13
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/module.controller.arrows.grid",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
24
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
25
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
26
|
+
},
|
|
27
|
+
"author": "",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@vnejs/module.core": "~0.2.0",
|
|
31
|
+
"@vnejs/contracts.controls": "~0.2.0",
|
|
32
|
+
"@vnejs/shared": "~0.2.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@vnejs/configs.ts-common": "~0.2.0",
|
|
36
|
+
"@vnejs/module.core": "~0.2.0",
|
|
37
|
+
"@vnejs/contracts.controls": "~0.2.0",
|
|
38
|
+
"@vnejs/shared": "~0.2.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { ControlsPopPayload, ControlsPushPayload } from "@vnejs/contracts.controls";
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
ModuleControllerArrowsGridConstants,
|
|
6
|
+
ModuleControllerArrowsGridEvents,
|
|
7
|
+
ModuleControllerArrowsGridParams,
|
|
8
|
+
ModuleControllerArrowsGridSettings,
|
|
9
|
+
ModuleControllerArrowsGridState,
|
|
10
|
+
} from "./types.js";
|
|
11
|
+
|
|
12
|
+
export type {
|
|
13
|
+
ModuleControllerArrowsGridConstants,
|
|
14
|
+
ModuleControllerArrowsGridEvents,
|
|
15
|
+
ModuleControllerArrowsGridParams,
|
|
16
|
+
ModuleControllerArrowsGridSettings,
|
|
17
|
+
ModuleControllerArrowsGridState,
|
|
18
|
+
} from "./types.js";
|
|
19
|
+
|
|
20
|
+
export abstract class ModuleControllerArrowsGrid<
|
|
21
|
+
TEvents extends ModuleControllerArrowsGridEvents = ModuleControllerArrowsGridEvents,
|
|
22
|
+
TConstants extends ModuleControllerArrowsGridConstants = ModuleControllerArrowsGridConstants,
|
|
23
|
+
TSettings extends ModuleControllerArrowsGridSettings = ModuleControllerArrowsGridSettings,
|
|
24
|
+
TParams extends ModuleControllerArrowsGridParams = ModuleControllerArrowsGridParams,
|
|
25
|
+
TState extends ModuleControllerArrowsGridState = ModuleControllerArrowsGridState,
|
|
26
|
+
> extends ModuleCore<TEvents, TConstants, TSettings, TParams, TState> {
|
|
27
|
+
abstract override name: string;
|
|
28
|
+
|
|
29
|
+
abstract EVENT_SHOW: string;
|
|
30
|
+
abstract EVENT_HIDE: string;
|
|
31
|
+
abstract EVENT_STATE_UPDATE: string;
|
|
32
|
+
abstract EVENT_UPDATE_FAST: string;
|
|
33
|
+
abstract EVENT_GRID_NEXT: string;
|
|
34
|
+
abstract EVENT_GRID_PREV: string;
|
|
35
|
+
abstract EVENT_GRID_CLEAR: string;
|
|
36
|
+
EVENT_GRID_UPDATE_ITEMS?: string;
|
|
37
|
+
|
|
38
|
+
abstract CONTROLS_ZINDEX: number;
|
|
39
|
+
|
|
40
|
+
CONTROLS = {
|
|
41
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_UP]: () => void this.decRow(),
|
|
42
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_RIGHT]: () => void this.incColumn(),
|
|
43
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_BOTTOM]: () => void this.incRow(),
|
|
44
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_LEFT]: () => void this.decColumn(),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
gridRow: number | null = null;
|
|
48
|
+
gridColumn: number | null = null;
|
|
49
|
+
|
|
50
|
+
isDisableGridChangeHorizontal = false;
|
|
51
|
+
isDisableGridChangeVertical = false;
|
|
52
|
+
|
|
53
|
+
cachedGridIndex = 0;
|
|
54
|
+
cachedGridsCount = 1;
|
|
55
|
+
cachedItemsCount: number | null = null;
|
|
56
|
+
|
|
57
|
+
abstract getMaxRows: () => number;
|
|
58
|
+
abstract getMaxColumns: () => number;
|
|
59
|
+
|
|
60
|
+
getItemsCount = () => this.cachedItemsCount ?? this.getMaxRows() * this.getMaxColumns();
|
|
61
|
+
getCurrentGridIndex = () => this.cachedGridIndex;
|
|
62
|
+
getGridsCount = () => this.cachedGridsCount;
|
|
63
|
+
|
|
64
|
+
getGridItemsCount = (gridItems: unknown[] = []) => gridItems.filter((item) => !(item as { isStub?: boolean } | null | undefined)?.isStub).length;
|
|
65
|
+
|
|
66
|
+
subscribe = () => {
|
|
67
|
+
this.on(this.EVENT_SHOW, this.onShow);
|
|
68
|
+
this.on(this.EVENT_HIDE, this.onHide);
|
|
69
|
+
this.on(this.EVENT_STATE_UPDATE, this.onStateUpdate);
|
|
70
|
+
this.on(this.EVENT_GRID_CLEAR, this.onGridClear);
|
|
71
|
+
if (this.EVENT_GRID_UPDATE_ITEMS) this.on(this.EVENT_GRID_UPDATE_ITEMS, this.onGridUpdateItems);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
getDefaultState = (): TState => ({}) as TState;
|
|
75
|
+
|
|
76
|
+
getGridItems = async (): Promise<unknown[]> => [];
|
|
77
|
+
|
|
78
|
+
onGridClear = () => {
|
|
79
|
+
this.gridRow = null;
|
|
80
|
+
this.gridColumn = null;
|
|
81
|
+
|
|
82
|
+
return this.emit(this.EVENT_STATE_UPDATE, {
|
|
83
|
+
gridRow: null,
|
|
84
|
+
gridColumn: null,
|
|
85
|
+
} satisfies Partial<ModuleControllerArrowsGridState>);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
onGridUpdateItems = async () => {
|
|
89
|
+
const gridItems = await this.getGridItems();
|
|
90
|
+
this.cachedItemsCount = this.getGridItemsCount(gridItems);
|
|
91
|
+
|
|
92
|
+
await this.emit(this.EVENT_STATE_UPDATE, { gridItems } satisfies Partial<ModuleControllerArrowsGridState>);
|
|
93
|
+
this.snapSelectionToGrid();
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
findNearestValidCell = (row: number, column: number) => {
|
|
97
|
+
const maxColumns = this.getMaxColumns();
|
|
98
|
+
const itemsCount = this.getItemsCount();
|
|
99
|
+
if (itemsCount <= 0) return null;
|
|
100
|
+
|
|
101
|
+
let bestRow = 0;
|
|
102
|
+
let bestColumn = 0;
|
|
103
|
+
let bestDist = Number.POSITIVE_INFINITY;
|
|
104
|
+
|
|
105
|
+
for (let index = 0; index < itemsCount; index++) {
|
|
106
|
+
const nextRow = Math.floor(index / maxColumns);
|
|
107
|
+
const nextColumn = index % maxColumns;
|
|
108
|
+
const dist = Math.abs(nextRow - row) + Math.abs(nextColumn - column);
|
|
109
|
+
if (dist >= bestDist) continue;
|
|
110
|
+
|
|
111
|
+
bestDist = dist;
|
|
112
|
+
bestRow = nextRow;
|
|
113
|
+
bestColumn = nextColumn;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return { row: bestRow, column: bestColumn };
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
snapSelectionToGrid = () => {
|
|
120
|
+
if (this.gridRow === null || this.gridColumn === null) return;
|
|
121
|
+
if (this.isCellInGrid(this.gridRow, this.gridColumn)) return;
|
|
122
|
+
|
|
123
|
+
const nearest = this.findNearestValidCell(this.gridRow, this.gridColumn);
|
|
124
|
+
if (!nearest) {
|
|
125
|
+
this.gridRow = null;
|
|
126
|
+
this.gridColumn = null;
|
|
127
|
+
this.emitPosition();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this.gridRow = nearest.row;
|
|
132
|
+
this.gridColumn = nearest.column;
|
|
133
|
+
this.emitPosition();
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
onShow = () => {
|
|
137
|
+
this.gridRow = null;
|
|
138
|
+
this.gridColumn = null;
|
|
139
|
+
|
|
140
|
+
return this.emit(this.EVENTS.CONTROLS.PUSH, {
|
|
141
|
+
key: this.name,
|
|
142
|
+
controls: this.CONTROLS,
|
|
143
|
+
index: this.CONTROLS_ZINDEX + 1,
|
|
144
|
+
checkNext: true,
|
|
145
|
+
} satisfies ControlsPushPayload);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
onHide = () => {
|
|
149
|
+
this.gridRow = null;
|
|
150
|
+
this.gridColumn = null;
|
|
151
|
+
|
|
152
|
+
return this.emit(this.EVENTS.CONTROLS.POP, { key: this.name } satisfies ControlsPopPayload);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
onStateUpdate = (payload: Partial<TState> = {} as Partial<TState>) => {
|
|
156
|
+
this.updateState(payload);
|
|
157
|
+
|
|
158
|
+
if (payload.gridIndex !== undefined) this.cachedGridIndex = payload.gridIndex;
|
|
159
|
+
if (payload.gridsCount !== undefined) this.cachedGridsCount = payload.gridsCount;
|
|
160
|
+
if (payload.gridItems !== undefined) this.cachedItemsCount = this.getGridItemsCount(payload.gridItems);
|
|
161
|
+
|
|
162
|
+
if ("gridRow" in payload) this.gridRow = payload.gridRow ?? null;
|
|
163
|
+
if ("gridColumn" in payload) this.gridColumn = payload.gridColumn ?? null;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
initRowAndColumns = () => {
|
|
167
|
+
this.gridRow = 0;
|
|
168
|
+
this.gridColumn = 0;
|
|
169
|
+
this.emitPosition();
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
isCellInGrid = (row: number, column: number) => {
|
|
173
|
+
const maxRows = this.getMaxRows();
|
|
174
|
+
const maxColumns = this.getMaxColumns();
|
|
175
|
+
if (row < 0 || column < 0 || row >= maxRows || column >= maxColumns) return false;
|
|
176
|
+
|
|
177
|
+
return row * maxColumns + column < this.getItemsCount();
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
hasNextGrid = () => this.getCurrentGridIndex() + 1 < this.getGridsCount();
|
|
181
|
+
hasPrevGrid = () => this.getCurrentGridIndex() > 0;
|
|
182
|
+
|
|
183
|
+
emitPosition = () => {
|
|
184
|
+
this.emit(this.EVENT_STATE_UPDATE, {
|
|
185
|
+
gridRow: this.gridRow,
|
|
186
|
+
gridColumn: this.gridColumn,
|
|
187
|
+
} satisfies Partial<ModuleControllerArrowsGridState>);
|
|
188
|
+
this.emit(this.EVENT_UPDATE_FAST);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
goNextGrid = (nextRow: number, nextColumn: number) => {
|
|
192
|
+
if (!this.hasNextGrid()) return;
|
|
193
|
+
|
|
194
|
+
this.gridRow = nextRow;
|
|
195
|
+
this.gridColumn = nextColumn;
|
|
196
|
+
this.emit(this.EVENT_STATE_UPDATE, {
|
|
197
|
+
gridRow: this.gridRow,
|
|
198
|
+
gridColumn: this.gridColumn,
|
|
199
|
+
} satisfies Partial<ModuleControllerArrowsGridState>);
|
|
200
|
+
this.emit(this.EVENT_GRID_NEXT);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
goPrevGrid = (nextRow: number, nextColumn: number) => {
|
|
204
|
+
if (!this.hasPrevGrid()) return;
|
|
205
|
+
|
|
206
|
+
this.gridRow = nextRow;
|
|
207
|
+
this.gridColumn = nextColumn;
|
|
208
|
+
this.emit(this.EVENT_STATE_UPDATE, {
|
|
209
|
+
gridRow: this.gridRow,
|
|
210
|
+
gridColumn: this.gridColumn,
|
|
211
|
+
} satisfies Partial<ModuleControllerArrowsGridState>);
|
|
212
|
+
this.emit(this.EVENT_GRID_PREV);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
setCell = (row: number, column: number) => {
|
|
216
|
+
if (!this.isCellInGrid(row, column)) return;
|
|
217
|
+
|
|
218
|
+
this.gridRow = row;
|
|
219
|
+
this.gridColumn = column;
|
|
220
|
+
this.emitPosition();
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
incRow = () => {
|
|
224
|
+
if (this.gridRow === null || this.gridColumn === null) return this.initRowAndColumns();
|
|
225
|
+
|
|
226
|
+
const nextRow = this.gridRow + 1;
|
|
227
|
+
|
|
228
|
+
if (this.isCellInGrid(nextRow, this.gridColumn)) return this.setCell(nextRow, this.gridColumn);
|
|
229
|
+
if (this.isDisableGridChangeVertical) return;
|
|
230
|
+
|
|
231
|
+
return this.goNextGrid(0, this.gridColumn);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
decRow = () => {
|
|
235
|
+
if (this.gridRow === null || this.gridColumn === null) return this.initRowAndColumns();
|
|
236
|
+
|
|
237
|
+
const nextRow = this.gridRow - 1;
|
|
238
|
+
|
|
239
|
+
if (this.isCellInGrid(nextRow, this.gridColumn)) return this.setCell(nextRow, this.gridColumn);
|
|
240
|
+
if (this.isDisableGridChangeVertical) return;
|
|
241
|
+
|
|
242
|
+
return this.goPrevGrid(this.getMaxRows() - 1, this.gridColumn);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
incColumn = () => {
|
|
246
|
+
if (this.gridRow === null || this.gridColumn === null) return this.initRowAndColumns();
|
|
247
|
+
|
|
248
|
+
const nextColumn = this.gridColumn + 1;
|
|
249
|
+
|
|
250
|
+
if (this.isCellInGrid(this.gridRow, nextColumn)) return this.setCell(this.gridRow, nextColumn);
|
|
251
|
+
if (this.isDisableGridChangeHorizontal) return;
|
|
252
|
+
|
|
253
|
+
return this.goNextGrid(this.gridRow, 0);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
decColumn = () => {
|
|
257
|
+
if (this.gridRow === null || this.gridColumn === null) return this.initRowAndColumns();
|
|
258
|
+
|
|
259
|
+
const nextColumn = this.gridColumn - 1;
|
|
260
|
+
|
|
261
|
+
if (this.isCellInGrid(this.gridRow, nextColumn)) return this.setCell(this.gridRow, nextColumn);
|
|
262
|
+
if (this.isDisableGridChangeHorizontal) return;
|
|
263
|
+
|
|
264
|
+
return this.goPrevGrid(this.gridRow, this.getMaxColumns() - 1);
|
|
265
|
+
};
|
|
266
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { Constants as ControlsConstants, PluginName as ControlsPluginName, SubscribeEvents as ControlsSubscribeEvents } from "@vnejs/contracts.controls";
|
|
3
|
+
|
|
4
|
+
export type ModuleControllerArrowsGridEvents = ModuleCoreEvents & Record<ControlsPluginName, ControlsSubscribeEvents>;
|
|
5
|
+
|
|
6
|
+
export type ModuleControllerArrowsGridConstants = ModuleCoreConstants & Record<ControlsPluginName, ControlsConstants>;
|
|
7
|
+
|
|
8
|
+
export type ModuleControllerArrowsGridSettings = ModuleCoreSettings;
|
|
9
|
+
|
|
10
|
+
export type ModuleControllerArrowsGridParams = ModuleCoreParams;
|
|
11
|
+
|
|
12
|
+
export type ModuleControllerArrowsGridState = {
|
|
13
|
+
gridRow?: number | null;
|
|
14
|
+
gridColumn?: number | null;
|
|
15
|
+
gridIndex?: number;
|
|
16
|
+
gridsCount?: number;
|
|
17
|
+
gridItems?: unknown[];
|
|
18
|
+
};
|