Interactive interface for browsing global, full-resolution satellite imagery
Refactoring: util, events, ui
Mike McGann1dc5213c8ba5
diff --git a/etc/deploy/wv.js.json b/etc/deploy/wv.js.jsonindex 24b6d1056..da47835a6 100644--- a/etc/deploy/wv.js.json+++ b/etc/deploy/wv.js.json@@ -11,6 +11,7 @@"src/js/events.js","src/js/wv.util.js",+ "src/js/wv.ui.js","src/js/Worldview.js","src/js/Worldview/About.js","src/js/Worldview/Support.js",@@ -18,6 +19,7 @@"src/js/link/wv.link.model.js","src/js/Worldview/Scheduler.js","src/js/Worldview/Preloader.js",+ "src/js/wv.events.js","src/js/Worldview/Events.js","src/js/Worldview/Indicator.js","src/js/Worldview/AjaxCache.js",diff --git a/src/index.html b/src/index.htmlindex 22f505180..210659027 100644--- a/src/index.html+++ b/src/index.html@@ -141,6 +141,7 @@<!-- link.dev --> <script type="text/javascript" src="js/events.js"></script><!-- link.dev --> <script type="text/javascript" src="js/wv.util.js"></script>+ <!-- link.dev --> <script type="text/javascript" src="js/wv.ui.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/About.js"></script>@@ -149,6 +150,7 @@<!-- link.dev --> <script type="text/javascript" src="js/link/wv.link.model.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/Preloader.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/Events.js"></script>+ <!-- link.dev --> <script type="text/javascript" src="js/wv.events.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/Indicator.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/AjaxCache.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/AjaxJoin.js"></script>diff --git a/src/js/date/wv.date.model.js b/src/js/date/wv.date.model.jsnew file mode 100644index 000000000..33e7eee03--- /dev/null+++ b/src/js/date/wv.date.model.js@@ -0,0 +1,67 @@+/*+ * 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.+ */++/**+ * @module wv.date+ */+var wv = wv || {};+wv.date = wv.date || {};++/**+ * @class wv.date.model+ */+wv.date.model = wv.date.model || function(config) {++ var self = {};++ self.archiveStartDate =+ Date.parseISOString(config.defaults.archiveStartDate);++ self.events = Worldview.Events();+ self.selected = null;++ var init = function() {+ self.set(Worldview.today());+ };++ self.select = function(date) {+ if ( date < self.archiveStartDate ) {+ date = self.archiveStartDate;+ } else if ( date > Worldview.today() ) {+ date = Worldview.today();+ }++ if ( !self.selected || date.getTime() !== self.selected.getTime() ) {+ self.selected = date;+ self.events.trigger("change", date);+ }+ };++ self.toPermalink = function() {+ return "time=" + self.selected.toISOString();+ };++ self.fromPermalink = function(queryString) {+ var query = Worldview.queryStringToObject(queryString);+ if ( query.time ) {+ try {+ var value = Date.parseISOString(query.time);+ self.set(value);+ } catch ( error ) {+ log.warn("Invalid date: " + query.time + ": " + error);+ }+ }+ };++ init();+ return self;++};diff --git a/src/js/wv.events.js b/src/js/wv.events.jsnew file mode 100644index 000000000..f665d3ed3--- /dev/null+++ b/src/js/wv.events.js@@ -0,0 +1,112 @@+/*+ * 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.+ */++/**+ * @module wv+ */+var wv = wv || {};++/**+ * General event pump.+ *+ * * Register listeners with on+ * * Unregister listeners with off+ * * Fire events with trigger+ *+ * Example:+ *+ * var events = wv.events();+ * events.on("answerReceived", function(answer) {+ * console.log("The answer is", answer);+ * });+ *+ * events.trigger("answerReceived", 42);+ *+ * @class wv.events+ */+wv.events = wv.events || function() {++ var self = {};++ // Object of event types. Each event type is an array of listeners.+ var events = {};++ /**+ * Registers a listener for an event.+ *+ * @method on+ *+ * @param {string} event Type of event to register for.+ *+ * @param {function} callback Function called when the event of the given+ * type is fired. Arguments to the fire method are passed to the callback+ * function.+ *+ * @return {wv.events} this object useful for chaining.+ */+ self.on = function(event, callback) {+ var listeners = events[event];+ if ( !listeners ) {+ listeners = [];+ events[event] = listeners;+ }+ listeners.push(callback);+ return self;+ };++ /**+ * Unregisters a listener for an event.+ *+ * @method off+ *+ * @param {string} event Type of event to unregister for.+ *+ * @param {function} callback Function that was previously registered for+ * this type of event. If this function has not been registered, this+ * method does nothing.+ *+ * @return {wv.events} this object useful for chaining.+ */+ self.off = function(event, callback) {+ var listeners = events[event];+ if ( listeners ) {+ _.pull(listeners, callback);+ }+ return self;+ };++ /**+ * Notifies all listeners of an event.+ *+ * @method trigger+ *+ * @param {string} event Type of event to fire. If no listeners are+ * registered for this event, this method does nothing.+ *+ * @param {any} [arguments]* Additional arguments to pass back to the+ * function of each listener.+ *+ * @return {wv.events} this object useful for chaining.+ */+ self.trigger = function(event) {+ var listeners = events[event];+ if ( !listeners ) {+ return;+ }+ var eventArguments = Array.prototype.slice.call(arguments, 1);+ _.each(events[event], function(listener) {+ listener.apply(self, eventArguments);+ });+ return self;+ };++ return self;+};\ No newline at end of filediff --git a/src/js/wv.ui.js b/src/js/wv.ui.jsnew file mode 100644index 000000000..1e46418fe--- /dev/null+++ b/src/js/wv.ui.js@@ -0,0 +1,71 @@+/*+ * 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.+ */++/**+ * @module wv+ */+var wv = wv || {};++/**+ * UI utilities+ *+ * @class wv.ui+ * @static+ */+wv.ui = wv.ui || (function() {++ var self = {};++ /**+ * General error handler.+ *+ * Displays a dialog box with the error message. If an exception object+ * is passed in, its contents will be printed to the console.+ *+ * @method error+ * @static+ *+ * @param {string} message Message to display to the end user.+ * @param {exception} cause The exception object that caused the error+ */+ self.error = function(message, cause) {+ if ( cause ) {+ console.error(cause);+ } else {+ console.error(message);+ }++ if ( window.YAHOO && window.YAHOO.widget &&+ window.YAHOO.widget.Panel ) {+ o = new YAHOO.widget.Panel("WVerror", {+ width: "300px",+ zIndex: 1020,+ visible: false,+ constraintoviewport: true+ });+ o.setHeader('Warning');+ o.setBody("An unexpected error has occurred.<br/><br/>" + message ++ "<br/><br/>Please reload the page and try again. If you " ++ "continue to have problems, contact us at " ++ "<a href='mailto:[email protected]'>" ++ "[email protected]</a>");+ o.render(document.body);+ o.show();+ o.center();+ o.hideEvent.subscribe(function(i) {+ setTimeout(function() {o.destroy();}, 25);+ });+ }+ };++ return self;++})();\ No newline at end of filediff --git a/src/js/wv.util.js b/src/js/wv.util.jsindex 273265a65..2973a915d 100644--- a/src/js/wv.util.js+++ b/src/js/wv.util.js@@ -15,7 +15,7 @@var wv = wv || {};/**- * General utilities.+ * General utilities** @class wv.util* @static@@ -25,35 +25,38 @@ wv.util = wv.util || (function() {var self = {};/**- * Converts a query string into an object representation. For example,- * the following query string:+ * Creates an object representation of a query string.*- * foo=a&format=image%2Fpng+ * For example, for the given query string, ``"?foo=a&bar=b"``, the+ * following object is returned:*- * converts to the following object:+ * {+ * foo: "a",+ * bar: "b"+ * }*- * { foo: "a", format: "image/png" }+ * Escaped values in the query string are decoded. For example ``%2f``+ * becomes ``/``.** @method fromQueryString* @static- * @param {String} queryString the string to convert to an object- * @return {Object} keys and values from the query string. Values are- * decoded as needed. If the query string is falsy, an empty object- * is returned.+ * @param {string} queryString the query string to convert to an+ * object.+ * @return {object} object representation of the query string.*/self.fromQueryString = function(queryString) {if ( !queryString ) {return {};}- if ( queryString.substring(0, 1) === "?" ) {+ if ( queryString[0] === "?" ) {queryString = queryString.substring(1);}- var result = {};- var kvps = queryString.split("&");- _.each(kvps, function(kvp) {- kv = kvp.split("=");- result[kv[0]] = decodeURIComponent(kv[1]);- });+ var parameters = queryString.split("&");+ result = {};+ for ( var i = 0; i < parameters.length; i++ ) {+ var fields = parameters[i].split("=");+ result[fields[0]] = decodeURIComponent(fields[1]);+ }return result;};@@ -96,6 +99,93 @@ wv.util = wv.util || (function() {return "?" + parts.join("&");};+ /**+ * Parses a UTC ISO 8601 date.+ *+ * @method parseDateISO+ * @static+ * @param str {string} Date to parse in the form of ``YYYY-MM-DD``.+ * @return {Date} converted string as a date object, throws an exception if+ * the string is invalid+ */+ self.parseDateUTC = function(str) {+ var d = new Date(Date.parse(str + "T00:00Z"));+ if ( _.isNaN(d.getTime()) ) {+ throw new Error("Invalid date: " + str);+ }+ return d;+ };++ /**+ * Sets a date to UTC midnight.+ *+ * @method clearTimeUTC+ * @static+ * @param date {Date} date to set the UTC hours, minutes, and seconds+ * to zero.+ * @return {Date} the date object+ */+ self.clearTimeUTC = function(date) {+ date.setUTCHours(0);+ date.setUTCMinutes(0);+ date.setUTCSeconds(0);+ date.setUTCMilliseconds(0);+ return date;+ };++ /**+ * Gets the current time. Use this instead of the Date methods to allow+ * debugging alternate "now" times.+ *+ * @method now+ * @static+ * @return {Date} The current time or an overriden value.+ */+ self.now = function() {+ return new Date();+ };++ /**+ * Gets the current day. Use this instead of the Date methods to allow+ * debugging alternate "now" times.+ *+ * @method today+ * @static+ * @return {Date} The current time with the UTC hours, minutes, and seconds+ * fields set to zero or an overriden value.+ */+ self.today = function() {+ return self.clearTimeUTC(new Date());+ };++ /**+ * General error handler.+ *+ * This function delegates to+ * {{#crossLink "wv.ui/error:method"}}wv.ui.error{{/crossLink}}. For+ * custom error handling, replace this function.+ *+ * @method error+ * @static+ * @param {string} message Message to display to the end user.+ * @param {exception} cause The exception object that caused the error+ */+ self.error = function(message, cause) {+ wv.ui.error(message, cause);+ };++ /**+ * General warning handler.+ *+ * Prints the messages to the console.+ *+ * @method warn+ * @static+ * @param {object*} messages Messages to display to the end user.+ */+ self.warn = ( console && console.warn && console.warn.bind ) ?+ console.warn.bind(console) : function () {};+return self;})();diff --git a/test/wv.events-test.js b/test/wv.events-test.jsnew file mode 100644index 000000000..467b9bff9--- /dev/null+++ b/test/wv.events-test.js@@ -0,0 +1,38 @@+/*+ * 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.+ */++buster.testCase("wv.events", {++ events: null,++ setUp: function() {+ this.events = wv.events();+ },++ "Triggers an event": function() {+ var listener1 = this.stub();+ var listener2 = this.stub();+ this.events.on("test", listener1);+ this.events.on("test", listener2);+ this.events.trigger("test", "a", 2);+ buster.assert.calledWith(listener1, "a", 2);+ buster.assert.calledWith(listener2, "a", 2);+ },++ "Removes listener": function() {+ var listener = this.stub();+ this.events.on("test", listener);+ this.events.trigger("test");+ this.events.off("test", listener);+ this.events.trigger("test");+ buster.assert.calledOnce(listener);+ }+});diff --git a/test/wv.util-test.js b/test/wv.util-test.jsindex c41300a83..2799ad0ad 100644--- a/test/wv.util-test.js+++ b/test/wv.util-test.js@@ -11,19 +11,25 @@buster.testCase("wv.util", {- "fromQueryString: Converts string to object": function() {- var query = wv.util.fromQueryString("foo=a&bar=b");- buster.assert.equals(query.foo, "a");- buster.assert.equals(query.bar, "b");+ "fromQueryString: Empty object when there is no query string": function() {+ buster.assert.equals(wv.util.fromQueryString(""), {});},- "fromQueryString: Empty object when there is no string": function() {- buster.assert.equals(wv.util.fromQueryString(), {});+ "fromQueryString: Values are parsed correctly": function() {+ var x = wv.util.fromQueryString("?foo=a&bar=b");+ buster.assert.equals("a", x.foo);+ buster.assert.equals("b", x.bar);},- "fromQueryString: Decodes value": function() {- var query = wv.util.fromQueryString("format=image%2fpng");- buster.assert.equals(query.format, "image/png");+ "fromQueryString: Does not need a leading ?": function() {+ var x = wv.util.fromQueryString("foo=a&bar=b");+ buster.assert.equals("a", x.foo);+ buster.assert.equals("b", x.bar);+ },++ "fromQueryString: Escaped values are converted": function() {+ var x = wv.util.fromQueryString("foo=image%2fjpeg");+ buster.assert.equals("image/jpeg", x.foo);},"toQueryString: Empty string with no values": function() {@@ -49,4 +55,32 @@ buster.testCase("wv.util", {var qs = wv.util.toQueryString({format: "/image/png/"}, ["%2f"]);buster.assert.equals(qs, "?format=/image/png/");},++ "parseDateUTC: Parses valid date": function() {+ var d = wv.util.parseDateUTC("2013-03-15");+ buster.assert.equals(2013, d.getUTCFullYear());+ buster.assert.equals(2, d.getUTCMonth());+ buster.assert.equals(15, d.getUTCDate());+ buster.assert.equals(0, d.getUTCHours());+ buster.assert.equals(0, d.getUTCMinutes());+ buster.assert.equals(0, d.getUTCSeconds());+ },++ "parseDateUTC: Exception on invalid date": function() {+ buster.assert.exception(function() {+ wv.util.parseDateUTC("x");+ });+ },++ "clearTimeUTC: Time set to UTC midnight": function() {+ var d = new Date(2013, 02, 15, 12, 34, 56, 789);+ wv.util.clearTimeUTC(d);+ buster.assert.equals(2013, d.getUTCFullYear());+ buster.assert.equals(2, d.getUTCMonth());+ buster.assert.equals(15, d.getUTCDate());+ buster.assert.equals(0, d.getUTCHours());+ buster.assert.equals(0, d.getUTCMinutes());+ buster.assert.equals(0, d.getUTCSeconds());+ }+});