diff --git a/js/geolocation-common-map.js b/js/geolocation-common-map.js index 475e9fd..e021355 100644 --- a/js/geolocation-common-map.js +++ b/js/geolocation-common-map.js @@ -2,97 +2,80 @@ * @file * Handle the common map. */ - (function ($) { - "use strict"; - - Drupal.behaviors.geolocationCommonMap = { + 'use strict'; + var google; + Drupal.behaviors.geolocationCommonMap = { attach: function (context, settings) { - if (typeof google !== 'object' || typeof google.maps !== 'object') { - // If google maps api is not present, we can already stop. + if (typeof google !== 'object' || typeof google.maps !== 'object') { + // If google maps api is not present, we can already stop. + return; + } + // Their could be several maps/views present. Go over each entry. + $.each(drupalSettings.geolocation.commonMap, function (index, settings) { + var bubble; // Keep track if a bubble is currently open. + var center; // Map center point. + var zoom = 12; // Map zoom level (1-20). + var fitBounds = false; // Whether to execute fitBounds(). + // The DOM-node the map and everything else resides in. + var map = $('#' + index, context); + // If the map is not present, we can go to the next entry. + if (!map.length) { return; } - - // Their could be several maps/views present. Go over each entry. - $.each(drupalSettings.geolocation.commonMap, function(index, settings) { - - var bubble; // Keep track if a bubble is currently open. - var center; // Map center point. - var zoom = 12; // Map zoom level (1-20). - var fitBounds = false; // Whether to execute fitBounds(). - - // The DOM-node the map and everything else resides in. - var map = $('#' + index, context); - - // If the map is not present, we can go to the next entry. - if (!map.length) { - return; + // A google maps API tool to re-center the map on its content. + var bounds = new google.maps.LatLngBounds(); + // Hide the graceful-fallback HTML list; map will propably work now. + map.children('.geolocation-common-map-locations').hide(); + // Map-container is hidden by default in case of graceful-fallback. + var container = map.children('.geolocation-common-map-container'); + container.show(); + if (map.data('centre-lat') && map.data('centre-lng')) { + center = new google.maps.LatLng(map.data('centre-lat'), map.data('centre-lng')); } - - // A google maps API tool to re-center the map on its content. - var bounds = new google.maps.LatLngBounds(); - - // Hide the graceful-fallback HTML list; map will propably work now. - map.children('.geolocation-common-map-locations').hide(); - // Map-container is hidden by default in case of graceful-fallback. - var container = map.children('.geolocation-common-map-container'); - container.show(); - - if (map.data('centre-lat') && map.data('centre-lng')) { - center = new google.maps.LatLng(map.data('centre-lat'), map.data('centre-lng')); - } - else { - center = new google.maps.LatLng(0, 0); - } - - if (map.data('zoom')) { - // Drupal value is offset 0 instead of 1. - zoom = 1 + map.data('zoom'); - } - - if (map.data('fitbounds')) { - fitBounds = map.data('fitbounds'); - } - - // Load the actual map. - // If enabled, the fitBounds() will later override these values. - var googleMap = new google.maps.Map(container[0], { + else { + center = new google.maps.LatLng(0, 0); + } + if (map.data('zoom')) { + // Drupal value is offset 0 instead of 1. + zoom = 1 + map.data('zoom'); + } + if (map.data('fitbounds')) { + fitBounds = map.data('fitbounds'); + } + // Load the actual map. + // If enabled, the fitBounds() will later override these values. + var googleMap = new google.maps.Map(container[0], { center: center, zoom: zoom }); - - // Add the locations to the map. - map.find('.geolocation-common-map-locations .geolocation').each(function (index, item) { - item = $(item); - var position = new google.maps.LatLng(item.data('lat'), item.data('lng')); - + // Add the locations to the map. + map.find('.geolocation-common-map-locations .geolocation').each(function (index, item) { + item = $(item); + var position = new google.maps.LatLng(item.data('lat'), item.data('lng')); if (fitBounds) { bounds.extend(position); } - var marker = new google.maps.Marker({ position: position, map: googleMap, title: item.children('h2').text(), content: item.html() }); - - marker.addListener('click', function () { - if (bubble) bubble.close(); - bubble = new google.maps.InfoWindow({ + marker.addListener('click', function () { + if (bubble) bubble.close(); + bubble = new google.maps.InfoWindow({ content: marker.content, maxWidth: 200 }); - bubble.open(googleMap, marker); - }); - }); - - if (fitBounds) { - // Fit map center and zoom to all currently loaded markers. - googleMap.fitBounds(bounds); - } + bubble.open(googleMap, marker); + }); }); + if (fitBounds) { + // Fit map center and zoom to all currently loaded markers. + googleMap.fitBounds(bounds); + } + }); } }; - })(jQuery); diff --git a/js/geolocation-formatter-googlemap.js b/js/geolocation-formatter-googlemap.js index feea4ac..80ce4cb 100644 --- a/js/geolocation-formatter-googlemap.js +++ b/js/geolocation-formatter-googlemap.js @@ -3,9 +3,10 @@ * Javascript for the geocoder Google map formatter. */ (function ($, Drupal) { - // Ensure and use shortcut for geolocation. + 'use strict'; + var google; + // Ensure and use shortcut for gwolocation. var geolocation = Drupal.geolocation = Drupal.geolocation || {}; - Drupal.behaviors.geolocationGoogleMaps = { attach: function (context, settings) { // Ensure itterables. @@ -13,20 +14,17 @@ // Make sure the lazy loader is available. if (typeof Drupal.geolocation.load_google === 'function') { // First load the library from google. - Drupal.geolocation.load_google(function(){ - + Drupal.geolocation.load_google(function () { initialize(settings.geolocation.maps); }); } } }; - function initialize(maps) { // Loop though all objects and add maps to the page. - $.each(maps, function(delta, map) { + $.each(maps, function (delta, map) { // Get the container object. map.container = document.getElementById(map.id); - if ($(map.container).length >= 1 && !$(map.container).hasClass('geolocation-processed') && typeof google !== 'undefined' diff --git a/js/geolocation-widget-googlegeocoder.js b/js/geolocation-widget-googlegeocoder.js index bff4879..0f639ab 100644 --- a/js/geolocation-widget-googlegeocoder.js +++ b/js/geolocation-widget-googlegeocoder.js @@ -2,13 +2,11 @@ * @file * Javascript for the Google geocoder widget. */ - (function ($, Drupal, drupalSettings) { - "use strict"; - + 'use strict'; + var google; // Ensure and add shortcut to the geolocation object. var geolocation = Drupal.geolocation = Drupal.geolocation || {}; - Drupal.behaviors.geolocationGooglemaps = { attach: function (context, settings) { // Ensure itterables. @@ -16,35 +14,33 @@ // Make sure the lazy loader is available. if (typeof geolocation.load_google === 'function') { // First load the library from google. - geolocation.load_google(function(){ + geolocation.load_google(function () { // This won't fire until window load. initialize(settings.geolocation.widget_maps); }); } } }; - /** * Adds the click listeners to the map. * @param map */ - geolocation.add_click_listener = function(map) { + geolocation.add_click_listener = function (map) { // Used for a single click timeout. var singleClick; // Add the click listener. - google.maps.event.addListener(map.google_map, 'click', function(e) { + google.maps.event.addListener(map.google_map, 'click', function (e) { // Create 500ms timeout to wait for double click. - singleClick = setTimeout(function() { + singleClick = setTimeout(function () { geolocation.codeLatLng(e.latLng, map, 'marker'); geolocation.setMapMarker(e.latLng, map); }, 500); }); // Add a doubleclick listener. - google.maps.event.addListener(map.google_map, 'dblclick', function(e) { + google.maps.event.addListener(map.google_map, 'dblclick', function (e) { clearTimeout(singleClick); }); }; - /** * Runs after the google maps api is available * @@ -52,11 +48,9 @@ */ function initialize(maps) { // Process drupalSettings for every Google map present on the current page. - $.each(maps, function(widget_id, map) { - + $.each(maps, function (widget_id, map) { // Get the container object. map.container = document.getElementById(map.id); - if ($(map.container).length >= 1 && !$(map.container).hasClass('geolocation-processed') && typeof google !== 'undefined' @@ -70,20 +64,15 @@ map.lat = $('.geolocation-hidden-lat.for-' + map.id).attr('value'); map.lng = $('.geolocation-hidden-lng.for-' + map.id).attr('value'); } - // Add the map by ID with settings. geolocation.add_map(map); - // Add the geocoder to the map. geolocation.add_geocoder(map); - // Add the click responders for setting the value. geolocation.add_click_listener(map); - // Set the already processed flag. $(map.container).addClass('geolocation-processed'); } }); } - })(jQuery, Drupal, drupalSettings); diff --git a/js/geolocation-widget-html5.js b/js/geolocation-widget-html5.js index be0d722..2004f83 100644 --- a/js/geolocation-widget-html5.js +++ b/js/geolocation-widget-html5.js @@ -2,109 +2,80 @@ * @file * Javascript for the Geolocation HTML5 widget. */ - (function ($, Drupal, navigator) { - "use strict"; - - + 'use strict'; + var geolocation; Drupal.behaviors.geolocationHTML5 = { - attach: function(context, settings) { - $('.geolocation-html5-button:not(.disabled)').each( function(index) { + attach: function (context, settings) { + $('.geolocation-html5-button:not(.disabled)').each (function (index) { // The parent element. var $thisButton = $(this); - // Set the values of hidden form inputs. var latDefault = $thisButton.siblings(".geolocation-hidden-lat").val(); var lngDefault = $thisButton.siblings(".geolocation-hidden-lng").val(); - if (latDefault.length > 1 && lngDefault.length > 1) { // Hide the default text. $('.default', $thisButton).hide(); - // Display a location. var locationString = Drupal.t('Browser location: @lat,@lng', {'@lat': latDefault, '@lng': lngDefault}); $('.location', $thisButton).html(locationString); - // Disable the button. $thisButton.addClass('disabled'); - // Show the clear icon. $('.clear', $thisButton).show(); } - }); - } }; - - $('.geolocation-html5-button .clear').on('click', function(event) { + $('.geolocation-html5-button .clear').on('click', function (event) { // The parent element. var $thisButton = $(this).parent(); - // Prevent form submission. - event.stopPropagation() - + event.stopPropagation(); // Clear the values of hidden form inputs. $thisButton.siblings(".geolocation-hidden-lat").val(''); $thisButton.siblings(".geolocation-hidden-lng").val(''); - // Show the default text. $('.default', $thisButton).show(); - // Clear the location message. $('.location', $thisButton).html(''); - // Hide the clear icon. $('.clear', $thisButton).hide(); - // Enable the button. $thisButton.removeClass('disabled'); - }); - - $('.geolocation-html5-button').on('click', function(event) { + $('.geolocation-html5-button').on('click', function (event) { // The parent element. var $thisButton = $(this); - if ($thisButton.hasClass('disabled')) { return; } - // If the browser supports W3C Geolocation API. if (navigator.geolocation) { - // Get the geolocation from the browser. navigator.geolocation.getCurrentPosition( - // Success handler for getCurrentPosition() function (position) { var lat = position.coords.latitude; var lng = position.coords.longitude; var accuracy = position.coords.accuracy / 1000; - // Set the values of hidden form inputs. $thisButton.siblings(".geolocation-hidden-lat").val(lat); $thisButton.siblings(".geolocation-hidden-lng").val(lng); - // Hide the default text. $('.default', $thisButton).hide(); - // Display a success message. var locationString = Drupal.t('Browser location: @lat,@lng Accuracy: @accuracy m', {'@lat': lat, '@lng': lng, '@accuracy': accuracy}); $('.location', $thisButton).html(locationString); - // Disable the button. $thisButton.addClass('disabled'); - // Show the clear icon. $('.clear', $thisButton).show(); }, - // Error handler for getCurrentPosition() - function(error) { - + function (error) { // Alert with error message. - switch(error.code) { + switch (error.code) { case error.PERMISSION_DENIED: alert(Drupal.t('No location data found. Reason: PERMISSION_DENIED.')); break; @@ -119,7 +90,6 @@ break; } }, - // Options for getCurrentPosition() { enableHighAccuracy: true, @@ -127,31 +97,25 @@ maximumAge: 6000 } ); - - } else { + } + else { alert(Drupal.t('No location data found. Your browser does not support the W3C Geolocation API.')); } }); - /** * Runs after the google maps api is available * * @param settings */ function initialize(settings) { - // Process drupalSettings for every Google map present on the current page. - $.each(settings.geolocation.widget_maps, function(widget_id, map) { - + $.each(settings.geolocation.widget_maps, function (widget_id, map) { // Add any missing settings. map.settings = $.extend(geolocation.default_settings(), map.settings); - // Add the map by ID with settings. geolocation.add_map(map); - // Add the click responders ffor setting the value. geolocation.add_click_listener(map); }); } - })(jQuery, Drupal, navigator); diff --git a/js/geolocation.js b/js/geolocation.js index 2fe0ee4..67fd2b4 100644 --- a/js/geolocation.js +++ b/js/geolocation.js @@ -3,15 +3,16 @@ * Javascript for the geocoder module. */ (function ($, _, Drupal, settings) { + 'use strict'; + var google; // Ensure the geolocation object without overwriting it. var geolocation = Drupal.geolocation = Drupal.geolocation || {}; - /** * Gets the default settings for the google map. * * @returns {{scrollwheel: boolean, panControl: boolean, mapTypeControl: boolean, scaleControl: boolean, streetViewControl: boolean, overviewMapControl: boolean, zoomControl: boolean, zoomControlOptions: {style: *, position: *}, mapTypeId: *, zoom: number}} */ - geolocation.default_settings = function() { + geolocation.default_settings = function () { return { scrollwheel: false, panControl: false, @@ -28,96 +29,88 @@ zoom: 2 }; }; - /** * Provides the callback that is called when maps loads. */ - geolocation.google_callback = function() { + geolocation.google_callback = function () { // Ensure callbacks array; geolocation.google_load_callbacks = geolocation.google_load_callbacks || []; // Wait until the window load event to try to use the maps library. - $(document).ready(function(e) { + $(document).ready(function (e) { _.invoke(settings.geolocation.google_load_callbacks, "callback"); geolocation.google_load_callbacks = []; }); }; - /** * Adds a callback that will be called once the maps library is loaded. * * @param callback */ - geolocation.add_callback = function(callback) { + geolocation.add_callback = function (callback) { settings.geolocation.google_load_callbacks = geolocation.google_load_callbacks || []; settings.geolocation.google_load_callbacks.push({callback: callback}); }; - /** * Load google maps and set a callback to run when it's ready. * * @param callback */ - geolocation.load_google = function(callback) { + geolocation.load_google = function (callback) { // Add the callback. geolocation.add_callback(callback); // Check for google maps. if (typeof google == 'undefined' || typeof google.maps == 'undefined') { // google maps isn't loaded so lazy load google maps. $.getScript("//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=Drupal.geolocation.google_callback"); - } else { + } + else { // Google maps loaded. Run callback. geolocation.google_callback(); } }; - /** * Load google maps and set a callback to run when it's ready. * * @param map */ - geolocation.add_map = function(map) { + geolocation.add_map = function (map) { // Set the container size. $(map.container).css({ height: map.settings.height, width: map.settings.width }); - // Get the center point. var center = new google.maps.LatLng(map.lat, map.lng); - // Create the map object and assign it to the map. map.google_map = new google.maps.Map(map.container, { zoom: parseInt(map.settings.zoom), center: center, mapTypeId: google.maps.MapTypeId[map.settings.type] }); - // Set the map marker. - if (map.lat != '' && map.lng != '') { + if (map.lat !== '' && map.lng !== '') { geolocation.setMapMarker(center, map); } if (!geolocation.hasOwnProperty('maps')) { geolocation.maps = []; } - geolocation.maps.push(map); }; - /** * Load google maps and set a callback to run when it's ready. * * @param map */ - geolocation.add_geocoder = function(map) { + geolocation.add_geocoder = function (map) { /** * Callback for geocoder controls click submit. * * @param e * The event from input keypress or the click of the submit button. */ - var handleControlEvent = function(e) { + var handleControlEvent = function (e) { if (typeof e.keyCode === 'undefined' || e.keyCode === 13 || e.keyCode === 0) { // We don't any forms submitting. e.preventDefault(); @@ -127,7 +120,7 @@ if (address.length > 1) { // Run the geocode function with google maps. map.geocoder.geocode({'address': address}, function (results, status) { - if (status == google.maps.GeocoderStatus.OK) { + if (status === google.maps.GeocoderStatus.OK) { // Set the map viewport. map.google_map.fitBounds(results[0].geometry.viewport); // Set the values for the field. @@ -143,7 +136,6 @@ } } }; - map.geocoder = new google.maps.Geocoder(); map.controls = $('
') .append($('')) @@ -157,22 +149,19 @@ .get(0); // Add the default indicator if the values aren't blank. - if (map.lat != '' && map.lng != '') { + if (map.lat !== '' && map.lng !== '') { $(map.controls).children('.geolocation-map-indicator') .addClass('has-location') .text(map.lat+', '+map.lng); } - map.controls.index = 1; - map.google_map.controls[google.maps.ControlPosition.TOP_LEFT].push(map.controls); - // Add the listened for the search click event. google.maps.event.addDomListener($(map.controls).children('button.submit')[0], 'click', handleControlEvent); // Add the listened for the search click event. google.maps.event.addDomListener($(map.controls).children('input.input')[0], 'keyup', handleControlEvent); // Add the event listener for the remove button. - google.maps.event.addDomListener($(map.controls).children('button.clear')[0], 'click', function(e) { + google.maps.event.addDomListener($(map.controls).children('button.clear')[0], 'click', function (e) { // Stop all that bubbling and form submitting. e.preventDefault(); // Remove the coordinates. @@ -187,7 +176,6 @@ $('.geolocation-hidden-lng.for-'+map.id).attr('value', ''); }); }; - /** * Set the latitude and longitude values to the input fields * And optionally update the address field @@ -199,12 +187,11 @@ * @param op * the op that was performed */ - geolocation.codeLatLng = function(latLng, map) { + geolocation.codeLatLng = function (latLng, map) { // Update the lat and lng input fields $('.geolocation-hidden-lat.for-'+map.id).attr('value', latLng.lat()); $('.geolocation-hidden-lng.for-'+map.id).attr('value', latLng.lng()); }; - /** * Set/Update a marker on a map * @@ -213,18 +200,17 @@ * @param map * The settings object that contains all of the necessary metadata for this map. */ - geolocation.setMapMarker = function(latLng, map) { + geolocation.setMapMarker = function (latLng, map) { // make sure the marker exists. if (typeof map.marker !== 'undefined') { map.marker.setPosition(latLng); map.marker.setMap(map.google_map); - } else { - + } + else { // Set the info popup text. map.infowindow = new google.maps.InfoWindow({ content: map.settings.info_text }); - // Add the marker to the map. map.marker = new google.maps.Marker({ position: latLng, @@ -232,10 +218,9 @@ title: map.settings.title, label: map.settings.label }); - // Add the info window event if the info text has been set. if (map.settings.info_text && map.settings.info_text.length > 0) { - map.marker.addListener('click', function() { + map.marker.addListener('click', function () { map.infowindow.open(map.google_map, map.marker); }); if (map.settings.info_auto_display) { @@ -243,11 +228,9 @@ } } } - // Add a visual indicator. $(map.controls).children('.geolocation-map-indicator') .text(latLng.lat()+', '+latLng.lng()) .addClass('has-location'); }; - })(jQuery, _, Drupal, drupalSettings);