/**
* @file
* Common marker routines.
*/
/*global $, Drupal, GEvent, GInfoWindowTab, GLatLng, GLatLngBounds */
Drupal.gmap.addHandler('gmap', function (elem) {
var obj = this;
obj.bind('init', function () {
if (obj.vars.behavior.autozoom) {
obj.bounds = new GLatLngBounds();
}
});
obj.bind('addmarker', function (marker) {
var m = null;
//create an array to store google markers for a given latitute/logitude and reuse
//for all objects in the given latitute/logitude without creating more
if (typeof Drupal.settings.gmap['markerlist'] == 'undefined') {
Drupal.settings.gmap['markerlist'] = new Array();
}
//create the latitude / logitude and store the marker
if (typeof Drupal.settings.gmap['markerlist'][marker.latitude] == 'undefined') {
Drupal.settings.gmap['markerlist'][marker.latitude] = new Array();
}
if (typeof Drupal.settings.gmap['markerlist'][marker.latitude][marker.longitude] == 'undefined') {
Drupal.settings.gmap['markerlist'][marker.latitude][marker.longitude] = new Array();
//no marker available for lat/long. create one
m = Drupal.gmap.factory.marker(new GLatLng(marker.latitude, marker.longitude), marker.opts);
}
else {
//marker available for lat/long. reuse and reduce the number of objects created.
m = Drupal.settings.gmap['markerlist'][marker.latitude][marker.longitude][0].marker;
}
marker.marker = m;
Drupal.settings.gmap['markerlist'][marker.latitude][marker.longitude].push(marker);
GEvent.addListener(m, 'click', function () {
obj.change('clicknodemarker', -1, marker);
});
if (obj.vars.behavior.highlight) {
GEvent.addListener(m, 'mouseover', function () {
var highlightColor = '#' + obj.vars.styles.highlight_color;
highlightMarker(obj.map, marker, 'hoverHighlight', highlightColor);
});
GEvent.addListener(m, 'mouseout', function () {
unHighlightMarker(obj.map, marker, 'hoverHighlight');
});
}
if (obj.vars.behavior.extramarkerevents) {
GEvent.addListener(m, 'mouseover', function () {
obj.change('mouseovermarker', -1, marker);
});
GEvent.addListener(m, 'mouseout', function () {
obj.change('mouseoutmarker', -1, marker);
});
GEvent.addListener(m, 'dblclick', function () {
obj.change('dblclickmarker', -1, marker);
});
}
/**
* Perform a synthetic marker click on this marker on load.
*/
if (marker.autoclick || (marker.options && marker.options.autoclick)) {
obj.deferChange('clickmarker', -1, marker);
}
if (obj.vars.behavior.autozoom) {
obj.bounds.extend(marker.marker.getPoint());
}
// If the highlight arg option is used in views highlight the marker.
if (marker.opts.highlight == 1) {
highlightMarker(obj.map, marker, 'viewHighlight', marker.opts.highlightcolor);
}
});
// Default marker actions.
//clicknodemaker provides the capability to merge contents of multiple markers at a given
//latitude/longitude
obj.bind('clicknodemarker', function (marker) {
//if there are more than one items at a lat/long, club them
if (Drupal.settings.gmap['markerlist'][marker.latitude][marker.longitude].length > 1) {
showMultipleNodes(marker);
}
else {
//delegate to single node display
obj.change('clickmarker', -1, marker);
}
});
obj.bind('clickmarker', function (marker) {
// Local/stored content
if (marker.text) {
marker.marker.openInfoWindowHtml(marker.text);
}
// Info Window Query / Info Window Offset
if (marker.iwq || (obj.vars.iwq && typeof marker.iwo != 'undefined')) {
var iwq, iwo;
if (obj.vars.iwq) {
iwq = obj.vars.iwq;
}
if (marker.iwq) {
iwq = marker.iwq;
}
iwo = 0;
if (marker.iwo) {
iwo = marker.iwo;
}
// Create a container to store the cloned DOM elements.
var el = document.createElement('div');
// Clone the matched object, run through the clone, stripping off ids, and move the clone into the container.
jQuery(iwq).eq(iwo).clone(false).find('*').removeAttr('id').appendTo(jQuery(el));
marker.marker.openInfoWindow(el);
}
// AJAX content
if (marker.rmt) {
obj.rmtcache = obj.rmtcache || {};
// Cached RMT.
if (obj.rmtcache[marker.rmt]) {
marker.marker.openInfoWindowHtml(obj.rmtcache[marker.rmt]);
}
else {
var uri = marker.rmt;
// If there was a callback, prefix that.
// (If there wasn't, marker.rmt was the FULL path.)
if (obj.vars.rmtcallback) {
uri = obj.vars.rmtcallback + '/' + marker.rmt;
}
// @Bevan: I think it makes more sense to do it in this order.
// @Bevan: I don't like your choice of variable btw, seems to me like
// @Bevan: it belongs in the map object, or at *least* somewhere in
// @Bevan: the gmap settings proper...
//if (!marker.text && Drupal.settings.loadingImage) {
// marker.marker.openInfoWindowHtml(Drupal.settings.loadingImage);
//}
$.get(uri, {}, function (data) {
obj.rmtcache[marker.rmt] = data;
marker.marker.openInfoWindowHtml(data);
});
}
}
// Tabbed content
else if (marker.tabs) {
var infoWinTabs = [];
for (var m in marker.tabs) {
if (marker.tabs.hasOwnProperty(m)) {
infoWinTabs.push(new GInfoWindowTab(m, marker.tabs[m]));
}
}
marker.marker.openInfoWindowTabsHtml(infoWinTabs);
}
// No content -- marker is a link
else if (marker.link) {
open(marker.link, '_self');
}
});
obj.bind('markersready', function () {
// If we are autozooming, set the map center at this time.
if (obj.vars.behavior.autozoom) {
if (!obj.bounds.isEmpty()) {
obj.map.setCenter(obj.bounds.getCenter(), Math.min(obj.map.getBoundsZoomLevel(obj.bounds), obj.vars.maxzoom));
}
}
});
obj.bind('clearmarkers', function () {
// Reset bounds if autozooming
// @@@ Perhaps we should have a bounds for both markers and shapes?
if (obj.vars.behavior.autozoom) {
obj.bounds = new GLatLngBounds();
}
});
// @@@ TODO: Some sort of bounds handling for deletemarker? We'd have to walk the whole thing to figure out the new bounds...
});
var nodeclicked = function(latitude, longitude, cnt) {
//if cnt is -1 then show the list of items at a given lat/long
if (cnt == -1) {
var marker = Drupal.settings.gmap['markerlist'][latitude][longitude][0];
marker.marker.closeInfoWindow();
showMultipleNodes(marker);
return false;
}
else {
//show the details for the selected item
var marker = Drupal.settings.gmap['markerlist'][latitude][longitude][cnt];
marker.marker.closeInfoWindow();
if (marker.text) {
var txt = '';
//get the text that needs to be displayed to return back to multiple items list. This facilitates
//translation. The module / any module can set the javascript setting for multiple_items_return_text
//parameter. If none is present, a default text is defined below.
if (typeof Drupal.settings.gmap['multiple_items_return_text'] != 'undefined') {
txt = Drupal.settings.gmap['multiple_items_return_text']
}
else {
txt = 'Click here to return to item title list.';
}
//add "Back" link to list of items
txt = marker.text + '
' + txt + ''
marker.marker.openInfoWindowHtml(txt);
return false;
}
}
//no text was found, propagate the click event further up
return true;
};
var showMultipleNodes = function(marker){
var location = window.location.pathname;
//path must be node/ or map/node/
var path = 'node/';
if (location.indexOf('node') == -1 && location.indexOf('user') == -1) {
//node or user is not in the path, create url to navigate to map/node/nid.
path = 'map/node/';
}
var txt = '';
//get the text that needs to be displayed on multiple items. This facilitates
//translation. The module / any module can set the javascript setting for multiple_items_text
//parameter. If none is present, a default text is defined below.
if (typeof Drupal.settings.gmap['multiple_items_text'] != 'undefined') {
txt = '