-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesdmaps.js
315 lines (271 loc) · 8.67 KB
/
esdmaps.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
/**
* esdmaps.js v0.0.1
*
* A simple JavaScript libary to generate web maps for Excellent Schools
* Detroit.
*/
(function(root, factory) {
if (typeof exports !== 'undefined') {
// @todo: Support node?
factory(root, exports, null);
}
else {
root.ESDMaps = factory(root, root.ESDMaps || {}, root.L);
}
})(this, function(root, ESDMaps, L) {
ESDMaps.VERSION = '0.0.1';
var settings = ESDMaps.settings = {
mapboxId: 'esd.ExcellentSchoolsDetroit',
altPopupClass: 'esdmaps-alt-popup',
altPopupOpenClass: 'esdmaps-alt-popup-open',
baseUrl: "//static.excellentschoolsdetroit.org/libs/esdmaps-js",
selectedMarkerColor: '#124472'
};
// Utility functions, so we don't need jQuery
function forEach(array, fn) {
for (var i = 0; i < array.length; i++)
fn(array[i], i);
}
function defaults(obj) {
forEach(Array.prototype.slice.call(arguments, 1), function(source) {
for (var prop in source) {
if (obj[prop] === void 0) obj[prop] = source[prop];
}
});
return obj;
}
function pick(obj, whitelist) {
var out = {};
forEach(whitelist, function(k) {
if (obj[k]) {
out[k] = obj[k];
}
});
return out;
}
function getJSON(url, success, error) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onreadystatechange = function() {
if (this.readyState === 4){
if (this.status >= 200 && this.status < 400){
success(JSON.parse(this.responseText));
} else {
if (error) {
error();
}
}
}
};
request.send();
request = null;
}
function elData(el, attrs) {
var data = {};
forEach(attrs, function(attr) {
var name = 'data-' + attr;
if (el.hasAttribute(name)) {
data[attr] = el.getAttribute(name);
}
});
return data;
}
function addClass(el, className) {
if (el.classList)
el.classList.add(className);
else
el.className += ' ' + className;
}
function removeClass(el, className) {
if (el.classList)
el.classList.remove(className);
else
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
// Presets API
ESDMaps._presets = {};
var registerPreset = ESDMaps.registerPreset = function(id, options) {
ESDMaps._presets[id] = options;
};
var getPreset = ESDMaps.getPreset = function(id) {
return ESDMaps._presets[id];
};
function initAltPopup(map, options) {
var altPopup;
if (typeof options.altPopup === "undefined") {
altPopup = makeAltPopupEl();
container = map.getContainer();
container.parentNode.insertBefore(altPopup, container.nextSibling);
}
else if (typeof options.altPopup === "string") {
altPopup = document.querySelector('#' + options.altPopup);
}
else {
// Assume it's an HTMLElement
altPopup = options.altPopup;
}
if (altPopup) {
// We wrap this in an if just in case selecting the element by
// querySelctor failed.
addClass(altPopup, settings.altPopupClass);
map.on('popupopen', function(e) {
altPopup.innerHTML = e.popup.getContent();
addClass(altPopup, settings.altPopupOpenClass);
});
map.on('popupclose', function(e) {
altPopup.innerHTML = '';
removeClass(altPopup, settings.altPopupOpenClass);
});
}
}
function addPointLayer(map, url, options) {
getJSON(url, function(data) {
L.geoJson(data, {
pointToLayer: L.mapbox.marker.style,
style: function(feature) { return feature.properties; },
onEachFeature: function(feature, layer) {
layer.bindPopup(renderTemplate(options.popupTemplate, feature.properties));
layer.on('popupopen', function(e) {
var marker = e.target;
marker.feature.properties['old-color'] = marker.feature.properties['marker-color'];
marker.feature.properties['marker-color'] = settings.selectedMarkerColor,
marker.setIcon(L.mapbox.marker.icon(marker.feature.properties));
});
layer.on('popupclose', function(e) {
var marker = e.target;
marker.feature.properties['marker-color'] = marker.feature.properties['old-color'];
marker.setIcon(L.mapbox.marker.icon(marker.feature.properties));
});
if (options.showPopupOnHover) {
initHoverPopup(layer, options);
}
}
}).addTo(map);
});
return map;
}
/**
* Connect event handlers to show different popup content on hover
* and click.
*
* @param layer {L.Layer} - Marker layer
* @param options.popupTemplate {(string|function)} - Template that will be
* used to render the popup content on click.
* @param [options.hoverPopupTemplate] {(string|function)} - Template that
* will be used to render the popup content on hover. If not specified
* the normal popup content is used.
*/
function initHoverPopup(layer, options) {
layer.on('mouseover', function(e) {
var popup = e.target.getPopup();
e.target._hovering = true;
if (options.hoverPopupTemplate) {
popup.setContent(renderTemplate(options.hoverPopupTemplate, e.target.feature.properties));
}
e.target.openPopup();
L.DomUtil.addClass(popup._container, 'esdmaps-hover');
});
layer.on('mouseout', function(e) {
var popup;
if (e.target._hovering) {
popup = e.target.getPopup();
e.target._hovering = false;
L.DomUtil.removeClass(popup._container, 'esdmaps-hover');
e.target.closePopup();
}
});
layer.on('click', function(e) {
var popup;
if (e.target._hovering) {
popup = e.target.getPopup();
e.target._hovering = false;
if (options.hoverPopupTemplate) {
popup.setContent(renderTemplate(options.popupTemplate, e.target.feature.properties));
}
e.target.openPopup();
L.DomUtil.removeClass(popup._container, 'esdmaps-hover');
}
});
}
/**
* Render a template.
*
* @param tpl {(string|function}) - A Mustache template string or a template
* function. If a string is provided, the template will be
* rendered using L.mapbox.template().
* @param ctx {object} - An object representing the template
* context. It contains key/value pairs corresponding to values
* referenced in the template.
* @returns {string} The rendered template.
*/
function renderTemplate(tpl, ctx) {
if (typeof tpl === "string") {
return L.mapbox.template(tpl, ctx);
}
else if (typeof tpl === "function") {
return tpl(ctx);
}
else {
return "";
}
}
ESDMaps.map = function(element, _, options) {
options = options || {};
var preset = getPreset(_);
var map;
var container;
if (!preset) {
map = L.mapbox.map(element, _ || settings.mapboxId, options);
}
else {
options = defaults(options, pick(preset, ['zoom', 'center', 'popupTemplate', 'pointsUrl', 'showPopupOnHover', 'hoverPopupTemplate']));
map = L.mapbox.map(element, preset.mapboxId, options);
}
initAltPopup(map, options);
if (options.pointsUrl) {
addPointLayer(map, options.pointsUrl, options);
}
return map;
};
function makeAltPopupEl() {
var el = document.createElement('div');
el.setAttribute('id', 'esdmaps-alt-popup');
return el;
}
function mapFromElement(el) {
var data = elData(el, ['preset', 'alt-popup', 'points-url', 'zoom', 'center']);
var centerBits;
if (data['center']) {
centerBits = data['center'].split(',');
data['center'] = L.latLng(parseFloat(centerBits[0]),
parseFloat(centerBits[1]));
}
ESDMaps.map(el, data.preset, {
altPopup: data['alt-popup'],
pointsUrl: data['points-url'],
zoom: data['zoom'],
center: data['center']
});
}
var detectMaps = ESDMaps.detectMaps = function() {
forEach(document.querySelectorAll('.esdmaps-map'), mapFromElement);
};
// Register presets
registerPreset('recommended-schools-k-8-2014', {
mapboxId: settings.mapboxId,
center: L.latLng(42.3484, -83.058),
zoom: 13,
pointsUrl: settings.baseUrl + '/data/recommended-k-8-schools-2014-spring.json',
popupTemplate: "<div><h2>{{schoolname}}</h2>" +
"<p>" +
"{{grades}}<br>" +
"{{address}}<br>" +
"Call: {{phone}}<br>" +
"<a href='{{scorecard-url}}' target='_blank'>See more detail</a>" +
"</p></div>",
showPopupOnHover: true,
hoverPopupTemplate: "<h2>{{schoolname}}</h2>"
});
detectMaps();
return ESDMaps;
});