UNPKG

4.91 kBJavaScriptView Raw
1
2import { re } from './style-re';
3
4/**
5 * Function to compose style string from object
6 * @function style.compose
7 * @param {object} styleObject - an object of style options
8 * @param {boolean} styleObject.colorize - boolean to use standardized colors for symbol groups
9 * @param {number} styleObject.padding - integer value for padding around symbol or sign
10 * @param {string} styleObject.background - css name or hex color for background
11 * @param {string[]} styleObject.detail - css name or hex color for line and optional fill
12 * @param {number} styleObject.zoom - decimal value for zoom level
13 * @param {{index:number,detail:string[]}[]} styleObject.detailsym - array of symbol indexes and detail color array
14 * @param {{index:number,zoom:number,offset:number[]}[]} styleObject.zoomsym - array of symbol indexes and zoom levels with optional x,y offset
15 * @param {string} styleObject.classes - list of class names separated with spaces used for SVG
16 * @param {string} styleObject.id - id name used for SVG
17 * @returns {string} style string
18 * @example
19 * style.compose({
20 * 'colorize': true,
21 * 'padding': 10,
22 * 'background': 'blue',
23 * 'detail': ['red', 'Cyan'],
24 * 'zoom': 1.1,
25 * 'detailsym': [
26 * {
27 * 'index': 1,
28 * 'detail': ['#ff00ff']
29 * },
30 * {
31 * 'index': 2,
32 * 'detail': ['yellow', 'green']
33 * }
34 * ],
35 * 'zoomsym': [
36 * {
37 * 'index': 1,
38 * 'zoom': 10,
39 * 'offset': [0, 0]
40 * },
41 * {
42 * 'index': 2,
43 * 'zoom': 5.5
44 * }
45 * ],
46 * 'classes': 'primary blinking',
47 * 'id': 'cursor'
48 * })
49 *
50 * return '-CP10G_blue_D_red,Cyan_Z1.1-D01_ff00ff_D02_yellow,green_Z01,10,500x500Z02,5.5-primary blinking!cursor!'
51 */
52const compose = (styleObject) => {
53 if (typeof styleObject !== 'object' || styleObject === null) return undefined;
54
55 // three sections
56 let style1 = '-';
57
58 style1 += !styleObject.colorize ? '' : 'C';
59
60 const padding = parseInt(styleObject.padding);
61 style1 += (!padding || padding <= 0 || padding > 99) ? '' : 'P' + (padding > 9 ? padding : '0' + padding);
62
63 const background = (!styleObject.background || !(typeof styleObject.background === 'string')) ? undefined : styleObject.background.match(re.colorbase)[0];
64 style1 += !background ? '' : 'G_' + background + '_';
65
66 const detail1 = (!styleObject.detail || !styleObject.detail[0] || !(typeof styleObject.detail[0] === 'string')) ? undefined : styleObject.detail[0].match(re.colorbase)[0];
67 const detail2 = (!styleObject.detail || !styleObject.detail[1] || !(typeof styleObject.detail[1] === 'string')) ? undefined : styleObject.detail[1].match(re.colorbase)[0];
68 if (detail1) {
69 style1 += 'D_' + detail1;
70 if (detail2) {
71 style1 += ',' + detail2;
72 }
73 style1 += '_';
74 }
75
76 const zoom = (styleObject.zoom === 'x') ? 'x' : parseFloat(styleObject.zoom);
77 style1 += (!zoom || zoom <= 0) ? '' : 'Z' + zoom;
78
79 let style2 = '';
80
81 const detailsym = (!styleObject.detailsym || !Array.isArray(styleObject.detailsym)) ? [] : styleObject.detailsym.map((styleObject) => {
82 const index = parseInt(styleObject.index);
83 if (!index || index <= 0 || index > 99) return '';
84 let style = 'D' + (index > 9 ? index : '0' + index);
85 const detail1 = (!styleObject.detail || !styleObject.detail[0]) ? undefined : styleObject.detail[0].match(re.colorbase)[0];
86 const detail2 = (!styleObject.detail || !styleObject.detail[1]) ? undefined : styleObject.detail[1].match(re.colorbase)[0];
87 if (detail1) {
88 style += '_' + detail1;
89 if (detail2) {
90 style += ',' + detail2;
91 }
92 style += '_';
93 }
94 return style;
95 })
96 style2 += detailsym.join('');
97
98 const zoomsym = (!styleObject.zoomsym || !Array.isArray(styleObject.zoomsym)) ? [] : styleObject.zoomsym.map((styleObject) => {
99 const index = parseInt(styleObject.index);
100 if (!index || index <= 0 || index > 99) return '';
101 let style = 'Z' + (index > 9 ? index : '0' + index);
102 const zoom = parseFloat(styleObject.zoom);
103 style += (!zoom || zoom <= 0) ? '' : ',' + zoom;
104 if (styleObject.offset && (0 in styleObject.offset) && (1 in styleObject.offset)) {
105 const x = parseInt(styleObject.offset[0]) + 500;
106 const y = parseInt(styleObject.offset[1]) + 500;
107 if (x >= 250 && x < 750 && y >= 250 && y < 750) {
108 style += ',' + x + 'x' + y;
109 }
110 }
111 return style;
112 })
113 style2 += zoomsym.join('');
114
115 let style3 = '';
116 const classes = (!styleObject.classes || !(typeof styleObject.classes === 'string')) ? undefined : styleObject.classes.match(re.classes)[0];
117 style3 += !classes ? '' : classes;
118 const id = (!styleObject.id || !(typeof styleObject.id === 'string')) ? undefined : styleObject.id.match(re.id)[0];
119 style3 += (classes || id) ? '!' : '';
120 style3 += !id ? '' : id + '!';
121
122 return style1 + ((style2 || style3) ? '-' + style2 : '') + (style3 ? '-' + style3 : '');
123}
124
125export { compose }