-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstory_map.js
540 lines (459 loc) · 21.3 KB
/
story_map.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/**
* @file
* A JavaScript file for the theme.
*/
(function ($, Drupal) {
PRINT_DEBUG = false;
/******************************************************************
Leafleft Map
******************************************************************/
Drupal.behaviors.leafletMap = {
attach:function (context, settings) {
/***********************************
Map Creation
************************************/
$(document).on("leaflet.map", function(event, map_settings, map) {
/***********************************
Map Layer Control
************************************/
if(!map.layer_control) {
//Create an empty overlay and set it as the active layer
no_overlay = L.tileLayer('').addTo(map);
// Create the Historical Image Overlay
var historical_overlay_url = Drupal.settings.story_map.historical_map_overlay_path;
var historical_overlay_bounds = [[ 49.301872803676254, -123.2984232902527], [49.20980365285424, -123.08159351348878]];
historical_overlay = L.imageOverlay(historical_overlay_url, historical_overlay_bounds);
// Add the overlays to a map layer control for toggling
var overlays = { "": historical_overlay }
var overlay_options = { collapsed: false }
map.layer_control = L.control.layers(null, overlays, overlay_options);
/****** START - UNCOMMENT FOR MAP LAYER CONTROL ******
map.layer_control.addTo(map);
// Customize map layer control
$('.leaflet-control-layers-overlays').find('input').hide();
map_overlay_control_html = '<img class="map-overlay-control" src="' + Drupal.settings.story_map.map_overlay_control_path +'">';
$('.leaflet-control-layers-overlays').find('span').append(map_overlay_control_html);
****** END - UNCOMMENT FOR MAP LAYER CONTROL ******/
//Add iCheck wrapper to input
$('.views-exposed-form input.form-control').iCheck({
checkboxClass: 'icheckbox_square-grey',
radioClass: 'iradio_square-grey',
increaseArea: '20%' // optional
});
//Keep track of which neighbourhood layers have been added
neighbourhood_layer_list = [];
}
/***********************************
Remove the 'filter-processing'
class on load
************************************/
if($('.icheckbox_square-grey').hasClass('filter-processing')) {
$('.icheckbox_square-grey').removeClass('filter-processing');
}
/***********************************
Use styles to prevent filter spam.
Auto-submit form when input state
is changed
************************************/
$('.views-exposed-form input.form-control').on('ifChanged', function(event){
$('.icheckbox_square-grey').addClass('filter-processing');
$(this).closest('form').find('.ctools-auto-submit-click').click();
});
/***********************************
Display the currently selected
Neighourhoods (KML file)
************************************/
$('.views-exposed-form .views-widget-filter-neighbourhood input.form-control').each(function(i, obj) {
filter_value = $(this).attr('value').toLowerCase().split(' ').join('_');
if($(this).prop("checked") && !(neighbourhood_layer_list.indexOf(filter_value) > -1)) {
neighbourhood_layer_list.push(filter_value);
var kml_layer = new L.KML(Drupal.settings.story_map.neighbourhoods + '/' + filter_value + '.kml', {async: true});
kml_layer.addTo(map);
}
});
/***********************************
URL Parameter Regex
************************************/
pid_regex = /(\??|\&?)pid=(islandora:\d*)/;
zoom_regex = /(\??|\&?)z=(\d*)/;
cluster_regex = /(\??|\&?)c=true/;
lat_regex = /(\??|\&?)lat=(-?\d*\.{0,1}\d*)*/;
lng_regex = /(\??|\&?)lng=(-?\d*\.{0,1}\d*)*/;
/***********************************
Get Parameters from URL
************************************/
if(document.location.search) {
if(PRINT_DEBUG) { console.log('Get Parameters from URL: ' + document.location.href); }
//PID
pid = document.location.search.match(pid_regex);
if(pid) {
if(PRINT_DEBUG) { console.log(' PID: ' + pid[2]); }
//Latitude and Longitude
lat_coord = document.location.search.match(lat_regex);
lng_coord = document.location.search.match(lng_regex);
if(lat_coord && lng_coord) {
map.panTo(L.latLng(parseFloat(lat_coord[2]), parseFloat(lng_coord[2])));
if(PRINT_DEBUG) { console.log(' Latitude: ' + lat_coord[2]); }
if(PRINT_DEBUG) { console.log(' Longitude: ' + lng_coord[2]); }
}
clustered = document.location.search.match(cluster_regex);
if(PRINT_DEBUG) { console.log(' Clustered: ' + clustered); }
if(clustered) {
setTimeout(function() {
map.setZoom(map.getMaxZoom());
}, 1000);
}
//Zoom Level
zoom_level = document.location.search.match(zoom_regex);
if(zoom_level && !clustered) {
if(PRINT_DEBUG) { console.log(' Zoom Level: ' + zoom_level[2]); }
setTimeout(function() {
map.setZoom(zoom_level[2]);
}, 1000);
}
setTimeout(function() {
$('.leaflet-marker-pane .leaflet-tagged-marker').each(function(i, obj) {
if(pid) {
if($(obj).find('div').length && $(obj).find('div')[0].innerHTML.match(/islandora:(\d*)/)[0] == pid[2]) {
$(obj).find('img').click();
}
}
});
}, 2000);
} else {
map.setMaxZoom(17);
}
} else {
map.setMaxZoom(17);
}
/***********************************
Sort the list of checkboxes by
value length
Adjust URL Parameters when filtering
************************************/
function sortByValueLength(a, b){
var a_value_length = $(a).attr('value').length;
var b_value_length = $(b).attr('value').length;
return ((a_value_length > b_value_length) ? -1 : ((a_value_length < b_value_length) ? 1 : 0));
}
var filter_checkbox_list = $(document).find('.bef-checkboxes input.form-control');
filter_checkbox_list.sort(sortByValueLength);
filter_checkbox_list.each(function(i, obj) {
filter_name = $(this).closest('.accordion').attr('class').split(' ').pop().split('-').pop();
filter_regex = new RegExp('(\\??|\\&?)' + filter_name + '\\[\\]=' + $(this).attr('value'));
if(document.location.href.match(filter_regex)) {
if(!$(this).prop("checked")) {
if(PRINT_DEBUG) { console.log('Removing Filter from URL: ' + filter_name); }
//Loading in with filter and popup
var new_url = document.location.href.replace(filter_regex, "");
clearQueryData(map, new_url);
if(PRINT_DEBUG) { console.log(' ' + document.location.href); }
} else {
return false;
}
}
});
/***********************************
Adjust URL Parameters
when popup closes
************************************/
$(document).find('.leaflet-popup-pane').each(function(i, obj) {
if(!$(obj).children() && document.location.href.match(pid_regex)) {
clearQueryData(map);
}
});
/***********************************
Popup Open
************************************/
map.on('popupopen', function(e) {
if(PRINT_DEBUG) { console.log('Popup Open'); }
// Add the media specific class to the wrapper for styling
media_type = $(e.popup._wrapper).find('.story-media').attr('data-media-type');
$(e.popup._wrapper).addClass('story-media-' + media_type);
/***********************************
Lightbox functionality
************************************/
$(e.popup._wrapper).find('.story-media img').on('click', function(e) {
e.preventDefault();
var story_media = new story_media_lightbox($(this));
story_media.init();
});
/***********************************
Download functionality
************************************/
if($(e.popup._wrapper).find('.story-media-download').length) {
if($(e.popup._wrapper).find('.story-media[data-media-type="audio"]').length) {
download_href = $(e.popup._wrapper).find('.story-media-download').attr('href').replace('OBJ', 'PROXY_MP3');
} else if($(e.popup._wrapper).find('.story-media[data-media-type="video"]').length) {
download_href = $(e.popup._wrapper).find('.story-media-download').attr('href').replace('OBJ', 'MP4');
}
$(e.popup._wrapper).find('.story-media-download').attr('href', download_href);
$(e.popup._wrapper).find('.story-media-download').siblings('br:last').remove();
}
/***********************************
Add URL parameters
************************************/
if(!document.location.href.match(pid_regex)) {
if($(e.popup._source._icon).find('div').length && document.location.href.match(/\?/)) {
history.pushState(null, null, document.location.href + "&pid=" + $(e.popup._source._icon).find('div')[0].innerHTML);
} else {
history.pushState(null, null, document.location.href + "?pid=" + $(e.popup._source._icon).find('div')[0].innerHTML);
}
history.pushState(null, null, document.location.href + "&z=" + map.getZoom());
history.pushState(null, null, document.location.href + "&lat=" + e.popup._source._latlng['lat']);
history.pushState(null, null, document.location.href + "&lng=" + e.popup._source._latlng['lng']);
if(e.popup._source._spiderLeg) {
history.pushState(null, null, document.location.href + "&c=true");
}
if(PRINT_DEBUG) { console.log(' ' + document.location.href); }
}
});
/***********************************
Popup Close
************************************/
map.on('popupclose', function(e) {
if(PRINT_DEBUG) { console.log('Popup Close'); }
/***********************************
Remove URL parameters
************************************/
clearQueryData(map);
if(PRINT_DEBUG) { console.log(' ' + document.location.href); }
});
/***********************************
Breakpoints
************************************/
ssm.addState({
id: 'narrow-screen-max',
query: '(max-width: 767px)',
onEnter: function() {
if(PRINT_DEBUG) { console.log('Enter -- Narrow Screen Max'); }
/***********************************
Resets the map size after load
************************************/
setTimeout(function() {
map.invalidateSize();
});
/***********************************
Auto-submit form when input state
is changed
Close mobile navigation menu
************************************/
$('.views-exposed-form input.form-control').on('ifChanged', function(event){
$('button.navbar-toggle').click();
$(this).closest('form').find('.ctools-auto-submit-click').click();
});
/***********************************
Add Sidebar and Footer Sponsers
to Mobile Navigation
************************************/
// Filters
if($('nav.navbar-nav .view-filters').length == 0) {
if($('nav.navbar-nav li').length) {
$('nav.navbar-nav li:last').after($('.view-story-city .view-filters').detach());
} else {
$('nav.navbar-nav').append($('.view-story-city .view-filters').detach());
}
}
if($('nav.navbar-nav .addtoany-wrapper').length == 0) {
// AddtoAny Wrapper
$('nav.navbar-nav .view-filters').after($('.view-story-city .addtoany-wrapper').detach());
}
// This Vancouver Wrapper
$('nav.navbar-nav .addtoany-wrapper').after($('.view-story-city .this-vancouver-wrapper').detach());
// Footer Sponsers
$('footer .funding-credit').parent('.col-xs-9').after('<div class="sponsor-placeholder"></div>');
$('nav.navbar-nav .this-vancouver-wrapper').after($('footer .funding-credit').parent('.col-xs-9').detach());
$('.funding-credit').parent('.col-xs-9').removeClass().addClass('sponsor-wrapper');
},
onLeave: function() {
if(PRINT_DEBUG) { console.log('Leave -- Narrow Screen Max'); }
/***********************************
Put back Sidebar and Footer Sponsors
************************************/
// Footer Sponsors
$('footer .sponsor-placeholder').after($('.funding-credit').parent('.sponsor-wrapper').detach());
$('.funding-credit').parent('.sponsor-wrapper').removeClass().addClass('col-xs-9');
$('footer .sponsor-placeholder').remove();
// This Vancouver Wrapper
$('.view-story-city .filter-share-wrapper').append($('nav.navbar-nav .this-vancouver-wrapper').detach());
// AddtoAny Wrapper
if($('.view-story-city .addtoany-wrapper').length) {
$('nav.navbar-nav .addtoany-wrapper').remove();
} else {
$('.view-story-city .this-vancouver-wrapper').before($('nav.navbar-nav .addtoany-wrapper').detach());
}
// Filters
if($('.view-story-city .view-filters').length) {
$('nav.navbar-nav .view-filters').remove();
} else {
$('.view-story-city .addtoany-wrapper').before($('nav.navbar-nav .view-filters').detach());
}
}
});
});
/***********************************
Filter Accordions
************************************/
$('.view-story-city .views-exposed-widgets .views-exposed-widget').each(function(i, obj) {
if(!$(obj).hasClass('views-submit-button') && !$(obj).hasClass('views-reset-button')) {
filter_accordion_html = '<div class="accordion ' + $(obj).attr('class').match(/views-widget-filter.*/) + '"">';
filter_accordion_html += '<details><summary><strong>' + $(obj).find('label').html() + '</strong></summary>';
filter_accordion_html += '<div class="accordion-content">' + $(obj).find('.views-widget')[0].outerHTML + '</div></details></div>';
$(obj).after(filter_accordion_html);
$(obj).remove();
}
// Delay displaying the Reset Button
if($(obj).hasClass('views-reset-button')) {
$(obj).show();
}
});
/***********************************
Wrapper for the sidebar elements
************************************/
if($('.filter-share-wrapper').length == 0) {
$('.view-story-city .view-content').before('<div class="filter-share-wrapper"></div>');
$('.view-story-city .filter-share-wrapper').append($('.view-story-city .view-filters').detach());
}
/***********************************
Keep Accordion open if it contains
an active filter
************************************/
$('.view-story-city .accordion-content .bef-checkboxes input').each(function(i, obj) {
if($(obj).prop('checked')) {
$(obj).closest('details').attr('open', true);
}
});
/***********************************
Format Filter icons for options
************************************/
$('.view-story-city .views-widget-filter-format .form-type-bef-checkbox label').each(function(i, obj) {
media_type = $(obj).attr('for').split('-').pop();
if(media_type == "imag") {
media_type = "image";
}
if($(obj).siblings('img').length == 0 && media_type in Drupal.settings.story_map.marker_icons ) {
$(obj).before('<img src="' + Drupal.settings.story_map.marker_icons[media_type] + '"/>');
}
});
/***********************************
Move the AddToAny Share buttons
after the Filter Accordions
************************************/
if($('.view-story-city .view-footer').length) {
$('.view-story-city .view-filters').after('<div class="addtoany-wrapper">' + $('.view-story-city .view-footer')[0].innerHTML + '</div>');
$('.view-story-city .view-footer').remove();
}
/***********************************
Opens the Format Filter by default
************************************/
if($('.views-widget-filter-format').length) {
$('.views-widget-filter-format').find('details').attr('open', true);
}
}
}
/******************************************************************
Clear Query Data from URL
******************************************************************/
function clearQueryData(map, new_url) {
if(new_url === undefined) {
new_url = "";
}
if(new_url) {
starting_url = new_url;
} else {
starting_url = document.location.href;
}
//PID
if(document.location.href.match(pid_regex)) {
new_url = starting_url.replace(pid_regex, "");
}
//Zoom
if(document.location.href.match(zoom_regex)) {
new_url = new_url.replace(zoom_regex, "");
}
//Cluster
if(document.location.href.match(cluster_regex)) {
new_url = new_url.replace(cluster_regex, "");
//Reset the map max zoom level
map.setMaxZoom(17);
}
//Latitude
if(document.location.href.match(lat_regex)) {
new_url = new_url.replace(lat_regex, "");
}
//Longitude
if(document.location.href.match(lng_regex)) {
new_url = new_url.replace(lng_regex, "");
}
//Replace URL
history.replaceState(null, null, new_url);
}
/******************************************************************
Lightbox
******************************************************************/
function story_media_lightbox(thumbnail) {
this.media = '';
this.media_link = thumbnail.parent('a').attr('href');
this.media_type = thumbnail.data('storyMediaType');
this.init = init;
function init() {
my_lightbox = this;
//Make the modal placeholder if it doesn't already exist
if($('.modal-placeholder').length == 0) {
$('body').append('<div class="modal-placeholder"></div>');
}
// Create the modal markup
modal_markup = '<div class="remodal remodal-story-media" data-remodal-id="story-media-modal">';
modal_markup += '<button data-remodal-action="close" class="remodal-close"></button>';
modal_markup += '<div class="story-media-wrapper">'
if(my_lightbox.media_type == "video") {
modal_markup += '<video controls controlsList="nodownload"> <source src="' + my_lightbox.media_link + '"> Your browser does not support the video element. </video>';
} else if(my_lightbox.media_type == "image") {
modal_markup += '<img alt="thumbnail" src="' + my_lightbox.media_link + '">';
}
modal_markup += '</div>';
modal_markup += '</div>';
//Apply the HTML to the modal placeholder
$('.modal-placeholder').html(modal_markup);
//Create and open the modal
var remodal_options = {
hashTracking: false
}
modal_instance = $('[data-remodal-id=story-media-modal]').remodal(remodal_options);
modal_instance.open();
//Destroy the modal when it's closed, so it can re-made later
$(document).on('closing', '.remodal', function (e) {
modal_instance.destroy();
});
}
}
/******************************************************************
AddToAny
******************************************************************/
// A custom "onShare" handler for AddToAny
function my_addtoany_onshare(share_data) {
// Some fragment identifier
var query_parameters = document.location.search;
// The shared URL
var old_url = share_data.url;
// Initialize new shared URL
var new_url = old_url;
// Add fragment identifier if not already added to the shared URL
if ( old_url.indexOf(query_parameters, old_url.length - query_parameters.length) === -1 ) {
new_url = old_url + query_parameters;
}
// Modify the share by returning an object
// with a "url" property containing the new URL
if (new_url != old_url) {
return {
url: new_url
};
}
}
// Setup AddToAny "onShare" callback function
var a2a_config = a2a_config || {};
a2a_config.callbacks = a2a_config.callbacks || [];
a2a_config.callbacks.push({
share: my_addtoany_onshare
});
}(jQuery, Drupal));