From b62c1c725dd3ecbfebb81cb675d80648d9f9eed6 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Tue, 22 Apr 2014 17:48:28 +1000 Subject: [PATCH] Load Google Maps script only when needed --- assets/js/sp-maps.js | 26 +++++++++++ assets/js/sportspress.js | 61 +++++++------------------- includes/class-sp-frontend-scripts.php | 7 ++- 3 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 assets/js/sp-maps.js diff --git a/assets/js/sp-maps.js b/assets/js/sp-maps.js new file mode 100644 index 00000000..ce9ee2e3 --- /dev/null +++ b/assets/js/sp-maps.js @@ -0,0 +1,26 @@ +(function($) { + function sp_maps() { + $maps = $('.sp-google-map'); + $maps.each(function() { + $self = $(this); + address = $self.attr('data-address'); + latitude = $self.attr('data-latitude'); + longitude = $self.attr('data-longitude'); + var ll = new google.maps.LatLng(latitude,longitude); + var mapOptions = { + scrollwheel: false, + zoom: 16, + center: ll + }; + var map = new google.maps.Map($self[0], mapOptions) + var marker = new google.maps.Marker({ + position: ll, + map: map, + animation: google.maps.Animation.DROP, + flat: true, + title: address + }); + }); + } + google.maps.event.addDomListener(window, "load", sp_maps); +})(jQuery); \ No newline at end of file diff --git a/assets/js/sportspress.js b/assets/js/sportspress.js index e81c1b28..b8304ea8 100644 --- a/assets/js/sportspress.js +++ b/assets/js/sportspress.js @@ -200,58 +200,31 @@ function viewport() { } function sp_unsplit_table(original) { - original.closest(".sp-responsive-table-wrapper").find(".sp-pinned-table").remove(); - original.unwrap(); - original.unwrap(); + original.closest(".sp-responsive-table-wrapper").find(".sp-pinned-table").remove(); + original.unwrap(); + original.unwrap(); } function sp_set_cell_heights(original, copy) { - var tr = original.find('tr'), - tr_copy = copy.find('tr'), - heights = []; + var tr = original.find('tr'), + tr_copy = copy.find('tr'), + heights = []; - tr.each(function (index) { - var self = $(this), - tx = self.find('th, td'); + tr.each(function (index) { + var self = $(this), + tx = self.find('th, td'); - tx.each(function () { - var height = $(this).outerHeight(true); - heights[index] = heights[index] || 0; - if (height > heights[index]) heights[index] = height; - }); + tx.each(function () { + var height = $(this).outerHeight(true); + heights[index] = heights[index] || 0; + if (height > heights[index]) heights[index] = height; + }); - }); + }); - tr_copy.each(function (index) { - $(this).height(heights[index]); - }); - } - - - /* Google Maps */ - function initialize_google_maps() { - $maps = $('.sp-google-map'); - $maps.each(function() { - $self = $(this); - address = $self.attr('data-address'); - latitude = $self.attr('data-latitude'); - longitude = $self.attr('data-longitude'); - var ll = new google.maps.LatLng(latitude,longitude); - var mapOptions = { - scrollwheel: false, - zoom: 16, - center: ll - }; - var map = new google.maps.Map($self[0], mapOptions) - var marker = new google.maps.Marker({ - position: ll, - map: map, - animation: google.maps.Animation.DROP, - flat: true, - title: address - }); + tr_copy.each(function (index) { + $(this).height(heights[index]); }); } - google.maps.event.addDomListener(window, "load", initialize_google_maps); })(jQuery); \ No newline at end of file diff --git a/includes/class-sp-frontend-scripts.php b/includes/class-sp-frontend-scripts.php index 748a4d8b..245a4c63 100644 --- a/includes/class-sp-frontend-scripts.php +++ b/includes/class-sp-frontend-scripts.php @@ -40,13 +40,18 @@ class SP_Frontend_Scripts { * @return void */ public function load_scripts() { + global $typenow; // Scripts wp_enqueue_script( 'jquery' ); - wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false', array(), '3.exp', true ); wp_enqueue_script( 'jquery-datatables', plugin_dir_url( SP_PLUGIN_FILE ) .'assets/js/jquery.dataTables.min.js', array( 'jquery' ), '1.9.4', true ); wp_enqueue_script( 'jquery-countdown', plugin_dir_url( SP_PLUGIN_FILE ) .'assets/js/jquery.countdown.min.js', array( 'jquery' ), '2.0.2', true ); wp_enqueue_script( 'sportspress', plugin_dir_url( SP_PLUGIN_FILE ) .'assets/js/sportspress.js', array( 'jquery' ), time(), true ); + if ( is_singular( 'sp_event' ) || is_tax( 'sp_venue' ) ): + wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false', array(), '3.exp', true ); + wp_enqueue_script( 'sp-maps', plugin_dir_url( SP_PLUGIN_FILE ) .'assets/js/sp-maps.js', array( 'jquery', 'google-maps' ), time(), true ); + endif; + // Localize scripts. wp_localize_script( 'sportspress', 'localized_strings', array( 'days' => __( 'days', 'sportspress' ), 'hrs' => __( 'hrs', 'sportspress' ), 'mins' => __( 'mins', 'sportspress' ), 'secs' => __( 'secs', 'sportspress' ), 'previous' => __( 'Previous', 'sportspress' ), 'next' => __( 'Next', 'sportspress' ) ) );