@vnejs/plugins.views.screens.gallery 0.1.5

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.
@@ -0,0 +1,13 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ OPEN: "vne:gallery:open",
3
+ CHECK: "vne:gallery:check",
4
+ CLEAR: "vne:gallery:clear",
5
+
6
+ SHOW: "vne:gallery:show",
7
+ HIDE: "vne:gallery:hide",
8
+ PAGE_SET: "vne:gallery:page_set",
9
+ TYPE_SET: "vne:gallery:type_set",
10
+ UPDATE: "vne:gallery:update",
11
+ PRELOAD: "vne:gallery:preload",
12
+ CHOOSE: "vne:gallery:choose",
13
+ };
@@ -0,0 +1,32 @@
1
+ export const TABS = {};
2
+
3
+ export const BACKGROUND = "save";
4
+
5
+ export const TRANSITION = 300;
6
+ export const ZINDEX = 4000;
7
+
8
+ export const LOC_LABEL = "gallery";
9
+
10
+ const [ITEM_WIDTH, ITEM_HEIGHT, ITEM_GAP] = [800, 450, 120];
11
+
12
+ export const VIEW_PROPS = {
13
+ screen: { withBackgroundDark: true, zIndex: ZINDEX, transition: TRANSITION },
14
+ close: {
15
+ position: { right: 120, top: 90 },
16
+ svg: { size: 60 },
17
+ },
18
+ controls: {
19
+ position: { top: 240, left: 120 },
20
+ controls: { flexDirection: "column", flexGap: 60, textSize: 96 },
21
+ },
22
+ items: {
23
+ position: { top: 240, right: 120 },
24
+ flex: { gap: ITEM_GAP, withWrap: true, width: 2 * ITEM_GAP + 3 * ITEM_WIDTH },
25
+ stub: { width: ITEM_WIDTH, height: ITEM_HEIGHT },
26
+ item: { width: ITEM_WIDTH, height: ITEM_HEIGHT, withHover: true },
27
+ },
28
+ pages: { position: { bottom: 120, right: 120 } },
29
+ };
30
+
31
+ export const ROWS = 3;
32
+ export const COLUMNS = 3;
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import { SUBSCRIBE_EVENTS } from "./const/events";
4
+ import * as params from "./const/params";
5
+
6
+ import { GalleryController } from "./modules/controller";
7
+ import { Gallery } from "./modules/gallery";
8
+ import { GalleryView } from "./modules/view";
9
+
10
+ regPlugin("GALLERY", { params, events: SUBSCRIBE_EVENTS }, [GalleryController, Gallery, GalleryView]);
@@ -0,0 +1,165 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+
3
+ export class GalleryController extends ModuleController {
4
+ name = "gallery.controller";
5
+
6
+ updateEvent = this.EVENTS.GALLERY.UPDATE;
7
+
8
+ bgName = this.PARAMS.GALLERY.BACKGROUND;
9
+
10
+ maxPagesCount = {};
11
+
12
+ emitHide = () => this.emit(this.EVENTS.GALLERY.HIDE);
13
+
14
+ initRowAndColumns = () => this.updateStateAndViewFast({ currentRow: 0, currentColumn: 0 });
15
+ incRow = () => {
16
+ if (this.state.currentRow === null || this.state.currentColumn === null) return this.initRowAndColumns();
17
+
18
+ const maxRows = this.PARAMS.GALLERY.ROWS;
19
+
20
+ if (this.state.currentRow + 1 < maxRows) {
21
+ this.updateStateAndView({ currentRow: this.state.currentRow + 1 });
22
+ } else if (this.state.currentPage + 1 < this.state.pages) {
23
+ this.updateState({ currentRow: 0 });
24
+ this.updatePageAndType(this.state.currentPage + 1, false, true);
25
+ }
26
+ };
27
+ decRow = () => {
28
+ if (this.state.currentRow === null || this.state.currentColumn === null) return this.initRowAndColumns();
29
+
30
+ const maxRows = this.PARAMS.GALLERY.ROWS;
31
+
32
+ if (this.state.currentRow !== 0) {
33
+ this.updateStateAndView({ currentRow: this.state.currentRow - 1 });
34
+ } else if (this.state.currentPage > 0) {
35
+ this.updateState({ currentRow: maxRows - 1 });
36
+ this.updatePageAndType(this.state.currentPage - 1, false, true);
37
+ }
38
+ };
39
+ incColumn = () => {
40
+ if (this.state.currentRow === null || this.state.currentColumn === null) return this.initRowAndColumns();
41
+
42
+ const maxColumns = this.PARAMS.GALLERY.COLUMNS;
43
+
44
+ if (this.state.currentColumn + 1 < maxColumns) {
45
+ this.updateStateAndView({ currentColumn: this.state.currentColumn + 1 });
46
+ } else if (this.state.currentPage + 1 < this.state.pages) {
47
+ this.updateState({ currentColumn: 0 });
48
+ this.updatePageAndType(this.state.currentPage + 1, false, true);
49
+ }
50
+ };
51
+ decColumn = () => {
52
+ if (this.state.currentRow === null || this.state.currentColumn === null) return this.initRowAndColumns();
53
+
54
+ const maxColumns = this.PARAMS.GALLERY.COLUMNS;
55
+
56
+ if (this.state.currentColumn !== 0) {
57
+ this.updateStateAndView({ currentColumn: this.state.currentColumn - 1 });
58
+ } else if (this.state.currentPage > 0) {
59
+ this.updateState({ currentColumn: maxColumns - 1 });
60
+ this.updatePageAndType(this.state.currentPage - 1, false, true);
61
+ }
62
+ };
63
+ accept = () => this.emit(this.EVENTS.GALLERY.CHOOSE, { index: this.state.currentColumn + this.state.currentRow * this.PARAMS.GALLERY.COLUMNS });
64
+ onPrevTab = () => {
65
+ const keys = Object.keys(this.PARAMS.GALLERY.TABS);
66
+ const currentTypeIndex = keys.findIndex((key) => key === this.state.currentType);
67
+ const type = currentTypeIndex === 0 ? keys[keys.length - 1] : keys[(currentTypeIndex - 1) % keys.length];
68
+
69
+ this.emit(this.EVENTS.GALLERY.TYPE_SET, { type });
70
+ };
71
+ onNextTab = () => {
72
+ const keys = Object.keys(this.PARAMS.GALLERY.TABS);
73
+ const currentTypeIndex = keys.findIndex((key) => key === this.state.currentType);
74
+ const type = currentTypeIndex === keys.length - 1 ? keys[0] : keys[(currentTypeIndex + 1) % keys.length];
75
+
76
+ this.emit(this.EVENTS.GALLERY.TYPE_SET, { type });
77
+ };
78
+
79
+ controls = {
80
+ [this.CONST.CONTROLS.BUTTONS.BACK]: this.emitHide,
81
+ [this.CONST.CONTROLS.BUTTONS.MENU]: this.emitHide,
82
+ [this.CONST.CONTROLS.BUTTONS.GAMEMENU]: this.emitHide,
83
+ [this.CONST.CONTROLS.BUTTONS.ARROW_UP]: this.decRow,
84
+ [this.CONST.CONTROLS.BUTTONS.ARROW_RIGHT]: this.incColumn,
85
+ [this.CONST.CONTROLS.BUTTONS.ARROW_BOTTOM]: this.incRow,
86
+ [this.CONST.CONTROLS.BUTTONS.ARROW_LEFT]: this.decColumn,
87
+ [this.CONST.CONTROLS.BUTTONS.TAB_PREV]: this.onPrevTab,
88
+ [this.CONST.CONTROLS.BUTTONS.TAB_NEXT]: this.onNextTab,
89
+ [this.CONST.CONTROLS.BUTTONS.ACCEPT]: this.accept,
90
+ [this.CONST.CONTROLS.BUTTONS.INTERACT]: this.accept,
91
+ };
92
+ controlsIndex = this.PARAMS.GALLERY.ZINDEX;
93
+
94
+ subscribe = () => {
95
+ this.on(this.EVENTS.GALLERY.SHOW, this.onShow);
96
+ this.on(this.EVENTS.GALLERY.HIDE, this.onHide);
97
+ this.on(this.EVENTS.GALLERY.PAGE_SET, this.onPageSet);
98
+ this.on(this.EVENTS.GALLERY.TYPE_SET, this.onTypeSet);
99
+ this.on(this.EVENTS.GALLERY.PRELOAD, this.onPreload);
100
+
101
+ this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
102
+
103
+ this.on(this.EVENTS.STATE.CLEAR, this.onHide);
104
+
105
+ this.on(this.EVENTS.SYSTEM.STARTED, this.onStarted);
106
+ };
107
+ init = () => {
108
+ this.itemsOnPage = this.PARAMS.GALLERY.ROWS * this.PARAMS.GALLERY.COLUMNS;
109
+
110
+ this.updateState({ currentType: Object.keys(this.PARAMS.GALLERY.TABS)[0], currentPage: 0 });
111
+ };
112
+
113
+ beforeShow = async () => {
114
+ this.updateState({ bgSrc: (await this.loadBgMedia()).src, currentRow: null, currentColumn: null });
115
+
116
+ return this.updatePageAndType(0, Object.keys(this.PARAMS.GALLERY.TABS)[0], false);
117
+ };
118
+ afterHide = () => this.setDefaultState();
119
+
120
+ onStarted = () => Object.keys(this.PARAMS.GALLERY.TABS).forEach(this.calcMaxPageCount);
121
+ onPreload = () => Promise.all([this.loadBgMedia(), this.getItemsInfo(Object.keys(this.PARAMS.GALLERY.TABS)[0], 0).map(this.mapTabContent)]);
122
+ onMemoryCleared = () => {
123
+ if (!this.state.isShow) return;
124
+
125
+ const { currentPage = 0, currentType = "" } = this.state;
126
+
127
+ const promises = [this.loadBgMedia(), ...this.getItemsInfo(currentType, currentPage).map(this.mapTabContent)];
128
+
129
+ if (currentPage > 0) promises.push(...this.getItemsInfo(currentType, currentPage - 1).map(this.mapTabContent));
130
+ if (currentPage + 1 < this.maxPagesCount[currentType]) promises.push(...this.getItemsInfo(currentType, currentPage + 1).map(this.mapTabContent));
131
+
132
+ Object.keys(this.PARAMS.GALLERY.TABS).forEach((type) => type !== currentType && promises.push(...this.getItemsInfo(type, 0).map(this.mapTabContent)));
133
+
134
+ return Promise.all(promises);
135
+ };
136
+
137
+ onPageSet = ({ page } = {}) => page !== this.state.currentPage && this.updatePageAndType(page);
138
+ onTypeSet = ({ type } = {}) => type !== this.state.currentType && this.updatePageAndType(0, type);
139
+
140
+ updatePageAndType = async (page, type, isUpdateView = true) => {
141
+ this.updateState({ currentPage: page });
142
+ if (type) this.updateState({ currentType: type, pages: this.maxPagesCount[type], currentColumn: null, currentRow: null });
143
+
144
+ const currentItems = await Promise.all(this.getItemsInfo(this.state.currentType, this.state.currentPage).map(this.mapTabContent));
145
+
146
+ while (currentItems.length < this.itemsOnPage) currentItems.push({ isStub: true });
147
+ this.updateState({ currentItems });
148
+ if (isUpdateView) await this.updateViewFast();
149
+
150
+ this.emit(this.EVENTS.MEMORY.CLEAR);
151
+ };
152
+
153
+ mapTabContent = async ({ check, mediaType, name, fullName } = {}) => {
154
+ const isOpened = await this.emitOne(this.EVENTS.GALLERY.CHECK, { key: check });
155
+ const src = isOpened ? (await this.loadImageMedia(name, mediaType, this.shared.settings[this.SETTINGS.LAYER.QUALITY])).src : "";
156
+
157
+ return { isOpened, fullName, src, mediaType };
158
+ };
159
+
160
+ getItemsInfo = (type, page) => this.PARAMS.GALLERY.TABS[type].content.slice(page * this.itemsOnPage, (page + 1) * this.itemsOnPage);
161
+
162
+ calcMaxPageCount = (type) => {
163
+ this.maxPagesCount[type] = Math.ceil(this.PARAMS.GALLERY.TABS[type].content.length / this.itemsOnPage);
164
+ };
165
+ }
@@ -0,0 +1,19 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ const STORAGE_VALUE = 1;
4
+
5
+ const isEqualStorageValueFunc = (value) => value === STORAGE_VALUE;
6
+
7
+ export class Gallery extends Module {
8
+ name = "gallery";
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.GALLERY.OPEN, this.onOpen);
12
+ this.on(this.EVENTS.GALLERY.CHECK, this.onCheck);
13
+ this.on(this.EVENTS.GALLERY.CLEAR, this.onClear);
14
+ };
15
+
16
+ onOpen = ({ key } = {}) => this.emit(this.EVENTS.STORAGE.SET, { module: this.name, key, value: STORAGE_VALUE });
17
+ onCheck = ({ key } = {}) => this.emitOne(this.EVENTS.STORAGE.GET, { module: this.name, key }).then(isEqualStorageValueFunc);
18
+ onClear = () => this.emit(this.EVENTS.STORAGE.RM_ALL, { module: this.name });
19
+ }
@@ -0,0 +1,14 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+
3
+ import { render } from "../view";
4
+
5
+ export class GalleryView extends ModuleView {
6
+ name = "gallery.view";
7
+
8
+ locLabel = this.PARAMS.GALLERY.LOC_LABEL;
9
+ animationTime = this.PARAMS.GALLERY.TRANSITION;
10
+ updateEvent = this.EVENTS.GALLERY.UPDATE;
11
+
12
+ renderFunc = render;
13
+ updateHandler = this.onUpdateStoreComponent;
14
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.views.screens.gallery",
3
+ "version": "0.1.5",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major:plugin": "npm run publish:major",
8
+ "publish:minor:plugin": "npm run publish:minor",
9
+ "publish:patch:plugin": "npm run publish:patch",
10
+ "publish:major": "npm version major && npm publish --access public",
11
+ "publish:minor": "npm version minor && npm publish --access public",
12
+ "publish:patch": "npm version patch && npm publish --access public"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": ""
17
+ }
@@ -0,0 +1,15 @@
1
+ import { useCallback, useMemo } from "react";
2
+
3
+ import { PositionBox, Svg } from "@vnejs/uis.react";
4
+
5
+ export const GalleryClose = (props) => {
6
+ const onClick = useCallback(() => props.emit(props.EVENTS.GALLERY.HIDE));
7
+
8
+ const propsSvg = useMemo(() => ({ ...props.PARAMS.GALLERY.VIEW_PROPS.close.svg, onClick }), []);
9
+
10
+ return (
11
+ <PositionBox {...props.PARAMS.GALLERY.VIEW_PROPS.close.position}>
12
+ <Svg.Cross {...propsSvg} />
13
+ </PositionBox>
14
+ );
15
+ };
@@ -0,0 +1,18 @@
1
+ import { useCallback, useMemo } from "react";
2
+
3
+ import { PositionBox, TextControls } from "@vnejs/uis.react";
4
+
5
+ export const GalleryControls = ({ locs, currentType, ...props } = {}) => {
6
+ const texts = useMemo(() => Object.keys(props.PARAMS.GALLERY.TABS).map((key) => ({ text: locs[props.PARAMS.GALLERY.TABS[key].locKey], value: key })), [locs]);
7
+ const selectedIndex = useMemo(() => Object.keys(props.PARAMS.GALLERY.TABS).findIndex((key) => key === currentType), [currentType]);
8
+
9
+ const onClick = useCallback((type) => props.emit(props.EVENTS.GALLERY.TYPE_SET, { type }), []);
10
+
11
+ const propsControls = useMemo(() => ({ ...props.PARAMS.GALLERY.VIEW_PROPS.controls.controls, selectedIndex, texts, onClick }), [selectedIndex, texts]);
12
+
13
+ return (
14
+ <PositionBox {...props.PARAMS.GALLERY.VIEW_PROPS.controls.position}>
15
+ <TextControls {...propsControls} />
16
+ </PositionBox>
17
+ );
18
+ };
@@ -0,0 +1,37 @@
1
+ import { useCallback } from "react";
2
+
3
+ import { Flex, PositionBox, WrapWithContent } from "@vnejs/uis.react";
4
+
5
+ export const GalleryItems = ({ currentItems = [], selectedIndex = -1, ...props } = {}) => {
6
+ const onSlotClick = useCallback((index) => props.emit(props.EVENTS.GALLERY.CHOOSE, { index }), []);
7
+
8
+ const renderSlot = useCallback(
9
+ (item, i) => {
10
+ if (item.isStub)
11
+ return (
12
+ <div
13
+ {...props.PARAMS.GALLERY.VIEW_PROPS.items.stub}
14
+ key={i}
15
+ />
16
+ );
17
+
18
+ return (
19
+ <WrapWithContent
20
+ {...props.PARAMS.GALLERY.VIEW_PROPS.items.item}
21
+ key={item.isOpened ? item.src : i}
22
+ value={i}
23
+ imageSrc={item.isOpened && item.src}
24
+ isSelected={selectedIndex === i}
25
+ onClick={item.isOpened ? onSlotClick : undefined}
26
+ />
27
+ );
28
+ },
29
+ [selectedIndex],
30
+ );
31
+
32
+ return (
33
+ <PositionBox {...props.PARAMS.GALLERY.VIEW_PROPS.items.position}>
34
+ <Flex {...props.PARAMS.GALLERY.VIEW_PROPS.items.flex}>{currentItems.map(renderSlot)}</Flex>
35
+ </PositionBox>
36
+ );
37
+ };
@@ -0,0 +1,16 @@
1
+ import { useCallback, useMemo } from "react";
2
+
3
+ import { PageSelector, PositionBox } from "@vnejs/uis.react";
4
+
5
+ export const GalleryPages = ({ pagesCount, currentPage, ...props } = {}) => {
6
+ const onPrevPage = useCallback(() => props.emit(props.EVENTS.GALLERY.PAGE_SET, { page: currentPage - 1 }), []);
7
+ const onNextPage = useCallback(() => props.emit(props.EVENTS.GALLERY.PAGE_SET, { page: currentPage + 1 }), []);
8
+
9
+ const propsSelector = useMemo(() => ({ onPrevPage, onNextPage, pagesCount, page: currentPage + 1, pagesMinCount: 1 }), [currentPage, pagesCount]);
10
+
11
+ return (
12
+ <PositionBox {...props.PARAMS.GALLERY.VIEW_PROPS.pages.position}>
13
+ <PageSelector {...propsSelector} />
14
+ </PositionBox>
15
+ );
16
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./GalleryClose";
2
+ export * from "./GalleryControls";
3
+ export * from "./GalleryItems";
4
+ export * from "./GalleryPages";
package/view/index.jsx ADDED
@@ -0,0 +1,33 @@
1
+ import { useEffect, useMemo, useState } from "react";
2
+ import { createRoot } from "react-dom/client";
3
+
4
+ import { Screen } from "@vnejs/uis.react";
5
+
6
+ import { GalleryClose, GalleryControls, GalleryItems, GalleryPages } from "./components";
7
+
8
+ const Gallery = ({ store, onMount, ...props } = {}) => {
9
+ const [state, setState] = useState({});
10
+
11
+ const { isShow = false, isForce = false, locs = {}, bgSrc = "" } = state;
12
+ const { currentType = "", currentItems = [], pages, currentColumn = null, currentRow = null, currentPage } = state;
13
+ const selectedIndex = currentColumn !== null && currentRow !== null ? currentColumn + currentRow * 3 : -1;
14
+
15
+ useEffect(() => store.subscribe(setState), []);
16
+ useEffect(() => void onMount(), []);
17
+
18
+ const propsScreen = useMemo(() => ({ ...props.PARAMS.GALLERY.VIEW_PROPS.screen, isShow, isForce, bgSrc }), [isShow, isForce, bgSrc]);
19
+ const propsControls = useMemo(() => ({ ...props, currentType, locs }), [currentType, locs]);
20
+ const propsPages = useMemo(() => ({ ...props, pagesCount: pages, currentPage }), [pages, currentPage]);
21
+ const propsItems = useMemo(() => ({ ...props, currentItems, currentType, selectedIndex, locs }), [currentItems, currentType, selectedIndex, locs]);
22
+
23
+ return (
24
+ <Screen {...propsScreen}>
25
+ <GalleryControls {...propsControls} />
26
+ <GalleryPages {...propsPages} />
27
+ <GalleryItems {...propsItems} />
28
+ <GalleryClose {...props} />
29
+ </Screen>
30
+ );
31
+ };
32
+
33
+ export const render = (props, root) => createRoot(root).render(<Gallery {...props} />);