UNPKG

4.25 kBJavaScriptView Raw
1import "core-js/modules/es.symbol.description";
2import "core-js/modules/es.array.iterator";
3import "core-js/modules/es.string.trim";
4import "core-js/modules/web.dom-collections.iterator";
5
6/******************************************************************************
7 *
8 * Copyright (c) 2017, the Perspective Authors.
9 *
10 * This file is part of the Perspective library, distributed under the terms of
11 * the Apache License 2.0. The full license can be found in the LICENSE file.
12 *
13 */
14import { get_type_config, get_types } from "@finos/perspective/dist/esm/config";
15export function get_style(elem, name) {
16 let value;
17
18 if (window.ShadyCSS) {
19 value = window.ShadyCSS.getComputedStyleValue(elem, name);
20 } else {
21 value = getComputedStyle(elem).getPropertyValue(name);
22 }
23
24 if (value.trim() === "") {
25 return undefined;
26 }
27
28 return value;
29}
30
31function get_measure(elem, name) {
32 let value;
33
34 if (window.ShadyCSS) {
35 value = window.ShadyCSS.getComputedStyleValue(elem, name);
36 } else {
37 value = getComputedStyle(elem).getPropertyValue(name);
38 }
39
40 if (value.trim() === "") {
41 return undefined;
42 }
43
44 return parseInt(value);
45}
46
47function get_font(elem, title) {
48 if (title.length > 0) {
49 title += "--";
50 }
51
52 const font_size = get_style(elem, "".concat(title, "font-size"));
53 const font_family = get_style(elem, "".concat(title, "font-family")); // FIXME this sucks but it is difficult to partially apply fonts in
54 // Hypergrid's API Fonts will not be picked up unless both font-size and
55 // font-family are defined for a specific scope.
56
57 if (!font_size || !font_family) {
58 return undefined;
59 }
60
61 return "".concat(font_size, " ").concat(font_family);
62}
63
64function copy_defined(source, dest, f) {
65 for (const key of Object.keys(source)) {
66 const val = source[key];
67
68 if (Array.isArray(val) && f) {
69 for (const candidate of val) {
70 const style = f(candidate);
71
72 if (style !== undefined) {
73 dest[key] = style;
74 break;
75 }
76 }
77 } else if (typeof val === "string" && f) {
78 const style = f(val);
79
80 if (style !== undefined) {
81 dest[key] = style;
82 }
83 } else if (typeof val === "object") {
84 dest[key] = dest[key] || {};
85 copy_defined(val, dest[key], f);
86 } else {
87 dest[key] = val;
88 }
89 }
90}
91
92function calc_rec(result, type, elem, types, iter, f) {
93 result[type] = result[type] || {};
94
95 for (const props of iter) {
96 copy_defined(props, result[type], name => f(elem, name));
97
98 for (const parent of types[type]) {
99 copy_defined(props, result[type], name => f(elem, "--".concat(parent).concat(name)));
100 }
101 }
102}
103
104function get_type_deps() {
105 const types = {
106 "": []
107 };
108
109 for (const type of get_types()) {
110 types[type] = [];
111 let parent = type;
112
113 while (parent && parent.length) {
114 types[type].unshift(parent);
115 parent = get_type_config(parent).type;
116 }
117 }
118
119 return types;
120}
121
122const STYLE_PROPERTIES = Symbol("Perspective Style Properties");
123export class PropsBuilder {
124 constructor() {
125 this._staged_props = [];
126 this._staged_fonts = [];
127 this._staged_measures = [];
128 }
129
130 add_measures(props) {
131 this._staged_measures.push(props);
132
133 this._initialized = false;
134 }
135
136 add_styles(props) {
137 this._staged_props.push(props);
138
139 this._initialized = false;
140 }
141
142 add_fonts(props) {
143 this._staged_fonts.push(props);
144
145 this._initialized = false;
146 }
147
148 clear_properties(elem) {
149 delete elem[STYLE_PROPERTIES];
150 }
151
152 get_properties(elem) {
153 if (!elem[STYLE_PROPERTIES]) {
154 const types = get_type_deps();
155 const result = elem[STYLE_PROPERTIES] = {};
156 calc_rec(result, "", elem, types, this._staged_measures, get_measure);
157 calc_rec(result, "", elem, types, this._staged_props, get_style);
158 calc_rec(result, "", elem, types, this._staged_fonts, get_font);
159
160 for (const type of get_types()) {
161 calc_rec(result, type, elem, types, this._staged_measures, get_measure);
162 calc_rec(result, type, elem, types, this._staged_props, get_style);
163 calc_rec(result, type, elem, types, this._staged_fonts, get_font);
164 }
165 }
166
167 return elem[STYLE_PROPERTIES];
168 }
169
170}
171//# sourceMappingURL=custom_styles.js.map
\No newline at end of file