-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegalMap.js
286 lines (237 loc) · 7.2 KB
/
legalMap.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
/*jslint nomen: true, browser: true */
var LegalMap = function () {
"use strict";
var self = this;
this.countries = {};
this.uniques = {};
this.glossary = {};
this.activeFilters = { treaties: [], memberships: [], situations: [], deathPenalty: [] };
this.init = function () {
self.map = new GMaps({
div: '#map',
lat: 0,
lng: 0,
zoom: 2,
mapTypeControl: false,
streetViewControl: false,
scaleControl: false
});
self.fetchGlossary(self.fetchCountries);
$(window).resize(self.resizeMap);
// Provide actions for the filter buttons.
$(document).on('click', 'button:not(#showall)', function () {
var button = this,
new_state = self.toggleButton(button);
_.each(
['treaties', 'memberships', 'situations', 'deathPenalty'],
function (type) {
var filter_value = $(button).data(type.toLowerCase());
if (typeof filter_value !== 'undefined') {
if (new_state === 'on') {
// If we're adding a filter, we need to push onto the activeFilters array.
self.activeFilters[type].push(filter_value);
} else {
// If we're removing a filter, then we need to remove an element from the activeFilters array.
self.activeFilters[type].splice([_.indexOf(self.activeFilters[type], filter_value)], 1);
}
}
}
);
self.filterCountries();
});
$('button#showall').click(function () {
self.clearCountries();
self.showAllCountries();
});
$('a#togglefilters').click(function (e) { e.preventDefault(); self.toggleFilters(); });
};
this.toggleButton = function (button) {
var new_state = 'off';
if ($(button).data('state') === 'on') {
$(button).text($(button).text().replace(' ✔', ''));
} else {
$(button).text($(button).text() + ' ✔');
new_state = 'on';
}
$(button).data('state', new_state);
return new_state;
};
this.fetchGlossary = function (callback) {
$.getJSON('glossary.txt', function (data) {
self.glossary = data;
if (callback) {
callback();
}
});
};
this.fetchCountries = function () {
$.getJSON('countries.txt', function (data) {
self.countries = data;
// Initially, add all countries.
self.showAllCountries();
// Parse out the unique values, in order to generate our filter buttons.
self.findUniques();
self.generateFilterButtons();
self.resizeMap();
});
};
this.getCountries = function (filter) {
var filtered = self.countries,
search;
search = function (type, values) { return _.filter(self.countries, function (country) { return _.intersection(country[type], values).length === values.length; }); };
if (filter.treaties.length > 0) {
filtered = _.intersection(filtered, search("treaties", filter.treaties));
}
if (filter.memberships.length > 0) {
filtered = _.intersection(filtered, search("memberships", filter.memberships));
}
if (filter.situations.length > 0) {
filtered = _.intersection(filtered, search("situations", filter.situations));
}
if (filter.deathPenalty.length > 0) {
filtered = _.intersection(filtered, _.filter(self.countries, function (country) { return _.indexOf(filter.deathPenalty, parseInt(country.deathPenalty, 10)) !== -1; }));
}
return filtered;
};
this.findUniques = function () {
_.each(['treaties', 'memberships', 'situations'], function (type) {
self.uniques[type] = _.unique(_.flatten(_.pluck(self.countries, type)));
});
};
this.generateFilterButtons = function () {
_.each(self.uniques, function (uniques, type) {
_.each(uniques, function (name) {
if (!name) {
return;
}
var longName, classes, button, original_name;
original_name = name;
longName = name;
if (typeof self.glossary[name] === 'object') {
longName = self.glossary[name].longName || name;
name = self.glossary[name].name || name;
}
if (typeof self.glossary[name] === 'string') {
longName = self.glossary[name];
name = self.glossary[name];
}
classes = 'btn';
if (type === 'treaties') {
classes += ' btn-inverse';
}
if (type === 'situations') {
classes += ' btn-info';
}
button = $('<button />')
.addClass(classes)
.data(type, original_name)
.text(name);
if (name !== longName) {
button.attr('title', longName);
}
button.appendTo($('#' + type));
});
});
};
this.makeList = function (title, items) {
if (items.length < 1) {
return '';
}
var html = '<h4>' + title + '</h4>';
html += '<ul>';
_.each(items, function (item) {
html += '<li>' + item + '</li>';
});
html += '</ul>';
return html;
};
this.addCountry = function (country) {
var infoWindow = '<h3>' + country.name + '</h3>';
infoWindow += self.makeList('Treaties signed:', country.treaties);
infoWindow += self.makeList('Current situations:', country.situations);
infoWindow += self.makeList('Memberships:', country.memberships);
infoWindow += '<h4>Death penalty:</h4><ul>';
if (country.deathPenalty === 0) {
infoWindow += '<li>Abolished</li>';
}
if (country.deathPenalty === 1) {
infoWindow += '<li><i>De-facto</i> abolished</li>';
}
if (country.deathPenalty === 2) {
infoWindow += '<li>Still in use</li>';
}
infoWindow += '</ul>';
try {
self.map.addMarker({
lat: country.lat,
lng: country.lng,
title: country.name,
infoWindow: {
content: infoWindow
}
});
} catch (e) {
//console.log(country.name);
}
};
this.clearCountries = function () {
self.map.removeMarkers();
};
this.clearFilters = function () {
self.activeFilters = { treaties: [], memberships: [], situations: [], deathPenalty: [] };
$('button').each(function () {
$(this).text($(this).text().replace(' ✔', ''));
$(this).data('state', 'off');
});
};
this.showAllCountries = function () {
self.clearFilters();
_.each(self.countries, function (country) {
self.addCountry(country);
});
};
this.filterCountries = function () {
self.clearCountries();
var matchedCountries = self.getCountries(self.activeFilters);
_.each(matchedCountries, function (country) { self.addCountry(country); });
};
this.resizeMap = function () {
// Size the map according to the viewport.
var height = $(window).height();
if ($('.top:visible').length > 0) {
height -= $('.top').height();
}
height -= $('.below').height();
height -= 50;
$('#map').height(height);
// If we don't also tell the Google Map that we've resized,
// we'll end up with odd visual artefacts (like grey squares).
google.maps.event.trigger(self.map.map, 'resize');
};
this.toggleFilters = function () {
var toggle = $('a#togglefilters'),
action = '',
icon = '',
text = '';
if (toggle.data('action') === 'close') {
action = 'open';
icon = 'icon-arrow-down';
text = 'Open filters';
$('.top').slideUp(function () { self.updateToggle(action, icon, text); });
} else {
action = 'close';
icon = 'icon-arrow-up';
text = 'Close filters';
$('.top').slideDown(function () { self.updateToggle(action, icon, text); });
}
};
this.updateToggle = function (action, icon, text) {
var toggle = $('a#togglefilters');
toggle.data('action', action);
toggle.find('i').attr('class', icon);
toggle.find('span').text(text);
self.resizeMap();
};
};
var legalMap = new LegalMap();
$(legalMap.init);