1
|
|
|
/** global: google, r, g, b */ |
2
|
|
|
|
3
|
|
|
import { IconObject } from "./icon_object.js"; |
4
|
|
|
|
5
|
|
|
import { createClusterIcon } from "./create_cluster_icon.js"; |
6
|
|
|
import { createTextMarker } from "./create_text_marker.js"; |
7
|
|
|
|
8
|
|
|
import { createFatMarkerIcon } from "./create_fat_marker_icon.js"; |
9
|
|
|
|
10
|
|
|
import { createGroupedIcon } from "./create_grouped_icon.js"; |
11
|
|
|
|
12
|
|
|
import { createTransparentMarkerIcon } from "./create_transparent_marker_icon.js"; |
13
|
|
|
|
14
|
|
|
import { parseHex, parseHSL, parseRGB, hslToRGB, rgbToHSL } from "./parsers.js"; |
15
|
|
|
|
16
|
|
|
import { omit, serializeOptions } from "./helpers.js"; |
17
|
|
|
|
18
|
|
|
function padHex(str_in) { |
19
|
|
|
if (("" + str_in).length === 1) { |
20
|
|
|
return "0" + String(str_in); |
21
|
|
|
} else { |
|
|
|
|
22
|
|
|
return String(str_in); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
const MarkerFactory = { |
27
|
|
|
createTransparentMarkerIcon: createTransparentMarkerIcon, |
28
|
|
|
createFatMarkerIcon: createFatMarkerIcon, |
29
|
|
|
createTextMarker: createTextMarker, |
30
|
|
|
createClusterIcon: createClusterIcon, |
31
|
|
|
createGroupedIcon: createGroupedIcon, |
32
|
|
|
|
33
|
|
|
readCache: function(cacheKey, options) { |
34
|
|
|
if (options.no_cache) { |
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
var cached = window.sessionStorage.getItem(cacheKey); |
39
|
|
|
if (cached === null) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
var cachedObj = JSON.parse(cached); |
44
|
|
|
var iconObj = new IconObject( |
45
|
|
|
cachedObj.url, |
46
|
|
|
cachedObj.fillColor, |
47
|
|
|
omit(cachedObj, function(key) { |
48
|
|
|
return ["url", "fillColor"].indexOf(key) !== -1; |
49
|
|
|
}) |
50
|
|
|
); |
51
|
|
|
iconObj.cached = true; |
52
|
|
|
return iconObj; |
53
|
|
|
}, |
54
|
|
|
|
55
|
|
|
setCache: function(cacheKey, iconObj) { |
56
|
|
|
var cached = iconObj.toJSON(); |
57
|
|
|
cached.url = iconObj.url; |
58
|
|
|
window.sessionStorage.setItem(cacheKey, JSON.stringify(cached)); |
59
|
|
|
return iconObj; |
60
|
|
|
}, |
61
|
|
|
|
62
|
|
|
generateAutoicon: function(options) { |
63
|
|
|
var generatorFN; |
64
|
|
|
|
65
|
|
|
if (!options.is_icon) { |
66
|
|
|
options.type = "textmarker"; |
67
|
|
|
generatorFN = MarkerFactory.createTextMarker; |
68
|
|
|
} else if (options.shadow || options.type === "grouped") { |
69
|
|
|
options.type = "grouped"; |
70
|
|
|
generatorFN = MarkerFactory.createGroupedIcon; |
71
|
|
|
} else if (options.transparent_background) { |
72
|
|
|
options.type = "transparent"; |
73
|
|
|
generatorFN = MarkerFactory.createTransparentMarkerIcon; |
74
|
|
|
} else { |
75
|
|
|
generatorFN = MarkerFactory.createFatMarkerIcon; |
76
|
|
|
options.type = "fatmarker"; |
77
|
|
|
} |
78
|
|
|
var cacheKey = serializeOptions(options); |
79
|
|
|
var iconObj = MarkerFactory.readCache(cacheKey, options); |
80
|
|
|
if (iconObj === null) { |
81
|
|
|
iconObj = generatorFN(options); |
82
|
|
|
iconObj.cached = false; |
83
|
|
|
} |
84
|
|
|
iconObj.cacheKey = cacheKey; |
85
|
|
|
if (options.no_cache) { |
86
|
|
|
return iconObj; |
87
|
|
|
} |
88
|
|
|
return MarkerFactory.setCache(cacheKey, iconObj); |
89
|
|
|
}, |
90
|
|
|
/** |
91
|
|
|
* Receives a color string rgb(a), hsl(a) or hex, returns its components |
92
|
|
|
* in rgba and hsla, with optional transparency |
93
|
|
|
* plus a darkened version (default is half of each RGB component) and a |
94
|
|
|
* |
95
|
|
|
* @param {string} somecolor - A color string in rgb(a), hsl(a) or hex format |
96
|
|
|
* @param {Number} opacity - Opacity to apply to the color. Optional, default 1 |
97
|
|
|
* @param {Number} darkenfactor - How much darker should the resulting color be. Optional, default 1 |
98
|
|
|
* |
99
|
|
|
* @return {Object} input color parsed and modified as requested |
100
|
|
|
*/ |
101
|
|
|
parseColorString: function(somecolor, opacity, darkenfactor) { |
102
|
|
|
let parsedcolor = { |
103
|
|
|
original: somecolor |
104
|
|
|
}, |
105
|
|
|
hsl, |
106
|
|
|
rgb; |
107
|
|
|
|
108
|
|
|
darkenfactor = darkenfactor || 1; |
109
|
|
|
opacity = isNaN(parseFloat(opacity, 10)) ? 1 : parseFloat(opacity, 10); |
110
|
|
|
|
111
|
|
|
if (somecolor.indexOf("hsl") !== -1) { |
112
|
|
|
hsl = parseHSL(somecolor, opacity); |
113
|
|
|
rgb = hslToRGB(hsl.h, hsl.s, hsl.l, hsl.a, darkenfactor); |
114
|
|
|
} else if (somecolor.indexOf("rgb") !== -1) { |
115
|
|
|
rgb = parseRGB(somecolor, opacity, darkenfactor); |
116
|
|
|
} else { |
117
|
|
|
rgb = parseHex(somecolor, opacity, darkenfactor); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
hsl = rgbToHSL(rgb.r, rgb.g, rgb.b, rgb.a); |
121
|
|
|
|
122
|
|
|
parsedcolor.hsl = { |
123
|
|
|
h: hsl.h, |
124
|
|
|
s: hsl.s, |
125
|
|
|
l: hsl.l, |
126
|
|
|
a: hsl.a |
127
|
|
|
}; |
128
|
|
|
parsedcolor.rgb = { |
129
|
|
|
r: rgb.r, |
130
|
|
|
g: rgb.g, |
131
|
|
|
b: rgb.b, |
132
|
|
|
a: rgb.a |
133
|
|
|
}; |
134
|
|
|
|
135
|
|
|
parsedcolor.fillColor = rgb.fillColor; |
136
|
|
|
parsedcolor.rgba = rgb.fillColor; |
137
|
|
|
parsedcolor.hsla = hsl.fillColor; |
138
|
|
|
parsedcolor.strokeColor = rgb.strokeColor; |
139
|
|
|
parsedcolor.hex = [ |
140
|
|
|
"#", |
141
|
|
|
padHex(rgb.r.toString(16)), |
142
|
|
|
padHex(rgb.g.toString(16)), |
143
|
|
|
padHex(rgb.b.toString(16)), |
144
|
|
|
rgb.a === 0 ? "00" : "" |
145
|
|
|
].join(""); |
146
|
|
|
return parsedcolor; |
147
|
|
|
}, |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Generates an google maps marker (or an image as dataurl from the given options) |
151
|
|
|
* |
152
|
|
|
* @param {Object} options The options |
153
|
|
|
* @return {Object} { description_of_the_return_value } |
154
|
|
|
*/ |
155
|
|
|
autoIcon: function(options) { |
156
|
|
|
if (typeof options !== "object") { |
157
|
|
|
console.warn("autoIcon expects an object as its only parameter"); |
158
|
|
|
return null; |
159
|
|
|
} |
160
|
|
|
// unless explicitly set to false, the icon doesn't have a marker-like wrapper |
161
|
|
|
options.transparent_background = |
162
|
|
|
options.transparent_background !== false; |
163
|
|
|
|
164
|
|
|
options.label = String(options.label || "A"); |
165
|
|
|
options.color = options.color || "#FF0000"; |
166
|
|
|
|
167
|
|
|
if ( |
168
|
|
|
options.label.length === 4 || |
169
|
|
|
options.label.substring(0, 2) === "0x" |
170
|
|
|
) { |
171
|
|
|
options.font = options.font || "fontello"; |
172
|
|
|
options.label = (options.label || "e836").slice(-4); |
173
|
|
|
options.unicodelabel = String.fromCharCode("0x" + options.label); |
174
|
|
|
options.scale = options.scale || 1; |
175
|
|
|
options.is_icon = true; |
176
|
|
|
|
177
|
|
|
return MarkerFactory.generateAutoicon(options); |
178
|
|
|
} else if (options.shadow) { |
179
|
|
|
console.log("createClusterIcon", JSON.stringify(options)); |
|
|
|
|
180
|
|
|
|
181
|
|
|
return MarkerFactory.createClusterIcon(options); |
182
|
|
|
} else { |
183
|
|
|
options.scale = options.scale || 0.75; |
184
|
|
|
options.label = String(options.label || "A"); |
185
|
|
|
options.fontsize = options.fontsize || 11; |
186
|
|
|
options.font = options.font || "Arial"; |
187
|
|
|
// This is text I should print literally |
188
|
|
|
|
189
|
|
|
return MarkerFactory.generateAutoicon(options); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
}; |
193
|
|
|
|
194
|
|
|
export { MarkerFactory }; |
195
|
|
|
|