{"version":3,"file":"init.mjs","sources":["../../src/govuk/init.mjs"],"sourcesContent":["import { isSupported } from './common/index.mjs'\nimport { Accordion } from './components/accordion/accordion.mjs'\nimport { Button } from './components/button/button.mjs'\nimport { CharacterCount } from './components/character-count/character-count.mjs'\nimport { Checkboxes } from './components/checkboxes/checkboxes.mjs'\nimport { ErrorSummary } from './components/error-summary/error-summary.mjs'\nimport { ExitThisPage } from './components/exit-this-page/exit-this-page.mjs'\nimport { Header } from './components/header/header.mjs'\nimport { NotificationBanner } from './components/notification-banner/notification-banner.mjs'\nimport { PasswordInput } from './components/password-input/password-input.mjs'\nimport { Radios } from './components/radios/radios.mjs'\nimport { ServiceNavigation } from './components/service-navigation/service-navigation.mjs'\nimport { SkipLink } from './components/skip-link/skip-link.mjs'\nimport { Tabs } from './components/tabs/tabs.mjs'\nimport { SupportError } from './errors/index.mjs'\n\n/**\n * Initialise all components\n *\n * Use the `data-module` attributes to find, instantiate and init all of the\n * components provided as part of GOV.UK Frontend.\n *\n * @param {Config & { scope?: Element, onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)\n */\nfunction initAll(config) {\n  config = typeof config !== 'undefined' ? config : {}\n\n  // Skip initialisation when GOV.UK Frontend is not supported\n  if (!isSupported()) {\n    if (config.onError) {\n      config.onError(new SupportError(), {\n        config\n      })\n    } else {\n      console.log(new SupportError())\n    }\n    return\n  }\n\n  const components = /** @type {const} */ ([\n    [Accordion, config.accordion],\n    [Button, config.button],\n    [CharacterCount, config.characterCount],\n    [Checkboxes],\n    [ErrorSummary, config.errorSummary],\n    [ExitThisPage, config.exitThisPage],\n    [Header],\n    [NotificationBanner, config.notificationBanner],\n    [PasswordInput, config.passwordInput],\n    [Radios],\n    [ServiceNavigation],\n    [SkipLink],\n    [Tabs]\n  ])\n\n  // Allow the user to initialise GOV.UK Frontend in only certain sections of the page\n  // Defaults to the entire document if nothing is set.\n  // const $scope = config.scope ?? document\n\n  const options = {\n    scope: config.scope ?? document,\n    onError: config.onError\n  }\n\n  components.forEach(([Component, config]) => {\n    createAll(Component, config, options)\n  })\n}\n\n/**\n * Create all instances of a specific component on the page\n *\n * Uses the `data-module` attribute to find all elements matching the specified\n * component on the page, creating instances of the component object for each\n * of them.\n *\n * Any component errors will be caught and logged to the console.\n *\n * @template {CompatibleClass} ComponentClass\n * @param {ComponentClass} Component - class of the component to create\n * @param {ComponentConfig<ComponentClass>} [config] - Config supplied to component\n * @param {OnErrorCallback<ComponentClass> | Element | Document | CreateAllOptions<ComponentClass> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init\n * @returns {Array<InstanceType<ComponentClass>>} - array of instantiated components\n */\nfunction createAll(Component, config, createAllOptions) {\n  let /** @type {Element | Document} */ $scope = document\n  let /** @type {OnErrorCallback<Component> | undefined} */ onError\n\n  if (typeof createAllOptions === 'object') {\n    createAllOptions = /** @type {CreateAllOptions<Component>} */ (\n      // eslint-disable-next-line no-self-assign\n      createAllOptions\n    )\n\n    $scope = createAllOptions.scope ?? $scope\n    onError = createAllOptions.onError\n  }\n\n  if (typeof createAllOptions === 'function') {\n    onError = createAllOptions\n  }\n\n  if (createAllOptions instanceof HTMLElement) {\n    $scope = createAllOptions\n  }\n\n  const $elements = $scope.querySelectorAll(\n    `[data-module=\"${Component.moduleName}\"]`\n  )\n\n  // Skip initialisation when GOV.UK Frontend is not supported\n  if (!isSupported()) {\n    if (onError) {\n      onError(new SupportError(), {\n        component: Component,\n        config\n      })\n    } else {\n      console.log(new SupportError())\n    }\n    return []\n  }\n\n  /* eslint-disable-next-line @typescript-eslint/no-unsafe-return --\n   * We can't define CompatibleClass as `{new(): CompatibleClass, moduleName: string}`,\n   * as when doing `typeof Accordion` (or any component), TypeScript doesn't seem\n   * to acknowledge the static `moduleName` that's set in our component classes.\n   * This means we have to set the constructor of `CompatibleClass` as `{new(): any}`,\n   * leading to ESLint frowning that we're returning `any[]`.\n   */\n  return Array.from($elements)\n    .map(($element) => {\n      try {\n        // Only pass config to components that accept it\n        // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n        return typeof config !== 'undefined'\n          ? new Component($element, config)\n          : new Component($element)\n      } catch (error) {\n        if (onError) {\n          onError(error, {\n            element: $element,\n            component: Component,\n            config\n          })\n        } else {\n          console.log(error)\n        }\n\n        return null\n      }\n    })\n    .filter(Boolean) // Exclude components that errored\n}\n\nexport { initAll, createAll }\n\n/* eslint-disable jsdoc/valid-types --\n * `{new(...args: any[] ): object}` is not recognised as valid\n * https://github.com/gajus/eslint-plugin-jsdoc/issues/145#issuecomment-1308722878\n * https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/131\n **/\n\n/**\n * @typedef {{new (...args: any[]): any, moduleName: string}} CompatibleClass\n */\n\n/* eslint-enable jsdoc/valid-types */\n\n/**\n * Config for all components via `initAll()`\n *\n * @typedef {object} Config\n * @property {AccordionConfig} [accordion] - Accordion config\n * @property {ButtonConfig} [button] - Button config\n * @property {CharacterCountConfig} [characterCount] - Character Count config\n * @property {ErrorSummaryConfig} [errorSummary] - Error Summary config\n * @property {ExitThisPageConfig} [exitThisPage] - Exit This Page config\n * @property {NotificationBannerConfig} [notificationBanner] - Notification Banner config\n * @property {PasswordInputConfig} [passwordInput] - Password input config\n */\n\n/**\n * Config for individual components\n *\n * @typedef {import('./components/accordion/accordion.mjs').AccordionConfig} AccordionConfig\n * @typedef {import('./components/accordion/accordion.mjs').AccordionTranslations} AccordionTranslations\n * @typedef {import('./components/button/button.mjs').ButtonConfig} ButtonConfig\n * @typedef {import('./components/character-count/character-count.mjs').CharacterCountConfig} CharacterCountConfig\n * @typedef {import('./components/character-count/character-count.mjs').CharacterCountTranslations} CharacterCountTranslations\n * @typedef {import('./components/error-summary/error-summary.mjs').ErrorSummaryConfig} ErrorSummaryConfig\n * @typedef {import('./components/exit-this-page/exit-this-page.mjs').ExitThisPageConfig} ExitThisPageConfig\n * @typedef {import('./components/exit-this-page/exit-this-page.mjs').ExitThisPageTranslations} ExitThisPageTranslations\n * @typedef {import('./components/notification-banner/notification-banner.mjs').NotificationBannerConfig} NotificationBannerConfig\n * @typedef {import('./components/password-input/password-input.mjs').PasswordInputConfig} PasswordInputConfig\n */\n\n/**\n * Component config keys, e.g. `accordion` and `characterCount`\n *\n * @typedef {keyof Config} ConfigKey\n */\n\n/**\n * @template {CompatibleClass} ComponentClass\n * @typedef {ConstructorParameters<ComponentClass>[1]} ComponentConfig\n */\n\n/**\n * @template {CompatibleClass} ComponentClass\n * @typedef {object} ErrorContext\n * @property {Element} [element] - Element used for component module initialisation\n * @property {ComponentClass} [component] - Class of component\n * @property {ComponentConfig<ComponentClass>} config - Config supplied to component\n */\n\n/**\n * @template {CompatibleClass} ComponentClass\n * @callback OnErrorCallback\n * @param {unknown} error - Thrown error\n * @param {ErrorContext<ComponentClass>} context - Object containing the element, component class and configuration\n */\n\n/**\n * @template {CompatibleClass} ComponentClass\n * @typedef {object} CreateAllOptions\n * @property {Element | Document} [scope] - scope of the document to search within\n * @property {OnErrorCallback<ComponentClass>} [onError] - callback function if error throw by component on init\n */\n"],"names":["initAll","config","_config$scope","isSupported","onError","SupportError","console","log","components","Accordion","accordion","Button","button","CharacterCount","characterCount","Checkboxes","ErrorSummary","errorSummary","ExitThisPage","exitThisPage","Header","NotificationBanner","notificationBanner","PasswordInput","passwordInput","Radios","ServiceNavigation","SkipLink","Tabs","options","scope","document","forEach","Component","createAll","createAllOptions","$scope","_createAllOptions$sco","HTMLElement","$elements","querySelectorAll","moduleName","component","Array","from","map","$element","error","element","filter","Boolean"],"mappings":";;;;;;;;;;;;;;;;AAgBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,OAAOA,CAACC,MAAM,EAAE;AAAA,EAAA,IAAAC,aAAA,CAAA;EACvBD,MAAM,GAAG,OAAOA,MAAM,KAAK,WAAW,GAAGA,MAAM,GAAG,EAAE,CAAA;AAGpD,EAAA,IAAI,CAACE,WAAW,EAAE,EAAE;IAClB,IAAIF,MAAM,CAACG,OAAO,EA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