@vnejs/module.components 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/index.d.ts +59 -0
- package/index.js +2 -0
- package/modules/controller.js +91 -0
- package/modules/view.js +58 -0
- package/package.json +17 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { GlobalState, Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class ModuleController extends Module {
|
|
4
|
+
currentItem: number | null;
|
|
5
|
+
maxCurrentItem: number;
|
|
6
|
+
|
|
7
|
+
beforeShow?: () => Promise<void>;
|
|
8
|
+
afterShow?: () => Promise<void>;
|
|
9
|
+
beforeHide?: () => Promise<void>;
|
|
10
|
+
afterHide?: () => Promise<void>;
|
|
11
|
+
|
|
12
|
+
bgName?: string;
|
|
13
|
+
updateEvent: string;
|
|
14
|
+
|
|
15
|
+
controls?: Record<string, any>;
|
|
16
|
+
controlsIndex?: number;
|
|
17
|
+
controlsCheckNext?: Boolean;
|
|
18
|
+
|
|
19
|
+
onHide: (...args: any[]) => Promise<void>;
|
|
20
|
+
onShow: (...args: any[]) => Promise<void>;
|
|
21
|
+
onHideForce: (...args: any[]) => Promise<void>;
|
|
22
|
+
onShowForce: (...args: any[]) => Promise<void>;
|
|
23
|
+
onUpdateStateFromGlobalState: (state: GlobalState) => Promise<void>;
|
|
24
|
+
|
|
25
|
+
setShow: (value: Boolean, isForce?: Boolean) => Promise<void>;
|
|
26
|
+
|
|
27
|
+
loadImageMedia: (name: string, mediaType: string, quality: string, isTmp: Boolean, isContant: Boolean) => Promise<void>;
|
|
28
|
+
loadBgMedia: () => Promise<void>;
|
|
29
|
+
|
|
30
|
+
updateView: (isForce: Boolean, isFast: Boolean) => Promise<void>;
|
|
31
|
+
|
|
32
|
+
updateViewForce: () => Promise<void>;
|
|
33
|
+
updateViewFast: () => Promise<void>;
|
|
34
|
+
updateStateAndView: (state: any, isForce: Boolean, isFast: Boolean) => Promise<void>;
|
|
35
|
+
updateStateAndViewForce: (state: any) => Promise<void>;
|
|
36
|
+
updateStateAndViewFast: (state: any) => Promise<void>;
|
|
37
|
+
|
|
38
|
+
incCurrentItem: () => void;
|
|
39
|
+
decCurrentItem: () => void;
|
|
40
|
+
setCurrentItem: (value: number, defaultValue: number) => void;
|
|
41
|
+
unsetCurrentItem: () => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class ModuleView extends Module {
|
|
45
|
+
animationTime: number;
|
|
46
|
+
renderFunc: () => void;
|
|
47
|
+
updateEvent: string;
|
|
48
|
+
updateHandler: () => Promise<void>;
|
|
49
|
+
canvasInfo: any;
|
|
50
|
+
locLabel?: string;
|
|
51
|
+
|
|
52
|
+
regCanvas: () => Promise<void>;
|
|
53
|
+
regComponent: () => Promise<void>;
|
|
54
|
+
|
|
55
|
+
onUpdateReactComponent: (state: any) => Promise<void>;
|
|
56
|
+
|
|
57
|
+
setIsForceReact: (isForce: Boolean) => Promise<void>;
|
|
58
|
+
insertExtraDataToState: (state: any) => Promise<any>;
|
|
59
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class ModuleController extends Module {
|
|
4
|
+
currentItem = null;
|
|
5
|
+
maxCurrentItem = 0;
|
|
6
|
+
|
|
7
|
+
beforeShow = null;
|
|
8
|
+
afterShow = null;
|
|
9
|
+
beforeHide = null;
|
|
10
|
+
afterHide = null;
|
|
11
|
+
|
|
12
|
+
bgName = null;
|
|
13
|
+
updateEvent = null;
|
|
14
|
+
|
|
15
|
+
controls = null;
|
|
16
|
+
controlsIndex = 0;
|
|
17
|
+
controlsCheckNext = false;
|
|
18
|
+
|
|
19
|
+
onHide = async ({ isForce = false, ...args } = {}) => {
|
|
20
|
+
if (!this.state.isShow) return;
|
|
21
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "hide" } });
|
|
22
|
+
if (this.beforeHide) await this.beforeHide(args);
|
|
23
|
+
if (this.controls) await this.emit(this.EVENTS.CONTROLS.POP, { key: this.name });
|
|
24
|
+
await this.setShow(false, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
25
|
+
if (this.onClose) {
|
|
26
|
+
this.onClose();
|
|
27
|
+
delete this.onClose;
|
|
28
|
+
}
|
|
29
|
+
if (this.afterHide) await this.afterHide(args);
|
|
30
|
+
this.unsetCurrentItem();
|
|
31
|
+
};
|
|
32
|
+
onShow = async ({ isForce = false, onClose, ...args } = {}) => {
|
|
33
|
+
if (this.state.isShow) return;
|
|
34
|
+
this.currentItem = null;
|
|
35
|
+
this.state = { ...this.state, currentItem: null };
|
|
36
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "show" } });
|
|
37
|
+
if (onClose) this.onClose = onClose;
|
|
38
|
+
if (this.beforeShow) await this.beforeShow(args);
|
|
39
|
+
await this.setShow(true, Boolean(isForce || this.shared.viewForceAnimationSources.length));
|
|
40
|
+
if (this.controls)
|
|
41
|
+
await this.emit(this.EVENTS.CONTROLS.PUSH, { key: this.name, controls: this.controls, index: this.controlsIndex, checkNext: this.controlsCheckNext });
|
|
42
|
+
if (this.afterShow) await this.afterShow(args);
|
|
43
|
+
};
|
|
44
|
+
onHideForce = (args) => this.onHide({ ...args, isForce: true });
|
|
45
|
+
onShowForce = (args) => this.onShow({ ...args, isForce: true });
|
|
46
|
+
onUpdateStateFromGlobalState = ({ [this.name]: state } = {}) => this.updateStateAndViewForce(state);
|
|
47
|
+
|
|
48
|
+
setShow = (value, isForce = false) => this.updateStateAndView({ isShow: value }, isForce);
|
|
49
|
+
|
|
50
|
+
loadImageMedia = (name, mediaType, quality, isTmp, isConstant) => {
|
|
51
|
+
const [type, priority] = [this.CONST.MEDIA.TYPES.IMAGE, this.CONST.MEDIA.PRIORITIES.HIGH];
|
|
52
|
+
|
|
53
|
+
return this.emitOne(this.EVENTS.MEDIA.LOAD, { type, priority, quality, name, mediaType, isTmp, isConstant });
|
|
54
|
+
};
|
|
55
|
+
loadBgMedia = () => {
|
|
56
|
+
const [layer, quality] = [this.CONST.CANVAS.LAYERS.BG, this.shared.settings[this.SETTINGS.CANVAS.QUALITY]];
|
|
57
|
+
|
|
58
|
+
return this.loadImageMedia(this.bgName, layer, quality, false, true);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
updateView = (isForce = false, isFast = false) => this.emit(this.updateEvent, { ...this.state, isForce, isFast });
|
|
62
|
+
updateViewForce = () => this.emit(this.updateEvent, { ...this.state, isForce: true });
|
|
63
|
+
updateViewFast = () => this.emit(this.updateEvent, { ...this.state, isFast: true });
|
|
64
|
+
updateStateAndView = (state, isForce = false, isFast = false) => {
|
|
65
|
+
this.updateState(state);
|
|
66
|
+
|
|
67
|
+
return this.updateView(isForce, isFast);
|
|
68
|
+
};
|
|
69
|
+
updateStateAndViewForce = (state) => this.updateStateAndView(state, true, false);
|
|
70
|
+
updateStateAndViewFast = (state) => this.updateStateAndView(state, false, true);
|
|
71
|
+
|
|
72
|
+
incCurrentItem = () => this.setCurrentItem(this.currentItem + 1, 0);
|
|
73
|
+
decCurrentItem = () => this.setCurrentItem(this.currentItem - 1, this.maxCurrentItem);
|
|
74
|
+
setCurrentItem = async (value, defaultValue) => {
|
|
75
|
+
if (this.currentItem === null) {
|
|
76
|
+
this.currentItem = defaultValue;
|
|
77
|
+
} else {
|
|
78
|
+
this.currentItem = value;
|
|
79
|
+
if (this.currentItem > this.maxCurrentItem) this.currentItem = 0;
|
|
80
|
+
if (this.currentItem < 0) this.currentItem = this.maxCurrentItem;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.updateStateAndViewFast({ currentItem: this.currentItem });
|
|
84
|
+
};
|
|
85
|
+
unsetCurrentItem = () => {
|
|
86
|
+
this.currentItem = null;
|
|
87
|
+
this.updateStateAndViewFast({ currentItem: this.currentItem });
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
getDefaultState = () => ({ isShow: false, isForce: false });
|
|
91
|
+
}
|
package/modules/view.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class ModuleView extends Module {
|
|
4
|
+
animationTime = 0;
|
|
5
|
+
renderFunc = null;
|
|
6
|
+
updateEvent = null;
|
|
7
|
+
updateHandler = null;
|
|
8
|
+
canvasInfo = null;
|
|
9
|
+
locLabel = null;
|
|
10
|
+
|
|
11
|
+
subscribe = () => {
|
|
12
|
+
if (this.updateEvent && this.updateHandler) this.on(this.updateEvent, this.updateHandler);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
init = async () => {
|
|
16
|
+
if (this.renderFunc) await this.regComponent();
|
|
17
|
+
if (this.canvasInfo) await this.regCanvas();
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
regCanvas = async () => {
|
|
21
|
+
if (this.canvasInfo) await this.emit(this.EVENTS.CANVAS.CANVAS_REG, this.canvasInfo);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
regComponent = async () => {
|
|
25
|
+
this.component = await this.emitOne(this.EVENTS.COMPONENTS.REG, { name: this.name, renderFunc: this.renderFunc });
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
onUpdateReactComponent = async (state = {}) => {
|
|
29
|
+
if (!this.isReady) await this.waitIsReady();
|
|
30
|
+
|
|
31
|
+
await this.insertExtraDataToState(state);
|
|
32
|
+
|
|
33
|
+
return new Promise(async (resolve) => {
|
|
34
|
+
if (state.isForce) {
|
|
35
|
+
this.component.setState(state);
|
|
36
|
+
await this.waitRerender();
|
|
37
|
+
this.component.setState({ ...state, isForce: false });
|
|
38
|
+
await this.waitRerender();
|
|
39
|
+
return resolve();
|
|
40
|
+
}
|
|
41
|
+
this.component.setState(state);
|
|
42
|
+
|
|
43
|
+
return state.isFast ? resolve() : setTimeout(resolve, this.animationTime);
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
setIsForceReact = async (isForce) => {
|
|
48
|
+
this.component.setState({ ...this.state, isForce });
|
|
49
|
+
|
|
50
|
+
await this.waitRerender();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
insertExtraDataToState = async (state) => {
|
|
54
|
+
if (this.locLabel) state.locs = await this.emitOne(this.EVENTS.LOCS.GET, { label: this.locLabel, isConstant: true });
|
|
55
|
+
|
|
56
|
+
return state;
|
|
57
|
+
};
|
|
58
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/module.components",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"publish:major": "npm version major && npm publish --access public",
|
|
9
|
+
"publish:minor": "npm version minor && npm publish --access public",
|
|
10
|
+
"publish:patch": "npm version patch && npm publish --access public"
|
|
11
|
+
},
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@vnejs/module": "*"
|
|
16
|
+
}
|
|
17
|
+
}
|