Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-955: Indicator has been reworked to allow stacking of conditions

Mike McGann968b85c51a0c
diff --git a/src/js/data/wv.data.ui.js b/src/js/data/wv.data.ui.js
index 600ea1912..20ce28862 100644
--- a/src/js/data/wv.data.ui.js
+++ b/src/js/data/wv.data.ui.js
@@ -23,6 +23,11 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
var lastResults = null;
var maps = ui.map;
+ var indicators = {
+ query: null,
+ noneInView: null
+ };
+
var self = {};
self.selector = "#wv-data";
self.id = "wv-data";
@@ -176,6 +181,8 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
};
self.onViewChange = function(map) {
+ var indicator;
+
if ( !model.active || queryActive || !lastResults ) {
return;
}
@@ -194,10 +201,10 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
}
}
});
+ wv.ui.indicator.hide(indicators.noneInView);
if ( hasCentroids && !inView ) {
- wv.ui.indicator.show("Zoom out or move map");
- } else {
- wv.ui.indicator.hide();
+ indicators.noneInView =
+ wv.ui.indicator.show("Zoom out or move map");
}
};
@@ -215,7 +222,7 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
};
var onDeactivate = function() {
- wv.ui.indicator.hide();
+ wv.ui.indicator.hide(indicators);
if ( selectionListPanel ) {
selectionListPanel.hide();
}
@@ -238,7 +245,7 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
var onQuery = function() {
queryActive = true;
- wv.ui.indicator.searching();
+ indicators.query = wv.ui.indicator.searching(indicators);
if ( selectionListPanel ) {
selectionListPanel.hide();
}
@@ -250,9 +257,9 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
var onQueryResults = function(results) {
queryActive = false;
lastResults = results;
- wv.ui.indicator.hide();
+ wv.ui.indicator.hide(indicators);
if ( model.selectedProduct !== null && results.granules.length === 0 ) {
- wv.ui.indicator.noData();
+ wv.ui.indicator.noData(indicators);
} else {
if ( results.meta.showList ) {
selectionListPanel =
@@ -269,12 +276,12 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
var onQueryCancel = function() {
queryActive = false;
- wv.ui.indicator.hide();
+ wv.ui.indicator.hide(indicators);
};
var onQueryError = function(status, error) {
queryActive = false;
- wv.ui.indicator.hide();
+ wv.ui.indicator.hide(indicators);
if ( status !== "abort" ) {
console.error("Unable to search", status, error);
wv.ui.notify("Unable to search at this time.<br/><br/>Please try " +
@@ -284,7 +291,7 @@ wv.data.ui = wv.data.ui || function(models, ui, config) {
var onQueryTimeout = function() {
queryActive = false;
- wv.ui.indicator.hide();
+ wv.ui.indicator.hide(indicators);
wv.ui.notify(
"No results received yet. This may be due to a " +
"connectivity issue. Please try again later."
diff --git a/src/js/ui/wv.ui.indicator.js b/src/js/ui/wv.ui.indicator.js
index bb38b1e23..03de34396 100644
--- a/src/js/ui/wv.ui.indicator.js
+++ b/src/js/ui/wv.ui.indicator.js
@@ -4,137 +4,117 @@
* This code was originally developed at NASA/Goddard Space Flight Center for
* the Earth Science Data and Information System (ESDIS) project.
*
- * Copyright (C) 2013 United States Government as represented by the
+ * Copyright (C) 2013 - 2014 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*/
-/**
- * @module wv.ui
- */
var wv = wv || {};
wv.ui = wv.ui || {};
-/**
- * Displays an indicator on the screen.
- *
- * The indicator has an image and text associated with it. Use show to
- * display and hide to remove. Example:
- *
- * wv.ui.indicator.show("Please Wait", "images/wait.png");
- *
- * Only one indicator can be visibile at a time. If show is called while
- * another indicator is already active, it will be replaced.
- *
- * @class wv.ui.indicator
- * @static
- */
wv.ui.indicator = wv.ui.indicator || (function() {
var self = {};
+ var $indicator;
+ var $icon;
+ var $message;
+
self.active = [];
- /**
- * Shows the indicator with a message and an icon. If another indicator
- * is already active, this call will replace the other one.
- *
- * @method show
- * @static
- *
- * @param message {string} The message to display
- * @param icon {string} URL to the icon to display
- */
+ var init = function() {
+ $indicator = $("<div></div>")
+ .attr("id", "indicator");
+ $icon = $("<img></img>");
+ $message = $("<span></span");
+
+ $indicator.append($icon).append($message).hide();
+ $("body").append($indicator);
+ };
+
self.show = function(message, icon) {
- self.hide();
- self.active.push(message);
+ self._show(message, icon);
+ var id = _.uniqueId();
+ self.active.push({
+ id: id,
+ message: message,
+ icon: icon
+ });
+ return id;
+ };
+
+ self.hide = function(hides) {
+ if ( _.isString(hides) ) {
+ hides = [hides];
+ }
+ _.each(hides, function(id) {
+ _.remove(self.active, { id: id });
+ });
+ if ( _.isEmpty(self.active) ) {
+ self._hide();
+ } else {
+ var def = _.last(self.active);
+ self._show(def.message, def.icon);
+ }
+ };
+
+ self.replace = function(hides, message, icon) {
+ self.hide(hides);
+ return self.show(message, icon);
+ };
+
+ self._show = function(message, icon) {
+ if ( !$indicator ) {
+ init();
+ }
if ( icon ) {
- $("body").append([
- "<div id='indicator'>",
- "<img src='" + icon + "'></img>",
- "<span>" + message + "</span>",
- "</div>"
- ].join("\n"));
+ $indicator.removeClass("message");
+ $icon.attr("src", icon).show();
} else {
- $("body").append([
- "<div id='indicator' class='message'>",
- message,
- "</div>"
- ].join("\n"));
+ $indicator.addClass("message");
+ $icon.removeAttr("src").hide();
}
+ $message.html(message);
+ $indicator.show();
};
- /**
- * Hides the indicator. If no indicator is displayed, this method does
- * nothing.
- *
- * @method hide
- * @static
- */
- self.hide = function() {
- $("#indicator").remove();
-
+ self._hide = function() {
+ if ( $indicator ) {
+ $indicator.hide();
+ }
};
- /**
- * Displays a "Searching" indicator. This is a convenience method for:
- *
- * wv.ui.indicator.show("Searching", "images/activity.gif")
- *
- * @method searching
- * @static
- */
- self.searching = function() {
- self.show("Searching ECHO for Data", "images/activity.gif");
+ self.searching = function(hides) {
+ if ( hides ) {
+ self.hide(hides);
+ }
+ return self.show("Searching ECHO for Data", "images/activity.gif");
};
- /**
- * Displays a "Loading" indicator. This is a convenience method for:
- *
- * wv.ui.indicator.show("Loading", "images/activity.gif")
- *
- * @method searching
- * @static
- */
- self.loading = function() {
- self.show("Loading", "images/activity.gif");
+ self.loading = function(hides) {
+ if ( hides ) {
+ self.hide(hides);
+ }
+ return self.show("Loading", "images/activity.gif");
};
- /**
- * Displays a "No data available" indicator. This is a convenience method
- * for:
- *
- * wv.ui.indicator.show("No data available", "images/red-x.svg")
- *
- * @method noData
- * @static
- */
- self.noData = function() {
- self.show("No Data Avaialble", "images/red-x.svg");
+ self.noData = function(hides) {
+ if ( hides ) {
+ self.hide(hides);
+ }
+ return self.show("No Data Avaialble", "images/red-x.svg");
};
- /**
- * Displays a "Loading" indicator if the specified promise is not
- * fulfilled in a certain amount of time. Once the promise is fulfilled,
- * any active indicator is hidden.
- *
- * @method delayed
- * @static
- * @param {jQuery.Deferred} The active promise
- * @param [int] Time, in milliseconds, to wait until showing the
- * indicator. If not specified, a delay of one second is used.
- */
self.delayed = function(promise, delay) {
delay = delay || 1000;
- var shown = false;
+ var id;
var timeout = setTimeout(function() {
- shown = true;
- self.loading();
+ id = self.loading();
}, delay);
promise.always(function() {
clearTimeout(timeout);
- if ( shown ) {
- self.hide();
+ if ( id ) {
+ self.hide(id);
}
});
};
diff --git a/src/js/util/wv.util.browser.js b/src/js/util/wv.util.browser.js
index 23046fcc4..c7dbc6cbc 100644
--- a/src/js/util/wv.util.browser.js
+++ b/src/js/util/wv.util.browser.js
@@ -91,7 +91,7 @@ wv.util.browser = wv.util.browser || (function() {
self.localStorage = true;
self.history = true;
-
+
var init = function() {
var tests = self.tests;
diff --git a/src/js/util/wv.util.js b/src/js/util/wv.util.js
index 82bd8f1a8..c26ec3869 100644
--- a/src/js/util/wv.util.js
+++ b/src/js/util/wv.util.js
@@ -459,14 +459,7 @@ wv.util = (function(self) {
UP: 38,
DOWN: 40
};
-
- self.removeOne = function(array, value) {
- var index = _.indexOf(array, value);
- if ( index >= 0 ) {
- array.splice(index, 1);
- }
- };
-
+
return self;
})(wv.util || {});
diff --git a/src/js/util/wv.util.load.js b/src/js/util/wv.util.load.js
index 2f9dc477f..d51fa2e99 100644
--- a/src/js/util/wv.util.load.js
+++ b/src/js/util/wv.util.load.js
@@ -18,6 +18,7 @@ wv.util.load = wv.util.load || (function() {
var configPromises = {};
var loading = 0;
var indicatorTimeout = null;
+ var indicatorId = null;
self.config = function(root, attr, url) {
promise = $.Deferred();
@@ -33,7 +34,7 @@ wv.util.load = wv.util.load || (function() {
promise = $.getJSON(wv.brand.url(url));
if ( loading === 1 ) {
indicatorTimeout = setTimeout(function() {
- wv.ui.indicator.loading();
+ indicatorId = wv.ui.indicator.loading();
}, 2000);
}
promise.done(function(result) {
@@ -44,7 +45,7 @@ wv.util.load = wv.util.load || (function() {
if ( loading === 0 ) {
clearTimeout(indicatorTimeout);
indicatorTimeout = null;
- wv.ui.indicator.hide();
+ wv.ui.indicator.hide(indicatorId);
}
}).fail(wv.util.error);
diff --git a/test/ui/wv.ui.indicator-test.js b/test/ui/wv.ui.indicator-test.js
index 9b0dda36c..5b366b3af 100644
--- a/test/ui/wv.ui.indicator-test.js
+++ b/test/ui/wv.ui.indicator-test.js
@@ -9,32 +9,77 @@
* All Rights Reserved.
*/
-buster.testCase("wv.ui.indicator", {
+buster.testCase("wv.ui.indicator", function() {
- "Delayed indicator shown": function(done) {
- this.stub(wv.ui.indicator, "loading");
+ var self = {};
+
+ self.setUp = function() {
+ wv.ui.indicator.active = [];
+ };
+
+ self["Delayed indicator shown"] = function(done) {
+ this.stub(wv.ui.indicator, "_show");
this.stub(wv.ui.indicator, "hide");
var promise = $.Deferred();
wv.ui.indicator.delayed(promise, 10);
setTimeout(function() {
promise.resolve();
- buster.assert.called(wv.ui.indicator.loading);
+ buster.assert.called(wv.ui.indicator._show);
buster.assert.called(wv.ui.indicator.hide);
done();
- }, 20);
- },
+ }, 50);
+ };
- "Delayed indicator not shown": function(done) {
- this.stub(wv.ui.indicator, "loading");
+ self["Delayed indicator not shown"] = function(done) {
+ this.stub(wv.ui.indicator, "_show");
this.stub(wv.ui.indicator, "hide");
var promise = $.Deferred();
wv.ui.indicator.delayed(promise, 1000);
setTimeout(function() {
promise.resolve();
- buster.refute.called(wv.ui.indicator.loading);
+ buster.refute.called(wv.ui.indicator._show);
buster.refute.called(wv.ui.indicator.hide);
done();
}, 20);
- },
+ };
+
+ self["Pop message"] = function() {
+ this.stub(wv.ui.indicator, "_show");
+ this.stub(wv.ui.indicator, "_hide");
+
+ wv.ui.indicator.show("Bottom");
+ var id = wv.ui.indicator.show("Top");
+ wv.ui.indicator.hide(id);
+ buster.assert.equals(1, wv.ui.indicator.active.length);
+ buster.assert.equals("Bottom", wv.ui.indicator.active[0].message);
+ };
+
+ self["Remove bottom message"] = function() {
+ this.stub(wv.ui.indicator, "_show");
+ this.stub(wv.ui.indicator, "_hide");
+
+ var id = wv.ui.indicator.show("Bottom");
+ wv.ui.indicator.show("Top");
+ wv.ui.indicator.hide(id);
+ buster.assert.equals(1, wv.ui.indicator.active.length);
+ buster.assert.equals("Top", wv.ui.indicator.active[0].message);
+ };
+
+ self["Hide group"] = function() {
+ this.stub(wv.ui.indicator, "_show");
+ this.stub(wv.ui.indicator, "_hide");
+ var indicators = {
+ two: null,
+ three: null,
+ };
+ wv.ui.indicator.show("One");
+ indicators.two = wv.ui.indicator.show("Two");
+ indicators.three = wv.ui.indicator.show("Three");
+ wv.ui.indicator.hide(indicators);
+ buster.assert.equals(1, wv.ui.indicator.active.length);
+ buster.assert.equals("One", wv.ui.indicator.active[0].message);
+ };
+
+ return self;
-});
+}());