{"version":3,"file":"govuk-frontend-component.mjs","sources":["../../src/govuk/govuk-frontend-component.mjs"],"sourcesContent":["import { isInitialised, isSupported } from './common/index.mjs'\nimport { ElementError, InitError, SupportError } from './errors/index.mjs'\n\n/**\n * Base Component class\n *\n * Centralises the behaviours shared by our components\n *\n * @virtual\n * @template {Element} [RootElementType=HTMLElement]\n */\nexport class GOVUKFrontendComponent {\n /**\n * @type {typeof Element}\n */\n static elementType = HTMLElement\n\n // allows Typescript user to work around the lack of types\n // in GOVUKFrontend package, Typescript is not aware of $root\n // in components that extend GOVUKFrontendComponent\n /**\n * Returns the root element of the component\n *\n * @protected\n * @returns {RootElementType} - the root element of component\n */\n get $root() {\n return this._$root\n }\n\n /**\n * @protected\n * @type {RootElementType}\n */\n _$root\n\n /**\n * Constructs a new component, validating that GOV.UK Frontend is supported\n *\n * @internal\n * @param {Element | null} [$root] - HTML element to use for component\n */\n constructor($root) {\n const childConstructor = /** @type {ChildClassConstructor} */ (\n this.constructor\n )\n\n // TypeScript does not enforce that inheriting classes will define a `moduleName`\n // (even if we add a `@virtual` `static moduleName` property to this class).\n // While we trust users to do this correctly, we do a little check to provide them\n // a helpful error message.\n //\n // After this, we'll be sure that `childConstructor` has a `moduleName`\n // as expected of the `ChildClassConstructor` we've cast `this.constructor` to.\n if (typeof childConstructor.moduleName !== 'string') {\n throw new InitError(`\\`moduleName\\` not defined in component`)\n }\n\n if (!($root instanceof childConstructor.elementType)) {\n throw new ElementError({\n element: $root,\n component: childConstructor,\n identifier: 'Root element (`$root`)',\n expectedType: childConstructor.elementType.name\n })\n } else {\n this._$root = /** @type {RootElementType} */ ($root)\n }\n\n childConstructor.checkSupport()\n\n this.checkInitialised()\n\n const moduleName = childConstructor.moduleName\n\n this.$root.setAttribute(`data-${moduleName}-init`, '')\n }\n\n /**\n * Validates whether component is already initialised\n *\n * @private\n * @throws {InitError} when component is already initialised\n */\n checkInitialised() {\n const constructor = /** @type {ChildClassConstructor} */ (this.constructor)\n const moduleName = constructor.moduleName\n\n if (moduleName && isInitialised(this.$root, moduleName)) {\n throw new InitError(constructor)\n }\n }\n\n /**\n * Validates whether components are supported\n *\n * @throws {SupportError} when the components are not supported\n */\n static checkSupport() {\n if (!isSupported()) {\n throw new SupportError()\n }\n }\n}\n\n/**\n * @typedef ChildClass\n * @property {string} moduleName - The module name that'll be looked for in the DOM when initialising the component\n */\n\n/**\n * @typedef {typeof GOVUKFrontendComponent & ChildClass} ChildClassConstructor\n */\n"],"names":["GOVUKFrontendComponent","$root","_$root","constructor","childConstructor","moduleName","InitError","elementType","ElementError","element","component","identifier","expectedType","name","checkSupport","checkInitialised","setAttribute","isInitialised","isSupported","SupportError","HTMLElement"],"mappings":";;;AAWO,MAAMA,sBAAsB,CAAC;AASlC;AACF;AACA;AACA;AACA;AACA;EACE,IAAIC,KAAKA,GAAG;IACV,OAAO,IAAI,CAACC,MAAM,CAAA;AACpB,GAAA;EAcAC,WAAWA,CAACF,KAAK,EAAE;AAAA,IAAA,IAAA,CARnBC,MAAM,GAAA,KAAA,CAAA,CAAA;AASJ,IAAA,MAAME,gBAAgB,GACpB,IAAI,CAACD,WACN,CAAA;AASD,IAAA,IAAI,OAAOC,gBAAgB,CAACC,UAAU,KAAK,QAAQ,EAAE;AACnD,MAAA,MAAM,IAAIC,SAAS,CAAC,CAAA,uCAAA,CAAyC,CAAC,CAAA;AAChE,KAAA;AAEA,IAAA,IAAI,EAAEL,KAAK,YAAYG,gBAAgB,CAACG,WAAW,CAAC,EAAE;MACpD,MAAM,IAAIC,YAAY,CAAC;AACrBC,QAAAA,OAAO,EAAER,KAAK;AACdS,QAAAA,SAAS,EAAEN,gBAAgB;AAC3BO,QAAAA,UAAU,EAAE,wBAAwB;AACpCC,QAAAA,YAAY,EAAER,gBAAgB,CAACG,WAAW,CAACM,IAAAA;AAC7C,OAAC,CAAC,CAAA;AACJ,KAAC,MAAM;MACL,IAAI,CAACX,MAAM,GAAmCD,KAAM,CAAA;AACtD,KAAA;IAEAG,gBAA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