forked from neos/neos-ui
/
index.ts
120 lines (108 loc) · 3.96 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import produce from 'immer';
import {action as createAction, ActionType} from 'typesafe-actions';
import {InitAction} from '../../System';
import {NodeContextPath} from '@neos-project/neos-ts-interfaces';
import * as selectors from './selectors';
export interface State extends Readonly<{
toggled: NodeContextPath[];
isLoading: boolean;
loading: NodeContextPath[];
errors: NodeContextPath[];
}> {}
export const defaultState: State = {
toggled: [],
isLoading: false,
loading: [],
errors: []
};
//
// Export the action types
//
export enum actionTypes {
TOGGLE = '@neos/neos-ui/UI/ContentTree/TOGGLE',
START_LOADING = '@neos/neos-ui/UI/ContentTree/START_LOADING',
STOP_LOADING = '@neos/neos-ui/UI/ContentTree/STOP_LOADING',
RELOAD_TREE = '@neos/neos-ui/UI/ContentTree/RELOAD_TREE',
REQUEST_CHILDREN = '@neos/neos-ui/UI/ContentTree/REQUEST_CHILDREN',
SET_AS_LOADING = '@neos/neos-ui/UI/ContentTree/SET_AS_LOADING',
SET_AS_LOADED = '@neos/neos-ui/UI/ContentTree/SET_AS_LOADED',
COLLAPSE_ALL = '@neos/neos-ui/UI/ContentTree/COLLAPSE_ALL'
}
const toggle = (contextPath: NodeContextPath) => createAction(actionTypes.TOGGLE, contextPath);
const startLoading = () => createAction(actionTypes.START_LOADING);
const stopLoading = () => createAction(actionTypes.STOP_LOADING);
const reloadTree = () => createAction(actionTypes.RELOAD_TREE);
const requestChildren = (contextPath: NodeContextPath, {unCollapse = true, activate = false} = {}) => createAction(actionTypes.REQUEST_CHILDREN, {contextPath, opts: {unCollapse, activate}});
const setAsLoading = (contextPath: NodeContextPath) => createAction(actionTypes.SET_AS_LOADING, {contextPath});
const setAsLoaded = (contextPath: NodeContextPath) => createAction(actionTypes.SET_AS_LOADED, {contextPath});
const collapseAll = (
nodeContextPaths: NodeContextPath[],
collapsedByDefaultNodeContextPaths: NodeContextPath[]
) => createAction(actionTypes.COLLAPSE_ALL, {nodeContextPaths, collapsedByDefaultNodeContextPaths});
//
// Export the actions
//
export const actions = {
toggle,
startLoading,
stopLoading,
reloadTree,
requestChildren,
setAsLoading,
setAsLoaded,
collapseAll
};
export type Action = ActionType<typeof actions>;
//
// Export the reducer
//
export const reducer = (state: State = defaultState, action: InitAction | Action) => produce(state, draft => {
switch (action.type) {
case actionTypes.START_LOADING: {
draft.isLoading = true;
break;
}
case actionTypes.STOP_LOADING: {
draft.isLoading = false;
break;
}
case actionTypes.TOGGLE: {
const contextPath = action.payload;
if (draft.toggled.includes(contextPath)) {
draft.toggled = draft.toggled.filter(i => i !== contextPath);
} else {
draft.toggled.push(contextPath);
}
break;
}
case actionTypes.SET_AS_LOADING: {
const {contextPath} = action.payload;
draft.errors = draft.errors.filter(i => i !== contextPath);
draft.loading.push(contextPath);
break;
}
case actionTypes.SET_AS_LOADED: {
const {contextPath} = action.payload;
draft.loading = draft.loading.filter(i => i !== contextPath);
break;
}
case actionTypes.COLLAPSE_ALL: {
const {nodeContextPaths, collapsedByDefaultNodeContextPaths} = action.payload;
nodeContextPaths.forEach(path => {
if (!draft.toggled.includes(path)) {
draft.toggled.push(path);
}
});
collapsedByDefaultNodeContextPaths.forEach(path => {
if (draft.toggled.includes(path)) {
draft.toggled = draft.toggled.filter(i => i !== path);
}
});
break;
}
}
});
//
// Export the selectors
//
export {selectors};