Completed
Push — master ( f2c63f...720388 )
by Donata
02:09
created

src/javascript/blocks/maps/modules/GoogleMap.js   A

Size

Lines of Code 93

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 93
rs 10
noi 1
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B GoogleMap.js ➔ ??? 0 36 1
1
import Marker from './Marker';
2
import {createRandomId} from '../functions/createRandomId';
3
4
class GoogleMap {
5
6
    constructor(container, options) {
7
        this.instanceId = options.instanceId || '';
8
        this.container = container;
9
        this.markers = [];
10
        this.map = null;
11
12
        this.options = {};
13
        this.defaults = {
14
            map: {
15
                center: {
16
                    lat: 55.1309504,
17
                    lng: 24.5499231
18
                },
19
                zoom: 7,
20
                scrollwheel: false,
21
                navigationControl: true,
22
                mapTypeControl: false,
23
                scaleControl: true,
24
                draggable: true,
25
                streetViewControl: false
26
            },
27
            /**
28
             * show info window event type:
29
             *  click - only show window when clicking on marker
30
             *  hover - only show window when mouse over/out marker
31
             */
32
            infoWindowOn: 'click',
33
            globalMarkerIcon: ''
34
        };
35
36
        // set options from data attribute
37
        this.placeInstanceId(options);
38
        this.populateOptions(options);
39
        this.initMap();
40
        this.collectMarkers();
41
    }
42
43
    placeInstanceId() {
44
        if (this.instanceId) {
45
            this.container.setAttribute('instance', this.instanceId);
46
        }
47
    }
48
49
    populateOptions(options = {}) {
50
        if('map' in options && 'styles' in options.map) {
51
            options.map.styles = JSON.parse(options.map.styles);
52
        }
53
54
        if ('map' in options) {
55
            options.map = Object.assign(this.defaults.map, options.map);
56
        }
57
58
        this.options = Object.assign(this.options, this.defaults, options);
59
    }
60
61
    initMap() {
62
        this.map = new google.maps.Map(this.container, this.options.map);
0 ignored issues
show
Bug introduced by
The variable google seems to be never declared. If this is a global, consider adding a /** global: google */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
63
64
        google.maps.event.addDomListener(window, 'resize', () => {
65
            this.map.setCenter(this.options.map.center);
66
        });
67
    }
68
69
    collectMarkers() {
70
        let data = this.container.getAttribute('data-markers');
71
72
        if (data != '' || typeof data != 'undefined') {
73
            data = JSON.parse(data);
74
        }
75
76
        data.forEach((option) => {
77
            this.markers.push(new Marker(Object.assign(option, {
78
                instanceId: createRandomId(),
79
                infoWindowOn: this.options.infoWindowOn,
80
                icon: this.options.globalMarkerIcon != '' ? this.options.globalMarkerIcon : '',
81
                events: {
82
                    onClick: (current) => this.markers.forEach((marker) => {
83
                        if (current.getInstanceId() != marker.getInstanceId()) {
84
                            marker.close();
85
                        }
86
                    })
87
                }
88
            }), this.map));
89
        });
90
    }
91
}
92
93
export default GoogleMap;