{"version":3,"file":"theme.context-XzjZ20Xg.js","sources":["../../../src/packages/core/themes/theme.context.ts"],"sourcesContent":["import { loadManifestPlainCss } from '@umbraco-cms/backoffice/extension-api';\nimport { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry';\nimport { UmbContextBase } from '@umbraco-cms/backoffice/class-api';\nimport { UmbContextToken } from '@umbraco-cms/backoffice/context-api';\nimport { UmbStringState } from '@umbraco-cms/backoffice/observable-api';\nimport type { ManifestTheme } from '@umbraco-cms/backoffice/extension-registry';\nimport type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';\nimport type { UmbObserverController } from '@umbraco-cms/backoffice/observable-api';\n\nconst LOCAL_STORAGE_KEY = 'umb-theme-alias';\n\nexport class UmbThemeContext extends UmbContextBase {\n\t#theme = new UmbStringState('umb-light-theme');\n\t#themeObserver?: UmbObserverController;\n\n\tpublic readonly theme = this.#theme.asObservable();\n\n\t#styleElement: HTMLLinkElement | HTMLStyleElement | null = null;\n\n\tconstructor(host: UmbControllerHost) {\n\t\tsuper(host, UMB_THEME_CONTEXT);\n\n\t\tconst storedTheme = localStorage.getItem(LOCAL_STORAGE_KEY);\n\t\tif (storedTheme) {\n\t\t\tthis.setThemeByAlias(storedTheme);\n\t\t}\n\t}\n\n\tpublic setThemeByAlias(themeAlias: string) {\n\t\tthis.#theme.setValue(themeAlias);\n\n\t\tthis.#themeObserver?.destroy();\n\t\tif (themeAlias) {\n\t\t\tlocalStorage.setItem(LOCAL_STORAGE_KEY, themeAlias);\n\t\t\tthis.#themeObserver = this.observe(\n\t\t\t\tumbExtensionsRegistry.byTypeAndFilter('theme', (extension) => extension.alias === themeAlias),\n\t\t\t\tasync (themes) => {\n\t\t\t\t\tthis.#styleElement?.remove();\n\t\t\t\t\tif (themes.length > 0 && themes[0].css) {\n\t\t\t\t\t\tconst activeTheme = themes[0];\n\t\t\t\t\t\tif (typeof activeTheme.css === 'function') {\n\t\t\t\t\t\t\tthis.#styleElement = document.createElement('style') as HTMLStyleElement;\n\t\t\t\t\t\t\t// We store the current style element so we can check if it has been replaced by another theme in between.\n\t\t\t\t\t\t\tconst currentStyleEl = this.#styleElement;\n\t\t\t\t\t\t\tcurrentStyleEl.setAttribute('type', 'text/css');\n\n\t\t\t\t\t\t\tconst result = await loadManifestPlainCss(activeTheme.css);\n\t\t\t\t\t\t\t// Checking that this is still our styleElement, it has not been replaced with another theme in between.\n\t\t\t\t\t\t\tif (result && currentStyleEl === this.#styleElement) {\n\t\t\t\t\t\t\t\tcurrentStyleEl.appendChild(document.createTextNode(result));\n\t\t\t\t\t\t\t\tdocument.head.appendChild(currentStyleEl);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if (typeof activeTheme.css === 'string') {\n\t\t\t\t\t\t\tthis.#styleElement = document.createElement('link');\n\t\t\t\t\t\t\tthis.#styleElement.setAttribute('rel', 'stylesheet');\n\t\t\t\t\t\t\tthis.#styleElement.setAttribute('href', activeTheme.css);\n\t\t\t\t\t\t\tdocument.head.appendChild(this.#styleElement);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// We could not load a theme for this alias, so we remove the theme.\n\t\t\t\t\t\tlocalStorage.removeItem(LOCAL_STORAGE_KEY);\n\t\t\t\t\t\tthis.#styleElement?.childNodes.forEach((node) => node.remove());\n\t\t\t\t\t\tthis.#styleElement?.setAttribute('href', '');\n\t\t\t\t\t\tthis.#styleElement = null;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t);\n\t\t} else {\n\t\t\t// Super clean, we got a falsy value, so we remove the theme.\n\n\t\t\tlocalStorage.removeItem(LOCAL_STORAGE_KEY);\n\t\t\tthis.#styleElement?.remove();\n\t\t\tthis.#styleElement?.childNodes.forEach((node) => node.remove());\n\t\t\tthis.#styleElement?.setAttribute('href', '');\n\t\t\tthis.#styleElement = null;\n\t\t}\n\t}\n}\n\nexport const UMB_THEME_CONTEXT = new UmbContextToken('umbThemeContext');\n\n// Default export to enable this as a globalContext extension js:\nexport default UmbThemeContext;\n"],"names":["LOCAL_STORAGE_KEY","UmbThemeContext","UmbContextBase","host","UMB_THEME_CONTEXT","#theme","UmbStringState","#styleElement","storedTheme","#themeObserver","themeAlias","umbExtensionsRegistry","extension","themes","activeTheme","currentStyleEl","result","loadManifestPlainCss","node","UmbContextToken"],"mappings":";;;;;AASA,MAAMA,IAAoB;AAEnB,MAAMC,UAAwBC,EAAgC;AAAA,EAQpE,YAAYC,GAAyB;AACpC,UAAMA,GAAMC,CAAiB,GARrB,KAAAC,KAAA,IAAIC,EAAe,iBAAiB,GAG7B,KAAA,QAAQ,KAAKD,GAAO,aAAa,GAEU,KAAAE,KAAA;AAKpD,UAAAC,IAAc,aAAa,QAAQR,CAAiB;AAC1D,IAAIQ,KACH,KAAK,gBAAgBA,CAAW;AAAA,EAElC;AAAA,EAdAH;AAAA,EACAI;AAAA,EAIAF;AAAA,EAWO,gBAAgBG,GAAoB;AACrC,SAAAL,GAAO,SAASK,CAAU,GAE/B,KAAKD,IAAgB,WACjBC,KACU,aAAA,QAAQV,GAAmBU,CAAU,GAClD,KAAKD,KAAiB,KAAK;AAAA,MAC1BE,EAAsB,gBAAgB,SAAS,CAACC,MAAcA,EAAU,UAAUF,CAAU;AAAA,MAC5F,OAAOG,MAAW;AAEjB,YADA,KAAKN,IAAe,UAChBM,EAAO,SAAS,KAAKA,EAAO,CAAC,EAAE,KAAK;AACjC,gBAAAC,IAAcD,EAAO,CAAC;AACxB,cAAA,OAAOC,EAAY,OAAQ,YAAY;AACrC,iBAAAP,KAAgB,SAAS,cAAc,OAAO;AAEnD,kBAAMQ,IAAiB,KAAKR;AACb,YAAAQ,EAAA,aAAa,QAAQ,UAAU;AAE9C,kBAAMC,IAAS,MAAMC,EAAqBH,EAAY,GAAG;AAErD,YAAAE,KAAUD,MAAmB,KAAKR,OACrCQ,EAAe,YAAY,SAAS,eAAeC,CAAM,CAAC,GACjD,SAAA,KAAK,YAAYD,CAAc;AAAA,UAE/B,MAAA,CAAA,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