@vnejs/module.controller.arrows.horizontal 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 +30 -0
- package/dist/index.js +70 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +1 -0
- package/package.json +40 -0
- package/src/index.ts +118 -0
- package/src/types.ts +18 -0
- package/tsconfig.json +9 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { ModuleControllerArrowsHorizontalConstants, ModuleControllerArrowsHorizontalEvents, ModuleControllerArrowsHorizontalParams, ModuleControllerArrowsHorizontalSettings, ModuleControllerArrowsHorizontalState } from "./types.js";
|
|
3
|
+
export type { ModuleControllerArrowsHorizontalConstants, ModuleControllerArrowsHorizontalEvents, ModuleControllerArrowsHorizontalParams, ModuleControllerArrowsHorizontalSettings, ModuleControllerArrowsHorizontalState, } from "./types.js";
|
|
4
|
+
export declare abstract class ModuleControllerArrowsHorizontal<TEvents extends ModuleControllerArrowsHorizontalEvents = ModuleControllerArrowsHorizontalEvents, TConstants extends ModuleControllerArrowsHorizontalConstants = ModuleControllerArrowsHorizontalConstants, TSettings extends ModuleControllerArrowsHorizontalSettings = ModuleControllerArrowsHorizontalSettings, TParams extends ModuleControllerArrowsHorizontalParams = ModuleControllerArrowsHorizontalParams, TState extends ModuleControllerArrowsHorizontalState = ModuleControllerArrowsHorizontalState> 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 CONTROLS_ZINDEX: number;
|
|
11
|
+
currentItem: number | null;
|
|
12
|
+
maxCurrentItem: number;
|
|
13
|
+
controls: {
|
|
14
|
+
abstract_arrow_left: () => undefined;
|
|
15
|
+
abstract_arrow_right: () => undefined;
|
|
16
|
+
};
|
|
17
|
+
subscribe: () => void;
|
|
18
|
+
getDefaultState: () => TState;
|
|
19
|
+
abstract getMaxCurrentItem: () => number | Promise<number>;
|
|
20
|
+
isArrowItemAvailable: (_index: number) => boolean | Promise<boolean>;
|
|
21
|
+
onShow: () => Promise<unknown[]> | undefined;
|
|
22
|
+
onHide: () => Promise<unknown[]> | undefined;
|
|
23
|
+
incCurrentItem: () => Promise<void>;
|
|
24
|
+
decCurrentItem: () => Promise<void>;
|
|
25
|
+
setCurrentItem: (value: number, defaultValue: number, direction?: 1 | -1) => Promise<void>;
|
|
26
|
+
applyCurrentItem: (value: number, defaultValue: number) => void;
|
|
27
|
+
skipUnavailableItems: (direction: 1 | -1) => Promise<void>;
|
|
28
|
+
emitCurrentItem: () => void;
|
|
29
|
+
syncMaxCurrentItem: () => Promise<void>;
|
|
30
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
export class ModuleControllerArrowsHorizontal extends ModuleCore {
|
|
3
|
+
currentItem = null;
|
|
4
|
+
maxCurrentItem = 0;
|
|
5
|
+
controls = {
|
|
6
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_LEFT]: () => void this.decCurrentItem(),
|
|
7
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_RIGHT]: () => void this.incCurrentItem(),
|
|
8
|
+
};
|
|
9
|
+
subscribe = () => {
|
|
10
|
+
this.on(this.EVENT_SHOW, this.onShow);
|
|
11
|
+
this.on(this.EVENT_HIDE, this.onHide);
|
|
12
|
+
};
|
|
13
|
+
getDefaultState = () => ({});
|
|
14
|
+
isArrowItemAvailable = (_index) => true;
|
|
15
|
+
onShow = () => {
|
|
16
|
+
this.currentItem = null;
|
|
17
|
+
return this.emit(this.EVENTS.CONTROLS.PUSH, {
|
|
18
|
+
key: this.name,
|
|
19
|
+
controls: this.controls,
|
|
20
|
+
index: this.CONTROLS_ZINDEX + 1,
|
|
21
|
+
checkNext: true,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
onHide = () => {
|
|
25
|
+
this.currentItem = null;
|
|
26
|
+
return this.emit(this.EVENTS.CONTROLS.POP, { key: this.name });
|
|
27
|
+
};
|
|
28
|
+
incCurrentItem = async () => {
|
|
29
|
+
await this.syncMaxCurrentItem();
|
|
30
|
+
return this.setCurrentItem((this.currentItem ?? 0) + 1, 0, 1);
|
|
31
|
+
};
|
|
32
|
+
decCurrentItem = async () => {
|
|
33
|
+
await this.syncMaxCurrentItem();
|
|
34
|
+
return this.setCurrentItem((this.currentItem ?? 0) - 1, this.maxCurrentItem, -1);
|
|
35
|
+
};
|
|
36
|
+
setCurrentItem = async (value, defaultValue, direction = 1) => {
|
|
37
|
+
this.applyCurrentItem(value, defaultValue);
|
|
38
|
+
await this.skipUnavailableItems(direction);
|
|
39
|
+
this.emitCurrentItem();
|
|
40
|
+
};
|
|
41
|
+
applyCurrentItem = (value, defaultValue) => {
|
|
42
|
+
if (this.currentItem === null) {
|
|
43
|
+
this.currentItem = defaultValue;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.currentItem = value;
|
|
47
|
+
if (this.currentItem > this.maxCurrentItem)
|
|
48
|
+
this.currentItem = 0;
|
|
49
|
+
if (this.currentItem < 0)
|
|
50
|
+
this.currentItem = this.maxCurrentItem;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
skipUnavailableItems = async (direction) => {
|
|
54
|
+
if (this.currentItem === null)
|
|
55
|
+
return;
|
|
56
|
+
const start = this.currentItem;
|
|
57
|
+
while (!(await this.isArrowItemAvailable(this.currentItem))) {
|
|
58
|
+
this.applyCurrentItem(this.currentItem + direction, direction === 1 ? 0 : this.maxCurrentItem);
|
|
59
|
+
if (this.currentItem === start)
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
emitCurrentItem = () => {
|
|
64
|
+
this.emit(this.EVENT_STATE_UPDATE, { currentArrowHorizontalItem: this.currentItem });
|
|
65
|
+
this.emit(this.EVENT_UPDATE_FAST);
|
|
66
|
+
};
|
|
67
|
+
syncMaxCurrentItem = async () => {
|
|
68
|
+
this.maxCurrentItem = await this.getMaxCurrentItem();
|
|
69
|
+
};
|
|
70
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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 ModuleControllerArrowsHorizontalEvents = ModuleCoreEvents & Record<ControlsPluginName, ControlsSubscribeEvents>;
|
|
4
|
+
export type ModuleControllerArrowsHorizontalConstants = ModuleCoreConstants & Record<ControlsPluginName, ControlsConstants>;
|
|
5
|
+
export type ModuleControllerArrowsHorizontalSettings = ModuleCoreSettings;
|
|
6
|
+
export type ModuleControllerArrowsHorizontalParams = ModuleCoreParams;
|
|
7
|
+
export type ModuleControllerArrowsHorizontalState = {
|
|
8
|
+
currentArrowHorizontalItem?: number | null;
|
|
9
|
+
};
|
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.horizontal",
|
|
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,118 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { ControlsPopPayload, ControlsPushPayload } from "@vnejs/contracts.controls";
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
ModuleControllerArrowsHorizontalConstants,
|
|
6
|
+
ModuleControllerArrowsHorizontalEvents,
|
|
7
|
+
ModuleControllerArrowsHorizontalParams,
|
|
8
|
+
ModuleControllerArrowsHorizontalSettings,
|
|
9
|
+
ModuleControllerArrowsHorizontalState,
|
|
10
|
+
} from "./types.js";
|
|
11
|
+
|
|
12
|
+
export type {
|
|
13
|
+
ModuleControllerArrowsHorizontalConstants,
|
|
14
|
+
ModuleControllerArrowsHorizontalEvents,
|
|
15
|
+
ModuleControllerArrowsHorizontalParams,
|
|
16
|
+
ModuleControllerArrowsHorizontalSettings,
|
|
17
|
+
ModuleControllerArrowsHorizontalState,
|
|
18
|
+
} from "./types.js";
|
|
19
|
+
|
|
20
|
+
export abstract class ModuleControllerArrowsHorizontal<
|
|
21
|
+
TEvents extends ModuleControllerArrowsHorizontalEvents = ModuleControllerArrowsHorizontalEvents,
|
|
22
|
+
TConstants extends ModuleControllerArrowsHorizontalConstants = ModuleControllerArrowsHorizontalConstants,
|
|
23
|
+
TSettings extends ModuleControllerArrowsHorizontalSettings = ModuleControllerArrowsHorizontalSettings,
|
|
24
|
+
TParams extends ModuleControllerArrowsHorizontalParams = ModuleControllerArrowsHorizontalParams,
|
|
25
|
+
TState extends ModuleControllerArrowsHorizontalState = ModuleControllerArrowsHorizontalState,
|
|
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
|
+
|
|
34
|
+
abstract CONTROLS_ZINDEX: number;
|
|
35
|
+
|
|
36
|
+
currentItem: number | null = null;
|
|
37
|
+
maxCurrentItem = 0;
|
|
38
|
+
|
|
39
|
+
controls = {
|
|
40
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_LEFT]: () => void this.decCurrentItem(),
|
|
41
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_RIGHT]: () => void this.incCurrentItem(),
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
subscribe = () => {
|
|
45
|
+
this.on(this.EVENT_SHOW, this.onShow);
|
|
46
|
+
this.on(this.EVENT_HIDE, this.onHide);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
getDefaultState = (): TState => ({}) as TState;
|
|
50
|
+
|
|
51
|
+
abstract getMaxCurrentItem: () => number | Promise<number>;
|
|
52
|
+
|
|
53
|
+
isArrowItemAvailable = (_index: number): boolean | Promise<boolean> => true;
|
|
54
|
+
|
|
55
|
+
onShow = () => {
|
|
56
|
+
this.currentItem = null;
|
|
57
|
+
|
|
58
|
+
return this.emit(this.EVENTS.CONTROLS.PUSH, {
|
|
59
|
+
key: this.name,
|
|
60
|
+
controls: this.controls,
|
|
61
|
+
index: this.CONTROLS_ZINDEX + 1,
|
|
62
|
+
checkNext: true,
|
|
63
|
+
} satisfies ControlsPushPayload);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
onHide = () => {
|
|
67
|
+
this.currentItem = null;
|
|
68
|
+
|
|
69
|
+
return this.emit(this.EVENTS.CONTROLS.POP, { key: this.name } satisfies ControlsPopPayload);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
incCurrentItem = async () => {
|
|
73
|
+
await this.syncMaxCurrentItem();
|
|
74
|
+
|
|
75
|
+
return this.setCurrentItem((this.currentItem ?? 0) + 1, 0, 1);
|
|
76
|
+
};
|
|
77
|
+
decCurrentItem = async () => {
|
|
78
|
+
await this.syncMaxCurrentItem();
|
|
79
|
+
|
|
80
|
+
return this.setCurrentItem((this.currentItem ?? 0) - 1, this.maxCurrentItem, -1);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
setCurrentItem = async (value: number, defaultValue: number, direction: 1 | -1 = 1) => {
|
|
84
|
+
this.applyCurrentItem(value, defaultValue);
|
|
85
|
+
await this.skipUnavailableItems(direction);
|
|
86
|
+
this.emitCurrentItem();
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
applyCurrentItem = (value: number, defaultValue: number) => {
|
|
90
|
+
if (this.currentItem === null) {
|
|
91
|
+
this.currentItem = defaultValue;
|
|
92
|
+
} else {
|
|
93
|
+
this.currentItem = value;
|
|
94
|
+
if (this.currentItem > this.maxCurrentItem) this.currentItem = 0;
|
|
95
|
+
if (this.currentItem < 0) this.currentItem = this.maxCurrentItem;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
skipUnavailableItems = async (direction: 1 | -1) => {
|
|
100
|
+
if (this.currentItem === null) return;
|
|
101
|
+
|
|
102
|
+
const start = this.currentItem;
|
|
103
|
+
|
|
104
|
+
while (!(await this.isArrowItemAvailable(this.currentItem))) {
|
|
105
|
+
this.applyCurrentItem(this.currentItem + direction, direction === 1 ? 0 : this.maxCurrentItem);
|
|
106
|
+
if (this.currentItem === start) break;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
emitCurrentItem = () => {
|
|
111
|
+
this.emit(this.EVENT_STATE_UPDATE, { currentArrowHorizontalItem: this.currentItem } satisfies ModuleControllerArrowsHorizontalState);
|
|
112
|
+
this.emit(this.EVENT_UPDATE_FAST);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
syncMaxCurrentItem = async () => {
|
|
116
|
+
this.maxCurrentItem = await this.getMaxCurrentItem();
|
|
117
|
+
};
|
|
118
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type {
|
|
3
|
+
Constants as ControlsConstants,
|
|
4
|
+
PluginName as ControlsPluginName,
|
|
5
|
+
SubscribeEvents as ControlsSubscribeEvents,
|
|
6
|
+
} from "@vnejs/contracts.controls";
|
|
7
|
+
|
|
8
|
+
export type ModuleControllerArrowsHorizontalEvents = ModuleCoreEvents & Record<ControlsPluginName, ControlsSubscribeEvents>;
|
|
9
|
+
|
|
10
|
+
export type ModuleControllerArrowsHorizontalConstants = ModuleCoreConstants & Record<ControlsPluginName, ControlsConstants>;
|
|
11
|
+
|
|
12
|
+
export type ModuleControllerArrowsHorizontalSettings = ModuleCoreSettings;
|
|
13
|
+
|
|
14
|
+
export type ModuleControllerArrowsHorizontalParams = ModuleCoreParams;
|
|
15
|
+
|
|
16
|
+
export type ModuleControllerArrowsHorizontalState = {
|
|
17
|
+
currentArrowHorizontalItem?: number | null;
|
|
18
|
+
};
|