Completed
Push — develop ( 7c8456...1887a3 )
by Xaver
32s
created

map.js ➔ ... ➔ map.load   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 6
rs 9.4285
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A map.js ➔ ... ➔ document.forEach 0 4 1
1
define(['map/clientlayer', 'map/labellayer', 'map/button', 'leaflet'],
2
  function (ClientLayer, LabelLayer, Button, L) {
3
    'use strict';
4
5
    var options = {
6
      worldCopyJump: true,
7
      zoomControl: true,
8
      minZoom: 0
9
    };
10
11
    return function (config, linkScale, sidebar, router, buttons) {
12
      var self = this;
13
      var savedView;
14
15
      var map;
16
      var layerControl;
17
      var baseLayers = {};
18
19
      function saveView() {
20
        savedView = {
21
          center: map.getCenter(),
22
          zoom: map.getZoom()
23
        };
24
      }
25
26
      function contextMenuOpenLayerMenu() {
27
        document.querySelector('.leaflet-control-layers').classList.add('leaflet-control-layers-expanded');
28
      }
29
30
      var el = document.createElement('div');
31
      el.classList.add('map');
32
33
      map = L.map(el, options);
34
      var now = new Date();
35
      config.mapLayers.forEach(function (item, i) {
36
        if ((typeof item.config.start === 'number' && item.config.start <= now.getHours()) || (typeof item.config.end === 'number' && item.config.end > now.getHours())) {
37
          item.config.order = item.config.start * -1;
38
        } else {
39
          item.config.order = i;
40
        }
41
      });
42
43
      config.mapLayers = config.mapLayers.sort(function (a, b) {
44
        return a.config.order - b.config.order;
45
      });
46
47
      var layers = config.mapLayers.map(function (d) {
48
        return {
49
          'name': d.name,
50
          'layer': L.tileLayer(d.url.replace('{retina}', L.Browser.retina ? '@2x' : ''), d.config)
51
        };
52
      });
53
54
      map.addLayer(layers[0].layer);
55
56
      layers.forEach(function (d) {
57
        baseLayers[d.name] = d.layer;
58
      });
59
60
      var button = new Button(config, map, router, buttons);
61
62
      map.on('locationfound', button.locationFound);
63
      map.on('locationerror', button.locationError);
64
      map.on('dragend', saveView);
65
      map.on('contextmenu', contextMenuOpenLayerMenu);
66
67
      button.init();
68
69
      layerControl = L.control.layers(baseLayers, [], { position: 'bottomright' });
70
      layerControl.addTo(map);
71
72
      map.zoomControl.setPosition('topright');
73
74
      var clientLayer = new ClientLayer({ minZoom: config.clientZoom });
75
      clientLayer.addTo(map);
76
      clientLayer.setZIndex(5);
77
78
      var labelLayer = new LabelLayer({ minZoom: config.labelZoom });
79
      labelLayer.addTo(map);
80
      labelLayer.setZIndex(6);
81
82
      map.on('zoom', function () {
83
        clientLayer.redraw();
84
        labelLayer.redraw();
85
      });
86
87
      map.on('baselayerchange', function (e) {
88
        map.options.maxZoom = e.layer.options.maxZoom;
89
        clientLayer.options.maxZoom = map.options.maxZoom;
90
        labelLayer.options.maxZoom = map.options.maxZoom;
91
        if (map.getZoom() > map.options.maxZoom) {
92
          map.setZoom(map.options.maxZoom);
93
        }
94
95
        var style = document.querySelector('.css-mode:not([media="not"])');
96
        if (style && e.layer.options.mode !== '' && !style.classList.contains(e.layer.options.mode)) {
97
          style.media = 'not';
98
          labelLayer.updateLayer();
99
        }
100
        if (e.layer.options.mode) {
101
          var newStyle = document.querySelector('.css-mode.' + e.layer.options.mode);
102
          newStyle.media = '';
103
          newStyle.appendChild(document.createTextNode(''));
104
          labelLayer.updateLayer();
105
        }
106
      });
107
108
      map.on('load', function () {
109
        document.querySelectorAll('.leaflet-control-layers-selector').forEach(function (input) {
110
          input.setAttribute('role', 'radiogroup');
111
          input.setAttribute('aria-label', input.nextSibling.innerHTML.trim());
112
        });
113
      });
114
115
      var nodeDict = {};
116
      var linkDict = {};
117
      var highlight;
118
119
      function resetMarkerStyles(nodes, links) {
120
        Object.keys(nodes).forEach(function (d) {
121
          nodes[d].resetStyle();
122
        });
123
124
        Object.keys(links).forEach(function (d) {
125
          links[d].resetStyle();
126
        });
127
      }
128
129
      function setView(bounds, zoom) {
130
        map.fitBounds(bounds, { paddingTopLeft: [sidebar(), 0], maxZoom: (zoom ? zoom : config.nodeZoom) });
131
      }
132
133
      function goto(m) {
134
        var bounds;
135
136
        if ('getBounds' in m) {
137
          bounds = m.getBounds();
138
        } else {
139
          bounds = L.latLngBounds([m.getLatLng()]);
140
        }
141
142
        setView(bounds);
143
144
        return m;
145
      }
146
147
      function updateView(nopanzoom) {
148
        resetMarkerStyles(nodeDict, linkDict);
149
        var m;
150
151
        if (highlight !== undefined) {
152
          if (highlight.type === 'node' && nodeDict[highlight.o.node_id]) {
153
            m = nodeDict[highlight.o.node_id];
154
            m.setStyle({ color: '#ad2358', weight: 8, fillOpacity: 1, opacity: 0.4, className: 'stroke-first' });
155
          } else if (highlight.type === 'link' && linkDict[highlight.o.id]) {
156
            m = linkDict[highlight.o.id];
157
            m.setStyle({ weight: 4, opacity: 1, dashArray: '5, 10' });
158
          }
159
        }
160
161
        if (!nopanzoom) {
162
          if (m) {
163
            goto(m);
164
          } else if (savedView) {
165
            map.setView(savedView.center, savedView.zoom);
166
          } else {
167
            setView(config.fixedCenter);
168
          }
169
        }
170
      }
171
172
      self.setData = function setData(data) {
173
        nodeDict = {};
174
        linkDict = {};
175
176
        clientLayer.setData(data);
177
        labelLayer.setData(data, map, nodeDict, linkDict, linkScale, router, config);
178
179
        updateView(true);
180
      };
181
182
      self.resetView = function resetView() {
183
        button.disableTracking();
184
        highlight = undefined;
185
        updateView();
186
      };
187
188
      self.gotoNode = function gotoNode(d) {
189
        button.disableTracking();
190
        highlight = { type: 'node', o: d };
191
        updateView();
192
      };
193
194
      self.gotoLink = function gotoLink(d) {
195
        button.disableTracking();
196
        highlight = { type: 'link', o: d };
197
        updateView();
198
      };
199
200
      self.gotoLocation = function gotoLocation(d) {
201
        button.disableTracking();
202
        map.setView([d.lat, d.lng], d.zoom);
203
      };
204
205
      self.destroy = function destroy() {
206
        button.clearButtons();
207
        map.remove();
208
209
        if (el.parentNode) {
210
          el.parentNode.removeChild(el);
211
        }
212
      };
213
214
      self.render = function render(d) {
215
        d.appendChild(el);
216
        map.invalidateSize();
217
      };
218
219
      return self;
220
    };
221
  });
222