/
jeo.js
388 lines (291 loc) · 7.88 KB
/
jeo.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
if(jeo_settings.mapbox_access_token) {
L.mapbox.accessToken = jeo_settings.mapbox_access_token;
}
var jeo = {};
(function($) {
jeo = function(conf, callback) {
var _init = function() {
if(conf.mainMap)
$('body').addClass('loading-map');
if(conf.admin) { // is admin panel
return jeo.build(conf, callback);
}
if(conf.dataReady || !conf.postID) { // data ready
return jeo.build(conf, callback);
}
return $.getJSON(jeo_localization.ajaxurl,
{
action: 'map_data',
map_id: conf.postID
},
function(map_data) {
mapConf = jeo.parseConf(map_data);
mapConf = _.extend(mapConf, conf);
return jeo.build(mapConf, callback);
});
}
if($.isReady) {
return _init();
} else {
return $(document).ready(_init);
}
};
jeo.maps = {};
jeo.build = function(conf, callback) {
/*
* Map settings
*/
var options = {
maxZoom: 17,
minZoom: 0,
zoom: 2,
center: [0,0],
attributionControl: false
};
if(conf.center && !isNaN(conf.center[0]))
options.center = conf.center;
if(conf.zoom && !isNaN(conf.zoom))
options.zoom = conf.zoom;
if(conf.bounds)
options.maxBounds = conf.bounds;
if(conf.maxZoom && !isNaN(conf.maxZoom) && !conf.preview)
options.maxZoom = conf.maxZoom;
if(conf.minZoom && !isNaN(conf.minZoom) && !conf.preview)
options.minZoom = conf.minZoom;
var map;
if(!conf.containerID)
conf.containerID = 'map_' + conf.postID + '_' + conf.count;
var map_id = conf.containerID;
// use mapbox map for more map resources
map = L.mapbox.map(map_id, null, options);
if(conf.mainMap)
jeo.map = map;
/*
* DOM settings
*/
// store jquery node
map.$ = $('#' + map_id);
if(conf.mainMap) {
$('body').removeClass('loading-map');
if(!$('body').hasClass('displaying-map'))
$('body').addClass('displaying-map');
}
// store conf
map.conf = conf;
// store map id
map.map_id = map_id;
if(conf.postID)
map.postID = conf.postID;
// layers
jeo.loadLayers(map, jeo.parseLayers(map, conf.layers));
// set bounds
if(conf.fitBounds instanceof L.LatLngBounds)
map.fitBounds(conf.fitBounds);
// Handlers
if(conf.disableHandlers) {
// mousewheel
if(conf.disableHandlers.mousewheel)
map.scrollWheelZoom.disable();
}
/*
* Legends
*/
if(conf.legend_full)
jeo.enableDetails(map, conf.legend_full);
/*
* Fullscreen
*/
map.addControl(new jeo.fullscreen());
/*
* Geocode
*/
if(map.conf.geocode)
map.addControl(new jeo.geocode());
/*
* Filter layers
*/
if(map.conf.filteringLayers)
map.addControl(new jeo.filterLayers());
/*
* CALLBACKS
*/
// conf passed callbacks
if(typeof conf.callbacks === 'function')
conf.callbacks(map);
// map is ready, do callbacks
jeo.runCallbacks('mapReady', [map]);
if(typeof callback === 'function')
callback(map);
return map;
}
/*
* Utils
*/
jeo.parseLayers = function(map, layers) {
var parsedLayers = [];
$.each(layers, function(i, layer) {
if(layer.type == 'cartodb' && layer.cartodb_type == 'viz') {
var cdbOpts = {
legends: false
};
if(jeo_localization.ssl)
cdbOpts.https = true;
var pLayer = cartodb.createLayer(map, layer.cartodb_viz_url, cdbOpts);
if(layer.legend) {
pLayer._legend = layer.legend;
}
parsedLayers.push(pLayer);
} else if(layer.type == 'mapbox') {
var pLayer = L.mapbox.tileLayer(layer.mapbox_id);
if(layer.legend) {
pLayer._legend = layer.legend;
}
parsedLayers.push(pLayer);
parsedLayers.push(L.mapbox.gridLayer(layer.mapbox_id));
} else if(layer.type == 'tilelayer') {
var options = {};
if(layer.tms)
options.tms = true;
var pLayer = L.tileLayer(layer.tile_url, options);
if(layer.legend) {
pLayer._legend = layer.legend;
}
parsedLayers.push(pLayer);
if(layer.utfgrid_url && layer.utfgrid_template) {
parsedLayers.push(L.mapbox.gridLayer({
"name": layer.title,
"tilejson": "2.0.0",
"scheme": "xyz",
"template": layer.utfgrid_template,
"grids": [layer.utfgrid_url.replace('{s}', 'a')]
}));
}
}
});
return parsedLayers;
};
jeo.loadLayers = function(map, parsedLayers) {
for(var key in map.legendControl._legends) {
if(key.indexOf('map-details-link') == -1)
map.legendControl.removeLegend(key);
}
if(map.coreLayers) {
for(var key in map.coreLayers._layers) {
map.coreLayers.removeLayer(key);
}
} else {
map.coreLayers = new L.layerGroup();
map.addLayer(map.coreLayers);
}
$.each(parsedLayers, function(i, layer) {
if(layer._legend) {
map.legendControl.addLegend(layer._legend);
}
layer.addTo(map.coreLayers);
if(layer._tilejson) {
map.addControl(L.mapbox.gridControl(layer));
}
});
return map.coreLayers;
}
jeo.parseConf = function(conf) {
var newConf = $.extend({}, conf);
newConf.server = conf.server;
if(conf.conf)
newConf = _.extend(newConf, conf.conf);
newConf.layers = [];
newConf.filteringLayers = {};
newConf.filteringLayers.switchLayers = [];
newConf.filteringLayers.swapLayers = [];
$.each(conf.layers, function(i, layer) {
newConf.layers.push(_.clone(layer));
if(layer.filtering == 'switch') {
var switchLayer = {
ID: layer.ID,
title: layer.title
};
if(layer.hidden)
switchLayer.hidden = true;
newConf.filteringLayers.switchLayers.push(switchLayer);
}
if(layer.filtering == 'swap') {
var swapLayer = {
ID: layer.ID,
title: layer.title
};
if(layer.first_swap)
swapLayer.first = true;
newConf.filteringLayers.swapLayers.push(swapLayer);
}
});
newConf.center = [parseFloat(conf.center.lat), parseFloat(conf.center.lon)];
if(conf.pan_limits.south && conf.pan_limits.north) {
newConf.bounds = [
[conf.pan_limits.south, conf.pan_limits.west],
[conf.pan_limits.north, conf.pan_limits.east]
];
}
newConf.zoom = parseInt(conf.zoom);
newConf.minZoom = parseInt(conf.min_zoom);
newConf.maxZoom = parseInt(conf.max_zoom);
if(conf.geocode)
newConf.geocode = true;
newConf.disableHandlers = {};
if(conf.disable_mousewheel)
newConf.disableHandlers.mousewheel = true;
if(conf.legend)
newConf.legend = conf.legend;
if(conf.legend_full)
newConf.legend_full = conf.legend_full;
return newConf;
}
/*
* Legend page (map details)
*/
jeo.enableDetails = function(map, full) {
map.conf.legend_full_content = '<span class="map-details-link">' + jeo_localization.more_label + '</span>';
map.legendControl.addLegend(map.conf.legend_full_content);
var isContentMap = map.$.parents('.content-map').length;
var $detailsContainer = map.$.parents('.map-container');
if(isContentMap)
$detailsContainer = map.$.parents('.content-map');
if(!$detailsContainer.hasClass('clearfix'))
$detailsContainer.addClass('clearfix');
map.$.on('click', '.map-details-link', function() {
$detailsContainer.append($('<div class="map-details-page"><div class="inner"><a href="#" class="close">×</a>' + full + '</div></div>'));
$detailsContainer.find('.map-details-page .close, .map-nav a').click(function() {
$detailsContainer.find('.map-details-page').remove();
return false;
});
});
}
/*
* Callback manager
*/
jeo.callbacks = {};
jeo.createCallback = function(name) {
jeo.callbacks[name] = [];
jeo[name] = function(callback) {
jeo.callbacks[name].push(callback);
}
}
jeo.runCallbacks = function(name, args) {
if(!jeo.callbacks[name]) {
console.log('A JEO callback tried to run, but wasn\'t initialized');
return false;
}
if(!jeo.callbacks[name].length)
return false;
var _run = function(callbacks) {
if(callbacks) {
_.each(callbacks, function(c, i) {
if(c instanceof Function)
c.apply(this, args);
});
}
}
_run(jeo.callbacks[name]);
}
jeo.createCallback('mapReady');
jeo.createCallback('layersReady');
})(jQuery);