-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdemo.js
206 lines (183 loc) · 6.64 KB
/
demo.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
var geocoder = new google.maps.Geocoder(),
map,
marker,
featureGroup,
representativeTemplate = _.template( // This is the only underscore dependency.
'<div class="col-xs-6 col-sm-4 col-md-2 representative">' +
'<div class="avatar" style="background-image: url(<% if (photo_url) { %><%= photo_url %><% } else { %>/static/img/silhouette.png<% } %>)"></div> ' +
'<p><% if (party_name) { %><%= party_name %><% } %> ' + '<%= elected_office %> ' +
'<strong><% if (url) { %><a href="<%= url %>"><%= name %></a><% } else { %><%= name %><% } %></strong></p> ' +
'<p class="district-name"><%= district_name %> <button type="button" class="btn btn-default btn-xs shape" data-url="<%= related.boundary_url %>">' + gettext('Map') + '</button></p> ' +
'<p><% if (email) { %><a href="mailto:<%= email %>">' + gettext('Email') + ' <%= first_name %></a><% } %></p> ' +
'</div>'
);
/**
* @see https://learn.jquery.com/code-organization/deferreds/examples/
*/
function createCache(url) {
var cache = {};
return function (arg) {
var key = arg.toString();
if (!cache[key]) {
cache[key] = $.ajax({dataType: 'json', url: url(arg)});
}
return cache[key];
};
}
/**
* @param L.LatLng latlng
*/
var getRepresentativesByLatLng = createCache(function (latlng) {
return '/representatives/?limit=0&point=' + latlng.lat + ',' + latlng.lng;
});
/**
* @param string path the boundary's path
*/
var getBoundaryShape = createCache(function (path) {
return path + 'simple_shape';
});
/**
* @param L.LatLng latlng
*/
function processLatLng(latlng) {
featureGroup.clearLayers();
$('#map').css('visibility', 'visible');
marker.setLatLng(latlng);
map.panTo(latlng);
getRepresentativesByLatLng(latlng).then(function (response) {
var representatives = response.objects.slice(),
groups = {},
order = 'MP|MHA|MLA|MNA|MPP|Chair|Maire|Mayor|Regional Chair|Warden|Deputy Mayor|Deputy Warden|Councillor at Large|Regional Councillor|Conseiller|Councillor|Commissioner';
representatives.sort(function (a, b) {
var x = order.indexOf(a['elected_office']),
y = order.indexOf(b['elected_office']);
if (x < y) {
return -1;
}
else if (x > y) {
return 1;
}
else if (a['last_name'] < b['last_name']) {
return -1;
}
else {
return 1;
}
});
var $representatives = $('<div id="representatives"></div>'), $row;
var i = 0;
$.each(representatives, function (j, object) {
if (object['elected_office'] && object['related']['representative_set_url'].indexOf('/campaign-set-') === -1) {
if (i % 6 == 0) {
$row = $('<div class="row"></div>');
$representatives.append($row);
}
else if (i % 3 == 0) {
$row.append('<div class="clearfix visible-sm"></div>')
}
else if (i % 2 == 0) {
$row.append('<div class="clearfix visible-xs"></div>')
}
$row.append($(representativeTemplate(object)));
i++;
}
});
$('#representatives').replaceWith($representatives);
$('#representatives').imagesLoaded().progress(function (instance, image) {
if (!image.isLoaded) {
$(image.img).attr('src', '/static/img/silhouette.png');
}
});
});
}
function processAddress() {
$('.alert').hide();
$('#addresses').empty();
geocoder.geocode({address: $('#address').val(), region: 'ca', language: gettext('en')}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results.length > 1) {
$('#addresses').append('<option>' + gettext('Select your address') + '</option>');
$.each(results, function (i, result) {
$('#addresses').append('<option data-latitude="' + result.geometry.location.lat() + '" data-longitude="' + result.geometry.location.lng() + '">' + result.formatted_address + '</option>');
});
$('#many-results').fadeIn('slow');
}
else {
processLatLng(L.latLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));
}
}
else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
$('#no-results').fadeIn('slow');
}
else {
$('#unknown-error').fadeIn('slow');
}
});
}
$(function ($) {
var latlng = L.latLng(45.444369, -75.693832), // 24 Sussex Drive, Ottawa
index = window.location.href.indexOf('#'),
anchor;
// Create the map, marker and feature group.
map = L.map('map', {
center: latlng,
layers: [
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'mapbox.streets',
accessToken: 'pk.eyJ1IjoianBtY2tpbm5leTIiLCJhIjoiY2p1M2Fzbm5pMGwzZTQ0bnl0ZDRrdHA3dyJ9.vtQ-f6tJQ3tYJc08ok03lQ'
})
],
maxZoom: 17,
zoom: 13,
scrollWheelZoom: false,
touchZoom: false
});
map.attributionControl.setPrefix(false);
marker = L.marker(latlng, {draggable: true});
featureGroup = L.featureGroup();
map.addLayer(marker);
map.addLayer(featureGroup);
// Moving the marker calls the API.
marker.on('dragend', function () {
$('.alert').hide();
processLatLng(marker.getLatLng());
});
// Geolocation calls the API.
map.on('locationfound', function (event) {
$('.alert').hide();
processLatLng(event.latlng);
});
// Call the API if the user submits an address.
$('#submit').click(function (event) {
processAddress();
event.preventDefault();
});
// Call the API if the user selects on an address.
$('#addresses').change(function (event) {
var $this = $(this).find(':selected');
processLatLng(L.latLng($this.data('latitude'), $this.data('longitude')));
event.preventDefault();
});
// Display a boundary if user clicks on a boundary name.
$(document).on('click', '.shape', function (event) {
featureGroup.clearLayers();
$.scrollTo('#map', {axis: 'y', duration: 600, easing: 'easeOutQuart', offset: -40});
getBoundaryShape($(this).data('url')).then(function (response) {
featureGroup.addLayer(L.geoJson(response));
map.fitBounds(featureGroup.getBounds());
});
event.preventDefault();
});
// Perform the first geolocation.
if (index !== -1) { // Backwards-compatibility.
anchor = window.location.href.substr(index + 1);
}
if (anchor) {
$('#address').val(unescape(anchor));
processAddress();
}
else {
map.locate({setView: true, maxZoom: 13});
}
});