Interactive interface for browsing global, full-resolution satellite imagery
Data download no longer crashes when an invalid product is passed in via permalink
Mike McGann3ef714cd9701
diff --git a/src/js/Worldview/DataDownload/Model.js b/src/js/Worldview/DataDownload/Model.jsindex a8a461d5c..aa675cc60 100644--- a/src/js/Worldview/DataDownload/Model.js+++ b/src/js/Worldview/DataDownload/Model.js@@ -1,10 +1,10 @@/** NASA Worldview- *+ ** This code was originally developed at NASA/Goddard Space Flight Center for- * the Earth Science Data and Information System (ESDIS) project.+ * the Earth Science Data and Information System (ESDIS) project.*- * Copyright (C) 2013 United States Government as represented by the+ * Copyright (C) 2013 United States Government as represented by the* Administrator of the National Aeronautics and Space Administration.* All Rights Reserved.*/@@ -13,16 +13,16 @@ Worldview.namespace("DataDownload");/*** Data download model.- *+ ** @module Worldview.DataDownload* @class Model* @constructor* @param config config*/Worldview.DataDownload.Model = function(config) {-+var log = Logging.getLogger("Worldview.DataDownload");-+var NO_PRODUCT_ID = "__NO_PRODUCT";var NO_PRODUCT = {name: "Not available for download <span>(?)</span>",@@ -37,27 +37,27 @@ Worldview.DataDownload.Model = function(config) {};var queryExecuting = false;var nextQuery = null;-+var ns = Worldview.DataDownload;-- var self = {};++ var self = {};/*** Fired when the data download mode is activated.- *+ ** @event EVENT_ACTIVATE* @final*/self.EVENT_ACTIVATE = "activate";-+/*** Fired when the data download mode is deactivated.- *+ ** @event EVENT_ACTIVATE* @final*/self.EVENT_DEACTIVATE = "deactivate";-+self.EVENT_PRODUCT_SELECT = "productSelect";self.EVENT_LAYER_UPDATE = "layerUpdate";self.EVENT_QUERY = "query";@@ -67,64 +67,70 @@ Worldview.DataDownload.Model = function(config) {self.EVENT_QUERY_TIMEOUT = "queryTimeout";self.EVENT_GRANULE_SELECT = "granuleSelect";self.EVENT_GRANULE_UNSELECT = "granuleUnselect";-+/*** Indicates if data download mode is active.- *+ ** @attribute active {boolean}* @default false* @readOnly*/self.active = false;-+/*** Handler for events fired by this class.- *+ ** @attribute events {Events}* @readOnly* @type Events*/self.events = Worldview.Events();-+self.selectedProduct = null;self.selectedGranules = {};self.layers = [];self.prefer = "science";-+self.granules = [];self.projection = null;self.crs = null;self.time = null;-+var init = function() {};-+/*** Activates data download mode. If the mode is already active, this method* does nothing.- *+ ** @method activate- */+ */self.activate = function(productName) {if ( !self.active ) {- self.active = true;- self.events.trigger(self.EVENT_ACTIVATE);- if ( productName ) {- self.selectProduct(productName);- } else if ( !self.selectedProduct ) {- self.selectProduct(findAvailableProduct());- } else {- self.events.trigger(self.EVENT_PRODUCT_SELECT,- self.selectedProduct);- query();+ try {+ self.active = true;+ self.events.trigger(self.EVENT_ACTIVATE);+ if ( productName ) {+ self.selectProduct(productName);+ } else if ( !self.selectedProduct ) {+ self.selectProduct(findAvailableProduct());+ } else {+ self.events.trigger(self.EVENT_PRODUCT_SELECT,+ self.selectedProduct);+ query();+ }+ } catch ( error ) {+ self.active = false;+ self.selectedProduct = null;+ throw error;}}};-+/**- * Deactivates data download mode. If the mode is not already active, this+ * Deactivates data download mode. If the mode is not already active, this* method does nothing.- *+ ** @method deactivate*/self.deactivate = function() {@@ -133,11 +139,11 @@ Worldview.DataDownload.Model = function(config) {self.events.trigger(self.EVENT_DEACTIVATE);}};-+/*** Toggles the current mode of data download. DeEVENT_ACTIVATEs if already* active. EVENT_ACTIVATEs if already inactive.- *+ ** @method toggleMode*/self.toggleMode = function() {@@ -147,7 +153,7 @@ Worldview.DataDownload.Model = function(config) {self.activate();}};-+self.groupByProducts = function() {var products = {};$.each(self.layers, function(index, layer) {@@ -167,7 +173,7 @@ Worldview.DataDownload.Model = function(config) {categories: { All: 1 }});});-+// Add not available to the end if it exists by removing it and// re-addingif ( products["__NO_PRODUCT"] ) {@@ -177,32 +183,32 @@ Worldview.DataDownload.Model = function(config) {}return products;};-+self.getProductsString = function() {var parts = [];var products = self.groupByProducts();$.each(products, function(key, product) {- var layers = [];+ var layers = [];$.each(product.items, function(index, item) {- layers.push(item.value);+ layers.push(item.value);});parts.push(key + "," + layers.join(","));});- return parts.join("~");+ return parts.join("~");};-+self.selectProduct = function(productName) {if ( self.selectedProduct === productName ) {return;}self.selectedProduct = productName;-+if ( self.active ) {- self.events.trigger(self.EVENT_PRODUCT_SELECT, self.selectedProduct);+ self.events.trigger(self.EVENT_PRODUCT_SELECT, self.selectedProduct);query();}};-+self.update = function(newState) {var oldState = state;state = newState;@@ -212,66 +218,66 @@ Worldview.DataDownload.Model = function(config) {if ( oldState.crs !== state.crs ) {updateProjection();}- if ( !oldState.time ||+ if ( !oldState.time ||oldState.time.getTime() !== state.time.getTime() ) {self.time = state.time;query();}};-+self.selectGranule = function(granule) {self.selectedGranules[granule.id] = granule;- self.events.trigger(self.EVENT_GRANULE_SELECT, granule);+ self.events.trigger(self.EVENT_GRANULE_SELECT, granule);};-+self.unselectGranule = function(granule) {delete self.selectedGranules[granule.id];- self.events.trigger(self.EVENT_GRANULE_UNSELECT, granule);+ self.events.trigger(self.EVENT_GRANULE_UNSELECT, granule);};-+self.isSelected = function(granule) {var selected = false;$.each(self.selectedGranules, function(index, selection) {if ( granule.id === selection.id ) {selected = true;- }+ }});- return selected;+ return selected;};-+self.getSelectionSize = function() {var totalSize = 0;var sizeValid = true;$.each(self.selectedGranules, function(index, granule) {if ( sizeValid && granule.granule_size ) {- totalSize += parseFloat(granule.granule_size);+ totalSize += parseFloat(granule.granule_size);} else {sizeValid = false;}});if ( sizeValid ) {return totalSize;- }+ }};-+self.getSelectionCounts = function() {counts = {};$.each(self.layers, function(index, layer) {if ( layer.product ) {- counts[layer.product] = 0;+ counts[layer.product] = 0;}});$.each(self.selectedGranules, function(index, granule) {counts[granule.product] ++;});- return counts;+ return counts;};-+self.setPreference = function(preference) {self.prefer = preference;query();};-+var query = function() {if ( !self.active ) {return;@@ -288,10 +294,10 @@ Worldview.DataDownload.Model = function(config) {if ( !productConfig ) {throw Error("Product not defined: " + self.selectedProduct);}-- var handlerFactory =++ var handlerFactory =Worldview.DataDownload.Handler.getByName(productConfig.handler);-+var handler = handlerFactory(config, self);handler.events.on("query", function() {self.events.trigger(self.EVENT_QUERY);@@ -307,7 +313,7 @@ Worldview.DataDownload.Model = function(config) {}).on("error", function(textStatus, errorThrown) {queryExecuting = false;if ( self.active ) {- self.events.trigger(self.EVENT_QUERY_ERROR, textStatus,+ self.events.trigger(self.EVENT_QUERY_ERROR, textStatus,errorThrown);}}).on("timeout", function() {@@ -318,7 +324,7 @@ Worldview.DataDownload.Model = function(config) {});executeQuery(handler);};-+var executeQuery = function(handler) {if ( !queryExecuting ) {try {@@ -330,9 +336,9 @@ Worldview.DataDownload.Model = function(config) {}} else {nextQuery = handler;- }+ }};-+var updateLayers = function() {if ( !state.layers ) {return;@@ -352,8 +358,8 @@ Worldview.DataDownload.Model = function(config) {});if ( productName === self.selectedProduct ) {foundSelected = true;- }- });+ }+ });if ( !foundSelected ) {self.selectProduct(null);}@@ -361,17 +367,17 @@ Worldview.DataDownload.Model = function(config) {if ( self.active && !foundSelected ) {self.selectProduct(findAvailableProduct());}-- // If a layer was removed and the product no longer exists,++ // If a layer was removed and the product no longer exists,// remove any selected items in that productvar products = self.groupByProducts();$.each(self.selectedGranules, function(index, selected) {if ( !products[selected.product] ) {self.unselectGranule(selected);- }- });+ }+ });};-+var updateProjection = function() {if ( !state.crs ) {return;@@ -380,10 +386,10 @@ Worldview.DataDownload.Model = function(config) {self.crs = state.crs;query();};-+var findAvailableProduct = function() {var foundProduct = null;-+// Find the top most layer that has a product entry in ECHOfor ( var i = state.layers.length - 1; i >= 0; i-- ) {var layerName = state.layers[i];@@ -393,7 +399,7 @@ Worldview.DataDownload.Model = function(config) {}return foundProduct;};-+init();- return self;+ return self;};\ No newline at end of filediff --git a/src/js/Worldview/Widget/DataDownload.js b/src/js/Worldview/Widget/DataDownload.jsindex 404983993..a168f13dd 100644--- a/src/js/Worldview/Widget/DataDownload.js+++ b/src/js/Worldview/Widget/DataDownload.js@@ -1,10 +1,10 @@/** NASA Worldview- *+ ** This code was originally developed at NASA/Goddard Space Flight Center for- * the Earth Science Data and Information System (ESDIS) project.+ * the Earth Science Data and Information System (ESDIS) project.*- * Copyright (C) 2013 United States Government as represented by the+ * Copyright (C) 2013 United States Government as represented by the* Administrator of the National Aeronautics and Space Administration.* All Rights Reserved.*/@@ -13,15 +13,15 @@ Worldview.namespace("Widget");/*** Handles UI interactions with the Data Download model.- *+ ** @module Worldview.Widget* @class DataDownload* @constructor- *+ ** @param model {Worldview.DataDownload.Model} FIXME- *+ ** @param config Worldview config- *+ ** @param spec.selector {string} jQuery selector for where the mode activation* button should be rendered.*/@@ -29,22 +29,22 @@ Worldview.Widget.DataDownload = function(config, spec) {var log = Logging.getLogger("Worldview.DataDownload");//Logging.debug("Worldview.DataDownload");-+var HTML_WIDGET_INACTIVE = "<img src='images/camera.png'></img>";var HTML_WIDGET_ACTIVE = "<img src='images/cameraon.png'></img>";-+var queryActive = false;var list = null;- var model = spec.model;+ var model = spec.model;var mapController = null;var selectionListPanel = null;var downloadListPanel = null;var lastResults = null;-+var self = {};self.containerId = "DataDownload";-- var init = function() {++ var init = function() {model.events.on("activate", onActivate).on("deactivate", onDeactivate)@@ -57,12 +57,12 @@ Worldview.Widget.DataDownload = function(config, spec) {.on("queryTimeout", onQueryTimeout).on("granuleSelect", updateSelection).on("granuleUnselect", updateSelection);-+REGISTRY.register(self.containerId, self);- REGISTRY.markComponentReady(self.containerId);- self.updateComponent();- };-+ REGISTRY.markComponentReady(self.containerId);+ self.updateComponent();+ };+self.updateComponent = function(queryString) {try {model.update(REGISTRY.getState());@@ -70,7 +70,7 @@ Worldview.Widget.DataDownload = function(config, spec) {Worldview.error("Internal error", error);}};-+self.getValue = function() {if ( model.active ) {return "dataDownload=" + model.selectedProduct;@@ -78,24 +78,29 @@ Worldview.Widget.DataDownload = function(config, spec) {return "";}};-+self.setValue = function(value) {throw new Error("Unsupported: setValue");};-+self.loadFromQuery = function(queryString) {var query = Worldview.queryStringToObject(queryString);if ( query.dataDownload ) {- var state = REGISTRY.getState(queryString);- model.activate(query.dataDownload);- }+ try {+ var state = REGISTRY.getState(queryString);+ model.activate(query.dataDownload);+ } catch ( error ) {+ log.warn("Invalid data download parameter: " + error);+ model.activate();+ }+ }};-+self.render = function() {var tabs_height = $(".ui-tabs-nav").outerHeight(true);$(spec.selector).addClass('bank');- $(spec.selector).height($(spec.selector).parent().outerHeight() - tabs_height);-+ $(spec.selector).height($(spec.selector).parent().outerHeight() - tabs_height);+list = new SOTE.widget.List(self.containerId, {config: config,data: [],@@ -104,7 +109,7 @@ Worldview.Widget.DataDownload = function(config, spec) {filter: true,search: false,action: {- text: "Download",+ text: "Download",callback: function() { showDownloadList(); }},selectableCategories: {@@ -115,7 +120,7 @@ Worldview.Widget.DataDownload = function(config, spec) {args: self});};-+self.onViewChange = function(map) {if ( !model.active || queryActive || !lastResults ) {return;@@ -134,7 +139,7 @@ Worldview.Widget.DataDownload = function(config, spec) {inView = true;return true;}- }+ }});log.debug("hasCentroids", hasCentroids, "inView", inView);if ( hasCentroids && !inView ) {@@ -143,22 +148,22 @@ Worldview.Widget.DataDownload = function(config, spec) {Worldview.Indicator.hide();}};-+var toggleMode = function() {- model.toggleMode();+ model.toggleMode();};-+var onActivate = function() {log.debug("activate");-+if ( !mapController ) {- mapController =+ mapController =Worldview.DataDownload.MapController(model, spec.maps, config);}updateSelection();onLayerUpdate();};-+var onDeactivate = function() {log.debug("deactivate");Worldview.Indicator.hide();@@ -169,12 +174,12 @@ Worldview.Widget.DataDownload = function(config, spec) {downloadListPanel.hide();}};-+var onProductSelect = function(product) {log.debug("selectProduct", product);list.selectCategory(product);};-+var onLayerUpdate = function() {if ( !model.active ) {return;@@ -183,19 +188,19 @@ Worldview.Widget.DataDownload = function(config, spec) {list.selected = list.unserialize(model.getProductsString())[1];list.update();};-+var onQuery = function() {queryActive = true;log.debug("query");Worldview.Indicator.searching();- if ( selectionListPanel ) {- selectionListPanel.hide();+ if ( selectionListPanel ) {+ selectionListPanel.hide();}if ( downloadListPanel ) {downloadListPanel.hide();}};-+var onQueryResults = function(results) {queryActive = false;lastResults = results;@@ -205,8 +210,8 @@ Worldview.Widget.DataDownload = function(config, spec) {Worldview.Indicator.noData();} else {if ( results.meta.showList ) {- selectionListPanel =- Worldview.DataDownload.SelectionListPanel(model, results);+ selectionListPanel =+ Worldview.DataDownload.SelectionListPanel(model, results);selectionListPanel.show();} else {if ( selectionListPanel ) {@@ -216,23 +221,23 @@ Worldview.Widget.DataDownload = function(config, spec) {}}};-+var onQueryCancel = function() {queryActive = false;log.debug("queryCancel");Worldview.Indicator.hide();};-+var onQueryError = function(status, error) {queryActive = false;log.debug("queryError", status, error);Worldview.Indicator.hide();if ( status !== "abort" ) {- Worldview.notify("Unable to search at this time. Please try " ++ Worldview.notify("Unable to search at this time. Please try " +"again later");}};-+var onQueryTimeout = function() {queryActive = false;log.debug("queryTimeout");@@ -242,7 +247,7 @@ Worldview.Widget.DataDownload = function(config, spec) {"connectivity issue. Please try again later.");};-+var updateSelection = function() {var selected = Worldview.size(model.selectedGranules);if ( selected > 0 ) {@@ -258,39 +263,39 @@ Worldview.Widget.DataDownload = function(config, spec) {list.setButtonEnabled(false);list.setActionButtonText("No Data Selected");}-+var counts = model.getSelectionCounts();$.each(counts, function(productId, count) {- list.setCategoryDynamicText(productId, "" + count + " selected");- });+ list.setCategoryDynamicText(productId, "" + count + " selected");+ });if ( downloadListPanel && downloadListPanel.visible() ) {downloadListPanel.show();- }+ }};-+var showDownloadList = function() {if ( selectionListPanel ) {selectionListPanel.setVisible(false);}if ( !downloadListPanel ) {- downloadListPanel =+ downloadListPanel =Worldview.DataDownload.DownloadListPanel(config, model);downloadListPanel.events.on("close", function() {if ( selectionListPanel ) {selectionListPanel.setVisible(true);- }+ }});}- downloadListPanel.show();+ downloadListPanel.show();};-+var updatePreference = function(event, ui) {model.setPreference(event.target.value);};-+init();return self;-+};