-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathsetup.js
120 lines (102 loc) · 3.22 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import events from './events.js';
import Store from './store.js';
import ui from './ui.js';
import * as Constants from './constants.js';
export default function(ctx) {
let controlContainer = null;
let mapLoadedInterval = null;
const setup = {
onRemove() {
// Stop connect attempt in the event that control is removed before map is loaded
ctx.map.off('load', setup.connect);
clearInterval(mapLoadedInterval);
setup.removeLayers();
ctx.store.restoreMapConfig();
ctx.ui.removeButtons();
ctx.events.removeEventListeners();
ctx.ui.clearMapClasses();
if (ctx.boxZoomInitial) ctx.map.boxZoom.enable();
ctx.map = null;
ctx.container = null;
ctx.store = null;
if (controlContainer && controlContainer.parentNode) controlContainer.parentNode.removeChild(controlContainer);
controlContainer = null;
return this;
},
connect() {
ctx.map.off('load', setup.connect);
clearInterval(mapLoadedInterval);
setup.addLayers();
ctx.store.storeMapConfig();
ctx.events.addEventListeners();
},
onAdd(map) {
ctx.map = map;
ctx.events = events(ctx);
ctx.ui = ui(ctx);
ctx.container = map.getContainer();
ctx.store = new Store(ctx);
controlContainer = ctx.ui.addButtons();
if (ctx.options.boxSelect) {
ctx.boxZoomInitial = map.boxZoom.isEnabled();
map.boxZoom.disable();
const dragPanIsEnabled = map.dragPan.isEnabled();
// Need to toggle dragPan on and off or else first
// dragPan disable attempt in simple_select doesn't work
map.dragPan.disable();
map.dragPan.enable();
if (!dragPanIsEnabled) {
map.dragPan.disable();
}
}
if (map.loaded()) {
setup.connect();
} else {
map.on('load', setup.connect);
mapLoadedInterval = setInterval(() => { if (map.loaded()) setup.connect(); }, 16);
}
ctx.events.start();
return controlContainer;
},
addLayers() {
// drawn features style
ctx.map.addSource(Constants.sources.COLD, {
data: {
type: Constants.geojsonTypes.FEATURE_COLLECTION,
features: []
},
type: 'geojson'
});
// hot features style
ctx.map.addSource(Constants.sources.HOT, {
data: {
type: Constants.geojsonTypes.FEATURE_COLLECTION,
features: []
},
type: 'geojson'
});
ctx.options.styles.forEach((style) => {
ctx.map.addLayer(style);
});
ctx.store.setDirty(true);
ctx.store.render();
},
// Check for layers and sources before attempting to remove
// If user adds draw control and removes it before the map is loaded, layers and sources will be missing
removeLayers() {
ctx.options.styles.forEach((style) => {
if (ctx.map.getLayer(style.id)) {
ctx.map.removeLayer(style.id);
}
});
if (ctx.map.getSource(Constants.sources.COLD)) {
ctx.map.removeSource(Constants.sources.COLD);
}
if (ctx.map.getSource(Constants.sources.HOT)) {
ctx.map.removeSource(Constants.sources.HOT);
}
}
};
ctx.setup = setup;
return setup;
}