forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.js
179 lines (161 loc) · 3.79 KB
/
controls.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* WordPress dependencies
*/
import { default as triggerApiFetch } from '@wordpress/api-fetch';
import { createRegistryControl } from '@wordpress/data';
/**
* Internal dependencies
*/
import { menuItemsQuery } from './utils';
/**
* Trigger an API Fetch request.
*
* @param {Object} request API Fetch Request Object.
* @return {Object} control descriptor.
*/
export function apiFetch( request ) {
return {
type: 'API_FETCH',
request,
};
}
/**
* Returns a list of pending actions for given post id.
*
* @param {number} postId Post ID.
* @return {Array} List of pending actions.
*/
export function getPendingActions( postId ) {
return {
type: 'GET_PENDING_ACTIONS',
postId,
};
}
/**
* Returns boolean indicating whether or not an action processing specified
* post is currently running.
*
* @param {number} postId Post ID.
* @return {Object} Action.
*/
export function isProcessingPost( postId ) {
return {
type: 'IS_PROCESSING_POST',
postId,
};
}
/**
* Selects menuItemId -> clientId mapping (necessary for saving the navigation).
*
* @param {number} postId Navigation post ID.
* @return {Object} Action.
*/
export function getMenuItemToClientIdMapping( postId ) {
return {
type: 'GET_MENU_ITEM_TO_CLIENT_ID_MAPPING',
postId,
};
}
/**
* Resolves navigation post for given menuId.
*
* @see selectors.js
* @param {number} menuId Menu ID.
* @return {Object} Action.
*/
export function getNavigationPostForMenu( menuId ) {
return {
type: 'SELECT',
registryName: 'core/edit-navigation',
selectorName: 'getNavigationPostForMenu',
args: [ menuId ],
};
}
/**
* Resolves menu items for given menu id.
*
* @param {number} menuId Menu ID.
* @return {Object} Action.
*/
export function resolveMenuItems( menuId ) {
return {
type: 'RESOLVE_MENU_ITEMS',
query: menuItemsQuery( menuId ),
};
}
/**
* Calls a selector using chosen registry.
*
* @param {string} registryName Registry name.
* @param {string} selectorName Selector name.
* @param {Array} args Selector arguments.
* @return {Object} control descriptor.
*/
export function select( registryName, selectorName, ...args ) {
return {
type: 'SELECT',
registryName,
selectorName,
args,
};
}
/**
* Dispatches an action using chosen registry.
*
* @param {string} registryName Registry name.
* @param {string} actionName Action name.
* @param {Array} args Selector arguments.
* @return {Object} control descriptor.
*/
export function dispatch( registryName, actionName, ...args ) {
return {
type: 'DISPATCH',
registryName,
actionName,
args,
};
}
const controls = {
API_FETCH( { request } ) {
return triggerApiFetch( request );
},
SELECT: createRegistryControl(
( registry ) => ( { registryName, selectorName, args } ) => {
return registry.select( registryName )[ selectorName ]( ...args );
}
),
GET_PENDING_ACTIONS: createRegistryControl(
( registry ) => ( { postId } ) => {
return (
getState( registry ).processingQueue[ postId ]
?.pendingActions || []
);
}
),
IS_PROCESSING_POST: createRegistryControl(
( registry ) => ( { postId } ) => {
return !! getState( registry ).processingQueue[ postId ]
?.inProgress;
}
),
GET_MENU_ITEM_TO_CLIENT_ID_MAPPING: createRegistryControl(
( registry ) => ( { postId } ) => {
return getState( registry ).mapping[ postId ] || {};
}
),
DISPATCH: createRegistryControl(
( registry ) => ( { registryName, actionName, args } ) => {
return registry.dispatch( registryName )[ actionName ]( ...args );
}
),
RESOLVE_MENU_ITEMS: createRegistryControl(
( registry ) => ( { query } ) => {
return registry
.__experimentalResolveSelect( 'core' )
.getMenuItems( query );
}
),
};
const getState = ( registry ) =>
registry.stores[ 'core/edit-navigation' ].store.getState();
export default controls;