-
Notifications
You must be signed in to change notification settings - Fork 34
/
mapbox.js
225 lines (184 loc) · 6.73 KB
/
mapbox.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* Copyright (c) 2014 Paulo Sérgio Borges de Oliveira Filho
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
var FILES = {
mapbox: {
js: ['https://api.tiles.mapbox.com/mapbox.js/v2.2.3/mapbox.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/v2.2.3/mapbox.css'],
},
mapboxgl: {
js: ['https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.1/mapbox-gl.js'],
css: ['https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.1/mapbox-gl.css'],
},
turf: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.2/turf.min.js'],
css: []
},
directions: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/mapbox-directions.js/v0.4.0/mapbox.directions.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/mapbox-directions.js/v0.4.0/mapbox.directions.css']
},
zoomslider: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-zoomslider/v0.7.0/L.Control.Zoomslider.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-zoomslider/v0.7.0/L.Control.Zoomslider.css']
},
pip: {
js: ['https://api.mapbox.com/mapbox.js/plugins/leaflet-pip/v0.1.0/leaflet-pip.min.js'],
css: []
},
osm: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-osm/v0.1.0/leaflet-osm.js'],
css: []
},
omnivore: {
js: ['https://api.mapbox.com/mapbox.js/plugins/leaflet-omnivore/v0.2.0/leaflet-omnivore.min.js'],
css: ['https://api.mapbox.com/mapbox.js/plugins/leaflet-minimap/v1.0.0/Control.MiniMap.css']
},
minimap: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-minimap/v1.0.0/Control.MiniMap.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-minimap/v1.0.0/Control.MiniMap.css']
},
markercluster: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/leaflet.markercluster.js'],
css: [
'https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.css',
'https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.Default.css'
],
},
// FIXME: Doesn't support IE<9
// https://www.mapbox.com/mapbox.js/example/v1.0.0/leaflet-locatecontrol/
locate: {
js: ['https://api.mapbox.com/mapbox.js/plugins/leaflet-locatecontrol/v0.43.0/L.Control.Locate.min.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-locatecontrol/v0.43.0/L.Control.Locate.css']
},
label: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-label/v0.2.1/leaflet.label.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-label/v0.2.1/leaflet.label.css']
},
image: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-image/v0.0.4/leaflet-image.js'],
css: []
},
heat: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-heat/v0.1.3/leaflet-heat.js'],
css: []
},
hash: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-hash/v0.2.1/leaflet-hash.js'],
css: []
},
geodesy: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-geodesy/v0.1.0/leaflet-geodesy.js'],
css: []
},
fullscreen: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v0.0.4/Leaflet.fullscreen.min.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v0.0.4/leaflet.fullscreen.css']
},
draw: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.js'],
css: ['https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.css']
},
geojsonExtend: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/geojson-extent/v0.0.1/geojson-extent.js'],
css: []
},
geoViewport: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/geo-viewport/v0.1.1/geo-viewport.js'],
css: []
},
arc: {
js: ['https://api.tiles.mapbox.com/mapbox.js/plugins/arc.js/v0.1.0/arc.js'],
css: []
}
};
var deps = new Deps.Dependency;
var loaded = false;
var onLoaded = function () {
loaded = true;
Mapbox.hook && Mapbox.hook.call && Mapbox.hook.call(this);
deps.changed();
};
var onMapboxLoaded = function (plugins, cb) {
var pluginCount = _.size(plugins);
if (pluginCount === 0) {
cb();
return;
}
var loadCb = function () {
pluginCount--;
if (pluginCount === 0) {
cb();
return;
}
};
_.each(plugins, function (plugin) {
loadFiles(FILES[plugin], loadCb);
});
};
var loadScript = function (src, cb) {
var elem = document.createElement('script');
elem.type = 'text/javascript';
elem.src = src;
elem.defer = true;
elem.addEventListener('load', _.partial(cb, src), false);
var head = document.getElementsByTagName('head')[0];
head.appendChild(elem);
};
var loadCss = function (href) {
var elem = document.createElement('link');
elem.rel = 'stylesheet';
elem.href = href;
var head = document.getElementsByTagName('head')[0];
head.appendChild(elem);
};
var loadFiles = function (files, cb) {
var loadCount = _.size(files.js);
var loadCb = function (url) {
if (Mapbox.debug)
console.log('Loaded:', url);
loadCount--;
if (loadCount === 0)
cb();
};
_.each(files.css, loadCss);
_.each(files.js, function (url) {
loadScript(url, loadCb);
});
};
Mapbox = {
hook: null,
debug: false,
load: function (opts) {
if (loaded)
return;
var opts = opts || {};
var plugins = opts.plugins || [];
var initialFiles = opts.gl ? FILES.mapboxgl : FILES.mapbox;
loadFiles(initialFiles, _.partial(onMapboxLoaded, plugins, onLoaded));
},
loaded: function () {
deps.depend();
return loaded;
},
onLoaded: function(cb) {
this.hook = cb;
}
};