-
Notifications
You must be signed in to change notification settings - Fork 8
/
vaadin-themable-mixin.html
93 lines (79 loc) · 3.28 KB
/
vaadin-themable-mixin.html
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
<link rel="import" href="../polymer/lib/elements/dom-module.html">
<link rel="import" href="vaadin-theme-property-mixin.html">
<script>
/**
* @namespace Vaadin
*/
window.Vaadin = window.Vaadin || {};
/**
* @polymerMixin
* @memberof Vaadin
* @mixes Vaadin.ThemePropertyMixin
*/
Vaadin.ThemableMixin = superClass => class VaadinThemableMixin extends Vaadin.ThemePropertyMixin(superClass) {
/** @protected */
static finalize() {
super.finalize();
const template = this.prototype._template;
const hasOwnTemplate = this.template && this.template.parentElement && this.template.parentElement.id === this.is;
const inheritedTemplate = Object.getPrototypeOf(this.prototype)._template;
if (inheritedTemplate && !hasOwnTemplate) {
// The element doesn't define its own template -> include the theme modules from the inherited template
Array.from(inheritedTemplate.content.querySelectorAll('style[include]')).forEach(s => {
this._includeStyle(s.getAttribute('include'), template);
});
}
this._includeMatchingThemes(template);
}
/** @private */
static _includeMatchingThemes(template) {
const domModule = Polymer.DomModule;
const modules = domModule.prototype.modules;
let hasThemes = false;
const defaultModuleName = this.is + '-default-theme';
Object.keys(modules)
.sort((moduleNameA, moduleNameB) => {
const vaadinA = moduleNameA.indexOf('vaadin-') === 0;
const vaadinB = moduleNameB.indexOf('vaadin-') === 0;
const vaadinThemePrefixes = ['lumo-', 'material-'];
const vaadinThemeA = vaadinThemePrefixes.filter(prefix => moduleNameA.indexOf(prefix) === 0).length > 0;
const vaadinThemeB = vaadinThemePrefixes.filter(prefix => moduleNameB.indexOf(prefix) === 0).length > 0;
if (vaadinA !== vaadinB) {
// Include vaadin core styles first
return vaadinA ? -1 : 1;
} else if (vaadinThemeA !== vaadinThemeB) {
// Include vaadin theme styles after that
return vaadinThemeA ? -1 : 1;
} else {
// Lastly include custom styles so they override all vaadin styles
return 0;
}
})
.forEach(moduleName => {
if (moduleName !== defaultModuleName) {
const themeFor = modules[moduleName].getAttribute('theme-for');
if (themeFor) {
themeFor.split(' ').forEach(themeForToken => {
if (new RegExp('^' + themeForToken.split('*').join('.*') + '$').test(this.is)) {
hasThemes = true;
this._includeStyle(moduleName, template);
}
});
}
}
});
if (!hasThemes && modules[defaultModuleName]) {
// No theme modules found, include the default module if it exists
this._includeStyle(defaultModuleName, template);
}
}
/** @private */
static _includeStyle(moduleName, template) {
if (template && !template.content.querySelector(`style[include="${moduleName}"]`)) {
const styleEl = document.createElement('style');
styleEl.setAttribute('include', moduleName);
template.content.appendChild(styleEl);
}
}
};
</script>