Skip to content

Commit

Permalink
👌 IMPROVE: Replace deprecated jQuery resize()
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengliebe committed Dec 7, 2020
1 parent fd7d0c9 commit 74c49fd
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions js/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@
* Maps JavaScript
*/

jQuery( document ).ready( function( $ ) {
jQuery(document).ready(function ($) {

// Loop map elements
$( '.ctfw-google-map' ).each( function() {
$('.ctfw-google-map').each(function () {

var id, lat, lng, type, zoom, latlng, map_type, map, marker, marker_image, marker_image_size;

// Get map data from element attributes
id = $( this ).attr( 'id' );
lat = $( this ).data( 'ctfw-map-lat' );
lng = $( this ).data( 'ctfw-map-lng' );
type = $( this ).data( 'ctfw-map-type' );
zoom = $( this ).data( 'ctfw-map-zoom' );
marker = $( this ).data( 'ctfw-map-marker' );
center_resize = $( this ).data( 'ctfw-map-center-resize' );
callback_loaded = $( this ).data( 'ctfw-map-callback-loaded' );
callback_resize = $( this ).data( 'ctfw-map-callback-resize' );
id = $(this).attr('id');
lat = $(this).data('ctfw-map-lat');
lng = $(this).data('ctfw-map-lng');
type = $(this).data('ctfw-map-type');
zoom = $(this).data('ctfw-map-zoom');
marker = $(this).data('ctfw-map-marker');
center_resize = $(this).data('ctfw-map-center-resize');
callback_loaded = $(this).data('ctfw-map-callback-loaded');
callback_resize = $(this).data('ctfw-map-callback-resize');

// Map being used? Have coordinates?
if ( $( '#' + id ).length && lat && lng ) {
if ($('#' + id).length && lat && lng) {

// Location Latitude / Longitude
latlng = new google.maps.LatLng( lat, lng );
latlng = new google.maps.LatLng(lat, lng);

// Map Type
map_type = google.maps.MapTypeId.HYBRID;
if ( type == 'ROADMAP' ) {
if (type == 'ROADMAP') {
map_type = google.maps.MapTypeId.ROADMAP;
} else if ( type == 'SATELLITE' ) {
} else if (type == 'SATELLITE') {
map_type = google.maps.MapTypeId.SATELLITE;
} else if ( type == 'TERRAIN' ) {
} else if (type == 'TERRAIN') {
map_type = google.maps.MapTypeId.TERRAIN;
}

// Zoom Default
if ( ! zoom ) {
if (!zoom) {
zoom = 14;
}

Expand All @@ -52,32 +52,32 @@ jQuery( document ).ready( function( $ ) {

// Custom Styles
// Apply globally if ctfw_map_styles is defined
if ( typeof ctfw_map_styles !== 'undefined' ) {
if (typeof ctfw_map_styles !== 'undefined') {
styles = ctfw_map_styles;
}

// Load Map
map = new google.maps.Map( document.getElementById( id ), {
zoom: parseInt( zoom ),
map = new google.maps.Map(document.getElementById(id), {
zoom: parseInt(zoom),
mapTypeId: map_type, // ROADMAP, SATELLITE, HYBRID or TERRAIN
disableDefaultUI: true, // remove map controls
scrollwheel: false,
draggable: false, // this can catch on mobile page touch-scrolling
disableDoubleClickZoom: true,
center: latlng,
styles: styles,
} );
});

// Custom Marker
if ( marker && typeof ctfw_map_marker_image !== 'undefined' && ctfw_map_marker_image.length ) {
if (marker && typeof ctfw_map_marker_image !== 'undefined' && ctfw_map_marker_image.length) {

// Global marker image
marker_icon = ctfw_map_marker_image;

// HiDPI/Retina?
if ( window.devicePixelRatio > 1.5 && typeof ctfw_map_marker_image_hidpi !== 'undefined' && typeof ctfw_map_marker_image_width !== 'undefined' && typeof ctfw_map_marker_image_height !== 'undefined' ) {
if (window.devicePixelRatio > 1.5 && typeof ctfw_map_marker_image_hidpi !== 'undefined' && typeof ctfw_map_marker_image_width !== 'undefined' && typeof ctfw_map_marker_image_height !== 'undefined') {

marker_image_size = new google.maps.Size( ctfw_map_marker_image_width, ctfw_map_marker_image_height );
marker_image_size = new google.maps.Size(ctfw_map_marker_image_width, ctfw_map_marker_image_height);

marker_icon = {
url: ctfw_map_marker_image_hidpi,
Expand All @@ -88,53 +88,53 @@ jQuery( document ).ready( function( $ ) {
}

// Add marker
marker = new google.maps.Marker( {
marker = new google.maps.Marker({
position: latlng,
map: map,
clickable: false,
icon: marker_icon
} );
});

}

// Store map object in data attribute so can manipulate the instance later
// Useful for adding custom styles, panning, etc.
// var map = $( 'element' ).data( 'ctfw-map' );
$( this ).data( 'ctfw-map', map );
$( this ).data( 'ctfw-map-latlng', latlng );
$(this).data('ctfw-map', map);
$(this).data('ctfw-map-latlng', latlng);

// After load callback
if ( callback_loaded ) {
if (callback_loaded) {
window[callback_loaded](); // run function
}

// On window resize
if ( center_resize || callback_resize ) {
if (center_resize || callback_resize) {

//google.maps.event.addDomListener( window, 'resize', function() {
$( window ).resize( function() {
$(window).on('resize', function () {

// Centered latitude/longitude on window resize
if ( center_resize ) {
if (center_resize) {

// Slight delay improve accuracy
setTimeout( function() {
map.setCenter( latlng );
}, 100 );
setTimeout(function () {
map.setCenter(latlng);
}, 100);

}

// On resize callback
if ( callback_resize ) {
if (callback_resize) {
window[callback_resize](); // run function
}

} );
});

}

}

} );
});

} );
});

0 comments on commit 74c49fd

Please sign in to comment.