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C,CAAC;YAC5E,CAAC;YAED,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,QAAQ,EAAE,EAAE;gBAC1E,MAAM,mBAAM,SAAS,EAAK,KAAK,CAAC,QAAQ,CAAC,EAAG;YAC7C,CAAC,EAAE,EAAE,CAAC,CAAC;YAEP,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC;QAClC,CAAC;KACD,CAAA;IA/CA;QAFC,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC;QAC9B,YAAY,CAAC,cAAc,EAAE,OAAO,CAAC;;;;qDAGrC;IA/Ca,MAAM;QATpB,MAAM,CAAC;YACP,IAAI,EAAE,kBAAkB;YACxB,aAAa,EAAE,CAAC,KAAY,EAAE,UAA4B,EAAoB,EAAE;gBAC/E,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;oBACvB,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;gBAClB,CAAC;gBACD,MAAM,CAAC,EAAE,CAAC;YACX,CAAC;SACD,CAAC;OACa,MAAM,CA4FpB;IAED,MAAM,CAAC,MAAM,CAAC;AACf,CAAC;AAED,eAAe,WAAW,CAAC","sourcesContent":["import { Constructor, WidgetProperties, SupportedClassName } from './../interfaces';\nimport { Registry } from './../Registry';\nimport { Injector } from './../Injector';\nimport { inject } from './../decorators/inject';\nimport { WidgetBase } from './../WidgetBase';\nimport { handleDecorator } from './../decorators/handleDecorator';\nimport { diffProperty } from './../decorators/diffProperty';\nimport { shallow } from './../diff';\n\n/**\n * A lookup object for available class names\n */\nexport type ClassNames = {\n\t[key: string]: string;\n};\n\n/**\n * A lookup object for available widget classes names\n */\nexport interface Theme {\n\t[key: string]: object;\n}\n\n/**\n * Properties required for the Themed mixin\n */\nexport interface ThemedProperties<T = ClassNames> extends WidgetProperties {\n\tinjectedTheme?: any;\n\ttheme?: Theme;\n\textraClasses?: { [P in keyof T]?: string };\n}\n\nconst THEME_KEY = ' _key';\n\nexport const INJECTED_THEME_KEY = Symbol('theme');\n\n/**\n * Interface for the ThemedMixin\n */\nexport interface ThemedMixin<T = ClassNames> {\n\ttheme(classes: SupportedClassName): SupportedClassName;\n\ttheme(classes: SupportedClassName[]): SupportedClassName[];\n\tproperties: ThemedProperties<T>;\n}\n\n/**\n * Decorator for base css classes\n */\nexport function theme(theme: {}) {\n\treturn handleDecorator((target) => {\n\t\ttarget.addDecorator('baseThemeClasses', theme);\n\t});\n}\n\n/**\n * Creates a reverse lookup for the classes passed in via the `theme` function.\n *\n * @param classes The baseClasses object\n * @requires\n */\nfunction createThemeClassesLookup(classes: ClassNames[]): ClassNames {\n\treturn classes.reduce(\n\t\t(currentClassNames, baseClass) => {\n\t\t\tObject.keys(baseClass).forEach((key: string) => {\n\t\t\t\tcurrentClassNames[baseClass[key]] = key;\n\t\t\t});\n\t\t\treturn currentClassNames;\n\t\t},\n\t\t<ClassNames>{}\n\t);\n}\n\n/**\n * Convenience function that is given a theme and an optional registry, the theme\n * injector is defined against the registry, returning the theme.\n *\n * @param theme the theme to set\n * @param themeRegistry registry to define the theme injector against. Defaults\n * to the global registry\n *\n * @returns the theme injector used to set the theme\n */\nexport function registerThemeInjector(theme: any, themeRegistry: Registry): Injector {\n\tconst themeInjector = new Injector(theme);\n\tthemeRegistry.defineInjector(INJECTED_THEME_KEY, (invalidator) => {\n\t\tthemeInjector.setInvalidator(invalidator);\n\t\treturn () => themeInjector.get();\n\t});\n\treturn themeInjector;\n}\n\n/**\n * Function that returns a class decorated with with Themed functionality\n */\n\nexport function ThemedMixin<E, T extends Constructor<WidgetBase<ThemedProperties<E>>>>(\n\tBase: T\n): Constructor<ThemedMixin<E>> & T {\n\t@inject({\n\t\tname: INJECTED_THEME_KEY,\n\t\tgetProperties: (theme: Theme, properties: ThemedProperties): ThemedProperties => {\n\t\t\tif (!properties.theme) {\n\t\t\t\treturn { theme };\n\t\t\t}\n\t\t\treturn {};\n\t\t}\n\t})\n\tabstract class Themed extends Base {\n\t\tpublic abstract properties: ThemedProperties<E>;\n\n\t\t/**\n\t\t * The Themed baseClasses\n\t\t */\n\t\tprivate _registeredBaseTheme: ClassNames | undefined;\n\n\t\t/**\n\t\t * Registered base theme keys\n\t\t */\n\t\tprivate _registeredBaseThemeKeys: string[] = [];\n\n\t\t/**\n\t\t * Reverse lookup of the theme classes\n\t\t */\n\t\tprivate _baseThemeClassesReverseLookup: ClassNames | undefined;\n\n\t\t/**\n\t\t * Indicates if classes meta data need to be calculated.\n\t\t */\n\t\tprivate _recalculateClasses = true;\n\n\t\t/**\n\t\t * Loaded theme\n\t\t */\n\t\tprivate _theme: ClassNames = {};\n\n\t\tpublic theme(classes: SupportedClassName): SupportedClassName;\n\t\tpublic theme(classes: SupportedClassName[]): SupportedClassName[];\n\t\tpublic theme(classes: SupportedClassName | SupportedClassName[]): SupportedClassName | SupportedClassName[] {\n\t\t\tif (this._recalculateClasses) {\n\t\t\t\tthis._recalculateThemeClasses();\n\t\t\t}\n\t\t\tif (Array.isArray(classes)) {\n\t\t\t\treturn classes.map((className) => this._getThemeClass(className));\n\t\t\t}\n\t\t\treturn this._getThemeClass(classes);\n\t\t}\n\n\t\t/**\n\t\t * Function fired when `theme` or `extraClasses` are changed.\n\t\t */\n\t\t@diffProperty('theme', shallow)\n\t\t@diffProperty('extraClasses', shallow)\n\t\tprotected onPropertiesChanged() {\n\t\t\tthis._recalculateClasses = true;\n\t\t}\n\n\t\tprivate _getThemeClass(className: SupportedClassName): SupportedClassName {\n\t\t\tif (className === undefined || className === null) {\n\t\t\t\treturn className;\n\t\t\t}\n\n\t\t\tconst extraClasses = this.properties.extraClasses || ({} as any);\n\t\t\tconst themeClassName = this._baseThemeClassesReverseLookup![className];\n\t\t\tlet resultClassNames: string[] = [];\n\t\t\tif (!themeClassName) {\n\t\t\t\tconsole.warn(`Class name: '${className}' not found in theme`);\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif (extraClasses[themeClassName]) {\n\t\t\t\tresultClassNames.push(extraClasses[themeClassName]);\n\t\t\t}\n\n\t\t\tif (this._theme[themeClassName]) {\n\t\t\t\tresultClassNames.push(this._theme[themeClassName]);\n\t\t\t} else {\n\t\t\t\tresultClassNames.push(this._registeredBaseTheme![themeClassName]);\n\t\t\t}\n\t\t\treturn resultClassNames.join(' ');\n\t\t}\n\n\t\tprivate _recalculateThemeClasses() {\n\t\t\tconst { theme = {} } = this.properties;\n\t\t\tconst baseThemes = this.getDecorator('baseThemeClasses');\n\t\t\tif (!this._registeredBaseTheme) {\n\t\t\t\tthis._registeredBaseTheme = baseThemes.reduce((finalBaseTheme, baseTheme) => {\n\t\t\t\t\tconst { [THEME_KEY]: key, ...classes } = baseTheme;\n\t\t\t\t\tthis._registeredBaseThemeKeys.push(key);\n\t\t\t\t\treturn { ...finalBaseTheme, ...classes };\n\t\t\t\t}, {});\n\t\t\t\tthis._baseThemeClassesReverseLookup = createThemeClassesLookup(baseThemes);\n\t\t\t}\n\n\t\t\tthis._theme = this._registeredBaseThemeKeys.reduce((baseTheme, themeKey) => {\n\t\t\t\treturn { ...baseTheme, ...theme[themeKey] };\n\t\t\t}, {});\n\n\t\t\tthis._recalculateClasses = false;\n\t\t}\n\t}\n\n\treturn Themed;\n}\n\nexport default ThemedMixin;\n"]}