data-controls.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. this["wp"] = this["wp"] || {}; this["wp"]["dataControls"] =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = 317);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ 17:
  90. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  91. "use strict";
  92. // CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/arrayWithoutHoles.js
  93. function _arrayWithoutHoles(arr) {
  94. if (Array.isArray(arr)) {
  95. for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
  96. arr2[i] = arr[i];
  97. }
  98. return arr2;
  99. }
  100. }
  101. // EXTERNAL MODULE: ./node_modules/@babel/runtime/helpers/esm/iterableToArray.js
  102. var iterableToArray = __webpack_require__(30);
  103. // CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/nonIterableSpread.js
  104. function _nonIterableSpread() {
  105. throw new TypeError("Invalid attempt to spread non-iterable instance");
  106. }
  107. // CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/toConsumableArray.js
  108. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _toConsumableArray; });
  109. function _toConsumableArray(arr) {
  110. return _arrayWithoutHoles(arr) || Object(iterableToArray["a" /* default */])(arr) || _nonIterableSpread();
  111. }
  112. /***/ }),
  113. /***/ 30:
  114. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  115. "use strict";
  116. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _iterableToArray; });
  117. function _iterableToArray(iter) {
  118. if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
  119. }
  120. /***/ }),
  121. /***/ 317:
  122. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  123. "use strict";
  124. __webpack_require__.r(__webpack_exports__);
  125. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "apiFetch", function() { return apiFetch; });
  126. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "select", function() { return select; });
  127. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "dispatch", function() { return dispatch; });
  128. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "controls", function() { return controls; });
  129. /* harmony import */ var _babel_runtime_helpers_esm_toConsumableArray__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(17);
  130. /* harmony import */ var _wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(34);
  131. /* harmony import */ var _wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_1__);
  132. /* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4);
  133. /* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_wordpress_data__WEBPACK_IMPORTED_MODULE_2__);
  134. /**
  135. * WordPress dependencies
  136. */
  137. /**
  138. * Dispatches a control action for triggering an api fetch call.
  139. *
  140. * @param {Object} request Arguments for the fetch request.
  141. *
  142. * @example
  143. * ```js
  144. * import { apiFetch } from '@wordpress/data-controls';
  145. *
  146. * // Action generator using apiFetch
  147. * export function* myAction {
  148. * const path = '/v2/my-api/items';
  149. * const items = yield apiFetch( { path } );
  150. * // do something with the items.
  151. * }
  152. * ```
  153. *
  154. * @return {Object} The control descriptor.
  155. */
  156. var apiFetch = function apiFetch(request) {
  157. return {
  158. type: 'API_FETCH',
  159. request: request
  160. };
  161. };
  162. /**
  163. * Dispatches a control action for triggering a registry select.
  164. *
  165. * Note: when this control action is handled, it automatically considers
  166. * selectors that may have a resolver. It will await and return the resolved
  167. * value when the selector has not been resolved yet.
  168. *
  169. * @param {string} storeKey The key for the store the selector belongs to
  170. * @param {string} selectorName The name of the selector
  171. * @param {Array} args Arguments for the select.
  172. *
  173. * @example
  174. * ```js
  175. * import { select } from '@wordpress/data-controls';
  176. *
  177. * // Action generator using select
  178. * export function* myAction {
  179. * const isSidebarOpened = yield select( 'core/edit-post', 'isEditorSideBarOpened' );
  180. * // do stuff with the result from the select.
  181. * }
  182. * ```
  183. *
  184. * @return {Object} The control descriptor.
  185. */
  186. function select(storeKey, selectorName) {
  187. for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  188. args[_key - 2] = arguments[_key];
  189. }
  190. return {
  191. type: 'SELECT',
  192. storeKey: storeKey,
  193. selectorName: selectorName,
  194. args: args
  195. };
  196. }
  197. /**
  198. * Dispatches a control action for triggering a registry dispatch.
  199. *
  200. * @param {string} storeKey The key for the store the action belongs to
  201. * @param {string} actionName The name of the action to dispatch
  202. * @param {Array} args Arguments for the dispatch action.
  203. *
  204. * @example
  205. * ```js
  206. * import { dispatch } from '@wordpress/data-controls';
  207. *
  208. * // Action generator using dispatch
  209. * export function* myAction {
  210. * yield dispatch( 'core/edit-post' ).togglePublishSidebar();
  211. * // do some other things.
  212. * }
  213. * ```
  214. *
  215. * @return {Object} The control descriptor.
  216. */
  217. function dispatch(storeKey, actionName) {
  218. for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
  219. args[_key2 - 2] = arguments[_key2];
  220. }
  221. return {
  222. type: 'DISPATCH',
  223. storeKey: storeKey,
  224. actionName: actionName,
  225. args: args
  226. };
  227. }
  228. /**
  229. * Utility for returning a promise that handles a selector with a resolver.
  230. *
  231. * @param {Object} registry The data registry.
  232. * @param {Object} options
  233. * @param {string} options.storeKey The store the selector belongs to
  234. * @param {string} options.selectorName The selector name
  235. * @param {Array} options.args The arguments fed to the selector
  236. *
  237. * @return {Promise} A promise for resolving the given selector.
  238. */
  239. var resolveSelect = function resolveSelect(registry, _ref) {
  240. var storeKey = _ref.storeKey,
  241. selectorName = _ref.selectorName,
  242. args = _ref.args;
  243. return new Promise(function (resolve) {
  244. var hasFinished = function hasFinished() {
  245. return registry.select('core/data').hasFinishedResolution(storeKey, selectorName, args);
  246. };
  247. var getResult = function getResult() {
  248. return registry.select(storeKey)[selectorName].apply(null, args);
  249. }; // trigger the selector (to trigger the resolver)
  250. var result = getResult();
  251. if (hasFinished()) {
  252. return resolve(result);
  253. }
  254. var unsubscribe = registry.subscribe(function () {
  255. if (hasFinished()) {
  256. unsubscribe();
  257. resolve(getResult());
  258. }
  259. });
  260. });
  261. };
  262. /**
  263. * The default export is what you use to register the controls with your custom
  264. * store.
  265. *
  266. * @example
  267. * ```js
  268. * // WordPress dependencies
  269. * import { controls } from '@wordpress/data-controls';
  270. * import { registerStore } from '@wordpress/data';
  271. *
  272. * // Internal dependencies
  273. * import reducer from './reducer';
  274. * import * as selectors from './selectors';
  275. * import * as actions from './actions';
  276. * import * as resolvers from './resolvers';
  277. *
  278. * registerStore ( 'my-custom-store', {
  279. * reducer,
  280. * controls,
  281. * actions,
  282. * selectors,
  283. * resolvers,
  284. * } );
  285. * ```
  286. *
  287. * @return {Object} An object for registering the default controls with the
  288. * store.
  289. */
  290. var controls = {
  291. API_FETCH: function API_FETCH(_ref2) {
  292. var request = _ref2.request;
  293. return _wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_1___default()(request);
  294. },
  295. SELECT: Object(_wordpress_data__WEBPACK_IMPORTED_MODULE_2__["createRegistryControl"])(function (registry) {
  296. return function (_ref3) {
  297. var _registry$select;
  298. var storeKey = _ref3.storeKey,
  299. selectorName = _ref3.selectorName,
  300. args = _ref3.args;
  301. return registry.select(storeKey)[selectorName].hasResolver ? resolveSelect(registry, {
  302. storeKey: storeKey,
  303. selectorName: selectorName,
  304. args: args
  305. }) : (_registry$select = registry.select(storeKey))[selectorName].apply(_registry$select, Object(_babel_runtime_helpers_esm_toConsumableArray__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(args));
  306. };
  307. }),
  308. DISPATCH: Object(_wordpress_data__WEBPACK_IMPORTED_MODULE_2__["createRegistryControl"])(function (registry) {
  309. return function (_ref4) {
  310. var _registry$dispatch;
  311. var storeKey = _ref4.storeKey,
  312. actionName = _ref4.actionName,
  313. args = _ref4.args;
  314. return (_registry$dispatch = registry.dispatch(storeKey))[actionName].apply(_registry$dispatch, Object(_babel_runtime_helpers_esm_toConsumableArray__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(args));
  315. };
  316. })
  317. };
  318. /***/ }),
  319. /***/ 34:
  320. /***/ (function(module, exports) {
  321. (function() { module.exports = this["wp"]["apiFetch"]; }());
  322. /***/ }),
  323. /***/ 4:
  324. /***/ (function(module, exports) {
  325. (function() { module.exports = this["wp"]["data"]; }());
  326. /***/ })
  327. /******/ });