Completed
Push — master ( 06507b...6aec3d )
by Felipe
01:55
created

MarkerFactory.serializeOptions   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/** global: google, r, g, b */
2
3
import { createClusterIcon } from "./create_cluster_icon.js";
4
import { createTextMarker } from "./create_text_marker.js";
5
6
import { createFatMarkerIcon } from "./create_fat_marker_icon.js";
7
8
import { createTransparentMarkerIcon } from "./create_transparent_marker_icon.js";
9
10
import { parseHex, parseHSL, parseRGB, hslToRGB, rgbToHSL } from "./parsers.js";
11
12
import { omit } from "./helpers.js";
13
14
function padHex(str_in) {
15
    if (("" + str_in).length === 1) {
16
        return "0" + String(str_in);
17
    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
18
        return String(str_in);
19
    }
20
}
21
22
const MarkerFactory = {
23
    createTransparentMarkerIcon: createTransparentMarkerIcon,
24
    createFatMarkerIcon: createFatMarkerIcon,
25
    createTextMarker: createTextMarker,
26
    createClusterIcon: createClusterIcon,
27
    serializeOptions: function(options) {
28
        if (typeof options !== "object") {
29
            return null;
30
        }
31
        var cleanOptions = omit(options, function(prop) {
32
                return prop.indexOf("gm_") === 0;
33
            }),
34
            sortedOpts = Object.entries(cleanOptions)
35
                .filter(function(item) {
36
                    return (
37
                        typeof item[1] !== "function" &&
38
                        typeof item[1] !== "object"
39
                    );
40
                })
41
                .sort();
42
        return JSON.stringify(sortedOpts);
43
    },
44
    generateAutoicon: function(options) {
45
        var generatorFN = MarkerFactory.createFatMarkerIcon,
46
            iconObj;
47
        options.type = "fatmarker";
48
49
        if (!options.is_icon) {
50
            options.type = "textmarker";
51
            generatorFN = MarkerFactory.createTextMarker;
52
        } else if (options.transparent_background) {
53
            options.type = "transparent";
54
            generatorFN = MarkerFactory.createTransparentMarkerIcon;
55
        }
56
57
        if (!options.no_cache) {
58
            var cacheKey = MarkerFactory.serializeOptions(options);
59
60
            iconObj = window.sessionStorage.getItem(cacheKey);
61
            if (iconObj !== null && !options.no_cache) {
62
                return JSON.parse(iconObj);
63
            }
64
        }
65
66
        iconObj = generatorFN(options);
67
68
        if (!options.no_cache) {
69
            var cached = iconObj.toJSON();
70
            cached.url = iconObj.url;
71
            window.sessionStorage.setItem(cacheKey, JSON.stringify(cached));
0 ignored issues
show
Bug introduced by
The variable cacheKey does not seem to be initialized in case !options.no_cache on line 57 is false. Are you sure the function setItem handles undefined variables?
Loading history...
72
        }
73
        return iconObj;
74
    },
75
    /**
76
     * Receives a color string rgb(a), hsl(a) or hex, returns its components
77
     * in rgba and hsla, with optional transparency
78
     * plus a darkened version (default is half of each RGB component) and a
79
     *
80
     * @param {string} somecolor    - A color string in  rgb(a), hsl(a) or hex format
81
     * @param {Number} opacity      - Opacity to apply to the color. Optional, default 1
82
     * @param {Number} darkenfactor - How much darker should the resulting color be. Optional, default 1
83
     *
84
     * @return     {Object}  input color parsed and modified as requested
85
     */
86
    parseColorString: function(somecolor, opacity, darkenfactor) {
87
        let parsedcolor = {
88
                original: somecolor
89
            },
90
            hsl,
91
            rgb;
92
93
        darkenfactor = darkenfactor || 1;
94
        opacity = opacity || 1;
95
96
        if (somecolor.indexOf("hsl") !== -1) {
97
            hsl = parseHSL(somecolor, opacity);
98
            rgb = hslToRGB(hsl.h, hsl.s, hsl.l, hsl.a, darkenfactor);
99
        } else if (somecolor.indexOf("rgb") !== -1) {
100
            rgb = parseRGB(somecolor, opacity, darkenfactor);
101
        } else {
102
            rgb = parseHex(somecolor, opacity, darkenfactor);
103
        }
104
105
        hsl = rgbToHSL(rgb.r, rgb.g, rgb.b, rgb.a);
106
107
        parsedcolor.hsl = {
108
            h: hsl.h,
109
            s: hsl.s,
110
            l: hsl.l,
111
            a: hsl.a
112
        };
113
        parsedcolor.rgb = {
114
            r: rgb.r,
115
            g: rgb.g,
116
            b: rgb.b,
117
            a: rgb.a
118
        };
119
120
        parsedcolor.fillColor = rgb.fillColor;
121
        parsedcolor.rgba = rgb.fillColor;
122
        parsedcolor.hsla = hsl.fillColor;
123
        parsedcolor.strokeColor = rgb.strokeColor;
124
        parsedcolor.hex = [
125
            "#",
126
            padHex(rgb.r.toString(16)),
127
            padHex(rgb.g.toString(16)),
128
            padHex(rgb.b.toString(16))
129
        ].join("");
130
        return parsedcolor;
131
    },
132
133
    /**
134
     * Generates an google maps marker (or an image as dataurl from the given options)
135
     *
136
     * @param      {Object}  options  The options
137
     * @return     {Object}  { description_of_the_return_value }
138
     */
139
    autoIcon: function(options) {
140
        if (typeof options !== "object") {
141
            console.warn("autoIcon expects an object as its only parameter");
142
            return null;
143
        }
144
        // unless explicitly set to false, the icon doesn't have a marker-like wrapper
145
        options.transparent_background =
146
            options.transparent_background !== false;
147
148
        options.label = String(options.label || "A");
149
        options.color = options.color || "#FF0000";
150
151
        if (
152
            options.label.length === 4 ||
153
            options.label.substring(0, 2) === "0x"
154
        ) {
155
            options.font = options.font || "fontello";
156
            options.label = (options.label || "e836").slice(-4);
157
            options.unicodelabel = String.fromCharCode("0x" + options.label);
158
            options.scale = options.scale || 1;
159
            options.is_icon = true;
160
161
            return MarkerFactory.generateAutoicon(options);
162
        } else if (options.shadow) {
163
            console.log("createClusterIcon", JSON.stringify(options));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
164
165
            return MarkerFactory.createClusterIcon(options);
166
        } else {
167
            options.scale = options.scale || 0.75;
168
            options.label = String(options.label || "A");
169
            options.fontsize = options.fontsize || 11;
170
            options.font = options.font || "Arial";
171
            // This is text I should print literally
172
173
            return MarkerFactory.generateAutoicon(options);
174
        }
175
    }
176
};
177
178
export { MarkerFactory };
179