Skip to content

Instantly share code, notes, and snippets.

@markdboyd
Last active June 1, 2017 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markdboyd/29b04019b9285594937845d5579a8fbe to your computer and use it in GitHub Desktop.
Save markdboyd/29b04019b9285594937845d5579a8fbe to your computer and use it in GitHub Desktop.
(function ($, Drupal, drupalSettings) {
'use strict';
/**
* @namespace
*/
drupalSettings.geolocation.html5 = drupalSettings.geolocation.html5 || {};
drupalSettings.geolocation.html5.proximity_view_ids = drupalSettings.geolocation.html5.proximity_view_ids || [];
Drupal.behaviors.geolocationProximityHTML5 = {
attach: function(context) {
var _this = this;
if (!drupalSettings.geolocation.html5.proximity_view_ids.length ||
drupalSettings.geolocation.html5.permissionDenied ||
drupalSettings.geolocation.html5.hasCoordinates) {
return;
}
$.each(drupalSettings.geolocation.html5.proximity_view_ids, function(key, dom_id) {
drupalSettings.geolocation.html5[dom_id].mapLoaded = $.Deferred();
drupalSettings.geolocation.html5[dom_id].receivedCoordinates = $.Deferred();
Drupal.geolocation.addMapLoadedCallback(function(map) {
console.log('map loaded');
drupalSettings.geolocation.html5[map.id].mapLoaded.resolve(map);
}, dom_id);
Drupal.geolocation.html5.addResultCallback(function (position) {
// If specified, auto refresh the view with the received
// HTML5 coordinates.
if (drupalSettings.geolocation.html5[dom_id] &&
drupalSettings.geolocation.html5[dom_id].auto_refresh) {
console.log('received coordinates');
drupalSettings.geolocation.html5[dom_id].receivedCoordinates.resolve(position);
}
});
$.when(
drupalSettings.geolocation.html5[dom_id].mapLoaded,
drupalSettings.geolocation.html5[dom_id].receivedCoordinates
).then(function(map, position) {
console.log('both loaded');
var refreshViewDelayTimer;
clearTimeout(refreshViewDelayTimer);
refreshViewDelayTimer = setTimeout(function () {
_this.refreshView(dom_id, context, position.coords);
}, 1000);
});
});
},
refreshView: function(dom_id, context, coordinates) {
// Get the AJAX settings for this view.
var viewSettings = Drupal.views.instances['views_dom_id:' + dom_id];
var geolocationAjaxSettings = viewSettings.element_settings;
// Change the progress indicator.
geolocationAjaxSettings.progress.type = 'throbber';
// Add the coordinates to the data to be submitted with the
// request.
geolocationAjaxSettings.submit['proximity_lat'] = coordinates.latitude;
geolocationAjaxSettings.submit['proximity_lng'] = coordinates.longitude;
// Use AJAX to refresh the view.
console.log('about to refresh view');
$('.js-view-dom-id-' + dom_id, context).trigger('RefreshView');
}
};
})(jQuery, Drupal, drupalSettings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment