-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawerModule.ts
90 lines (78 loc) · 3.13 KB
/
DrawerModule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import {DrawerContextItems} from '@klipper/bow/drawer/DrawerContextItems';
import {DrawerItem} from '@klipper/bow/drawer/DrawerItem';
import {DrawerModuleState} from '@klipper/bow/stores/drawer/DrawerModuleState';
import {DrawerState} from '@klipper/bow/stores/drawer/DrawerState';
import {Module, MutationTree} from 'vuex';
/**
* @author François Pluchino <[email protected]>
*/
export class DrawerModule<R extends DrawerModuleState, C extends DrawerContextItems> implements Module<DrawerState, R> {
private readonly storage: Storage;
private initContextItems: C|undefined;
public constructor(contextItems?: C, storage?: Storage) {
this.storage = storage ? storage : localStorage;
this.initContextItems = contextItems;
}
public get namespaced(): boolean {
return true;
}
public get state(): DrawerState<C> {
const contextItems = this.initContextItems;
if (this.initContextItems) {
this.initContextItems = undefined;
}
return {
mini: null === this.storage.getItem('drawer:mini')
? false
: 'true' === (this.storage.getItem('drawer:mini')),
show: null === this.storage.getItem('drawer:show')
? true
: 'true' === (this.storage.getItem('drawer:show')),
context: 'user',
temporary: false,
temporaryShow: false,
contextItems: contextItems || {
user: [] as DrawerItem[],
organization: [] as DrawerItem[],
} as C,
};
}
public get mutations(): MutationTree<DrawerState> {
const self = this;
return {
toggleMini(state: DrawerState, mini?: boolean): void {
state.mini = undefined === mini ? !state.mini : mini;
self.storage.setItem('drawer:mini', state.mini ? 'true' : 'false');
},
toggle(state: DrawerState, show?: boolean): void {
if (state.temporary) {
state.temporaryShow = undefined === show ? !state.temporaryShow : show;
return;
}
state.show = undefined === show ? !state.show : show;
self.storage.setItem('drawer:show', state.show ? 'true' : 'false');
},
setTemporary(state: DrawerState, temporary: boolean): void {
state.temporary = temporary;
},
setContext(state: DrawerState, context: string): void {
state.context = context;
},
setContextItems(state: DrawerState, contextItems: C): void {
state.contextItems = contextItems;
},
syncState(state: DrawerState, newState: DrawerState): void {
state.mini = newState.mini;
state.show = newState.show;
},
};
}
}