This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy paththeming.js
1407 lines (1247 loc) · 47.6 KB
/
theming.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(angular) {
'use strict';
/**
* @ngdoc module
* @name material.core.theming
* @description
* Theming
*/
angular.module('material.core.theming', ['material.core.theming.palette', 'material.core.meta'])
.directive('mdTheme', ThemingDirective)
.directive('mdThemable', ThemableDirective)
.directive('mdThemesDisabled', disableThemesDirective)
.provider('$mdTheming', ThemingProvider)
.config(detectDisabledThemes)
.run(generateAllThemes);
/**
* Detect if the HTML or the BODY tags has a [md-themes-disabled] attribute
* If yes, then immediately disable all theme stylesheet generation and DOM injection
*/
/**
* @ngInject
*/
function detectDisabledThemes($mdThemingProvider) {
var isDisabled = !!document.querySelector('[md-themes-disabled]');
$mdThemingProvider.disableTheming(isDisabled);
}
/**
* @ngdoc service
* @name $mdThemingProvider
* @module material.core.theming
*
* @description Provider to configure the `$mdTheming` service.
*
* ### Default Theme
* The `$mdThemingProvider` uses by default the following theme configuration:
*
* - Primary Palette: `Blue`
* - Accent Palette: `Pink`
* - Warn Palette: `Deep-Orange`
* - Background Palette: `Grey`
*
* If you don't want to use the `md-theme` directive on the elements itself, you may want to overwrite
* the default theme.<br/>
* This can be done by using the following markup.
*
* <hljs lang="js">
* myAppModule.config(function($mdThemingProvider) {
* $mdThemingProvider
* .theme('default')
* .primaryPalette('blue')
* .accentPalette('teal')
* .warnPalette('red')
* .backgroundPalette('grey');
* });
* </hljs>
*
* ### Dynamic Themes
*
* By default, if you change a theme at runtime, the `$mdTheming` service will not detect those changes.<br/>
* If you have an application, which changes its theme on runtime, you have to enable theme watching.
*
* <hljs lang="js">
* myAppModule.config(function($mdThemingProvider) {
* // Enable theme watching.
* $mdThemingProvider.alwaysWatchTheme(true);
* });
* </hljs>
*
* ### Custom Theme Styles
*
* Sometimes you may want to use your own theme styles for some custom components.<br/>
* You are able to register your own styles by using the following markup.
*
* <hljs lang="js">
* myAppModule.config(function($mdThemingProvider) {
* // Register our custom stylesheet into the theming provider.
* $mdThemingProvider.registerStyles(STYLESHEET);
* });
* </hljs>
*
* The `registerStyles` method only accepts strings as value, so you're actually not able to load an external
* stylesheet file into the `$mdThemingProvider`.
*
* If it's necessary to load an external stylesheet, we suggest using a bundler, which supports including raw content,
* like [raw-loader](https://github.com/webpack/raw-loader) for `webpack`.
*
* <hljs lang="js">
* myAppModule.config(function($mdThemingProvider) {
* // Register your custom stylesheet into the theming provider.
* $mdThemingProvider.registerStyles(require('../styles/my-component.theme.css'));
* });
* </hljs>
*
* ### Browser color
*
* Enables browser header coloring
* for more info please visit:
* https://developers.google.com/web/fundamentals/design-and-ui/browser-customization/theme-color
*
* Options parameter: <br/>
* `theme` - A defined theme via `$mdThemeProvider` to use the palettes from. Default is `default` theme. <br/>
* `palette` - Can be any one of the basic material design palettes, extended defined palettes and 'primary',
* 'accent', 'background' and 'warn'. Default is `primary`. <br/>
* `hue` - The hue from the selected palette. Default is `800`<br/>
*
* <hljs lang="js">
* myAppModule.config(function($mdThemingProvider) {
* // Enable browser color
* $mdThemingProvider.enableBrowserColor({
* theme: 'myTheme', // Default is 'default'
* palette: 'accent', // Default is 'primary', any basic material palette and extended palettes are available
* hue: '200' // Default is '800'
* });
* });
* </hljs>
*/
/**
* Some Example Valid Theming Expressions
* =======================================
*
* Intention group expansion: (valid for primary, accent, warn, background)
*
* {{primary-100}} - grab shade 100 from the primary palette
* {{primary-100-0.7}} - grab shade 100, apply opacity of 0.7
* {{primary-100-contrast}} - grab shade 100's contrast color
* {{primary-hue-1}} - grab the shade assigned to hue-1 from the primary palette
* {{primary-hue-1-0.7}} - apply 0.7 opacity to primary-hue-1
* {{primary-color}} - Generates .md-hue-1, .md-hue-2, .md-hue-3 with configured shades set for each hue
* {{primary-color-0.7}} - Apply 0.7 opacity to each of the above rules
* {{primary-contrast}} - Generates .md-hue-1, .md-hue-2, .md-hue-3 with configured contrast (ie. text) color shades set for each hue
* {{primary-contrast-0.7}} - Apply 0.7 opacity to each of the above rules
* {{primary-contrast-divider}} - Apply divider opacity to contrast color
*
* Foreground expansion: Applies rgba to black/white foreground text
*
* Old Foreground Expressions:
* {{foreground-1}} - used for primary text
* {{foreground-2}} - used for secondary text/divider
* {{foreground-3}} - used for disabled text
* {{foreground-4}} - used for dividers
*
* New Foreground Expressions:
*
* Apply primary text color for contrasting with default background
* {{background-default-contrast}} - default opacity
* {{background-default-contrast-secondary}} - opacity for secondary text
* {{background-default-contrast-hint}} - opacity for hints and placeholders
* {{background-default-contrast-disabled}} - opacity for disabled text
* {{background-default-contrast-divider}} - opacity for dividers
*
* Apply contrast color for specific shades
* {{background-50-contrast-icon}} - Apply contrast color for icon on background's shade 50 hue
*/
// In memory generated CSS rules; registered by theme.name
var GENERATED = { };
// In memory storage of defined themes and color palettes (both loaded by CSS, and user specified)
var PALETTES;
// Text colors are automatically generated based on background color when not specified
// Custom palettes can provide override colors
// @see https://material.io/archive/guidelines/style/color.html#color-usability
var DARK_FOREGROUND = {
name: 'dark',
};
var LIGHT_FOREGROUND = {
name: 'light',
};
var DARK_SHADOW = '1px 1px 0px rgba(0,0,0,0.4), -1px -1px 0px rgba(0,0,0,0.4)';
var LIGHT_SHADOW = '';
var DARK_CONTRAST_COLOR = colorToRgbaArray('rgba(0,0,0,0.87)');
var LIGHT_CONTRAST_COLOR = colorToRgbaArray('rgba(255,255,255,0.87)');
var STRONG_LIGHT_CONTRAST_COLOR = colorToRgbaArray('rgb(255,255,255)');
var THEME_COLOR_TYPES = ['primary', 'accent', 'warn', 'background'];
var DEFAULT_COLOR_TYPE = 'primary';
// A color in a theme will use these hues by default, if not specified by user.
var LIGHT_DEFAULT_HUES = {
'accent': {
'default': 'A200',
'hue-1': 'A100',
'hue-2': 'A400',
'hue-3': 'A700'
},
'background': {
'default': '50',
'hue-1': 'A100',
'hue-2': '100',
'hue-3': '300'
}
};
var DARK_DEFAULT_HUES = {
'background': {
'default': 'A400',
'hue-1': '800',
'hue-2': '900',
'hue-3': 'A200'
}
};
// Icon opacity values (active/inactive) from
// https://material.io/archive/guidelines/style/color.html#color-usability
var DARK_CONTRAST_OPACITY = {
'icon': 0.54,
'secondary': 0.54,
'disabled': 0.38,
'hint': 0.38,
'divider': 0.12,
};
var LIGHT_CONTRAST_OPACITY = {
'icon': 0.87,
'secondary': 0.7,
'disabled': 0.5,
'hint': 0.5,
'divider': 0.12
};
// Icon opacity values (active/inactive) from
// https://material.io/archive/guidelines/style/color.html#color-usability
var STRONG_LIGHT_CONTRAST_OPACITY = {
'icon': 1.0,
'secondary': 0.7,
'disabled': 0.5,
'hint': 0.5,
'divider': 0.12
};
THEME_COLOR_TYPES.forEach(function(colorType) {
// Color types with unspecified default hues will use these default hue values
var defaultDefaultHues = {
'default': '500',
'hue-1': '300',
'hue-2': '800',
'hue-3': 'A100'
};
if (!LIGHT_DEFAULT_HUES[colorType]) LIGHT_DEFAULT_HUES[colorType] = defaultDefaultHues;
if (!DARK_DEFAULT_HUES[colorType]) DARK_DEFAULT_HUES[colorType] = defaultDefaultHues;
});
var VALID_HUE_VALUES = [
'50', '100', '200', '300', '400', '500', '600',
'700', '800', '900', 'A100', 'A200', 'A400', 'A700'
];
var themeConfig = {
disableTheming : false, // Generate our themes at run time; also disable stylesheet DOM injection
generateOnDemand : false, // Whether or not themes are to be generated on-demand (vs. eagerly).
registeredStyles : [], // Custom styles registered to be used in the theming of custom components.
nonce : null // Nonce to be added as an attribute to the generated themes style tags.
};
/**
*
*/
function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
PALETTES = { };
var THEMES = { };
var themingProvider;
var alwaysWatchTheme = false;
var defaultTheme = 'default';
// Load JS Defined Palettes
angular.extend(PALETTES, $mdColorPalette);
// Default theme defined in core.js
/**
* Adds `theme-color` and `msapplication-navbutton-color` meta tags with the color parameter
* @param {string} color Hex value of the wanted browser color
* @returns {function} Remove function of the meta tags
*/
var setBrowserColor = function (color) {
// Chrome, Firefox OS and Opera
var removeChrome = $$mdMetaProvider.setMeta('theme-color', color);
// Windows Phone
var removeWindows = $$mdMetaProvider.setMeta('msapplication-navbutton-color', color);
return function () {
removeChrome();
removeWindows();
};
};
/**
* @ngdoc method
* @name $mdThemingProvider#enableBrowserColor
* @description
* Enables browser header coloring. For more info please visit
* <a href="https://developers.google.com/web/fundamentals/design-and-ui/browser-customization/theme-color">
* Web Fundamentals</a>.
* @param {object=} options Options for the browser color, which include:<br/>
* - `theme` - `{string}`: A defined theme via `$mdThemeProvider` to use the palettes from. Default is `default` theme. <br/>
* - `palette` - `{string}`: Can be any one of the basic material design palettes, extended defined palettes, or `primary`,
* `accent`, `background`, and `warn`. Default is `primary`.<br/>
* - `hue` - `{string}`: The hue from the selected palette. Default is `800`.<br/>
* @returns {function} Function that removes the browser coloring when called.
*/
var enableBrowserColor = function (options) {
options = angular.isObject(options) ? options : {};
var theme = options.theme || 'default';
var hue = options.hue || '800';
var palette = PALETTES[options.palette] ||
PALETTES[THEMES[theme].colors[options.palette || 'primary'].name];
var color = angular.isObject(palette[hue]) ? palette[hue].hex : palette[hue];
if (color.substr(0, 1) !== '#') color = '#' + color;
return setBrowserColor(color);
};
return themingProvider = {
definePalette: definePalette,
extendPalette: extendPalette,
theme: registerTheme,
/**
* return a read-only clone of the current theme configuration
*/
configuration : function() {
return angular.extend({ }, themeConfig, {
defaultTheme : defaultTheme,
alwaysWatchTheme : alwaysWatchTheme,
registeredStyles : [].concat(themeConfig.registeredStyles)
});
},
/**
* @ngdoc method
* @name $mdThemingProvider#disableTheming
* @description
* An easier way to disable theming without having to use `.constant("$MD_THEME_CSS","");`.
* This disables all dynamic theme style sheet generations and injections.
* @param {boolean=} isDisabled Disable all dynamic theme style sheet generations and injections
* if `true` or `undefined`.
*/
disableTheming: function(isDisabled) {
themeConfig.disableTheming = angular.isUndefined(isDisabled) || !!isDisabled;
},
/**
* @ngdoc method
* @name $mdThemingProvider#registerStyles
* @param {string} styles The styles to be appended to AngularJS Material's built in theme CSS.
*/
registerStyles: function(styles) {
themeConfig.registeredStyles.push(styles);
},
/**
* @ngdoc method
* @name $mdThemingProvider#setNonce
* @param {string} nonceValue The nonce to be added as an attribute to the theme style tags.
* Setting a value allows the use of CSP policy without using the `'unsafe-inline'` directive.
* The string must already be base64 encoded. You can use `btoa(string)` to do this encoding.
* In your CSP's `style-src`, you would then add an entry for `'nonce-nonceValue'`.
*/
setNonce: function(nonceValue) {
themeConfig.nonce = nonceValue;
},
generateThemesOnDemand: function(onDemand) {
themeConfig.generateOnDemand = onDemand;
},
/**
* @ngdoc method
* @name $mdThemingProvider#setDefaultTheme
* @param {string} theme Default theme name to be applied to elements.
* Default value is `default`.
*/
setDefaultTheme: function(theme) {
defaultTheme = theme;
},
/**
* @ngdoc method
* @name $mdThemingProvider#alwaysWatchTheme
* @param {boolean} alwaysWatch Whether or not to always watch themes for changes and re-apply
* classes when they change. Default is `false`. Enabling can reduce performance.
*/
alwaysWatchTheme: function(alwaysWatch) {
alwaysWatchTheme = alwaysWatch;
},
enableBrowserColor: enableBrowserColor,
$get: ThemingService,
_LIGHT_DEFAULT_HUES: LIGHT_DEFAULT_HUES,
_DARK_DEFAULT_HUES: DARK_DEFAULT_HUES,
_PALETTES: PALETTES,
_THEMES: THEMES,
_parseRules: parseRules,
_rgba: rgba
};
/**
* @ngdoc method
* @name $mdThemingProvider#definePalette
* @description
* In the event that you need to define a custom color palette, you can use this function to
* make it available to your theme for use in its intention groups.<br>
* Note that you must specify all hues in the definition map.
* @param {string} name Name of palette being defined
* @param {object} map Palette definition that includes hue definitions and contrast colors:
* - `'50'` - `{string}`: HEX color
* - `'100'` - `{string}`: HEX color
* - `'200'` - `{string}`: HEX color
* - `'300'` - `{string}`: HEX color
* - `'400'` - `{string}`: HEX color
* - `'500'` - `{string}`: HEX color
* - `'600'` - `{string}`: HEX color
* - `'700'` - `{string}`: HEX color
* - `'800'` - `{string}`: HEX color
* - `'900'` - `{string}`: HEX color
* - `'A100'` - `{string}`: HEX color
* - `'A200'` - `{string}`: HEX color
* - `'A400'` - `{string}`: HEX color
* - `'A700'` - `{string}`: HEX color
* - `'contrastDefaultColor'` - `{string}`: `light` or `dark`
* - `'contrastDarkColors'` - `{string[]}`: Hues which should use dark contrast colors (i.e. raised button text).
* For example: `['50', '100', '200', '300', '400', 'A100']`.
* - `'contrastLightColors'` - `{string[]}`: Hues which should use light contrast colors (i.e. raised button text).
* For example: `['500', '600', '700', '800', '900', 'A200', 'A400', 'A700']`.
*/
function definePalette(name, map) {
map = map || {};
PALETTES[name] = checkPaletteValid(name, map);
return themingProvider;
}
/**
* @ngdoc method
* @name $mdThemingProvider#extendPalette
* @description
* Sometimes it is easier to extend an existing color palette and then change a few properties,
* rather than defining a whole new palette.
* @param {string} name Name of palette being extended
* @param {object} map Palette definition that includes optional hue definitions and contrast colors:
* - `'50'` - `{string}`: HEX color
* - `'100'` - `{string}`: HEX color
* - `'200'` - `{string}`: HEX color
* - `'300'` - `{string}`: HEX color
* - `'400'` - `{string}`: HEX color
* - `'500'` - `{string}`: HEX color
* - `'600'` - `{string}`: HEX color
* - `'700'` - `{string}`: HEX color
* - `'800'` - `{string}`: HEX color
* - `'900'` - `{string}`: HEX color
* - `'A100'` - `{string}`: HEX color
* - `'A200'` - `{string}`: HEX color
* - `'A400'` - `{string}`: HEX color
* - `'A700'` - `{string}`: HEX color
* - `'contrastDefaultColor'` - `{string}`: `light` or `dark`
* - `'contrastDarkColors'` - `{string[]}`: Hues which should use dark contrast colors (i.e. raised button text).
* For example: `['50', '100', '200', '300', '400', 'A100']`.
* - `'contrastLightColors'` - `{string[]}`: Hues which should use light contrast colors (i.e. raised button text).
* For example: `['500', '600', '700', '800', '900', 'A200', 'A400', 'A700']`.
* @returns {object} A new object which is a copy of the given palette, `name`,
* with variables from `map` overwritten.
*/
function extendPalette(name, map) {
return checkPaletteValid(name, angular.extend({}, PALETTES[name] || {}, map));
}
// Make sure that palette has all required hues
function checkPaletteValid(name, map) {
var missingColors = VALID_HUE_VALUES.filter(function(field) {
return !map[field];
});
if (missingColors.length) {
throw new Error("Missing colors %1 in palette %2!"
.replace('%1', missingColors.join(', '))
.replace('%2', name));
}
return map;
}
/**
* @ngdoc method
* @name $mdThemingProvider#theme
* @description
* Register a theme (which is a collection of color palettes); i.e. `warn`, `accent`,
* `background`, and `primary`.<br>
* Optionally inherit from an existing theme.
* @param {string} name Name of theme being registered
* @param {string=} inheritFrom Existing theme name to inherit from
*/
function registerTheme(name, inheritFrom) {
if (THEMES[name]) return THEMES[name];
inheritFrom = inheritFrom || 'default';
var parentTheme = typeof inheritFrom === 'string' ? THEMES[inheritFrom] : inheritFrom;
var theme = new Theme(name);
if (parentTheme) {
angular.forEach(parentTheme.colors, function(color, colorType) {
theme.colors[colorType] = {
name: color.name,
// Make sure a COPY of the hues is given to the child color,
// not the same reference.
hues: angular.extend({}, color.hues)
};
});
}
THEMES[name] = theme;
return theme;
}
function Theme(name) {
var self = this;
self.name = name;
self.colors = {};
self.dark = setDark;
setDark(false);
function setDark(isDark) {
isDark = arguments.length === 0 ? true : !!isDark;
// If no change, abort
if (isDark === self.isDark) return;
self.isDark = isDark;
self.foregroundPalette = self.isDark ? LIGHT_FOREGROUND : DARK_FOREGROUND;
self.foregroundShadow = self.isDark ? DARK_SHADOW : LIGHT_SHADOW;
// Light and dark themes have different default hues.
// Go through each existing color type for this theme, and for every
// hue value that is still the default hue value from the previous light/dark setting,
// set it to the default hue value from the new light/dark setting.
var newDefaultHues = self.isDark ? DARK_DEFAULT_HUES : LIGHT_DEFAULT_HUES;
var oldDefaultHues = self.isDark ? LIGHT_DEFAULT_HUES : DARK_DEFAULT_HUES;
angular.forEach(newDefaultHues, function(newDefaults, colorType) {
var color = self.colors[colorType];
var oldDefaults = oldDefaultHues[colorType];
if (color) {
for (var hueName in color.hues) {
if (color.hues[hueName] === oldDefaults[hueName]) {
color.hues[hueName] = newDefaults[hueName];
}
}
}
});
return self;
}
THEME_COLOR_TYPES.forEach(function(colorType) {
var defaultHues = (self.isDark ? DARK_DEFAULT_HUES : LIGHT_DEFAULT_HUES)[colorType];
self[colorType + 'Palette'] = function setPaletteType(paletteName, hues) {
var color = self.colors[colorType] = {
name: paletteName,
hues: angular.extend({}, defaultHues, hues)
};
Object.keys(color.hues).forEach(function(name) {
if (!defaultHues[name]) {
throw new Error("Invalid hue name '%1' in theme %2's %3 color %4. Available hue names: %4"
.replace('%1', name)
.replace('%2', self.name)
.replace('%3', paletteName)
.replace('%4', Object.keys(defaultHues).join(', '))
);
}
});
Object.keys(color.hues).map(function(key) {
return color.hues[key];
}).forEach(function(hueValue) {
if (VALID_HUE_VALUES.indexOf(hueValue) === -1) {
throw new Error("Invalid hue value '%1' in theme %2's %3 color %4. Available hue values: %5"
.replace('%1', hueValue)
.replace('%2', self.name)
.replace('%3', colorType)
.replace('%4', paletteName)
.replace('%5', VALID_HUE_VALUES.join(', '))
);
}
});
return self;
};
});
}
/**
* @ngdoc service
* @name $mdTheming
* @module material.core.theming
* @description
* Service that makes an element apply theming related <b>classes</b> to itself.
*
* For more information on the hue objects, their default values, as well as valid hue values, please visit <a ng-href="Theming/03_configuring_a_theme#specifying-custom-hues-for-color-intentions">the custom hues section of Configuring a Theme</a>.
*
* <hljs lang="js">
* // Example component directive that we want to apply theming classes to.
* app.directive('myFancyDirective', function($mdTheming) {
* return {
* restrict: 'AE',
* link: function(scope, element, attrs) {
* // Initialize the service using our directive's element
* $mdTheming(element);
*
* $mdTheming.defineTheme('myTheme', {
* primary: 'blue',
* primaryHues: {
* default: '500',
* hue-1: '300',
* hue-2: '900',
* hue-3: 'A100'
* },
* accent: 'pink',
* accentHues: {
* default: '600',
* hue-1: '300',
* hue-2: '200',
* hue-3: 'A500'
* },
* warn: 'red',
* // It's not necessary to specify all hues in the object.
* warnHues: {
* default: '200',
* hue-3: 'A100'
* },
* // It's not necessary to specify custom hues at all.
* background: 'grey',
* dark: true
* });
* // Your directive's custom code here.
* }
* };
* });
* </hljs>
* @param {element=} element Element that will have theming classes applied to it.
*/
/**
* @ngdoc property
* @name $mdTheming#THEMES
* @description
* Property to get all the themes defined
* @returns {object} All the themes defined with their properties.
*/
/**
* @ngdoc property
* @name $mdTheming#PALETTES
* @description
* Property to get all the palettes defined
* @returns {object} All the palettes defined with their colors.
*/
/**
* @ngdoc method
* @name $mdTheming#registered
* @description
* Determine is specified theme name is a valid, registered theme
* @param {string} themeName the theme to check if registered
* @returns {boolean} whether the theme is registered or not
*/
/**
* @ngdoc method
* @name $mdTheming#defaultTheme
* @description
* Returns the default theme
* @returns {string} The default theme
*/
/**
* @ngdoc method
* @name $mdTheming#generateTheme
* @description
* Lazy generate themes - by default, every theme is generated when defined.
* You can disable this in the configuration section using the
* `$mdThemingProvider.generateThemesOnDemand(true);`
*
* The theme name that is passed in must match the name of the theme that was defined as part of
* the configuration block.
*
* @param {string} name theme name to generate
*/
/**
* @ngdoc method
* @name $mdTheming#setBrowserColor
* @description
* Enables browser header coloring. For more info please visit
* <a href="https://developers.google.com/web/fundamentals/design-and-ui/browser-customization/theme-color">
* Web Fundamentals</a>.
* @param {object=} options Options for the browser color, which include:<br/>
* - `theme` - `{string}`: A defined theme via `$mdThemeProvider` to use the palettes from.
* Default is `default` theme. <br/>
* - `palette` - `{string}`: Can be any one of the basic material design palettes, extended
* defined palettes, or `primary`, `accent`, `background`, and `warn`. Default is `primary`.
* <br/>
* - `hue` - `{string}`: The hue from the selected palette. Default is `800`.<br/>
* @returns {function} Function that removes the browser coloring when called.
*/
/**
* @ngdoc method
* @name $mdTheming#defineTheme
* @description
* Dynamically define a theme by using an options object that contains palette names.
*
* @param {string} name Theme name to define
* @param {object} options Theme definition options
*
* Options are:<br/>
* - `primary` - `{string}`: The name of the primary palette to use in the theme.<br/>
* - `primaryHues` - `{object=}`: Override hues for primary palette.<br/>
* - `accent` - `{string}`: The name of the accent palette to use in the theme.<br/>
* - `accentHues` - `{object=}`: Override hues for accent palette.<br/>
* - `warn` - `{string}`: The name of the warn palette to use in the theme.<br/>
* - `warnHues` - `{object=}`: Override hues for warn palette.<br/>
* - `background` - `{string}`: The name of the background palette to use in the theme.<br/>
* - `backgroundHues` - `{object=}`: Override hues for background palette.<br/>
* - `dark` - `{boolean}`: Indicates if it's a dark theme.<br/>
* @returns {Promise<string>} A resolved promise with the new theme name.
*/
/* @ngInject */
function ThemingService($rootScope, $mdUtil, $q, $log) {
// Allow us to be invoked via a linking function signature.
var applyTheme = function (scope, el) {
if (el === undefined) { el = scope; scope = undefined; }
if (scope === undefined) { scope = $rootScope; }
applyTheme.inherit(el, el);
};
Object.defineProperty(applyTheme, 'THEMES', {
get: function () {
return angular.extend({}, THEMES);
}
});
Object.defineProperty(applyTheme, 'PALETTES', {
get: function () {
return angular.extend({}, PALETTES);
}
});
Object.defineProperty(applyTheme, 'ALWAYS_WATCH', {
get: function () {
return alwaysWatchTheme;
}
});
applyTheme.inherit = inheritTheme;
applyTheme.registered = registered;
applyTheme.defaultTheme = function() { return defaultTheme; };
applyTheme.generateTheme = function(name) { generateTheme(THEMES[name], name, themeConfig.nonce); };
applyTheme.defineTheme = function(name, options) {
options = options || {};
var theme = registerTheme(name);
if (options.primary) {
theme.primaryPalette(options.primary, options.primaryHues);
}
if (options.accent) {
theme.accentPalette(options.accent, options.accentHues);
}
if (options.warn) {
theme.warnPalette(options.warn, options.warnHues);
}
if (options.background) {
theme.backgroundPalette(options.background, options.backgroundHues);
}
if (options.dark){
theme.dark();
}
this.generateTheme(name);
return $q.resolve(name);
};
applyTheme.setBrowserColor = enableBrowserColor;
return applyTheme;
/**
* Determine is specified theme name is a valid, registered theme
*/
function registered(themeName) {
if (themeName === undefined || themeName === '') return true;
return applyTheme.THEMES[themeName] !== undefined;
}
/**
* Get theme name for the element, then update with Theme CSS class
*/
function inheritTheme (el, parent) {
var ctrl = parent.controller('mdTheme') || el.data('$mdThemeController');
var scope = el.scope();
updateThemeClass(lookupThemeName());
if (ctrl) {
var watchTheme = alwaysWatchTheme ||
ctrl.$shouldWatch ||
$mdUtil.parseAttributeBoolean(el.attr('md-theme-watch'));
if (watchTheme || ctrl.isAsyncTheme) {
var clearNameWatcher = function () {
if (unwatch) {
unwatch();
unwatch = undefined;
}
};
var unwatch = ctrl.registerChanges(function(name) {
updateThemeClass(name);
if (!watchTheme) {
clearNameWatcher();
}
});
if (scope) {
scope.$on('$destroy', clearNameWatcher);
} else {
el.on('$destroy', clearNameWatcher);
}
}
}
/**
* Find the theme name from the parent controller or element data
*/
function lookupThemeName() {
// As a few components (dialog) add their controllers later, we should also watch for a controller init.
return ctrl && ctrl.$mdTheme || (defaultTheme === 'default' ? '' : defaultTheme);
}
/**
* Remove old theme class and apply a new one
* NOTE: if not a valid theme name, then the current name is not changed
*/
function updateThemeClass(theme) {
if (!theme) return;
if (!registered(theme)) {
$log.warn('Attempted to use unregistered theme \'' + theme + '\'. ' +
'Register it with $mdThemingProvider.theme().');
}
var oldTheme = el.data('$mdThemeName');
if (oldTheme) el.removeClass('md-' + oldTheme +'-theme');
el.addClass('md-' + theme + '-theme');
el.data('$mdThemeName', theme);
if (ctrl) {
el.data('$mdThemeController', ctrl);
}
}
}
}
}
function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
return {
priority: 101, // has to be more than 100 to be before interpolation (issue on IE)
link: {
pre: function(scope, el, attrs) {
var registeredCallbacks = [];
var startSymbol = $interpolate.startSymbol();
var endSymbol = $interpolate.endSymbol();
var theme = attrs.mdTheme.trim();
var hasInterpolation =
theme.substr(0, startSymbol.length) === startSymbol &&
theme.lastIndexOf(endSymbol) === theme.length - endSymbol.length;
var oneTimeOperator = '::';
var oneTimeBind = attrs.mdTheme
.split(startSymbol).join('')
.split(endSymbol).join('')
.trim()
.substr(0, oneTimeOperator.length) === oneTimeOperator;
var getTheme = function () {
var interpolation = $interpolate(attrs.mdTheme)(scope);
return $parse(interpolation)(scope) || interpolation;
};
var ctrl = {
isAsyncTheme: angular.isFunction(getTheme()) || angular.isFunction(getTheme().then),
registerChanges: function (cb, context) {
if (context) {
cb = angular.bind(context, cb);
}
registeredCallbacks.push(cb);
return function () {
var index = registeredCallbacks.indexOf(cb);
if (index > -1) {
registeredCallbacks.splice(index, 1);
}
};
},
$setTheme: function (theme) {
if (!$mdTheming.registered(theme)) {
$log.warn('attempted to use unregistered theme \'' + theme + '\'');
}
ctrl.$mdTheme = theme;
// Iterating backwards to support unregistering during iteration
// http://stackoverflow.com/a/9882349/890293
// we don't use `reverse()` of array because it mutates the array and we don't want it
// to get re-indexed
for (var i = registeredCallbacks.length; i--;) {
registeredCallbacks[i](theme);
}
},
$shouldWatch: $mdUtil.parseAttributeBoolean(el.attr('md-theme-watch')) ||
$mdTheming.ALWAYS_WATCH ||
(hasInterpolation && !oneTimeBind)
};
el.data('$mdThemeController', ctrl);
var setParsedTheme = function (theme) {
if (typeof theme === 'string') {
return ctrl.$setTheme(theme);
}
$q.when(angular.isFunction(theme) ? theme() : theme)
.then(function(name) {
ctrl.$setTheme(name);
});
};
setParsedTheme(getTheme());
var unwatch = scope.$watch(getTheme, function(theme) {
if (theme) {
setParsedTheme(theme);
if (!ctrl.$shouldWatch) {
unwatch();
}
}
});
}
}
};
}
/**
* Special directive that will disable ALL runtime Theme style generation and DOM injection
*
* <link rel="stylesheet" href="angular-material.min.css">
* <link rel="stylesheet" href="angular-material.themes.css">
*
* <body md-themes-disabled>
* ...
* </body>
*
* Note: Using md-themes-css directive requires the developer to load external
* theme stylesheets; e.g. custom themes from Material-Tools:
*
* `angular-material.themes.css`
*
* Another option is to use the ThemingProvider to configure and disable the attribute
* conversions; this would obviate the use of the `md-themes-css` directive
*
*/
function disableThemesDirective() {
themeConfig.disableTheming = true;
// Return a 1x-only, first-match attribute directive
return {
restrict : 'A',
priority : '900'
};
}
function ThemableDirective($mdTheming) {
return $mdTheming;
}