/
mdColors.app.js
131 lines (113 loc) · 5.82 KB
/
mdColors.app.js
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
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* https://github.com/angular/material/issues/1269
* https://gist.github.com/senthilprabhut/dd2147ebabc89bf223e7
*/
(function() {
'use strict';
var _$mdThemingProvider;
angular
.module('mdColors', ['ngMaterial'])
.config(configure)
.run(runBlock);
/**
* @ngInject
*/
configure.$inject = ['$mdThemingProvider'];
function configure($mdThemingProvider) {
_$mdThemingProvider = $mdThemingProvider;
}
/**
* @ngInject
*/
runBlock.$inject = ['$interpolate', '$document', '$log'];
function runBlock($interpolate, $document, $log) {
function buildCssSelectors(selectors) {
var result = selectors.join('');
return result;
}
var fgDefault = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-fg']) + ' { color:{{value}};}'),
bgDefault = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-bg']) + ' { background-color:{{value}};}'),
bdrDefault = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-bdr']) + ' { border-color:{{value}};}'),
fgDefaultHue = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-{{hue}}','.md-fg']) + ' { color:{{value}};}'),
bgDefaultHue = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-{{hue}}','.md-bg']) + ' { background-color:{{value}};}'),
fgColor = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-{{palette}}','.md-fg']) + ' { color:{{value}};}'),
bgColor = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-{{palette}}','.md-bg']) + ' { background-color:{{value}};}'),
bdrColor = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-{{palette}}','.md-bdr']) + ' { border-color:{{value}};}'),
fgHue = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-{{palette}}.md-{{hue}}','.md-fg']) + ' { color:{{value}};}'),
bgHue = $interpolate(buildCssSelectors(['.md-{{theme}}-theme','.md-{{palette}}.md-{{hue}}','.md-bg']) + ' { background-color:{{value}};}'),
customSheet = getStyleSheet(),
index = 0;
// Clear out old rules from stylesheet
while (customSheet.cssRules.length > 0 ) {
customSheet.deleteRule(0);
}
angular.forEach(_$mdThemingProvider._THEMES, function(theme, themeName){
// Add default selectors - primary is the default palette
addRule(fgDefault, bgDefault, themeName, 'primary',
_$mdThemingProvider._PALETTES[theme.colors.primary.name][theme.colors.primary.hues.default]);
addRule(fgDefaultHue, bgDefaultHue, themeName, 'primary',
_$mdThemingProvider._PALETTES[theme.colors.primary.name][theme.colors.primary.hues['hue-2'] ], 'hue-2');
addRule(fgDefaultHue, bgDefaultHue, themeName, 'primary',
_$mdThemingProvider._PALETTES[theme.colors.primary.name][theme.colors.primary.hues['hue-3'] ], 'hue-3');
addRule(fgDefaultHue, bgDefaultHue, themeName, 'primary',
_$mdThemingProvider._PALETTES[theme.colors.primary.name][theme.colors.primary.hues['hue-1'] ], 'hue-1');
addBorderRule(bdrDefault, themeName, 'primary',
_$mdThemingProvider._PALETTES[theme.colors.primary.name][theme.colors.primary.hues.default]);
// Add selectors for palettes - accent, background, primary and warn
angular.forEach(theme.colors, function(color, paletteName){
addRule(fgColor, bgColor, themeName, paletteName, _$mdThemingProvider._PALETTES[color.name][color.hues.default]);
addBorderRule(bdrColor, themeName, paletteName, _$mdThemingProvider._PALETTES[color.name][color.hues.default]);
addRule(fgHue, bgHue, themeName, paletteName, _$mdThemingProvider._PALETTES[color.name][color.hues['hue-2'] ], 'hue-2');
addRule(fgHue, bgHue, themeName, paletteName, _$mdThemingProvider._PALETTES[color.name][color.hues['hue-3'] ], 'hue-3');
addRule(fgHue, bgHue, themeName, paletteName, _$mdThemingProvider._PALETTES[color.name][color.hues['hue-1'] ], 'hue-1');
});
//$log.debug(_.pluck(customSheet.cssRules, 'cssText').join("\n"));
});
function addRule(fgInterpolate, bgInterpolate, themeName, paletteName, colorArray, hueName){
// Set up interpolation functions to build css rules.
if (!colorArray) return;
var colorValue = 'rgb(' + colorArray.value[0] + ',' + colorArray.value[1] + ',' + colorArray.value[2] + ')';
// Insert foreground color rule
customSheet.insertRule(fgInterpolate({
theme: themeName,
palette: paletteName,
value: colorValue,
hue: hueName
}), index);
index += 1;
// Insert background color rule
customSheet.insertRule(bgInterpolate({
theme: themeName,
palette: paletteName,
value: colorValue,
hue: hueName
}), index);
index += 1;
}
function addBorderRule(bdrInterpolate, themeName, paletteName, colorArray, hueName){
// Set up interpolation functions to build css rule for border color.
if (!colorArray) return;
var colorValue = 'rgb(' + colorArray.value[0] + ',' + colorArray.value[1] + ',' + colorArray.value[2] + ')';
customSheet.insertRule(bdrInterpolate({
theme: themeName,
palette: paletteName,
value: colorValue,
hue: hueName
}), index);
index += 1;
}
function getStyleSheet() {
// function to add a dynamic style-sheet to the document
var style = $document[0].head.querySelector('style[title="Dynamic-Generated-by-mdColors"]');
if (style === null) {
style = $document[0].createElement('style');
style.title = 'Dynamic-Generated-by-mdColors';
// WebKit hack... (not sure if still needed)
style.appendChild($document[0].createTextNode(''));
$document[0].head.appendChild(style);
}
return style.sheet;
}
}
})();