/** * Adds a full-screen mode and switcher to a GMap */ // Add stuff to the map. // This is based on other drupal gmap.module javascripts. Drupal.gmap.addHandler('gmap', function(elem) { var obj = this; obj.bind('init', function() { obj.map.addControl(new FullScreenControl()); }); }); /** * FullScreenControl is a GControl that provides a button to change between full-screen and regular modes. * Based on http://code.google.com/apis/maps/documentation/controls.html#Custom_Controls and * http://code.google.com/apis/maps/documentation/examples/control-custom.html */ function FullScreenControl() {} // To "subclass" the GControl, we set the prototype object to an instance of the GControl object. FullScreenControl.prototype = new GControl(); // Creates a DOM element for the control. We add the control to the map container and return the element for the GMap2 API to position properly. FullScreenControl.prototype.initialize = function(map) { // Get the map container with jQuery. var mapContainer = $('#' + map.getContainer().id); // Create the button. mapContainer.append('
'); // What mode do we want to be in? (swapped because FullScreenSwitch swaps at start). Drupal.settings.fullScreenModeDesired = (document.location.hash != '#full-screen'); // Initialize map and button. fullScreenSwitch(map); // Get DOM element for the button container. var buttonContainer = document.getElementById('full-screen-container'); // Listen for clicks to the button. GEvent.addDomListener(buttonContainer, 'click', function() { fullScreenSwitch(map); }); // Return the DOM element we appended, so that GMap2 API can position it. return buttonContainer; } // Sets the position of the button. FullScreenControl.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(220, 7)); } /** * Does the heavy lifting of initializing the button and changing in and out of full-screen mode. */ function fullScreenSwitch(map) { // What mode do we want to be in? Drupal.settings.fullScreenModeDesired = !Drupal.settings.fullScreenModeDesired; // Get required jQuery DOM elements. var button = $('#full-screen-button'); var mapContainer = $('#' + map.getContainer().id); // Store the map's center before we change anything anything. var center = map.getCenter(); // To change the destination (href="" attribute) of the full-screen mode 'link' (button), we need to invoke a separate event, else we end up changing the destination of the click event. We use a basic Timeout function to invoke a separate event. This is the time to wait: var clickTimeout = 100; // Are we going into full-screen mode or regular mode? if (Drupal.settings.fullScreenModeDesired) { // Fullscreen mode. // Store the current children of the body. var children = $('body').children(); // Add a new child to the body. It will be a wrapper. $('body').append(''); // Put the other children inside the wrapper. $('#full-screen-wrapper').append(children); // Move the map to be a child of the body (and sibling of the wrapper), and leave a placeholder in the map's original place. mapContainer.after('').appendTo('body'); // Set styles for full-screen mode. mapContainer.addClass('full-screen-map'); // IE6 sucks. Fix it. $('body').height('100%'); // Make sure we're not so far zoomed out that the map is duplicated around the world (for typical screen resolutions). if (map.getZoom() < 2) { map.setZoom(2) } // Enable ScrollWheelZooming, since the scroll wheel doesn't serve any other purpose in this context. map.enableScrollWheelZoom(); // Set the button text and style. button.empty(); button.append(Drupal.settings.t.fullScreenSwitcher.textOn); // Set the destination of the button link in a separate event so that we don't break this event. setTimeout(function() { button.attr({ 'href': '#', 'title': Drupal.settings.t.fullScreenSwitcher.titleOn, 'class': 'on' }); }, clickTimeout); } else { // Not fullscreen mode. // Move the map back to it's original place and remove the placeholder. $('#full-screen-placeholder').after(mapContainer).remove(); // Move the body's original children back to the body and remove the wrapper. $('#full-screen-wrapper').remove().children().appendTo('body'); // Restore the map's styles. mapContainer.removeClass('full-screen-map'); // Zoom out a bit so that the user doesn't lose the context of the location they're viewing. if (map.getZoom() > 1) { map.zoomOut(); } // Turn off ScrollWheelZoom so the user can scroll normally with the scroll wheel. map.disableScrollWheelZoom(); // Set the button text and style. button.empty(); button.append(Drupal.settings.t.fullScreenSwitcher.textOff); // Set the destination of the button link in a separate event. setTimeout(function() { button.attr({ 'href': '#full-screen', 'title': Drupal.settings.t.fullScreenSwitcher.titleOff, 'class': 'off' }); }, clickTimeout); } // GMap API needs to that know we resized. map.checkResize(); // Re-center the map. If there is an open info window... var infoWindow = map.getInfoWindow(); if (infoWindow.isHidden()) { // Restore the previously saved center. map.setCenter(center); } else { // Otherwise center the info window. map.setCenter(infoWindow.getPoint()); // The info window is always above the point, so let's slide down a little to center the window, instead of the point. Values are relative to half of map size. if (Drupal.settings.fullScreenModeDesired) { map.panDirection(0, 0.5); } else { map.panDirection(0, 1); } } // Save this as the starting position for this map (The 'X' button in the center of the pan buttons uses this). map.savePosition(); }