Skip to content

Commit 01592a7

Browse files
committed
Improved handling of callbacks with events
1 parent 55e26e9 commit 01592a7

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

examples.js

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ $(function(){
1212
$(".maparea2").mapael({
1313
map : {
1414
name : "france_departments"
15+
, zoom: {
16+
enabled: true
17+
}
1518
},
1619
areas: {
1720
"department-56" : {
@@ -50,30 +53,74 @@ $(function(){
5053
$(".maparea3").mapael({
5154
map : {
5255
name : "france_departments",
56+
zoom : {
57+
enabled : true
58+
},
5359
defaultArea: {
5460
attrs : {
5561
fill: "#5ba4ff",
5662
stroke: "#99c7ff",
5763
cursor: "pointer"
5864
},
65+
attrsHover : {
66+
animDuration:0
67+
},
5968
textAttrs : {
6069
cursor: "pointer",
70+
"font-size" : 10,
6171
fill :"#000"
6272
},
63-
onclick: function(params, mapElem, textElem) {
64-
mapElem.attr({fill: '#ff0000'});
73+
textAttrsHover : {
74+
animDuration:0
75+
},
76+
eventHandlers : {
77+
click: function(id, mapElem, textElem) {
78+
var newData = {'areas' : {}};
79+
if (mapElem.originalAttrs.fill == "#5ba4ff") {
80+
newData.areas[id] = {
81+
attrs : {
82+
fill : "#0088db"
83+
}
84+
};
85+
} else {
86+
newData.areas[id] = {
87+
attrs : {
88+
fill : "#5ba4ff"
89+
}
90+
};
91+
}
92+
$(".maparea3").trigger('update', [newData, false, false, 0]);
93+
}
6594
}
6695
}
6796
},
6897
areas: {
6998
"department-29" : {
70-
text : "29",
99+
text : "dblclick",
71100
attrs : {
72101
fill :"#0088db"
73102
},
103+
textPosition: "top",
74104
tooltip: {content : "Finistère (29)"},
75-
onclick: function(params, mapElem, textElem) {
76-
mapElem.attr({fill: '#24ff00'});
105+
eventHandlers : {
106+
click: function() {},
107+
dblclick: function(id, mapElem, textElem) {
108+
var newData = {'areas' : {}};
109+
if (mapElem.originalAttrs.fill == "#5ba4ff") {
110+
newData.areas[id] = {
111+
attrs : {
112+
fill : "#0088db"
113+
}
114+
};
115+
} else {
116+
newData.areas[id] = {
117+
attrs : {
118+
fill : "#5ba4ff"
119+
}
120+
};
121+
}
122+
$(".maparea3").trigger('update', [newData, false, false, 0]);
123+
}
77124
}
78125
}
79126
}
@@ -97,6 +144,7 @@ $(function(){
97144
area : {
98145
display : true,
99146
title :"Population of France by department",
147+
labelAttrs : {title : "Hide the matching departments"},
100148
slices : [
101149
{
102150
max :300000,

js/jquery.mapael.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
}
100100
};
101101

102-
if (!animDuration) animDuration = 300;
102+
if (typeof animDuration == "undefined") animDuration = 300;
103103
if (!easing) easing = 'linear';
104104
if (resetAreas) options.areas = {};
105105
if (resetPlots) options.plots = {};
@@ -235,11 +235,11 @@
235235
options.href && $.fn.mapael.setHref(elem.textElem, options.href);
236236
$.fn.mapael.setHoverOptions(elem.textElem, options.textAttrs, options.textAttrsHover);
237237
$.fn.mapael.setHover(paper, elem.mapElem, elem.textElem);
238-
$.fn.mapael.setCallbacks(options, elem.mapElem, elem.textElem);
238+
options.eventHandlers && $.fn.mapael.setEventHandlers(id, options, elem.mapElem, elem.textElem);
239239
$(elem.textElem.node).attr('class', id);
240240
} else {
241241
$.fn.mapael.setHover(paper, elem.mapElem);
242-
$.fn.mapael.setCallbacks(options, elem.mapElem);
242+
options.eventHandlers && $.fn.mapael.setEventHandlers(id, options, elem.mapElem);
243243
}
244244

245245
if (options.tooltip && options.tooltip.content) {
@@ -289,21 +289,18 @@
289289
};
290290

291291
/**
292-
* Set user defined callbacks on areas and plots
292+
* Set user defined handlers for events on areas and plots
293+
* @param id the id of the element
293294
* @param elemOptions the element parameters
294295
* @param mapElem the map element to set callback on
295296
* @param textElem the optional text within the map element
296297
*/
297-
$.fn.mapael.setCallbacks = function(elemOptions, mapElem, textElem) {
298-
var availableCallbacks = ['click', 'mouseover', 'mouseout']
299-
, callbackFct = {};
300-
301-
for(var i = 0, length = availableCallbacks.length; i < length; ++i) {
302-
if (elemOptions["on" + availableCallbacks[i]]) {
303-
callbackFct = elemOptions["on" + availableCallbacks[i]];
304-
$(mapElem.node).on(availableCallbacks[i], function() {!$.fn.mapael.panning && callbackFct(elemOptions, mapElem, textElem)});
305-
textElem && $(textElem.node).on(availableCallbacks[i], function() {!$.fn.mapael.panning && callbackFct(elemOptions, mapElem, textElem)});
306-
}
298+
$.fn.mapael.setEventHandlers = function(id, elemOptions, mapElem, textElem) {
299+
for(var event in elemOptions.eventHandlers) {
300+
(function(event) {
301+
$(mapElem.node).on(event, function() {!$.fn.mapael.panning && elemOptions.eventHandlers[event](id, mapElem, textElem)});
302+
textElem && $(textElem.node).on(event, function() {!$.fn.mapael.panning && elemOptions.eventHandlers[event](id, mapElem, textElem)});
303+
})(event);
307304
}
308305
}
309306

0 commit comments

Comments
 (0)