Skip to content

Commit a2923c9

Browse files
committed
Improve updateElem() performance
1 parent 8e2d46b commit a2923c9

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

js/jquery.mapael.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,6 @@
10071007
(self.options.areas[id] ? self.options.areas[id] : {}),
10081008
self.options.legend.area
10091009
);
1010-
10111010
self.updateElem(elemOptions, self.areas[id], animDuration);
10121011
}
10131012
});
@@ -1217,12 +1216,26 @@
12171216
return elem;
12181217
},
12191218

1219+
/*
1220+
* Check wether newAttrs object bring modifications to originalAttrs object
1221+
*/
1222+
isAttrsChanged: function(originalAttrs, newAttrs) {
1223+
for (var key in newAttrs) {
1224+
if (typeof originalAttrs[key] === 'undefined' || newAttrs[key] !== originalAttrs[key]) {
1225+
return true;
1226+
}
1227+
}
1228+
return false;
1229+
},
1230+
12201231
/*
12211232
* Update the element "elem" on the map with the new elemOptions options
12221233
*/
12231234
updateElem: function (elemOptions, elem, animDuration) {
1235+
12241236
var self = this;
1225-
var bbox = elem.mapElem.getBBox();
1237+
1238+
var bbox;
12261239
var textPosition;
12271240
var plotOffsetX;
12281241
var plotOffsetY;
@@ -1235,6 +1248,8 @@
12351248
if (elemOptions.text !== undefined && elemOptions.text.content !== undefined && elemOptions.text.content != elem.textElem.attrs.text)
12361249
elem.textElem.attr({text: elemOptions.text.content});
12371250

1251+
bbox = elem.mapElem.getBBox();
1252+
12381253
if (elemOptions.size || (elemOptions.width && elemOptions.height)) {
12391254
if (elemOptions.type == "image" || elemOptions.type == "svg") {
12401255
plotOffsetX = (elemOptions.width - bbox.width) / 2;
@@ -1271,13 +1286,20 @@
12711286

12721287
// Update elements attrs and attrsHover
12731288
self.setHoverOptions(elem.mapElem, elemOptions.attrs, elemOptions.attrsHover);
1274-
if (animDuration > 0)
1275-
elem.mapElem.animate(elemOptions.attrs, animDuration);
1276-
else
1277-
elem.mapElem.attr(elemOptions.attrs);
1289+
1290+
if (self.isAttrsChanged(elem.mapElem.attrs, elemOptions.attrs)) {
1291+
if (animDuration > 0)
1292+
elem.mapElem.animate(elemOptions.attrs, animDuration);
1293+
else
1294+
elem.mapElem.attr(elemOptions.attrs);
1295+
}
12781296

12791297
// Update dimensions of SVG plots
12801298
if (elemOptions.type == "svg") {
1299+
1300+
if (bbox === undefined) {
1301+
bbox = elem.mapElem.getBBox();
1302+
}
12811303
elem.mapElem.transform("m" + (elemOptions.width / elem.mapElem.originalWidth) + ",0,0," + (elemOptions.height / elem.mapElem.originalHeight) + "," + bbox.x + "," + bbox.y);
12821304
}
12831305

@@ -1395,8 +1417,8 @@
13951417

13961418
var updateTooltipPosition = function (x, y) {
13971419

1398-
var offsetLeft = 10;
1399-
var offsetTop = 20;
1420+
var offsetLeft = 10;
1421+
var offsetTop = 20;
14001422

14011423
if (typeof elem.tooltip.offset === "object") {
14021424
if (typeof elem.tooltip.offset.left !== "undefined") {

0 commit comments

Comments
 (0)