import { store } from "quasar/wrappers"; import { ModeEntity, ModeTreeItemEntity } from "src/entities/ModeEntity"; import { WindowOpenNotifyEntity } from "src/entities/MultimediaWindowEntity"; import { PlanEntity, PlanTreeItemEntity } from "src/entities/PlanEntity"; import { SignalSourceEntity, SignalSourceTreeItemEntity, } from "src/entities/SignalSourceEntity"; import { InjectionKey, reactive } from "vue"; import { createStore, Store as VuexStore, useStore as vuexUseStore, } from "vuex"; // import example from './module-example' // import { ExampleStateInterface } from './module-example/state'; /* * If not building with SSR mode, you can * directly export the Store instantiation; * * The function below can be async too; either use * async/await or return a Promise which resolves * with the Store instance. */ export interface StateInterface { // Define your own store structure, using submodules if needed // example: ExampleStateInterface; // Declared as unknown to avoid linting issue. Best to strongly type as per the line above. initialized: boolean; signal_source_tree: SignalSourceTreeItemEntity[]; mode_tree: ModeTreeItemEntity[]; plan_tree: PlanTreeItemEntity[]; wall_row: number; wall_col: number; device_screen_width: number; device_screen_height: number; windows: WindowOpenNotifyEntity[]; } // provide typings for `this.$store` declare module "@vue/runtime-core" { interface ComponentCustomProperties { $store: VuexStore; } } class _TreeHelper { public static findNode(node: any, key: string, value: string): any { if (node && node.children && key && value != null && value != undefined) { if (node[key] == value) { return node; } for (let child of node.children) { if (child && child[key] == value) { return child; } else { let _r = _TreeHelper.findNode(child, key, value); if (_r) { return _r; } } } } } public static findNodeParent(node: any, key: string, value: string): any { if (node && node.children && key && value != null && value != undefined) { for (let child of node.children) { if (child && child[key] == value) { return node; } else { let _r = _TreeHelper.findNodeParent(child, key, value); if (_r) { return _r; } } } } } public static addTreeItem( tree: any, defaultItem: () => any, createTreeItem: ( uuid: string, parent: string, name: string, is_group: boolean ) => any, playload?: any ) { if (tree && playload) { const parent = playload.parent ?? ""; const is_group = playload.is_group ?? false; let item_data = playload.item_data ?? defaultItem(); let node = _TreeHelper.findNode(tree[0], "uuid", item_data.uuid); if (node) { return; } node = _TreeHelper.findNode(tree[0], "uuid", parent); if (node) { const node_item = createTreeItem( item_data.uuid, parent, item_data.name, is_group ); node_item.item_data = item_data; (node.children).push(node_item); } } } public static setTreeItem( tree: any, defaultItem: () => any, copy: (left: any, right: any) => void, playload?: any ) { if (tree && playload) { let item_data = playload.item_data ?? defaultItem(); let node = _TreeHelper.findNode(tree[0], "uuid", item_data.uuid); if (node) { copy(node.item_data, item_data); node.name = item_data.name; } } } public static deleteTreeItem(tree: any, playload?: any) { if (tree && playload && playload.uuid) { let parent = _TreeHelper.findNodeParent(tree[0], "uuid", playload.uuid); if (parent) { let node_index = (parent.children).findIndex( (item) => item && item.uuid == playload.uuid ); if (node_index != -1) { (parent.children).splice(node_index, 1); } } } } public static findGroupsByParent(parent: string, array: any[]) { return array.filter((item) => { return item && item.parent_uuid == parent; }); } public static findItemsByParent(parent: string, array: any[]) { return array.filter((item) => { return item && item.group_uuid == parent; }); } public static buildGroup( parent: any, createTreeItem: ( uuid?: string, parent?: string, name?: string, is_group?: boolean, item_data?: any, children?: any[] ) => any, group_array: any[], item_array: any[] ) { if (parent) { for (let item of _TreeHelper.findGroupsByParent( parent.uuid, group_array )) { const node_item = createTreeItem( item.uuid, parent.uuid, item.name, true ); node_item.item_data = item; parent.children.push(node_item); _TreeHelper.buildGroup( node_item, createTreeItem, group_array, item_array ); } for (let item of _TreeHelper.findItemsByParent(parent.uuid, item_array)) { const node_item = createTreeItem( item.uuid, parent.uuid, item.name, false, item ); parent.children.push(node_item); } } } public static buildTree( tree: any, group_name: string, items_name: string, createTreeItem: ( uuid?: string, parent?: string, name?: string, is_group?: boolean, item_data?: any, children?: any[] ) => any, playload?: any ) { if ( tree && playload && playload.response && playload.options && playload.options.$t ) { const $t = playload.options.$t; const groups = playload.response[group_name] ?? []; const items = playload.response[items_name] ?? []; const root = createTreeItem("", "", $t.t("root"), true); tree.splice(0, tree.length); tree.push(root); _TreeHelper.buildGroup(root, createTreeItem, groups, items); } else { console.error(playload); } } } // provide typings for `useStore` helper export const storeKey: InjectionKey> = Symbol("vuex-key"); export default store(function (/* { ssrContext } */) { const Store = createStore({ modules: { // example }, state: { // state initialized: false, signal_source_tree: [], mode_tree: [], plan_tree: [], wall_col: 1, wall_row: 1, device_screen_width: 1920, device_screen_height: 1080, windows: [], }, mutations: { setInitialized(state: StateInterface, playload?: any[]) { state.initialized = true; }, setWindowPropertys(state: StateInterface, playload?: any[]) { if (playload && Array.isArray(playload)) { for (let item of playload) { const window = item.window; const property_name = item.property_name; const value = item.value; try { if (window && property_name) { window[property_name] = value; } } catch (e) { console.log(e); } } } }, setWindowProperty(state: StateInterface, playload?: any) { const window = playload.window; const property_name = playload.property_name; const value = playload.value; if (window && property_name) { window[property_name] = value; } }, setWindows(state: StateInterface, playload?: WindowOpenNotifyEntity[]) { if (playload) { state.windows = playload; } }, clearWindows(state: StateInterface, playload?: any) { state.windows = []; }, pushWindow(state: StateInterface, playload?: WindowOpenNotifyEntity) { if (playload) { state.windows.push(playload); } }, removeWindow(state: StateInterface, playload?: any) { const window_id = playload.window_id; if (window_id) { let index = state.windows.findIndex( (item) => item && item.window_id == window_id ); if (index >= 0) { state.windows.splice(index, 1); } } else { console.log("window_id???"); } }, editWindow(state: StateInterface, playload?: any) { const uuid = playload.uuid; const new_window = playload.new_window as WindowOpenNotifyEntity; if (uuid && new_window) { let index = state.windows.findIndex( (item) => item && item.uuid == uuid ); if (index >= 0) { state.windows[index] = new_window; } } }, setWallCol(state: StateInterface, playload?: any) { const num = parseInt(playload); if (num != NaN && num > 0) { state.wall_col = num; } }, setWallRow(state: StateInterface, playload?: any) { const num = parseInt(playload); if (num != NaN && num > 0) { state.wall_row = num; } }, setDeviceScreenWidth(state: StateInterface, playload?: any) { const num = parseInt(playload); if (num != NaN && num > 0) { state.device_screen_width = num; } }, setDeviceScreenHeight(state: StateInterface, playload?: any) { const num = parseInt(playload); if (num != NaN && num > 0) { state.device_screen_height = num; } }, // signal source setSignalSourceTree( state: StateInterface, playload?: SignalSourceTreeItemEntity[] ) { if (playload) { state.signal_source_tree = playload; } }, clearSignalSourceTree(state: StateInterface, playload?: any) { state.signal_source_tree = []; }, pushSignalSourceTreeItem( state: StateInterface, playload?: SignalSourceTreeItemEntity ) { if (playload) { state.signal_source_tree.push(playload); } }, addSignalSourceTreeItem(state: StateInterface, playload?: any) { if (playload) { const parent = playload.parent ?? ""; const is_group = playload.is_group ?? false; let item_data = playload.item_data ?? new SignalSourceEntity(); let node = _TreeHelper.findNode( state.signal_source_tree[0], "uuid", item_data.uuid ); if (node) { return; } node = _TreeHelper.findNode( state.signal_source_tree[0], "uuid", parent ); if (node) { const node_item = new SignalSourceTreeItemEntity( item_data.uuid, parent, item_data.name, is_group ); node_item.item_data = item_data; (node.children).push(node_item); } } }, setSignalSourceTreeItem(state: StateInterface, playload?: any) { if (playload) { let item_data = playload.item_data ?? new SignalSourceEntity(); let node = _TreeHelper.findNode( state.signal_source_tree[0], "uuid", item_data.uuid ); if (node) { SignalSourceEntity.copy(node.item_data, item_data); node.name = item_data.name; } } }, deleteSignalSourceTreeItem(state: StateInterface, playload?: any) { if (playload && playload.uuid) { let parent = _TreeHelper.findNodeParent( state.signal_source_tree[0], "uuid", playload.uuid ); if (parent) { let node_index = (parent.children).findIndex( (item) => item && item.uuid == playload.uuid ); if (node_index != -1) { (parent.children).splice(node_index, 1); } } } }, buildSignalSourceTree(state: StateInterface, playload?: any) { const buildGroup = ( parent: SignalSourceTreeItemEntity, group_array: any[], item_array: any[] ) => { if (parent) { for (let item of findGroupsByParent(parent.uuid, group_array)) { const node_item = new SignalSourceTreeItemEntity( item.uuid, parent.uuid, item.name, true ); node_item.item_data = item; parent.children.push(node_item); buildGroup(node_item, group_array, item_array); } for (let item of findItemsByParent(parent.uuid, item_array)) { const node_item = new SignalSourceTreeItemEntity( item.uuid, parent.uuid, item.name, false, item ); parent.children.push(node_item); } } }; const findGroupsByParent = (parent: string, array: any[]) => { return array.filter((item) => { return item && item.parent_uuid == parent; }); }; const findItemsByParent = (parent: string, array: any[]) => { return array.filter((item) => { return item && item.group_uuid == parent; }); }; if ( playload && playload.response && playload.options && playload.options.$t ) { const $t = playload.options.$t; const signal_source_groups = playload.response.signal_source_groups ?? []; const signal_sources = playload.response.signal_sources ?? []; const root = new SignalSourceTreeItemEntity( "", "", $t.t("root"), true ); state.signal_source_tree.splice(0, state.signal_source_tree.length); state.signal_source_tree.push(root); buildGroup(root, signal_source_groups, signal_sources); } else { console.error(playload); } }, //mode tree setModeTree(state: StateInterface, playload?: ModeTreeItemEntity[]) { if (playload) { state.mode_tree = playload; } }, clearModeTree(state: StateInterface, playload?: any) { state.mode_tree = []; }, pushModeTreeItem(state: StateInterface, playload?: ModeTreeItemEntity) { if (playload) { state.mode_tree.push(playload); } }, addModeTreeItem(state: StateInterface, playload?: any) { _TreeHelper.addTreeItem( state.mode_tree, () => new ModeEntity(), (uuid: string, parent: string, name: string, is_group: boolean) => new ModeTreeItemEntity(uuid, parent, name, is_group), playload ); }, setModeTreeItem(state: StateInterface, playload?: any) { _TreeHelper.setTreeItem( state.mode_tree, () => new ModeEntity(), (left: any, right: any) => ModeEntity.copy(left, right), playload ); }, deleteModeTreeItem(state: StateInterface, playload?: any) { _TreeHelper.deleteTreeItem(state.mode_tree, playload); }, buildModeTree(state: StateInterface, playload?: any) { _TreeHelper.buildTree( state.mode_tree, "mode_groups", "modes", ( uuid?: string, parent?: string, name?: string, is_group?: boolean, item_data?: any, children?: ModeTreeItemEntity[] ) => new ModeTreeItemEntity( uuid, parent, name, is_group, item_data, children ), playload ); }, // plan tree setPlanTree(state: StateInterface, playload?: PlanTreeItemEntity[]) { if (playload) { state.plan_tree = playload; } }, clearPlanTree(state: StateInterface, playload?: any) { state.plan_tree = []; }, pushPlanTreeItem(state: StateInterface, playload?: PlanTreeItemEntity) { if (playload) { state.plan_tree.push(playload); } }, addPlanTreeItem(state: StateInterface, playload?: any) { _TreeHelper.addTreeItem( state.plan_tree, () => new PlanEntity(), (uuid: string, parent: string, name: string, is_group: boolean) => new PlanTreeItemEntity(uuid, parent, name, is_group), playload ); }, setPlanTreeItem(state: StateInterface, playload?: any) { _TreeHelper.setTreeItem( state.plan_tree, () => new PlanEntity(), (left: any, right: any) => PlanEntity.copy(left, right), playload ); }, deletePlanTreeItem(state: StateInterface, playload?: any) { _TreeHelper.deleteTreeItem(state.plan_tree, playload); }, buildPlanTree(state: StateInterface, playload?: any) { _TreeHelper.buildTree( state.plan_tree, "plan_groups", "plans", ( uuid?: string, parent?: string, name?: string, is_group?: boolean, item_data?: any, children?: PlanTreeItemEntity[] ) => new PlanTreeItemEntity( uuid, parent, name, is_group, item_data, children ), playload ); }, }, // enable strict mode (adds overhead!) // for dev mode and --debug builds only strict: !!process.env.DEBUGGING, }); return Store; }); export function useStore() { return vuexUseStore(storeKey); }