Interactive interface for browsing global, full-resolution satellite imagery
When loading config dynamically, return a promise that will not be fulfilled if a request is already outstanding
Mike McGannd818191df152
diff --git a/etc/deploy/wv.js.json b/etc/deploy/wv.js.jsonindex 80e6e2abd..25fefc584 100644--- a/etc/deploy/wv.js.json+++ b/etc/deploy/wv.js.json@@ -4,6 +4,7 @@"src/js/util/wv.util.browser.js","src/js/util/wv.util.js",+ "src/js/util/wv.util.load.js","src/js/util/wv.util.events.js","src/js/ui/wv.ui.js",diff --git a/src/index.html b/src/index.htmlindex f0e788aea..223b9b8c5 100644--- a/src/index.html+++ b/src/index.html@@ -143,6 +143,7 @@<!-- link.dev --> <script type="text/javascript" src="js/util/wv.util.browser.js"></script><!-- link.dev --> <script type="text/javascript" src="js/util/wv.util.js"></script>+ <!-- link.dev --> <script type="text/javascript" src="js/util/wv.util.load.js"></script><!-- link.dev --> <script type="text/javascript" src="js/util/wv.util.events.js"></script><!-- link.dev --> <script type="text/javascript" src="js/ui/wv.ui.js"></script>diff --git a/src/js/layers/wv.layers.options.js b/src/js/layers/wv.layers.options.jsindex 4457ffbdd..a53debd62 100644--- a/src/js/layers/wv.layers.options.js+++ b/src/js/layers/wv.layers.options.js@@ -27,8 +27,7 @@ wv.layers.options = wv.layers.options || function(config, models, layer) {canvas = document.createElement("canvas");canvas.width = 140;canvas.height = 10;- var promise = wv.palettes.loadCustom(config).done(loaded);- wv.ui.indicator.delayed(promise);+ wv.palettes.loadCustom(config).done(loaded);};var loaded = function(custom) {diff --git a/src/js/palettes/wv.palettes.js b/src/js/palettes/wv.palettes.jsindex d163816c9..6bf69a4d3 100644--- a/src/js/palettes/wv.palettes.js+++ b/src/js/palettes/wv.palettes.js@@ -118,13 +118,14 @@ wv.palettes = (function(self) {};self.loadCustom = function(config) {- return wv.util.load(config.palettes, "custom", "config/palettes-custom.json");+ return wv.util.load.config(config.palettes,+ "custom", "config/palettes-custom.json");};self.loadRendered = function(config, layerId) {var layer = config.layers[layerId];- return wv.util.load(config.palettes.rendered, layer.palette.id,- "config/palettes/" + layer.palette.id + ".json");+ return wv.util.load.config(config.palettes.rendered,+ layer.palette.id, "config/palettes/" + layer.palette.id + ".json");};self.requirements = function(state, config) {diff --git a/src/js/palettes/wv.palettes.legend.js b/src/js/palettes/wv.palettes.legend.jsindex ddbd91044..296864c97 100644--- a/src/js/palettes/wv.palettes.legend.js+++ b/src/js/palettes/wv.palettes.legend.js@@ -34,11 +34,13 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {render();} else {wv.palettes.loadRendered(config, layer.id).done(function() {- loaded = true;- render();- self.update();- if ( spec.onLoad ) {- spec.onLoad();+ if ( !loaded ) {+ loaded = true;+ render();+ self.update();+ if ( spec.onLoad ) {+ spec.onLoad();+ }}});}@@ -48,7 +50,6 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {var $parent = $(selector);var paletteId = layer.palette.id;var palette = config.palettes.rendered[paletteId];- var singleClass = palette.classes && palette.classes.colors.length === 1;var $legendPanel = $("<div></div>").addClass("wv-palettes-panel");diff --git a/src/js/util/wv.util.js b/src/js/util/wv.util.jsindex 1019ac997..2c1c7dea7 100644--- a/src/js/util/wv.util.js+++ b/src/js/util/wv.util.js@@ -378,20 +378,6 @@ wv.util = (function(self) {}};- self.load = function(root, attr, url) {- var promise = $.Deferred();- if ( root[attr] && _.size(root[attr]) > 0 ) {- promise.resolve(root[attr]);- } else {- promise = $.getJSON(wv.brand.url(url));- promise.done(function(result) {- root[attr] = result;- });- }- promise.fail(wv.util.error);- return promise;- };-/*** Wraps a function in a try/catch block that invokes wv.util.error* if an exception is thrown.diff --git a/src/js/util/wv.util.load.js b/src/js/util/wv.util.load.jsnew file mode 100644index 000000000..d7c9fed84--- /dev/null+++ b/src/js/util/wv.util.load.js@@ -0,0 +1,53 @@+/*+ * NASA Worldview+ *+ * This code was originally developed at NASA/Goddard Space Flight Center for+ * the Earth Science Data and Information System (ESDIS) project.+ *+ * Copyright (C) 2013 - 2014 United States Government as represented by the+ * Administrator of the National Aeronautics and Space Administration.+ * All Rights Reserved.+ */++var wv = wv || {};+wv.util = wv.util || {};++wv.util.load = wv.util.load || (function() {++ var self = {};+ var configPromises = {};+ var loading = 0;++ self.config = function(root, attr, url) {+ promise = $.Deferred();+ // If a request is already outstanding, "ignore" it by returning+ // a promise that will never be fulfilled.+ if ( configPromises[url]) {+ return promise;+ }+ if ( root[attr] && _.size(root[attr]) > 0 ) {+ promise.resolve(root[attr]);+ } else {+ promise = $.getJSON(wv.brand.url(url));+ promise.done(function(result) {+ root[attr] = result;+ }).always(function() {+ delete configPromises[url];+ loading -= 1;+ if ( loading === 0 ) {+ wv.ui.indicator.hide();+ }+ }).fail(wv.util.error);++ if ( loading === 0 ) {+ wv.ui.indicator.delayed(promise, 1000);+ }+ loading += 1;+ configPromises[url] = promise;+ }+ return promise;+ };++ return self;++})();