1 | import mapboxgl from 'mapbox-gl';
|
2 | import { EventEmitter as events } from 'events';
|
3 | import { render as reactDOM } from 'react-dom';
|
4 | import React from 'react';
|
5 |
|
6 | import { getChangeset } from './getChangeset';
|
7 | import { Sidebar } from './sidebar';
|
8 | import { Map as GlMap } from './map';
|
9 |
|
10 | import { config } from './config';
|
11 |
|
12 | export const cmap = new events();
|
13 |
|
14 | let map;
|
15 |
|
16 | window.cmap = cmap;
|
17 |
|
18 | export function render(container, changesetId, options) {
|
19 | container.style.width = options.width || '1000px';
|
20 | container.style.height = options.height || '500px';
|
21 |
|
22 | options = options || {};
|
23 | options.overpassBase = options.overpassBase || config.overpassBase;
|
24 | mapboxgl.accessToken = config.mapboxAccessToken;
|
25 | container.classList.add('cmap-loading');
|
26 | if (!map) {
|
27 | map = new GlMap();
|
28 | }
|
29 |
|
30 | if (options.data) {
|
31 | _render(container, changesetId, options.data, options.disableSidebar);
|
32 | } else {
|
33 | getChangeset(changesetId, options.overpassBase)
|
34 | .then(result => _render(container, changesetId, result))
|
35 | .catch(err => {
|
36 | errorMessage(err.msg);
|
37 | });
|
38 | }
|
39 |
|
40 | return cmap;
|
41 | }
|
42 | export function getMapInstance() {
|
43 | return map;
|
44 | }
|
45 |
|
46 | function _render(container, changesetId, result, disableSidebar) {
|
47 | renderHTML(container, changesetId, result, disableSidebar);
|
48 |
|
49 | container.classList.remove('cmap-loading');
|
50 |
|
51 | map.renderMap(false, result);
|
52 |
|
53 | var featureMap = result.featureMap;
|
54 |
|
55 | cmap.removeAllListeners();
|
56 | cmap.on('remove', () => {
|
57 | map.remove();
|
58 | });
|
59 |
|
60 | cmap.on('selectFeature', (geometryType, featureId) => {
|
61 | if (geometryType && featureId) {
|
62 | map.selectFeature(featureMap[featureId][0], featureMap);
|
63 | }
|
64 | });
|
65 |
|
66 | cmap.on('clearFeature', () => {
|
67 | map.clearFeature();
|
68 | });
|
69 | }
|
70 |
|
71 |
|
72 | function renderHTML(container, changesetId, result, disableSidebar) {
|
73 | var info;
|
74 | if (document.getElementById('seat')) {
|
75 | info = document.getElementById('seat');
|
76 | } else {
|
77 | info = document.createElement('div');
|
78 | info.id = 'seat';
|
79 | container.appendChild(info);
|
80 | }
|
81 | container.classList.add('cmap-container');
|
82 |
|
83 |
|
84 | result.geojson.features.forEach(feature => {
|
85 | var tags = feature.properties.tags || {};
|
86 | feature.properties.tagsCount = Object.keys(tags).length;
|
87 | });
|
88 |
|
89 | reactDOM(
|
90 | <div>
|
91 | <div className="cmap-map" />
|
92 |
|
93 | <div className="cmap-diff" style={{ display: 'none' }}>
|
94 | <div
|
95 | className="cmap-diff-metadata cmap-scroll-styled"
|
96 | style={{ display: 'none' }}
|
97 | />
|
98 | <div
|
99 | className="cmap-diff-tags cmap-scroll-styled"
|
100 | style={{ display: 'none' }}
|
101 | />
|
102 | </div>
|
103 | {!disableSidebar &&
|
104 | <Sidebar
|
105 | result={result}
|
106 | changesetId={changesetId}
|
107 | filterLayers={map.filterLayers}
|
108 | toggleLayer={function(e) {
|
109 | var layer = e.target.value;
|
110 | if (layer === 'satellite') {
|
111 | map.renderMap(
|
112 | 'mapbox://styles/rasagy/cizp6lsah00ct2snu6gi3p16q',
|
113 | result
|
114 | );
|
115 | }
|
116 |
|
117 | if (layer === 'dark') {
|
118 | map.renderMap('mapbox://styles/mapbox/dark-v9', result);
|
119 | }
|
120 |
|
121 | if (layer === 'streets') {
|
122 | map.renderMap('mapbox://styles/mapbox/streets-v9', result);
|
123 | }
|
124 | }}
|
125 | />}
|
126 | </div>,
|
127 | info
|
128 | );
|
129 | }
|
130 |
|
131 | function errorMessage(message) {
|
132 | message = message || 'An unexpected error occured';
|
133 | document.querySelector('.cmap-info').innerHTML = message;
|
134 | document.querySelector('.cmap-sidebar').style.display = 'block';
|
135 | document.querySelector('.cmap-layer-selector').style.display = 'none';
|
136 | document.querySelector('.cmap-type-selector').style.display = 'none';
|
137 | }
|