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,29 +1668,52 @@ Metamaps.Visualize = {
self.mGraph.graph.empty(); self.mGraph.graph.empty();
} }
Metamaps.Loading.hide(); function runAnimation() {
// load JSON data, if it's not empty Metamaps.Loading.hide();
if (!self.loadLater) { // load JSON data, if it's not empty
//load JSON data. if (!self.loadLater) {
var rootIndex = 0; //load JSON data.
if (Metamaps.Active.Topic) { var rootIndex = 0;
var node = _.find(Metamaps.JIT.vizData, function(node){ if (Metamaps.Active.Topic) {
return node.id === Metamaps.Active.Topic.id; var node = _.find(Metamaps.JIT.vizData, function(node){
}); return node.id === Metamaps.Active.Topic.id;
rootIndex = _.indexOf(Metamaps.JIT.vizData, node); });
} rootIndex = _.indexOf(Metamaps.JIT.vizData, node);
self.mGraph.loadJSON(Metamaps.JIT.vizData, rootIndex); }
//compute positions and plot. self.mGraph.loadJSON(Metamaps.JIT.vizData, rootIndex);
self.computePositions(); //compute positions and plot.
self.mGraph.busy = true; self.computePositions();
if (self.type == "RGraph") { self.mGraph.busy = true;
self.mGraph.fx.animate(Metamaps.JIT.RGraph.animate); if (self.type == "RGraph") {
} else if (self.type == "ForceDirected") { self.mGraph.fx.animate(Metamaps.JIT.RGraph.animate);
self.mGraph.animate(Metamaps.JIT.ForceDirected.animateSavedLayout); } else if (self.type == "ForceDirected") {
} else if (self.type == "ForceDirected3D") { self.mGraph.animate(Metamaps.JIT.ForceDirected.animateSavedLayout);
self.mGraph.animate(Metamaps.JIT.ForceDirected.animateFDLayout); } else if (self.type == "ForceDirected3D") {
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 // update the url now that the map is ready
setTimeout(function(){ setTimeout(function(){