Skip to content

Commit

Permalink
Improve updateElem() performance
Browse files Browse the repository at this point in the history
  • Loading branch information
neveldo committed Dec 13, 2016
1 parent 8e2d46b commit a2923c9
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions js/jquery.mapael.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,6 @@
(self.options.areas[id] ? self.options.areas[id] : {}),
self.options.legend.area
);

self.updateElem(elemOptions, self.areas[id], animDuration);
}
});
Expand Down Expand Up @@ -1217,12 +1216,26 @@
return elem;
},

/*
* Check wether newAttrs object bring modifications to originalAttrs object
*/
isAttrsChanged: function(originalAttrs, newAttrs) {
for (var key in newAttrs) {
if (typeof originalAttrs[key] === 'undefined' || newAttrs[key] !== originalAttrs[key]) {
return true;
}
}
return false;
},

/*
* Update the element "elem" on the map with the new elemOptions options
*/
updateElem: function (elemOptions, elem, animDuration) {

var self = this;
var bbox = elem.mapElem.getBBox();

var bbox;
var textPosition;
var plotOffsetX;
var plotOffsetY;
Expand All @@ -1235,6 +1248,8 @@
if (elemOptions.text !== undefined && elemOptions.text.content !== undefined && elemOptions.text.content != elem.textElem.attrs.text)
elem.textElem.attr({text: elemOptions.text.content});

bbox = elem.mapElem.getBBox();

if (elemOptions.size || (elemOptions.width && elemOptions.height)) {
if (elemOptions.type == "image" || elemOptions.type == "svg") {
plotOffsetX = (elemOptions.width - bbox.width) / 2;
Expand Down Expand Up @@ -1271,13 +1286,20 @@

// Update elements attrs and attrsHover
self.setHoverOptions(elem.mapElem, elemOptions.attrs, elemOptions.attrsHover);
if (animDuration > 0)
elem.mapElem.animate(elemOptions.attrs, animDuration);
else
elem.mapElem.attr(elemOptions.attrs);

if (self.isAttrsChanged(elem.mapElem.attrs, elemOptions.attrs)) {
if (animDuration > 0)
elem.mapElem.animate(elemOptions.attrs, animDuration);
else
elem.mapElem.attr(elemOptions.attrs);
}

// Update dimensions of SVG plots
if (elemOptions.type == "svg") {

if (bbox === undefined) {
bbox = elem.mapElem.getBBox();
}
elem.mapElem.transform("m" + (elemOptions.width / elem.mapElem.originalWidth) + ",0,0," + (elemOptions.height / elem.mapElem.originalHeight) + "," + bbox.x + "," + bbox.y);
}

Expand Down Expand Up @@ -1395,8 +1417,8 @@

var updateTooltipPosition = function (x, y) {

var offsetLeft = 10;
var offsetTop = 20;
var offsetLeft = 10;
var offsetTop = 20;

if (typeof elem.tooltip.offset === "object") {
if (typeof elem.tooltip.offset.left !== "undefined") {
Expand Down

0 comments on commit a2923c9

Please sign in to comment.