UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

1 lines 2.65 kB
{"version":3,"file":"_id-generator-chunk.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/cdk/a11y/id-generator.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {APP_ID, inject, Service} from '@angular/core';\n\n/**\n * Keeps track of the ID count per prefix. This helps us make the IDs a bit more deterministic\n * like they were before the service was introduced. Note that ideally we wouldn't have to do\n * this, but there are some internal tests that rely on the IDs.\n *\n * Note: use a map to avoid conflicts with built-in properties.\n */\nconst counters = new Map<string, number>();\n\n/** Service that generates unique IDs for DOM nodes. */\n@Service()\nexport class _IdGenerator {\n private _appId = inject(APP_ID);\n private static _infix = `a${Math.floor(Math.random() * 100000).toString()}`;\n\n /**\n * Generates a unique ID with a specific prefix.\n * @param prefix Prefix to add to the ID.\n * @param randomize Add a randomized infix string.\n */\n getId(prefix: string, randomize: boolean = false): string {\n // Omit the app ID if it's the default `ng`. Since the vast majority of pages have one\n // Angular app on them, we can reduce the amount of breakages by not adding it.\n if (this._appId !== 'ng') {\n prefix += this._appId;\n }\n\n let count = counters.get(prefix);\n\n if (count === undefined) {\n count = 0;\n } else {\n count++;\n }\n\n counters.set(prefix, count);\n return `${prefix}${randomize ? _IdGenerator._infix + '-' : ''}${count}`;\n }\n}\n"],"names":[],"mappings":";;;AAiBA,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB;MAI7B,YAAY,CAAA;AACf,EAAA,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;AACvB,EAAA,OAAO,MAAM,GAAG,CAAA,CAAA,EAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAA,CAAE;AAO3E,EAAA,KAAK,CAAC,MAAc,EAAE,SAAA,GAAqB,KAAK,EAAA;AAG9C,IAAA,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,EAAE;MACxB,MAAM,IAAI,IAAI,CAAC,MAAM;AACvB,IAAA;AAEA,IAAA,IAAI,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC;IAEhC,IAAI,KAAK,KAAK,SAAS,EAAE;AACvB,MAAA,KAAK,GAAG,CAAC;AACX,IAAA,CAAA,MAAO;AACL,MAAA,KAAK,EAAE;AACT,IAAA;AAEA,IAAA,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC;AAC3B,IAAA,OAAO,CAAA,EAAG,MAAM,CAAA,EAAG,SAAS,GAAG,YAAY,CAAC,MAAM,GAAG,GAAG,GAAG,EAAE,CAAA,EAAG,KAAK,CAAA,CAAE;AACzE,EAAA;;;;;UA1BW,YAAY;AAAA,IAAA,IAAA,EAAA,EAAA;AAAA,IAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA;AAAA,GAAA,CAAA;;;;;UAAZ;AAAY,GAAA,CAAA;;;;;;QAAZ,YAAY;AAAA,EAAA,UAAA,EAAA,CAAA;UADxB;;;;;;"}