Files
librenms-librenms/js/jquery.mapael.js

558 lines
17 KiB
JavaScript
Raw Normal View History

2013-06-23 18:00:13 +02:00
/**
*
* Jquery Mapael - Dynamic maps jQuery plugin (based on raphael.js)
* Requires jQuery and raphael.js
*
2013-07-15 23:28:53 +02:00
* Version: 0.3.0 (15-07-2013)
2013-06-23 18:00:13 +02:00
*
* Copyright (c) 2013 Vincent Brouté (http://www.neveldo.fr/mapael)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
*
*/
(function($) {
"use strict";
$.fn.mapael = function(options) {
options = $.extend(true, {}, $.fn.mapael.defaultOptions, options);
return this.each(function() {
var $tooltip = $("<div>").addClass(options.map.tooltip.cssClass).css("display", "none")
2013-07-15 22:53:59 +02:00
, $container = $('.' + options.map.cssClass, this).empty().append($tooltip)
2013-06-23 18:00:13 +02:00
, mapConf = $.fn.mapael.maps[options.map.name]
2013-07-15 22:53:59 +02:00
, paper = new Raphael($container[0], mapConf.width, mapConf.height)
, elemParams = {}
2013-06-30 22:55:03 +02:00
, coords = {}
, resizeTO = 0
2013-07-15 22:53:59 +02:00
, areas = {}
, plots = {}
, id = 0;
2013-06-23 18:00:13 +02:00
2013-06-30 22:55:03 +02:00
options.map.tooltip.css && $tooltip.css(options.map.tooltip.css);
paper.setViewBox(0, 0, mapConf.width, mapConf.height, false);
2013-06-23 18:00:13 +02:00
// Draw map areas
2013-07-15 22:53:59 +02:00
for (id in mapConf.elems) {
elemParams = $.fn.mapael.getElemParams(
options.map.defaultArea
2013-06-23 18:00:13 +02:00
, (options.areas[id] ? options.areas[id] : {})
2013-07-15 22:53:59 +02:00
, options.legend.area
2013-06-23 18:00:13 +02:00
);
2013-07-15 22:53:59 +02:00
areas[id] = {'mapElem' : paper.path(mapConf.elems[id]).attr(elemParams.attrs)};
$.fn.mapael.initElem(paper, areas[id], elemParams, $tooltip);
2013-06-23 18:00:13 +02:00
}
2013-06-30 22:55:03 +02:00
// Draw plots
2013-07-15 22:53:59 +02:00
for (id in options.plots) {
elemParams = $.fn.mapael.getElemParams(
options.map.defaultPlot
, (options.plots[id] ? options.plots[id] : {})
, options.legend.plot
2013-06-30 22:55:03 +02:00
);
2013-07-15 22:53:59 +02:00
if (elemParams.x && elemParams.y)
coords = {x : elemParams.x, y : elemParams.y};
else
coords = mapConf.getCoords(elemParams.latitude, elemParams.longitude);
2013-06-23 18:00:13 +02:00
2013-07-15 22:53:59 +02:00
if ("square" == elemParams.type) {
plots[id] = {'mapElem' : paper.rect(
coords.x - (elemParams.size / 2)
, coords.y - (elemParams.size / 2)
, elemParams.size
, elemParams.size
).attr(elemParams.attrs)};
} else { // Default = circle
plots[id] = {'mapElem' : paper.circle(coords.x, coords.y, elemParams.size / 2).attr(elemParams.attrs)};
2013-06-23 18:00:13 +02:00
}
2013-07-15 22:53:59 +02:00
$.fn.mapael.initElem(paper, plots[id], elemParams, $tooltip);
}
/**
*
* Update the current map
* Refresh attributes and tooltips for areas and plots
* @params options options to refresh
* @params reset true to reset previous areas and plots individual options
*/
$(this).bind('update', function(e, updateOptions, resetAreas, resetPlots, animDuration, easing) {
var elemParams = {}
, legend = {}
, id = 0
, bbox = {}
, textPosition = {}
, plotOffset = 0;
2013-06-23 18:00:13 +02:00
2013-07-15 22:53:59 +02:00
if (!animDuration) animDuration = 300;
if (!easing) easing = 'linear';
if (resetAreas) options.areas = {};
if (resetPlots) options.plots = {};
2013-06-23 18:00:13 +02:00
2013-07-15 22:53:59 +02:00
$.extend(true, options, updateOptions);
2013-07-15 22:53:59 +02:00
// Update areas attributes and tooltips
for (id in areas) {
elemParams = $.fn.mapael.getElemParams(
options.map.defaultArea
, (options.areas[id] ? options.areas[id] : {})
, options.legend.area
);
$.fn.mapael.paramHover(areas[id].mapElem, elemParams.attrs, areas[id].mapElem.attrsHover);
areas[id].mapElem.animate(elemParams.attrs, animDuration, easing);
if (elemParams.tooltip && elemParams.tooltip.content) {
areas[id].mapElem.tooltipContent = elemParams.tooltip.content;
if (areas[id].textElem) {
areas[id].textElem.tooltipContent = elemParams.tooltip.content;
}
}
2013-07-15 22:53:59 +02:00
}
// Update plots attributes and tooltips
for (id in plots) {
elemParams = $.fn.mapael.getElemParams(
options.map.defaultPlot
, (options.plots[id] ? options.plots[id] : {})
, options.legend.plot
);
2013-06-23 18:00:13 +02:00
2013-07-15 22:53:59 +02:00
// Update text position
if (plots[id].textElem) {
bbox = plots[id].mapElem.getBBox();
plotOffset = (elemParams.size - bbox.height) / 2;
bbox.x -= plotOffset;
bbox.x2 += plotOffset;
bbox.y -= plotOffset;
bbox.y2 += plotOffset;
textPosition = $.fn.mapael.getTextPosition(bbox, elemParams.textPosition);
plots[id].textElem.animate({x : textPosition.x, y : textPosition.y}, animDuration, easing);
}
// Update plot size
if ("square" == elemParams.type) {
elemParams.attrs.width = elemParams.size;
elemParams.attrs.height = elemParams.size;
} else { // Default : circle
elemParams.attrs.r = elemParams.size / 2;
}
$.fn.mapael.paramHover(plots[id].mapElem, elemParams.attrs, plots[id].mapElem.attrsHover);
plots[id].mapElem.animate(elemParams.attrs, animDuration, easing);
if (elemParams.tooltip && elemParams.tooltip.content) {
plots[id].mapElem.tooltipContent = elemParams.tooltip.content;
if (plots[id].textElem) {
plots[id].textElem.tooltipContent = elemParams.tooltip.content;
}
}
2013-06-23 18:00:13 +02:00
}
2013-07-15 22:53:59 +02:00
});
2013-06-23 18:00:13 +02:00
// Create the legends for areas and plots
2013-07-15 22:53:59 +02:00
if (options.legend.area.slices && options.legend.area.display)
$.fn.mapael.createLegend($(this), options, 'area');
2013-06-23 18:00:13 +02:00
2013-07-15 22:53:59 +02:00
if (options.legend.plot.slices && options.legend.plot.display)
$.fn.mapael.createLegend($(this), options, 'plot');
2013-06-30 22:55:03 +02:00
2013-07-15 22:53:59 +02:00
// Handle size of the map
if (options.map.width) {
paper.setSize(options.map.width, mapConf.height * (options.map.width / mapConf.width));
} else {
$(window).bind('resize', function(){
clearTimeout(resizeTO);
resizeTO = setTimeout(function(){$container.trigger('resizeEnd');}, 150);
});
$(document).bind('ready', function(){$container.trigger('resizeEnd');});
$container.bind('resizeEnd', function(e) {
var containerWidth = $container.width();
if (paper.width != containerWidth) {
paper.setSize(containerWidth, mapConf.height * (containerWidth / mapConf.width));
}
});
$container.trigger('resizeEnd');
}
2013-06-30 22:55:03 +02:00
$(paper.desc).append(" and Mapael (http://neveldo.fr/mapael)");
2013-06-23 18:00:13 +02:00
});
};
2013-07-15 22:53:59 +02:00
/**
* Get element parameters by merging default params, element params and legend params
*/
$.fn.mapael.getElemParams = function(defaultParams, elemParams, legendOptions) {
var elemParams = $.extend(true, {}, defaultParams, elemParams);
if (elemParams.value) {
$.extend(true, elemParams, $.fn.mapael.getLegendEl(elemParams.value, legendOptions));
}
return elemParams;
}
/**
* Init the element of the map (draw it, set attributes, events, tooltip, ...)
*/
$.fn.mapael.initElem = function(paper, elem, params, $tooltip) {
var bbox = {}, textPosition = {};
$.fn.mapael.paramHover(elem.mapElem, params.attrs, params.attrsHover);
// Set a text label in the area
if (params.text) {
bbox = elem.mapElem.getBBox();
textPosition = $.fn.mapael.getTextPosition(bbox, params.textPosition);
params.textAttrs['text-anchor'] = textPosition.textAnchor;
elem.textElem = paper.text(textPosition.x, textPosition.y, params.text).attr(params.textAttrs);
params.attrs.href && (elem.textElem.attr({href: params.attrs.href}));
$.fn.mapael.paramHover(elem.textElem, params.textAttrs, params.textAttrsHover);
$.fn.mapael.setHover(paper, elem.mapElem, elem.textElem);
$.fn.mapael.setCallbacks(params, elem.mapElem, elem.textElem);
} else {
$.fn.mapael.setHover(paper, elem.mapElem);
$.fn.mapael.setCallbacks(params, elem.mapElem);
}
if (params.tooltip && params.tooltip.content) {
elem.mapElem.tooltipContent = params.tooltip.content;
$.fn.mapael.setTooltip(elem.mapElem, $tooltip);
if (params.text) {
elem.textElem.tooltipContent = params.tooltip.content;
$.fn.mapael.setTooltip(elem.textElem, $tooltip);
}
}
}
/**
* Get the text position (x, y and text-anchor)
* @param bbox the boundary box of the element
* @param textPosition the wanted text position (inner, right, left, top or bottom)
*/
$.fn.mapael.getTextPosition = function(bbox, textPosition) {
var textX = 0
, textY = 0
, textAnchor = '';
switch (textPosition) {
case 'bottom' :
textX = (bbox.x + bbox.x2) / 2;
textY = bbox.y2 + 15;
textAnchor = "middle";
break;
case 'top' :
textX = (bbox.x + bbox.x2) / 2;
textY = bbox.y - 15;
textAnchor = "middle";
break;
case 'left' :
textX = bbox.x - 10;
textY = (bbox.y + bbox.y2) / 2;
textAnchor = "end";
break;
case 'right' :
textX = bbox.x2 + 10;
textY = (bbox.y + bbox.y2) / 2;
textAnchor = "start";
break;
default : // 'inner' position
textX = (bbox.x + bbox.x2) / 2;
textY = (bbox.y + bbox.y2) / 2;
textAnchor = "middle";
}
return {'x' : textX, 'y' : textY, 'textAnchor' : textAnchor};
}
2013-06-23 18:00:13 +02:00
/**
* Set user defined callbacks on areas and plots
2013-06-30 22:55:03 +02:00
* @param elemParams the element parameters
2013-06-23 18:00:13 +02:00
* @param mapElem the map element to set callback on
* @param textElem the optional text within the map element
*/
2013-06-30 22:55:03 +02:00
$.fn.mapael.setCallbacks = function(elemParams, mapElem, textElem) {
2013-07-15 22:53:59 +02:00
var availableCallbacks = ['click', 'mouseover', 'mouseout']
, callbackFct = {};
for(var i = 0, length = availableCallbacks.length; i < length; ++i) {
if (elemParams["on" + availableCallbacks[i]]) {
callbackFct = elemParams["on" + availableCallbacks[i]];
$(mapElem.node).bind(availableCallbacks[i], function() {callbackFct(elemParams, mapElem, textElem)});
textElem && $(textElem.node).bind(availableCallbacks[i], function() {callbackFct(elemParams, mapElem, textElem)});
}
2013-06-23 18:00:13 +02:00
}
}
/**
* Get the legend conf matching with the value
* @param value the value to match with a slice in the legend
* @param legend the legend params object
* @return the legend slice matching with the value
*/
$.fn.mapael.getLegendEl = function (value, legend) {
for(var i = 0, length = legend.slices.length; i < length; ++i) {
if ((!legend.slices[i].min || value >= legend.slices[i].min)
&& (!legend.slices[i].max || value < legend.slices[i].max)
) {
return legend.slices[i];
}
}
2013-07-15 22:53:59 +02:00
return {};
2013-06-23 18:00:13 +02:00
};
/**
* Join a tooltip to areas and plots
* @param elem area or plot element
* @param $tooltip the tooltip container
* @param content the content to set in the tooltip
*/
2013-07-15 22:53:59 +02:00
$.fn.mapael.setTooltip = function(elem, $tooltip) {
2013-06-30 22:55:03 +02:00
var tooltipTO = 0;
$(elem.node).bind("mouseover", function() {
2013-07-15 22:53:59 +02:00
tooltipTO = setTimeout(function() {$tooltip.html(elem.tooltipContent).css("display", "block");}, 120);
2013-06-30 22:55:03 +02:00
}).bind("mouseout", function() {
clearTimeout(tooltipTO);
2013-06-23 18:00:13 +02:00
$tooltip.css("display", "none");
}).bind("mousemove", function(e) {
2013-06-30 22:55:03 +02:00
$tooltip.css("left", e.pageX + 15).css("top", e.pageY + 15 - $(window).scrollTop());
2013-06-23 18:00:13 +02:00
});
};
/**
* Draw a legend for areas and / or plots
* @param $container the legend container
* @param options map options
* @param legendType the type of the legend : 'area' or 'plot'
*/
$.fn.mapael.createLegend = function ($container, options, legendType) {
var legendParams = options.legend[legendType]
2013-07-15 22:53:59 +02:00
, $legend = (legendType == 'plot') ? $('.' + options.legend.plot.cssClass, $container).empty() : $('.' + options.legend.area.cssClass, $container).empty()
2013-06-23 18:00:13 +02:00
, paper = new Raphael($legend.get(0))
, width = 5
, height = 5
, title = {}
2013-07-15 22:53:59 +02:00
, defaultElemParams = {}
2013-06-23 18:00:13 +02:00
, elem = {}
2013-07-15 22:53:59 +02:00
, label = {};
2013-06-23 18:00:13 +02:00
if(legendParams.title) {
2013-07-15 22:53:59 +02:00
title = paper.text(legendParams.marginLeftTitle, legendParams.marginBottom, legendParams.title)
2013-06-23 18:00:13 +02:00
.attr(legendParams.titleAttrs);
2013-07-15 22:53:59 +02:00
width = legendParams.marginLeftTitle + title.getBBox().width;
height += legendParams.marginBottom + title.getBBox().height;
2013-06-23 18:00:13 +02:00
}
for(var i = 0, length = legendParams.slices.length; i < length; ++i) {
2013-07-15 22:53:59 +02:00
defaultElemParams = (legendType == 'plot') ? options.map['defaultPlot'] : options.map['defaultArea'];
2013-06-23 18:00:13 +02:00
legendParams.slices[i].attrs = $.extend(
{}
2013-07-15 22:53:59 +02:00
, defaultElemParams.attrs
2013-06-23 18:00:13 +02:00
, legendParams.slices[i].attrs
);
legendParams.slices[i].attrsHover = $.extend(
{}
2013-07-15 22:53:59 +02:00
, defaultElemParams.attrsHover
2013-06-23 18:00:13 +02:00
, legendParams.slices[i].attrsHover
);
if(legendType == 'area' || legendParams.slices[i].type == "square") {
2013-06-23 18:00:13 +02:00
// Draw a square for squared plots AND areas
!legendParams.slices[i].size && (legendParams.slices[i].size = 20);
elem = paper.rect(
2013-07-15 22:53:59 +02:00
legendParams.marginLeft
2013-06-23 18:00:13 +02:00
, height
, legendParams.slices[i].size
, legendParams.slices[i].size
).attr(legendParams.slices[i].attrs);
} else {
elem = paper.circle(
legendParams.marginLeft + legendParams.slices[i].size / 2
, height + legendParams.slices[i].size / 2
, legendParams.slices[i].size / 2
).attr(legendParams.slices[i].attrs);
}
2013-06-30 22:55:03 +02:00
2013-06-23 18:00:13 +02:00
label = paper.text(
2013-07-15 22:53:59 +02:00
legendParams.marginLeft + legendParams.slices[i].size + legendParams.marginLeftLabel
2013-06-23 18:00:13 +02:00
, height + legendParams.slices[i].size / 2
, legendParams.slices[i].label
).attr(legendParams.labelAttrs);
2013-07-15 22:53:59 +02:00
height += legendParams.marginBottom + legendParams.slices[i].size;
width = Math.max(width, legendParams.marginLeft + legendParams.slices[i].size + legendParams.marginLeftLabel + label.getBBox().width);
2013-06-23 18:00:13 +02:00
$.fn.mapael.paramHover(elem, legendParams.slices[i].attrs, legendParams.slices[i].attrsHover);
$.fn.mapael.paramHover(label, legendParams.labelAttrs, legendParams.labelAttrs);
$.fn.mapael.setHover(paper, elem, label);
}
// VMLWidth option allows you to set static width for the legend
// only for VML render because text.getBBox() returns wrong values on IE6/7
2013-07-15 22:53:59 +02:00
if (Raphael.type != 'SVG' && legendParams.VMLWidth)
width = legendParams.VMLWidth;
2013-06-23 18:00:13 +02:00
paper.setSize(width, height);
}
2013-06-30 22:55:03 +02:00
2013-06-23 18:00:13 +02:00
/**
2013-06-30 22:55:03 +02:00
* Set he behaviour for 'mouseover' event
2013-06-23 18:00:13 +02:00
* @param paper paper Raphael paper object
* @param mapElem mapElem the map element
* @param textElem the optional text element (within the map element)
*/
$.fn.mapael.hoverIn = function (paper, mapElem, textElem) {
2013-07-15 22:53:59 +02:00
mapElem.animate(mapElem.attrsHover, mapElem.attrsHover.animDuration);
textElem && textElem.animate(textElem.attrsHover, textElem.attrsHover.animDuration);
paper.safari();
2013-06-23 18:00:13 +02:00
}
/**
2013-06-30 22:55:03 +02:00
* Set he behaviour for 'mouseout' event
2013-06-23 18:00:13 +02:00
* @param paper Raphael paper object
* @param mapElem the map element
* @param textElem the optional text element (within the map element)
*/
$.fn.mapael.hoverOut = function (paper, mapElem, textElem) {
2013-07-15 22:53:59 +02:00
mapElem.animate(mapElem.originalAttrs, mapElem.attrsHover.animDuration);
textElem && textElem.animate(textElem.originalAttrs, textElem.attrsHover.animDuration);
2013-06-23 18:00:13 +02:00
paper.safari();
};
/**
2013-06-30 22:55:03 +02:00
* Set the hover behavior (mouseover & mouseout) for plots and areas
2013-06-23 18:00:13 +02:00
* @param paper Raphael paper object
* @param mapElem the map element
* @param textElem the optional text element (within the map element)
*/
$.fn.mapael.setHover = function (paper, mapElem, textElem) {
var $mapElem = {}
2013-06-30 22:55:03 +02:00
, $textElem = {}
2013-07-15 22:53:59 +02:00
, hoverTO = 0
, overBehaviour = function() {hoverTO = setTimeout(function () {$.fn.mapael.hoverIn(paper, mapElem, textElem);}, 120);}
, outBehaviour = function () {clearTimeout(hoverTO);$.fn.mapael.hoverOut(paper, mapElem, textElem);};
$mapElem = $(mapElem.node);
$mapElem.bind("mouseover", overBehaviour);
$mapElem.bind("mouseout", outBehaviour);
2013-06-23 18:00:13 +02:00
if (textElem) {
$textElem = $(textElem.node);
2013-07-15 22:53:59 +02:00
$textElem.bind("mouseover", overBehaviour);
$(textElem.node).bind("mouseout", outBehaviour);
2013-06-23 18:00:13 +02:00
}
};
/**
* Set the attributes on hover and the attributes to restore for a map element
* @param elem the map element
2013-06-30 22:55:03 +02:00
* @param originalAttrs the original attributes to restore on mouseout event
* @param attrsHover the attributes to set on mouseover event
2013-06-23 18:00:13 +02:00
*/
$.fn.mapael.paramHover = function (elem, originalAttrs, attrsHover) {
2013-07-15 22:53:59 +02:00
// Disable transform option on hover for VML (IE<9) because of several bugs
if (Raphael.type != 'SVG') delete attrsHover.transform;
elem.attrsHover = attrsHover;
2013-06-23 18:00:13 +02:00
2013-07-15 22:53:59 +02:00
if (elem.attrsHover.transform) elem.originalAttrs = $.extend({transform : "s1"}, originalAttrs);
else elem.originalAttrs = originalAttrs;
2013-06-23 18:00:13 +02:00
};
// Default map options
$.fn.mapael.defaultOptions = {
map: {
2013-07-15 22:53:59 +02:00
cssClass: "map"
, tooltip: {
2013-06-23 18:00:13 +02:00
cssClass: "mapTooltip"
}
, defaultArea: {
attrs: {
fill: "#343434"
, stroke: "#5d5d5d"
, "stroke-width": 1
, "stroke-linejoin": "round"
}
, attrsHover: {
fill: "#f38a03"
, animDuration : 300
}
, textPosition: 'inner'
2013-06-23 18:00:13 +02:00
, textAttrs: {
"font-size": 15
, fill:"#c7c7c7"
}
, textAttrsHover: {
fill:"#eaeaea"
, "animDuration" : 300
}
}
, defaultPlot: {
type: "circle"
, size: 15
, attrs: {
fill: "#0088db"
, stroke: "#fff"
, "stroke-width": 0
, "stroke-linejoin": "round"
}
, attrsHover: {
"stroke-width": 3
, animDuration : 300
}
, textPosition: 'right'
2013-06-23 18:00:13 +02:00
, textAttrs: {
"font-size": 15
, fill:"#c7c7c7"
},
textAttrsHover: {
fill:"#eaeaea"
, animDuration : 300
}
}
}
, legend: {
area: {
2013-07-15 22:53:59 +02:00
cssClass: "areaLegend"
2013-06-23 18:00:13 +02:00
, display: false
, marginLeft: 15
, marginLeftTitle: 5
, marginLeftLabel: 10
, marginBottom: 15
, titleAttrs: {
"font-size" : 18
, fill : "#343434"
, "text-anchor" : "start"
}
, labelAttrs: {
"font-size" : 15
, fill : "#343434"
, "text-anchor" : "start"
}
, slices : []
2013-07-15 22:53:59 +02:00
}
, plot: {
cssClass: "plotLegend"
2013-06-23 18:00:13 +02:00
, display: false
, marginLeft: 15
, marginLeftTitle: 5
, marginLeftLabel: 10
, marginBottom: 15
, titleAttrs: {
"font-size" : 18
, fill : "#343434"
, "text-anchor" : "start"
}
, labelAttrs: {
"font-size" : 15
, fill : "#343434"
, "text-anchor" : "start"
}
, slices : []
}
}
, areas: {}
, plots: {}
};
})(jQuery);