[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> notices.js (source)

   1  /******/ (() => { // webpackBootstrap
   2  /******/     "use strict";
   3  /******/     // The require scope
   4  /******/     var __webpack_require__ = {};
   5  /******/     
   6  /************************************************************************/
   7  /******/     /* webpack/runtime/define property getters */
   8  /******/     (() => {
   9  /******/         // define getter functions for harmony exports
  10  /******/         __webpack_require__.d = (exports, definition) => {
  11  /******/             for(var key in definition) {
  12  /******/                 if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13  /******/                     Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14  /******/                 }
  15  /******/             }
  16  /******/         };
  17  /******/     })();
  18  /******/     
  19  /******/     /* webpack/runtime/hasOwnProperty shorthand */
  20  /******/     (() => {
  21  /******/         __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
  22  /******/     })();
  23  /******/     
  24  /******/     /* webpack/runtime/make namespace object */
  25  /******/     (() => {
  26  /******/         // define __esModule on exports
  27  /******/         __webpack_require__.r = (exports) => {
  28  /******/             if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  29  /******/                 Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  30  /******/             }
  31  /******/             Object.defineProperty(exports, '__esModule', { value: true });
  32  /******/         };
  33  /******/     })();
  34  /******/     
  35  /************************************************************************/
  36  var __webpack_exports__ = {};
  37  // ESM COMPAT FLAG
  38  __webpack_require__.r(__webpack_exports__);
  39  
  40  // EXPORTS
  41  __webpack_require__.d(__webpack_exports__, {
  42    store: () => (/* reexport */ store)
  43  });
  44  
  45  // NAMESPACE OBJECT: ./node_modules/@wordpress/notices/build-module/store/actions.js
  46  var actions_namespaceObject = {};
  47  __webpack_require__.r(actions_namespaceObject);
  48  __webpack_require__.d(actions_namespaceObject, {
  49    createErrorNotice: () => (createErrorNotice),
  50    createInfoNotice: () => (createInfoNotice),
  51    createNotice: () => (createNotice),
  52    createSuccessNotice: () => (createSuccessNotice),
  53    createWarningNotice: () => (createWarningNotice),
  54    removeAllNotices: () => (removeAllNotices),
  55    removeNotice: () => (removeNotice),
  56    removeNotices: () => (removeNotices)
  57  });
  58  
  59  // NAMESPACE OBJECT: ./node_modules/@wordpress/notices/build-module/store/selectors.js
  60  var selectors_namespaceObject = {};
  61  __webpack_require__.r(selectors_namespaceObject);
  62  __webpack_require__.d(selectors_namespaceObject, {
  63    getNotices: () => (getNotices)
  64  });
  65  
  66  ;// CONCATENATED MODULE: external ["wp","data"]
  67  const external_wp_data_namespaceObject = window["wp"]["data"];
  68  ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/utils/on-sub-key.js
  69  /**
  70   * Higher-order reducer creator which creates a combined reducer object, keyed
  71   * by a property on the action object.
  72   *
  73   * @param {string} actionProperty Action property by which to key object.
  74   *
  75   * @return {Function} Higher-order reducer.
  76   */
  77  const onSubKey = actionProperty => reducer => (state = {}, action) => {
  78    // Retrieve subkey from action. Do not track if undefined; useful for cases
  79    // where reducer is scoped by action shape.
  80    const key = action[actionProperty];
  81    if (key === undefined) {
  82      return state;
  83    }
  84  
  85    // Avoid updating state if unchanged. Note that this also accounts for a
  86    // reducer which returns undefined on a key which is not yet tracked.
  87    const nextKeyState = reducer(state[key], action);
  88    if (nextKeyState === state[key]) {
  89      return state;
  90    }
  91    return {
  92      ...state,
  93      [key]: nextKeyState
  94    };
  95  };
  96  /* harmony default export */ const on_sub_key = (onSubKey);
  97  
  98  ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/reducer.js
  99  /**
 100   * Internal dependencies
 101   */
 102  
 103  
 104  /**
 105   * Reducer returning the next notices state. The notices state is an object
 106   * where each key is a context, its value an array of notice objects.
 107   *
 108   * @param {Object} state  Current state.
 109   * @param {Object} action Dispatched action.
 110   *
 111   * @return {Object} Updated state.
 112   */
 113  const notices = on_sub_key('context')((state = [], action) => {
 114    switch (action.type) {
 115      case 'CREATE_NOTICE':
 116        // Avoid duplicates on ID.
 117        return [...state.filter(({
 118          id
 119        }) => id !== action.notice.id), action.notice];
 120      case 'REMOVE_NOTICE':
 121        return state.filter(({
 122          id
 123        }) => id !== action.id);
 124      case 'REMOVE_NOTICES':
 125        return state.filter(({
 126          id
 127        }) => !action.ids.includes(id));
 128      case 'REMOVE_ALL_NOTICES':
 129        return state.filter(({
 130          type
 131        }) => type !== action.noticeType);
 132    }
 133    return state;
 134  });
 135  /* harmony default export */ const reducer = (notices);
 136  
 137  ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/constants.js
 138  /**
 139   * Default context to use for notice grouping when not otherwise specified. Its
 140   * specific value doesn't hold much meaning, but it must be reasonably unique
 141   * and, more importantly, referenced consistently in the store implementation.
 142   *
 143   * @type {string}
 144   */
 145  const DEFAULT_CONTEXT = 'global';
 146  
 147  /**
 148   * Default notice status.
 149   *
 150   * @type {string}
 151   */
 152  const DEFAULT_STATUS = 'info';
 153  
 154  ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/actions.js
 155  /**
 156   * Internal dependencies
 157   */
 158  
 159  
 160  /**
 161   * @typedef {Object} WPNoticeAction Object describing a user action option associated with a notice.
 162   *
 163   * @property {string}    label   Message to use as action label.
 164   * @property {?string}   url     Optional URL of resource if action incurs
 165   *                               browser navigation.
 166   * @property {?Function} onClick Optional function to invoke when action is
 167   *                               triggered by user.
 168   */
 169  
 170  let uniqueId = 0;
 171  
 172  /**
 173   * Returns an action object used in signalling that a notice is to be created.
 174   *
 175   * @param {string|undefined}      status                       Notice status ("info" if undefined is passed).
 176   * @param {string}                content                      Notice message.
 177   * @param {Object}                [options]                    Notice options.
 178   * @param {string}                [options.context='global']   Context under which to
 179   *                                                             group notice.
 180   * @param {string}                [options.id]                 Identifier for notice.
 181   *                                                             Automatically assigned
 182   *                                                             if not specified.
 183   * @param {boolean}               [options.isDismissible=true] Whether the notice can
 184   *                                                             be dismissed by user.
 185   * @param {string}                [options.type='default']     Type of notice, one of
 186   *                                                             `default`, or `snackbar`.
 187   * @param {boolean}               [options.speak=true]         Whether the notice
 188   *                                                             content should be
 189   *                                                             announced to screen
 190   *                                                             readers.
 191   * @param {Array<WPNoticeAction>} [options.actions]            User actions to be
 192   *                                                             presented with notice.
 193   * @param {string}                [options.icon]               An icon displayed with the notice.
 194   *                                                             Only used when type is set to `snackbar`.
 195   * @param {boolean}               [options.explicitDismiss]    Whether the notice includes
 196   *                                                             an explicit dismiss button and
 197   *                                                             can't be dismissed by clicking
 198   *                                                             the body of the notice. Only applies
 199   *                                                             when type is set to `snackbar`.
 200   * @param {Function}              [options.onDismiss]          Called when the notice is dismissed.
 201   *
 202   * @example
 203   * ```js
 204   * import { __ } from '@wordpress/i18n';
 205   * import { useDispatch } from '@wordpress/data';
 206   * import { store as noticesStore } from '@wordpress/notices';
 207   * import { Button } from '@wordpress/components';
 208   *
 209   * const ExampleComponent = () => {
 210   *     const { createNotice } = useDispatch( noticesStore );
 211   *     return (
 212   *         <Button
 213   *             onClick={ () => createNotice( 'success', __( 'Notice message' ) ) }
 214   *         >
 215   *             { __( 'Generate a success notice!' ) }
 216   *         </Button>
 217   *     );
 218   * };
 219   * ```
 220   *
 221   * @return {Object} Action object.
 222   */
 223  function createNotice(status = DEFAULT_STATUS, content, options = {}) {
 224    const {
 225      speak = true,
 226      isDismissible = true,
 227      context = DEFAULT_CONTEXT,
 228      id = `$context}${++uniqueId}`,
 229      actions = [],
 230      type = 'default',
 231      __unstableHTML,
 232      icon = null,
 233      explicitDismiss = false,
 234      onDismiss
 235    } = options;
 236  
 237    // The supported value shape of content is currently limited to plain text
 238    // strings. To avoid setting expectation that e.g. a React Element could be
 239    // supported, cast to a string.
 240    content = String(content);
 241    return {
 242      type: 'CREATE_NOTICE',
 243      context,
 244      notice: {
 245        id,
 246        status,
 247        content,
 248        spokenMessage: speak ? content : null,
 249        __unstableHTML,
 250        isDismissible,
 251        actions,
 252        type,
 253        icon,
 254        explicitDismiss,
 255        onDismiss
 256      }
 257    };
 258  }
 259  
 260  /**
 261   * Returns an action object used in signalling that a success notice is to be
 262   * created. Refer to `createNotice` for options documentation.
 263   *
 264   * @see createNotice
 265   *
 266   * @param {string} content   Notice message.
 267   * @param {Object} [options] Optional notice options.
 268   *
 269   * @example
 270   * ```js
 271   * import { __ } from '@wordpress/i18n';
 272   * import { useDispatch } from '@wordpress/data';
 273   * import { store as noticesStore } from '@wordpress/notices';
 274   * import { Button } from '@wordpress/components';
 275   *
 276   * const ExampleComponent = () => {
 277   *     const { createSuccessNotice } = useDispatch( noticesStore );
 278   *     return (
 279   *         <Button
 280   *             onClick={ () =>
 281   *                 createSuccessNotice( __( 'Success!' ), {
 282   *                     type: 'snackbar',
 283   *                     icon: '🔥',
 284   *                 } )
 285   *             }
 286   *         >
 287   *             { __( 'Generate a snackbar success notice!' ) }
 288   *        </Button>
 289   *     );
 290   * };
 291   * ```
 292   *
 293   * @return {Object} Action object.
 294   */
 295  function createSuccessNotice(content, options) {
 296    return createNotice('success', content, options);
 297  }
 298  
 299  /**
 300   * Returns an action object used in signalling that an info notice is to be
 301   * created. Refer to `createNotice` for options documentation.
 302   *
 303   * @see createNotice
 304   *
 305   * @param {string} content   Notice message.
 306   * @param {Object} [options] Optional notice options.
 307   *
 308   * @example
 309   * ```js
 310   * import { __ } from '@wordpress/i18n';
 311   * import { useDispatch } from '@wordpress/data';
 312   * import { store as noticesStore } from '@wordpress/notices';
 313   * import { Button } from '@wordpress/components';
 314   *
 315   * const ExampleComponent = () => {
 316   *     const { createInfoNotice } = useDispatch( noticesStore );
 317   *     return (
 318   *         <Button
 319   *             onClick={ () =>
 320   *                createInfoNotice( __( 'Something happened!' ), {
 321   *                   isDismissible: false,
 322   *                } )
 323   *             }
 324   *         >
 325   *         { __( 'Generate a notice that cannot be dismissed.' ) }
 326   *       </Button>
 327   *       );
 328   * };
 329   *```
 330   *
 331   * @return {Object} Action object.
 332   */
 333  function createInfoNotice(content, options) {
 334    return createNotice('info', content, options);
 335  }
 336  
 337  /**
 338   * Returns an action object used in signalling that an error notice is to be
 339   * created. Refer to `createNotice` for options documentation.
 340   *
 341   * @see createNotice
 342   *
 343   * @param {string} content   Notice message.
 344   * @param {Object} [options] Optional notice options.
 345   *
 346   * @example
 347   * ```js
 348   * import { __ } from '@wordpress/i18n';
 349   * import { useDispatch } from '@wordpress/data';
 350   * import { store as noticesStore } from '@wordpress/notices';
 351   * import { Button } from '@wordpress/components';
 352   *
 353   * const ExampleComponent = () => {
 354   *     const { createErrorNotice } = useDispatch( noticesStore );
 355   *     return (
 356   *         <Button
 357   *             onClick={ () =>
 358   *                 createErrorNotice( __( 'An error occurred!' ), {
 359   *                     type: 'snackbar',
 360   *                     explicitDismiss: true,
 361   *                 } )
 362   *             }
 363   *         >
 364   *             { __(
 365   *                 'Generate an snackbar error notice with explicit dismiss button.'
 366   *             ) }
 367   *         </Button>
 368   *     );
 369   * };
 370   * ```
 371   *
 372   * @return {Object} Action object.
 373   */
 374  function createErrorNotice(content, options) {
 375    return createNotice('error', content, options);
 376  }
 377  
 378  /**
 379   * Returns an action object used in signalling that a warning notice is to be
 380   * created. Refer to `createNotice` for options documentation.
 381   *
 382   * @see createNotice
 383   *
 384   * @param {string} content   Notice message.
 385   * @param {Object} [options] Optional notice options.
 386   *
 387   * @example
 388   * ```js
 389   * import { __ } from '@wordpress/i18n';
 390   * import { useDispatch } from '@wordpress/data';
 391   * import { store as noticesStore } from '@wordpress/notices';
 392   * import { Button } from '@wordpress/components';
 393   *
 394   * const ExampleComponent = () => {
 395   *     const { createWarningNotice, createInfoNotice } = useDispatch( noticesStore );
 396   *     return (
 397   *         <Button
 398   *             onClick={ () =>
 399   *                 createWarningNotice( __( 'Warning!' ), {
 400   *                     onDismiss: () => {
 401   *                         createInfoNotice(
 402   *                             __( 'The warning has been dismissed!' )
 403   *                         );
 404   *                     },
 405   *                 } )
 406   *             }
 407   *         >
 408   *             { __( 'Generates a warning notice with onDismiss callback' ) }
 409   *         </Button>
 410   *     );
 411   * };
 412   * ```
 413   *
 414   * @return {Object} Action object.
 415   */
 416  function createWarningNotice(content, options) {
 417    return createNotice('warning', content, options);
 418  }
 419  
 420  /**
 421   * Returns an action object used in signalling that a notice is to be removed.
 422   *
 423   * @param {string} id                 Notice unique identifier.
 424   * @param {string} [context='global'] Optional context (grouping) in which the notice is
 425   *                                    intended to appear. Defaults to default context.
 426   *
 427   * @example
 428   * ```js
 429   * import { __ } from '@wordpress/i18n';
 430   * import { useDispatch } from '@wordpress/data';
 431   * import { store as noticesStore } from '@wordpress/notices';
 432   * import { Button } from '@wordpress/components';
 433   *
 434   * const ExampleComponent = () => {
 435   *    const notices = useSelect( ( select ) => select( noticesStore ).getNotices() );
 436   *    const { createWarningNotice, removeNotice } = useDispatch( noticesStore );
 437   *
 438   *    return (
 439   *         <>
 440   *             <Button
 441   *                 onClick={ () =>
 442   *                     createWarningNotice( __( 'Warning!' ), {
 443   *                         isDismissible: false,
 444   *                     } )
 445   *                 }
 446   *             >
 447   *                 { __( 'Generate a notice' ) }
 448   *             </Button>
 449   *             { notices.length > 0 && (
 450   *                 <Button onClick={ () => removeNotice( notices[ 0 ].id ) }>
 451   *                     { __( 'Remove the notice' ) }
 452   *                 </Button>
 453   *             ) }
 454   *         </>
 455   *     );
 456   *};
 457   * ```
 458   *
 459   * @return {Object} Action object.
 460   */
 461  function removeNotice(id, context = DEFAULT_CONTEXT) {
 462    return {
 463      type: 'REMOVE_NOTICE',
 464      id,
 465      context
 466    };
 467  }
 468  
 469  /**
 470   * Removes all notices from a given context. Defaults to the default context.
 471   *
 472   * @param {string} noticeType The context to remove all notices from.
 473   * @param {string} context    The context to remove all notices from.
 474   *
 475   * @example
 476   * ```js
 477   * import { __ } from '@wordpress/i18n';
 478   * import { useDispatch, useSelect } from '@wordpress/data';
 479   * import { store as noticesStore } from '@wordpress/notices';
 480   * import { Button } from '@wordpress/components';
 481   *
 482   * export const ExampleComponent = () => {
 483   *     const notices = useSelect( ( select ) =>
 484   *         select( noticesStore ).getNotices()
 485   *     );
 486   *     const { removeAllNotices } = useDispatch( noticesStore );
 487   *     return (
 488   *         <>
 489   *             <ul>
 490   *                 { notices.map( ( notice ) => (
 491   *                     <li key={ notice.id }>{ notice.content }</li>
 492   *                 ) ) }
 493   *             </ul>
 494   *             <Button
 495   *                 onClick={ () =>
 496   *                     removeAllNotices()
 497   *                 }
 498   *             >
 499   *                 { __( 'Clear all notices', 'woo-gutenberg-products-block' ) }
 500   *             </Button>
 501   *             <Button
 502   *                 onClick={ () =>
 503   *                     removeAllNotices( 'snackbar' )
 504   *                 }
 505   *             >
 506   *                 { __( 'Clear all snackbar notices', 'woo-gutenberg-products-block' ) }
 507   *             </Button>
 508   *         </>
 509   *     );
 510   * };
 511   * ```
 512   *
 513   * @return {Object}        Action object.
 514   */
 515  function removeAllNotices(noticeType = 'default', context = DEFAULT_CONTEXT) {
 516    return {
 517      type: 'REMOVE_ALL_NOTICES',
 518      noticeType,
 519      context
 520    };
 521  }
 522  
 523  /**
 524   * Returns an action object used in signalling that several notices are to be removed.
 525   *
 526   * @param {string[]} ids                List of unique notice identifiers.
 527   * @param {string}   [context='global'] Optional context (grouping) in which the notices are
 528   *                                      intended to appear. Defaults to default context.
 529   * @example
 530   * ```js
 531   * import { __ } from '@wordpress/i18n';
 532   * import { useDispatch, useSelect } from '@wordpress/data';
 533   * import { store as noticesStore } from '@wordpress/notices';
 534   * import { Button } from '@wordpress/components';
 535   *
 536   * const ExampleComponent = () => {
 537   *     const notices = useSelect( ( select ) =>
 538   *         select( noticesStore ).getNotices()
 539   *     );
 540   *     const { removeNotices } = useDispatch( noticesStore );
 541   *     return (
 542   *         <>
 543   *             <ul>
 544   *                 { notices.map( ( notice ) => (
 545   *                     <li key={ notice.id }>{ notice.content }</li>
 546   *                 ) ) }
 547   *             </ul>
 548   *             <Button
 549   *                 onClick={ () =>
 550   *                     removeNotices( notices.map( ( { id } ) => id ) )
 551   *                 }
 552   *             >
 553   *                 { __( 'Clear all notices' ) }
 554   *             </Button>
 555   *         </>
 556   *     );
 557   * };
 558   * ```
 559   * @return {Object} Action object.
 560   */
 561  function removeNotices(ids, context = DEFAULT_CONTEXT) {
 562    return {
 563      type: 'REMOVE_NOTICES',
 564      ids,
 565      context
 566    };
 567  }
 568  
 569  ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/selectors.js
 570  /**
 571   * Internal dependencies
 572   */
 573  
 574  
 575  /** @typedef {import('./actions').WPNoticeAction} WPNoticeAction */
 576  
 577  /**
 578   * The default empty set of notices to return when there are no notices
 579   * assigned for a given notices context. This can occur if the getNotices
 580   * selector is called without a notice ever having been created for the
 581   * context. A shared value is used to ensure referential equality between
 582   * sequential selector calls, since otherwise `[] !== []`.
 583   *
 584   * @type {Array}
 585   */
 586  const DEFAULT_NOTICES = [];
 587  
 588  /**
 589   * @typedef {Object} WPNotice Notice object.
 590   *
 591   * @property {string}           id             Unique identifier of notice.
 592   * @property {string}           status         Status of notice, one of `success`,
 593   *                                             `info`, `error`, or `warning`. Defaults
 594   *                                             to `info`.
 595   * @property {string}           content        Notice message.
 596   * @property {string}           spokenMessage  Audibly announced message text used by
 597   *                                             assistive technologies.
 598   * @property {string}           __unstableHTML Notice message as raw HTML. Intended to
 599   *                                             serve primarily for compatibility of
 600   *                                             server-rendered notices, and SHOULD NOT
 601   *                                             be used for notices. It is subject to
 602   *                                             removal without notice.
 603   * @property {boolean}          isDismissible  Whether the notice can be dismissed by
 604   *                                             user. Defaults to `true`.
 605   * @property {string}           type           Type of notice, one of `default`,
 606   *                                             or `snackbar`. Defaults to `default`.
 607   * @property {boolean}          speak          Whether the notice content should be
 608   *                                             announced to screen readers. Defaults to
 609   *                                             `true`.
 610   * @property {WPNoticeAction[]} actions        User actions to present with notice.
 611   */
 612  
 613  /**
 614   * Returns all notices as an array, optionally for a given context. Defaults to
 615   * the global context.
 616   *
 617   * @param {Object}  state   Notices state.
 618   * @param {?string} context Optional grouping context.
 619   *
 620   * @example
 621   *
 622   *```js
 623   * import { useSelect } from '@wordpress/data';
 624   * import { store as noticesStore } from '@wordpress/notices';
 625   *
 626   * const ExampleComponent = () => {
 627   *     const notices = useSelect( ( select ) => select( noticesStore ).getNotices() );
 628   *     return (
 629   *         <ul>
 630   *         { notices.map( ( notice ) => (
 631   *             <li key={ notice.ID }>{ notice.content }</li>
 632   *         ) ) }
 633   *        </ul>
 634   *    )
 635   * };
 636   *```
 637   *
 638   * @return {WPNotice[]} Array of notices.
 639   */
 640  function getNotices(state, context = DEFAULT_CONTEXT) {
 641    return state[context] || DEFAULT_NOTICES;
 642  }
 643  
 644  ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/index.js
 645  /**
 646   * WordPress dependencies
 647   */
 648  
 649  
 650  /**
 651   * Internal dependencies
 652   */
 653  
 654  
 655  
 656  
 657  /**
 658   * Store definition for the notices namespace.
 659   *
 660   * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
 661   */
 662  const store = (0,external_wp_data_namespaceObject.createReduxStore)('core/notices', {
 663    reducer: reducer,
 664    actions: actions_namespaceObject,
 665    selectors: selectors_namespaceObject
 666  });
 667  (0,external_wp_data_namespaceObject.register)(store);
 668  
 669  ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/index.js
 670  
 671  
 672  (window.wp = window.wp || {}).notices = __webpack_exports__;
 673  /******/ })()
 674  ;


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref