This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathThemed.ts
205 lines (178 loc) · 5.57 KB
/
Themed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { Constructor, WidgetProperties, SupportedClassName } from './../interfaces';
import { Registry } from './../Registry';
import { Injector } from './../Injector';
import { inject } from './../decorators/inject';
import { WidgetBase } from './../WidgetBase';
import { handleDecorator } from './../decorators/handleDecorator';
import { diffProperty } from './../decorators/diffProperty';
import { shallow } from './../diff';
/**
* A lookup object for available class names
*/
export type ClassNames = {
[key: string]: string;
};
/**
* A lookup object for available widget classes names
*/
export interface Theme {
[key: string]: object;
}
/**
* Properties required for the Themed mixin
*/
export interface ThemedProperties<T = ClassNames> extends WidgetProperties {
injectedTheme?: any;
theme?: Theme;
extraClasses?: { [P in keyof T]?: string };
}
const THEME_KEY = ' _key';
export const INJECTED_THEME_KEY = Symbol('theme');
/**
* Interface for the ThemedMixin
*/
export interface ThemedMixin<T = ClassNames> {
theme(classes: SupportedClassName): SupportedClassName;
theme(classes: SupportedClassName[]): SupportedClassName[];
properties: ThemedProperties<T>;
}
/**
* Decorator for base css classes
*/
export function theme(theme: {}) {
return handleDecorator((target) => {
target.addDecorator('baseThemeClasses', theme);
});
}
/**
* Creates a reverse lookup for the classes passed in via the `theme` function.
*
* @param classes The baseClasses object
* @requires
*/
function createThemeClassesLookup(classes: ClassNames[]): ClassNames {
return classes.reduce(
(currentClassNames, baseClass) => {
Object.keys(baseClass).forEach((key: string) => {
currentClassNames[baseClass[key]] = key;
});
return currentClassNames;
},
<ClassNames>{}
);
}
/**
* Convenience function that is given a theme and an optional registry, the theme
* injector is defined against the registry, returning the theme.
*
* @param theme the theme to set
* @param themeRegistry registry to define the theme injector against. Defaults
* to the global registry
*
* @returns the theme injector used to set the theme
*/
export function registerThemeInjector(theme: any, themeRegistry: Registry): Injector {
const themeInjector = new Injector(theme);
themeRegistry.defineInjector(INJECTED_THEME_KEY, (invalidator) => {
themeInjector.setInvalidator(invalidator);
return () => themeInjector.get();
});
return themeInjector;
}
/**
* Function that returns a class decorated with with Themed functionality
*/
export function ThemedMixin<E, T extends Constructor<WidgetBase<ThemedProperties<E>>>>(
Base: T
): Constructor<ThemedMixin<E>> & T {
@inject({
name: INJECTED_THEME_KEY,
getProperties: (theme: Theme, properties: ThemedProperties): ThemedProperties => {
if (!properties.theme) {
return { theme };
}
return {};
}
})
abstract class Themed extends Base {
public abstract properties: ThemedProperties<E>;
/**
* The Themed baseClasses
*/
private _registeredBaseTheme: ClassNames | undefined;
/**
* Registered base theme keys
*/
private _registeredBaseThemeKeys: string[] = [];
/**
* Reverse lookup of the theme classes
*/
private _baseThemeClassesReverseLookup: ClassNames | undefined;
/**
* Indicates if classes meta data need to be calculated.
*/
private _recalculateClasses = true;
/**
* Loaded theme
*/
private _theme: ClassNames = {};
public theme(classes: SupportedClassName): SupportedClassName;
public theme(classes: SupportedClassName[]): SupportedClassName[];
public theme(classes: SupportedClassName | SupportedClassName[]): SupportedClassName | SupportedClassName[] {
if (this._recalculateClasses) {
this._recalculateThemeClasses();
}
if (Array.isArray(classes)) {
return classes.map((className) => this._getThemeClass(className));
}
return this._getThemeClass(classes);
}
/**
* Function fired when `theme` or `extraClasses` are changed.
*/
@diffProperty('theme', shallow)
@diffProperty('extraClasses', shallow)
protected onPropertiesChanged() {
this._recalculateClasses = true;
}
private _getThemeClass(className: SupportedClassName): SupportedClassName {
if (className === undefined || className === null) {
return className;
}
const extraClasses = this.properties.extraClasses || ({} as any);
const themeClassName = this._baseThemeClassesReverseLookup![className];
let resultClassNames: string[] = [];
if (!themeClassName) {
console.warn(`Class name: '${className}' not found in theme`);
return null;
}
if (extraClasses[themeClassName]) {
resultClassNames.push(extraClasses[themeClassName]);
}
if (this._theme[themeClassName]) {
resultClassNames.push(this._theme[themeClassName]);
} else {
resultClassNames.push(this._registeredBaseTheme![themeClassName]);
}
return resultClassNames.join(' ');
}
private _recalculateThemeClasses() {
const { theme = {} } = this.properties;
const baseThemes = this.getDecorator('baseThemeClasses');
if (!this._registeredBaseTheme) {
this._registeredBaseTheme = baseThemes.reduce((finalBaseTheme, baseTheme) => {
const { [THEME_KEY]: key, ...classes } = baseTheme;
this._registeredBaseThemeKeys.push(key);
return { ...finalBaseTheme, ...classes };
}, {});
this._baseThemeClassesReverseLookup = createThemeClassesLookup(baseThemes);
}
this._theme = this._registeredBaseThemeKeys.reduce((baseTheme, themeKey) => {
return { ...baseTheme, ...theme[themeKey] };
}, {});
this._recalculateClasses = false;
}
}
return Themed;
}
export default ThemedMixin;