Skip to content

Commit

Permalink
Merge pull request #282 from neveldo/hotfix/improve-elements-update
Browse files Browse the repository at this point in the history
Improve updateElem() performance
  • Loading branch information
neveldo authored Dec 14, 2016
2 parents 8e2d46b + ea0c9e3 commit a4628a9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
36 changes: 28 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,24 @@
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 +1246,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 +1284,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 +1415,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
Loading

0 comments on commit a4628a9

Please sign in to comment.