prevented map from loading before all metacode images were loaded

This commit is contained in:
Connor Turland 2015-01-30 00:09:45 -05:00
parent 84e03d5475
commit 5cf75f3ea2

View file

@ -1668,6 +1668,7 @@ Metamaps.Visualize = {
self.mGraph.graph.empty();
}
function runAnimation() {
Metamaps.Loading.hide();
// load JSON data, if it's not empty
if (!self.loadLater) {
@ -1691,6 +1692,28 @@ Metamaps.Visualize = {
self.mGraph.animate(Metamaps.JIT.ForceDirected.animateFDLayout);
}
}
}
// hold until all the needed metacode images are loaded
// hold for a maximum of 80 passes, or 4 seconds of waiting time
var tries = 0;
function hold() {
var unique = _.uniq(Metamaps.Topics.models, function (metacode) { return metacode.get('metacode_id'); }),
requiredMetacodes = _.map(unique, function (metacode) { return metacode.get('metacode_id'); }),
loadedCount = 0;
_.each(requiredMetacodes, function (metacode_id) {
var metacode = Metamaps.Metacodes.get(metacode_id),
img = metacode ? metacode.get('image') : false;
if (img && (img.complete || (typeof img.naturalWidth !== "undefined" && img.naturalWidth !== 0))) {
loadedCount += 1;
}
});
if (loadedCount === requiredMetacodes.length || tries > 80) runAnimation();
else setTimeout(function(){ tries++; hold() }, 50);
}
hold();
// update the url now that the map is ready
setTimeout(function(){