diff --git a/gmap.module b/gmap.module index 61dbe69d593afdac1dfd4178615f56b932fd2743..169d03fedf3bb4587b8fd0ca71b1a4681f847d2c 100644 --- a/gmap.module +++ b/gmap.module @@ -1263,3 +1263,36 @@ function gmap_views_plugins() { ), ); } + +/** + * Implementation of hook_views_pre_render(). + */ +function gmap_views_pre_render(&$view) { + static $gmap_processed; + // Add js only for gmap style views with ajax and not already processed. + if (($view->plugin_name != 'gmap' && !$view->use_ajax) || $gmap_processed) { + return; + } + // Mark the view as already processed. + $gmap_processed = TRUE; + // Add js with new views callback. + drupal_add_js(drupal_get_path('module', 'gmap') . '/js/gmap_views_ajax.js', 'module'); +} + +/** + * Implementation of hook_ajax_data_alter(). + */ +function gmap_ajax_data_alter(&$object, $views, $view) { + // Add js callback only with gmap style plugins and with ajax. + if ($view->plugin_name != 'gmap' || !$view->use_ajax) { + return; + } + + // Save settings. + $js = drupal_add_js(NULL, NULL, 'header'); + $object->settings = $js['setting']; + + // Register custom callback. + $object->__callbacks = array_merge($object->__callbacks, array('Drupal.gmapAjax.ajaxFixMaps')); +} + diff --git a/js/gmap_views_ajax.js b/js/gmap_views_ajax.js new file mode 100644 index 0000000000000000000000000000000000000000..1353dc051160a3ff1b04d9c1d4049983a0baf378 --- /dev/null +++ b/js/gmap_views_ajax.js @@ -0,0 +1,45 @@ +// $Id$ + +/** + * @file + * When using views with ajax enabled, the use of ajaxified + * exposed filters breaks the gmap javascript. + * This file is part of the solution to this problem. + */ + +$(document).ready(function() { + Drupal.gmapAjax = Drupal.gmapAjax || {}; + + /** + * An ajax responder that accepts a packet of JSON data and acts appropriately. + * + * The following fields control behavior. + * - 'display': Display the associated data in the view area. + */ + Drupal.gmapAjax.ajaxFixMaps = function(target, response) { + var $view = $(target); + + if (response.settings) { + var i = 0; + var gmap = {}; + + for (i = 0; i < response.settings.length; i++) { + if (typeof(response.settings[i]['gmap']) == 'object') { + gmap = response.settings[i]['gmap']; + } + } + + $view.find('.gmap-map').each(function() { + var id = '#' + $(this).attr("id"); + var t = id.split('-'); + var mapid = t[1]; + Drupal.gmap.unloadMap(mapid); + if (gmap && gmap[mapid]) { + Drupal.settings.gmap[mapid] = gmap[mapid]; + } + $(id).empty().each(Drupal.gmap.setup); + }); + } + }; +}); +