UNPKG

3.01 kBPlain TextView Raw
1import {PreDestroy, Bean, Qualifier, Autowired, PostConstruct, Optional, Context} from './context/context';
2
3let themeNames = ['fresh', 'dark', 'blue', 'bootstrap', 'material', 'balham-dark', 'balham'];
4const themes = themeNames.concat(themeNames.map(name => `theme-${name}`));
5const themeClass = new RegExp(`ag-(${themes.join('|')})`);
6
7const matGridSize = 8;
8type HardCodedSize = {[key: string]: {[key: string]: number}};
9const freshGridSize = 4;
10const balhamGridSize = 4;
11
12const HARD_CODED_SIZES: HardCodedSize = {
13 'ag-theme-material': {
14 headerHeight: matGridSize * 7,
15 virtualItemHeight: matGridSize * 5,
16 rowHeight: matGridSize * 6
17 },
18 'ag-theme-classic': {
19 headerHeight: 25,
20 virtualItemHeight: freshGridSize * 5,
21 rowHeight: 25
22 },
23 'ag-theme-balham': {
24 headerHeight: balhamGridSize * 8,
25 virtualItemHeight: balhamGridSize * 7,
26 rowHeight: balhamGridSize * 7
27 }
28};
29
30@Bean('environment')
31export class Environment {
32 @Autowired('eGridDiv') private eGridDiv: HTMLElement;
33
34 private gridSize: number;
35 private iconSize: number;
36 private sassVariables: {[key: string]: string} = {};
37
38 // Approach described here:
39 // https://www.ofcodeandcolor.com/2017/04/02/encoding-data-in-css/
40 public loadSassVariables(): void {
41 /*
42 var element = document.createElement('div');
43 element.className = 'sass-variables';
44 this.eGridDiv.appendChild(element);
45
46 var content = window.getComputedStyle(element, '::after').content;
47
48 try {
49 this.sassVariables = JSON.parse(JSON.parse(content));
50 } catch (e) {
51 throw new Error("Failed loading the theme sizing - check that you have the theme set up correctly.");
52 }
53
54 this.eGridDiv.removeChild(element);
55 */
56 }
57
58 public getSassVariable(theme: string, key: string): number {
59 if (theme == 'ag-theme-material') {
60 return HARD_CODED_SIZES['ag-theme-material'][key];
61 }
62 else if (theme == 'ag-theme-balham' || theme == 'ag-theme-balham-dark') {
63 return HARD_CODED_SIZES['ag-theme-balham'][key];
64 }
65 return HARD_CODED_SIZES['ag-theme-classic'][key];
66 /*
67 const result = parseInt(this.sassVariables[key]);
68 if (!result || isNaN(result)) {
69 throw new Error(`Failed loading ${key} Sass variable from ${this.sassVariables}`);
70 }
71 return result;
72 */
73 }
74
75 public getTheme(): string {
76 let themeMatch: RegExpMatchArray;
77 let element: HTMLElement = this.eGridDiv;
78
79 while (element != document.documentElement && themeMatch == null) {
80 themeMatch = element.className.match(themeClass);
81 element = element.parentElement;
82 if (element == null) {
83 break;
84 }
85 }
86
87 if (themeMatch) {
88 return themeMatch[0];
89 } else {
90 return 'ag-fresh';
91 }
92 }
93}