forked from neos/neos-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
166 lines (153 loc) · 6.2 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import produce from 'immer';
import {action as createAction, ActionType} from 'typesafe-actions';
import {actionTypes as system, InitAction, GlobalState} from '../../System';
import {NodeContextPath, SelectionModeTypes} from '@neos-project/neos-ts-interfaces';
import * as selectors from './selectors';
import {calculateNewFocusedNodes} from '../../CR/Nodes/helpers';
export interface State extends Readonly<{
focused: NodeContextPath[];
toggled: NodeContextPath[];
hidden: NodeContextPath[];
intermediate: NodeContextPath[];
loading: NodeContextPath[];
errors: NodeContextPath[];
query: string;
filterNodeType: string;
}> {}
export const defaultState: State = {
focused: [],
toggled: [],
hidden: [],
intermediate: [],
loading: [],
errors: [],
query: '',
filterNodeType: ''
};
//
// Export the action types
//
export enum actionTypes {
FOCUS = '@neos/neos-ui/UI/PageTree/FOCUS',
TOGGLE = '@neos/neos-ui/UI/PageTree/TOGGLE',
INVALIDATE = '@neos/neos-ui/UI/PageTree/INVALIDATE',
SET_AS_LOADING = '@neos/neos-ui/UI/PageTree/SET_AS_LOADING',
SET_AS_LOADED = '@neos/neos-ui/UI/PageTree/SET_AS_LOADED',
REQUEST_CHILDREN = '@neos/neos-ui/UI/PageTree/REQUEST_CHILDREN',
COMMENCE_SEARCH = '@neos/neos-ui/UI/PageTree/COMMENCE_SEARCH',
SET_SEARCH_RESULT = '@neos/neos-ui/UI/PageTree/SET_SEARCH_RESULT',
COLLAPSE_ALL = '@neos/neos-ui/UI/PageTree/COLLAPSE_ALL'
}
const focus = (contextPath: NodeContextPath, _: undefined, selectionMode: SelectionModeTypes = SelectionModeTypes.SINGLE_SELECT) => createAction(actionTypes.FOCUS, {contextPath, selectionMode});
const toggle = (contextPath: NodeContextPath) => createAction(actionTypes.TOGGLE, {contextPath});
const invalidate = (contextPath: NodeContextPath) => createAction(actionTypes.INVALIDATE, {contextPath});
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});
interface CommenceSearchOptions extends Readonly<{
query: string;
filterNodeType: string;
}> {}
const commenceSearch = (contextPath: NodeContextPath, {query, filterNodeType}: CommenceSearchOptions) => createAction(actionTypes.COMMENCE_SEARCH, {contextPath, query, filterNodeType});
interface SearchResult extends Readonly<{
hiddenContextPaths: NodeContextPath[];
toggledContextPaths: NodeContextPath[];
intermediateContextPaths: NodeContextPath[];
}> {}
const setSearchResult = (result: SearchResult) => createAction(actionTypes.SET_SEARCH_RESULT, result);
//
// Export the actions
//
export const actions = {
focus,
toggle,
invalidate,
setAsLoading,
setAsLoaded,
requestChildren,
commenceSearch,
setSearchResult,
collapseAll
};
export type Action = ActionType<typeof actions>;
//
// Export the reducer
//
export const reducer = (state: State = defaultState, action: InitAction | Action, globalState: GlobalState) => produce(state, draft => {
switch (action.type) {
case system.INIT: {
const contextPath = action.payload.cr.nodes.documentNode || action.payload.cr.nodes.siteNode;
draft.focused = contextPath ? [contextPath] : [];
break;
}
case actionTypes.FOCUS: {
const {contextPath, selectionMode} = action.payload;
const newFocusedNodes = calculateNewFocusedNodes(selectionMode, contextPath, draft.focused, globalState.cr.nodes.byContextPath);
if (newFocusedNodes) {
draft.focused = newFocusedNodes;
}
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.INVALIDATE: {
const {contextPath} = action.payload;
draft.toggled = draft.toggled.filter(i => i !== contextPath);
draft.loading = draft.loading.filter(i => i !== contextPath);
draft.errors.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.SET_SEARCH_RESULT: {
draft.hidden = action.payload.hiddenContextPaths;
draft.toggled = action.payload.toggledContextPaths;
draft.intermediate = action.payload.intermediateContextPaths;
break;
}
case actionTypes.COMMENCE_SEARCH: {
// Store search arguments, to be used during tree reload
draft.query = action.payload.query;
draft.filterNodeType = action.payload.filterNodeType;
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};