Commit c1397e96 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

fix on asynochronous rendering of overlays

when overlay contain more than one element previous implementation
didn't work 
parent acea939c
......@@ -1333,6 +1333,7 @@ CustomMap.prototype.openInfoWindowForAlias = function(aliasId, modelId, onready)
* all elements
*/
CustomMap.prototype.renderOverlayCollection = function(overlayCollection, fitBounds, onready) {
var self = this;
if (overlayCollection instanceof OverlayCollection) {
logger.warn("Deprecated method use: renderOverlayCollection(overlayCollection)");
} else {
......@@ -1340,19 +1341,8 @@ CustomMap.prototype.renderOverlayCollection = function(overlayCollection, fitBou
fitBounds = overlayCollection.fitBounds;
overlayCollection = overlayCollection.overlayCollection;
}
var elementsToBeRendered = 0;
var elementsRendered = 0;
var fireElementsReady = function() {
if (elementsToBeRendered === elementsRendered) {
if (typeof (onready) === "function") {
onready();
}
}
};
var elements = overlayCollection.elements;
var missingElements = false;
var boundsArray = [];
boundsArray[this.getId()] = new google.maps.LatLngBounds();
......@@ -1361,76 +1351,69 @@ CustomMap.prototype.renderOverlayCollection = function(overlayCollection, fitBou
}
var bounds;
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var model = this.getSubmodelById(element.modelId);
if (element.type === "ALIAS") {
if (overlayCollection.aliasMarkers[element.getId()] !== undefined) {
logger.warn("More than one marker in " + overlayCollection.name + " for alias " + element.getId()
+ ". Skipping duplicates.");
} else {
elementsToBeRendered++;
model.getModel().getAliasById(element.getId()).then(function(aliasData) {
var aliasMarker = new AliasMarker(element.getId(), element.icon, aliasData, model);
overlayCollection.aliasMarkers[element.getId()] = aliasMarker;
bounds = aliasMarker.getBounds();
var promise = Promise.each(
elements,
function(element) {
var model = self.getSubmodelById(element.modelId);
if (element.type === "ALIAS") {
if (overlayCollection.aliasMarkers[element.getId()] !== undefined) {
logger.warn("More than one marker in " + overlayCollection.name + " for alias " + element.getId()
+ ". Skipping duplicates.");
} else {
model.getModel().getAliasById(element.getId()).then(function(aliasData) {
var aliasMarker = new AliasMarker(element.getId(), element.icon, aliasData, model);
overlayCollection.aliasMarkers[element.getId()] = aliasMarker;
bounds = aliasMarker.getBounds();
boundsArray[element.getModelId()].extend(bounds.getNorthEast());
boundsArray[element.getModelId()].extend(bounds.getSouthWest());
});
}
} else if (element.type === "REACTION") {
model.getModel().getReactionById(element.getId()).then(function(reactionData) {
var marker = null;
var icon = element.getIcon();
if (icon === null || icon === undefined) {
// this should happen when we visualize search data (there is
// no marker, but only flat overlay of the reaction lines)
//
marker = new ReactionOverlay(null, reactionData, model, false, element.getId());
} else {
// when we have icon defined (for instance when it comes from
// comment) then we don't want to have overlayed reaction lines
// but icon marker
marker = new ReactionMarker(element.getId(), icon, reactionData, model);
}
overlayCollection.reactionMarkers[element.getId()] = marker;
bounds = marker.getBounds();
boundsArray[element.getModelId()].extend(bounds.getNorthEast());
boundsArray[element.getModelId()].extend(bounds.getSouthWest());
});
} else if (element.type === "POINT") {
var pointData = model.getModel().getPointDataByPoint(element.getPoint());
var pointMarker = new PointMarker(pointData, element.icon, model);
overlayCollection.pointMarkers[pointData.getId()] = pointMarker;
bounds = pointMarker.getBounds();
boundsArray[element.getModelId()].extend(bounds.getNorthEast());
boundsArray[element.getModelId()].extend(bounds.getSouthWest());
elementsRendered++;
fireElementsReady();
});
}
} else if (element.type === "REACTION") {
elementsToBeRendered++;
model.getModel().getReactionById(element.getId()).then(function(reactionData) {
var marker = null;
var icon = element.getIcon();
if (icon === null || icon === undefined) {
// this should happen when we visualize search data (there is
// no marker, but only flat overlay of the reaction lines)
//
marker = new ReactionOverlay(null, reactionData, model, false, element.getId());
} else {
// when we have icon defined (for instance when it comes from
// comment) then we don't want to have overlayed reaction lines
// but icon marker
marker = new ReactionMarker(element.getId(), icon, reactionData, model);
throw new Error("Unknown type of the element in overlay: " + element.type);
}
overlayCollection.reactionMarkers[element.getId()] = marker;
bounds = marker.getBounds();
boundsArray[element.getModelId()].extend(bounds.getNorthEast());
boundsArray[element.getModelId()].extend(bounds.getSouthWest());
elementsRendered++;
fireElementsReady();
});
} else if (element.type === "POINT") {
var pointData = model.getModel().getPointDataByPoint(element.getPoint());
var pointMarker = new PointMarker(pointData, element.icon, model);
overlayCollection.pointMarkers[pointData.getId()] = pointMarker;
if (!missingElements) {
bounds = pointMarker.getBounds();
boundsArray[element.getModelId()].extend(bounds.getNorthEast());
boundsArray[element.getModelId()].extend(bounds.getSouthWest());
}).then(function() {
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var infoWindow = self.getInfoWindowForIdentifiedElement(element);
if (infoWindow !== null && infoWindow !== undefined) {
self.retrieveOverlayDetailDataForElement(element, infoWindow.getOverlayFullViewArray());
self.updateInfoWindowForIdentifiedElement(element);
}
} else {
logger.warn("Unknown type of the element in overlay: " + element.type);
}
var infoWindow = this.getInfoWindowForIdentifiedElement(element);
if (infoWindow !== null && infoWindow !== undefined) {
this.retrieveOverlayDetailDataForElement(element, infoWindow.getOverlayFullViewArray());
this.updateInfoWindowForIdentifiedElement(element);
}
}
if (missingElements) {
this.retrieveMissingReactions();
this.retrieveMissingAliases();
} else {
if (elements.length > 0 && fitBounds) {
for ( var mapId in boundsArray) {
if (boundsArray.hasOwnProperty(mapId)) {
var map = this.getSubmodelById(mapId).map;
var map = self.getSubmodelById(mapId).map;
bounds = boundsArray[mapId];
if (map !== undefined && !bounds.isEmpty()) {
if (typeof map.fitBounds2 !== "undefined") {
......@@ -1442,8 +1425,12 @@ CustomMap.prototype.renderOverlayCollection = function(overlayCollection, fitBou
}
}
}
}
fireElementsReady();
if (typeof (onready) === "function") {
onready();
}
});
};
/**
......
......@@ -308,8 +308,32 @@ describe('CustomMap', function() {
assert.equal(1, aliasMarkerCount);
done();
}});
});
it("renderOverlayCollection for set of aliases", function(done) {
var map = helper.createCustomMap();
var oc = new OverlayCollection(map, 'name');
var aliases = [];
for (var i=0;i<3;i++) {
var alias = helper.createAlias(map);
map.getModel().addAlias(alias);
aliases.push(alias);
oc.elements.push(helper.createIdentifiedElement(alias));
}
map.renderOverlayCollection({overlayCollection:oc, onready: function(){
var aliasMarkerCount = 0;
for ( var id in oc.aliasMarkers) {
if (oc.aliasMarkers.hasOwnProperty(id)) {
aliasMarkerCount++;
}
}
assert.equal(aliasMarkerCount,3);
done();
}});
});
it("renderOverlayCollection for point", function(done) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment