-
Notifications
You must be signed in to change notification settings - Fork 26
/
bg-utils.js
353 lines (315 loc) · 7.89 KB
/
bg-utils.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
//const hexToRgba = window.require('hex-to-rgba');
const rgbaToHex = require('hex-and-rgba').rgbaToHex;
const hexToRgba = window.require('hex-and-rgba').hexToRgba;
const COMP_OPERATORS = [
"EQUAL TO",
"NOT EQUAL TO",
"GREATER THAN",
"LESS THAN",
"GREATER OR EQUAL TO",
"LESS OR EQUAL TO",
"IN",
"NOT IN",
"ENDS WITH",
"STARTS WITH",
"MATCHES REGEXP",
"CONTAINS",
"LENGTH EQUAL TO",
"LENGTH LESS THAN",
"LENGTH GREATER THAN"
];
const COMP_VALUE_TYPES = ["COLUMN", "INPUT"];
const COMP_PROPERTIES = ['background-color', 'color'];
const CSS_EXCELJS_PROP_MAP = {
"background-color": "fill-fgColor",
"color": "font-color"
};
//https://www.quackit.com/css/css_color_codes.cfm
const COLOR_NAME_TO_HEX = {
"indianred": "cd5c5c",
"lightcoral": "f08080",
"salmon": "fa8072",
"darksalmon": "e9967a",
"lightsalmon": "ffa07a",
"crimson": "dc143c",
"red": "ff0000",
"firebrick": "b22222",
"darkred": "8b0000",
"pink": "ffc0cb",
"lightpink": "ffb6c1",
"hotpink": "ff69b4",
"deeppink": "ff1493",
"mediumvioletred": "c71585",
"palevioletred": "db7093",
"greenyellow": "adff2f",
"chartreuse": "7fff00",
"lawngreen": "7cfc00",
"lime": "00ff00",
"limegreen": "32cd32",
"palegreen": "98fb98",
"lightgreen": "90ee90",
"mediumspringgreen": "00fa9a",
"springgreen": "00ff7f",
"mediumseagreen": "3cb371",
"seagreen": "2e8b57",
"forestgreen": "228b22",
"green": "008000",
"darkgreen": "006400",
"yellowgreen": "9acd32",
"olivedrab": "6b8e23",
"olive": "808000",
"darkolivegreen": "556b2f",
"cornsilk": "fff8dc",
"blanchedalmond": "ffebcd",
"bisque": "ffe4c4",
"navajowhite": "ffdead",
"wheat": "f5deb3",
"burlywood": "deb887",
"tan": "d2b48c",
"rosybrown": "bc8f8f",
"sandybrown": "f4a460",
"goldenrod": "daa520",
"darkgoldenrod": "b8860b",
"peru": "cd853f",
"chocolate": "d2691e",
"saddlebrown": "8b4513",
"sienna": "a0522d",
"brown": "a52a2a",
"maroon": "800000",
"pink": "ffc0cb",
"lightpink": "ffb6c1",
"hotpink": "ff69b4",
"deeppink": "ff1493",
"mediumvioletred": "c71585",
"palevioletred": "db7093",
"coral": "ff7f50",
"tomato": "ff6347",
"orangered": "ff4500",
"darkorange": "ff8c00",
"orange": "ffa500",
"gold": "ffd700",
"yellow": "ffff00",
"lightyellow": "ffffe0",
"lemonchiffon": "fffacd",
"lightgoldenrodyellow": "fafad2",
"papayawhip": "ffefd5",
"moccasin": "ffe4b5",
"peachpuff": "ffdab9",
"palegoldenrod": "eee8aa",
"khaki": "f0e68c",
"darkkhaki": "bdb76b",
"lavender": "e6e6fa",
"thistle": "d8bfd8",
"plum": "dda0dd",
"violet": "ee82ee",
"orchid": "da70d6",
"fuchsia": "ff00ff",
"magenta": "ff00ff",
"mediumorchid": "ba55d3",
"mediumpurple": "9370db",
"blueviolet": "8a2be2",
"darkviolet": "9400d3",
"darkorchid": "9932cc",
"darkmagenta": "8b008b",
"purple": "800080",
"rebeccapurple": "663399",
"indigo": "4b0082",
"mediumslateblue": "7b68ee",
"slateblue": "6a5acd",
"darkslateblue": "483d8b",
"aqua": "00ffff",
"cyan": "00ffff",
"lightcyan": "e0ffff",
"paleturquoise": "afeeee",
"aquamarine": "7fffd4",
"turquoise": "40e0d0",
"mediumturquoise": "48d1cc",
"darkturquoise": "00ced1",
"cadetblue": "5f9ea0",
"steelblue": "4682b4",
"lightsteelblue": "b0c4de",
"powderblue": "b0e0e6",
"lightblue": "add8e6",
"skyblue": "87ceeb",
"lightskyblue": "87cefa",
"deepskyblue": "00bfff",
"dodgerblue": "1e90ff",
"cornflowerblue": "6495ed",
"royalblue": "4169e1",
"blue": "0000ff",
"mediumblue": "0000cd",
"darkblue": "00008b",
"navy": "000080",
"midnightblue": "191970",
"white": "ffffff",
"snow": "fffafa",
"honeydew": "f0fff0",
"mintcream": "f5fffa",
"azure": "f0ffff",
"aliceblue": "f0f8ff",
"ghostwhite": "f8f8ff",
"whitesmoke": "f5f5f5",
"seashell": "fff5ee",
"beige": "f5f5dc",
"oldlace": "fdf5e6",
"floralwhite": "fffaf0",
"ivory": "fffff0",
"antiquewhite": "faebd7",
"linen": "faf0e6",
"lavenderblush": "fff0f5",
"mistyrose": "ffe4e1",
"gainsboro": "dcdcdc",
"lightgray": "d3d3d3",
"lightgrey": "d3d3d3",
"silver": "c0c0c0",
"darkgray": "a9a9a9",
"darkgrey": "a9a9a9",
"gray": "808080",
"grey": "808080",
"dimgray": "696969",
"dimgrey": "696969",
"lightslategray": "778899",
"lightslategrey": "778899",
"slategray": "708090",
"slategrey": "708090",
"darkslategray": "2f4f4f",
"darkslategrey": "2f4f4f",
"black": "000000",
};
/*
* Generate table cell style classes
*/
function generateStyleClass(reportId, fieldName, conditionIndex){
const cleanedFieldName = fieldName.toLowerCase().replace(/[^a-zA-Z0-9]/g,"")
return `tbl-rpt-${reportId}-${cleanedFieldName}-${conditionIndex}`
}
function numberParser(str) {
var newValue = str;
var valueAsNumber;
if (newValue === null || newValue === undefined || newValue === '') {
valueAsNumber = null;
} else {
valueAsNumber = parseFloat(str);
}
return (isNaN(valueAsNumber) || valueAsNumber === null) ? str : valueAsNumber;
}
function checkStyleCondition(lValue, op, rValue){
if(op === 'EQUAL TO'){
return lValue == rValue;
}
if(op === 'NOT EQUAL TO'){
return lValue != rValue;
}
if(op === 'GREATER THAN'){
rValue = numberParser(rValue)
lValue = numberParser(lValue)
return lValue > rValue;
}
if(op === 'LESS THAN'){
rValue = numberParser(rValue)
lValue = numberParser(lValue)
return lValue < rValue;
}
if(op === 'GREATER OR EQUAL TO'){
rValue = numberParser(rValue)
lValue = numberParser(lValue)
return lValue >= rValue;
}
if(op === 'LESS OR EQUAL TO'){
rValue = numberParser(rValue)
lValue = numberParser(lValue)
return lValue <= rValue;
}
//Comma separated range
if(op === 'IN'){
return rValue.split(',').indexOf(lValue) >= 0;
}
//Comma separated range
if(op === 'NOT IN'){
return rValue.split(',').indexOf(lValue) == -1;
}
if(op === 'ENDS WITH'){
return new RegExp(`${rValue}\$`).test(lValue);
}
if(op === 'STARTS WITH'){
return new RegExp(`^${rValue}`).test(lValue);
}
if(op === 'CONTAINS'){
return new RegExp(`.*${rValue}.*`).test(lValue);
}
if(op === 'MATCHES REGEXP'){
return new RegExp(rValue).test(lValue);
}
if(op === 'LENGTH EQUAL TO'){
return lValue === null ? false : lValue.length === rValue;
}
if(op === 'LENGTH LESS THAN'){
return lValue === null ? false : lValue.length < rValue;
}
if(op === 'LENGTH GREATER THAN'){
return lValue === null ? false : lValue.length > rValue;
}
return false;
}
function cssColorToRGBA(cssColor){
let hexValue = cssColor;
if(typeof COLOR_NAME_TO_HEX[cssColor] !== 'undefined'){
hexValue = "#" + COLOR_NAME_TO_HEX[cssColor];
}
return rgbaToHex(hexToRgba(hexValue)).replace('#','').toUpperCase();
}
function RGBAToARGB(hexRGBAVal){
const a = hexRGBAVal.slice(6);
const rgb = hexRGBAVal.slice(0,6);
return `${a}${rgb}`
}
function cssColorToARGB(cssColor){
return RGBAToARGB(cssColorToRGBA(cssColor));
}
//Converts css to ExcelJS styles
function getExcelJsCellStyle(propName, propValue){
if(typeof CSS_EXCELJS_PROP_MAP[propName] !== 'undefined'){
//get category-property corresponding to CSS property
const catProp = CSS_EXCELJS_PROP_MAP[propName].split('-');
const cat = catProp[0];
const prop = catProp[1];
let fill = {};
if(cat === 'fill'){
if(prop === 'bgColor'){
fill = {
...fill,
type: 'pattern',
pattern: 'solid',
bgColor: { argb: cssColorToARGB(propValue) }
}
}
//background-color --> fill-fgColor
if(prop === 'fgColor'){
fill = {
...fill,
type: 'pattern',
pattern: 'solid',
fgColor: { argb: cssColorToARGB(propValue) }
}
}
}
let font = {}
if(cat === 'font'){
//color --> font-color
if(prop === 'color'){
font = {
...font,
color: {argb: cssColorToARGB(propValue) }
}
}
}
return {fill: fill, font: font}
}
return {}
}
exports.getExcelJsCellStyle = getExcelJsCellStyle;
exports.checkStyleCondition = checkStyleCondition;
exports.COMP_OPERATORS = COMP_OPERATORS;
exports.COMP_VALUE_TYPES = COMP_VALUE_TYPES;
exports.COMP_PROPERTIES = COMP_PROPERTIES;
exports.cssColorToARGB = cssColorToARGB;