Interactive interface for browsing global, full-resolution satellite imagery
Modularize wv.ui.js
Jo Spraguefecb92791869
diff --git a/web/js/wv.feedback.js b/web/js/feedback.jssimilarity index 58%rename from web/js/wv.feedback.jsrename to web/js/feedback.jsindex b5bb6c1d4..8eb478c86 100644--- a/web/js/wv.feedback.js+++ b/web/js/feedback.js@@ -1,5 +1,13 @@-var wv = wv || {};-wv.feedback = wv.feedback || (function () {+/* global feedback */++/* The feedback global variable is defined by+ * https://fbm.earthdata.nasa.gov/for/worldview/feedback.js+ * which is included via config.scripts in web/config/wv.json+*/++import util from './util/util.js';++export const feedbackModal = (function () {var self = {};var feedbackInit = false;@@ -8,7 +16,7 @@ wv.feedback = wv.feedback || (function () {'mailto:@MAIL@?subject=Feedback for @LONG_NAME@ tool');$element.click(function (event) {- if (!wv.util.browser.small && window.feedback) {+ if (!util.browser.small && window.feedback) {event.preventDefault();if (!feedbackInit) {feedback.init({diff --git a/web/js/ui/wv.ui.indicator.js b/web/js/ui/indicator.jssimilarity index 81%rename from web/js/ui/wv.ui.indicator.jsrename to web/js/ui/indicator.jsindex cbe495a24..f70c446bc 100644--- a/web/js/ui/wv.ui.indicator.js+++ b/web/js/ui/indicator.js@@ -1,6 +1,12 @@-var wv = wv || {};-wv.ui = wv.ui || {};-wv.ui.indicator = wv.ui.indicator || (function () {+import $ from 'jquery';+import uniqueId from 'lodash/uniqueId';+import isString from 'lodash/isString';+import each from 'lodash/each';+import remove from 'lodash/remove';+import isEmpty from 'lodash/isEmpty';+import last from 'lodash/last';++export const indicator = (function () {var self = {};var $indicator;@@ -24,7 +30,7 @@ wv.ui.indicator = wv.ui.indicator || (function () {self.show = function (message, icon) {self._show(message, icon);- var id = _.uniqueId();+ var id = uniqueId();self.active.push({id: id,message: message,@@ -34,18 +40,18 @@ wv.ui.indicator = wv.ui.indicator || (function () {};self.hide = function (hides) {- if (_.isString(hides)) {+ if (isString(hides)) {hides = [hides];}- _.each(hides, function (id) {- _.remove(self.active, {+ each(hides, function (id) {+ remove(self.active, {id: id});});- if (_.isEmpty(self.active)) {+ if (isEmpty(self.active)) {self._hide();} else {- var def = _.last(self.active);+ var def = last(self.active);self._show(def.message, def.icon);}};diff --git a/web/js/ui/wv.ui.info.js b/web/js/ui/info.jssimilarity index 90%rename from web/js/ui/wv.ui.info.jsrename to web/js/ui/info.jsindex 082406f9a..740456ad8 100644--- a/web/js/ui/wv.ui.info.js+++ b/web/js/ui/info.js@@ -1,10 +1,9 @@-/**- * @module wv.ui- */-var wv = wv || {};-wv.ui = wv.ui || {};+import $ from 'jquery';+import wvui from './ui.js';+import util from '../util/util.js';+import feedbackModal from '../feedback.js';-wv.ui.info = wv.ui.info || function (ui, config) {+export function info (ui, config) {var selector = '#wv-info-button';var $button = $('<input />').attr('type', 'checkbox')@@ -28,7 +27,7 @@ wv.ui.info = wv.ui.info || function (ui, config) {$button.click(function (event) {event.stopPropagation();- wv.ui.close();+ wvui.close();var checked = $('#wv-info-button-check').prop('checked');if (checked) {@@ -37,7 +36,7 @@ wv.ui.info = wv.ui.info || function (ui, config) {});var show = function () {- var $menu = wv.ui.getMenu()+ var $menu = wvui.getMenu().attr('id', 'wv-info-menu');var $alerts, $new;var whatsNewUrl = 'https://github.com/nasa-gibs/worldview/releases';@@ -75,7 +74,7 @@ wv.ui.info = wv.ui.info || function (ui, config) {$menu.append($menuItems);$menuItems.menu();- wv.ui.positionMenu($menuItems, {+ wvui.positionMenu($menuItems, {my: 'left top',at: 'left bottom + 5',of: $label@@ -83,10 +82,10 @@ wv.ui.info = wv.ui.info || function (ui, config) {$menuItems.hide();$about.click(function () {- if (wv.util.browser.small || wv.util.browser.touchDevice) {+ if (util.browser.small || util.browser.touchDevice) {window.open('brand/pages/about.html?v=@BUILD_NONCE@', '_blank');} else {- wv.ui.getDialog()+ wvui.getDialog().dialog({title: 'About',width: 625,@@ -106,7 +105,7 @@ wv.ui.info = wv.ui.info || function (ui, config) {window.open('https://github.com/nasa-gibs/worldview', '_blank');});- wv.feedback.decorate($feedback.find('a'));+ feedbackModal.decorate($feedback.find('a'));$tour.click(function () {ui.tour.start();diff --git a/web/js/ui/mouse.js b/web/js/ui/mouse.jsnew file mode 100644index 000000000..a4c8a4d4c--- /dev/null+++ b/web/js/ui/mouse.js@@ -0,0 +1,111 @@+import util from '../util/util.js';++export const mouse = {+ click: function ($element, callback) {+ var self = {};+ self.sensitivity = 5; // pixels++ var startX, startY;++ var init = function () {+ $element.mousedown(mousedown);+ $element.mouseup(mouseup);+ };++ var mousedown = function (event) {+ startX = event.clientX;+ startY = event.clientY;+ };++ var mouseup = function (event) {+ if (withinClickDistance(event)) {+ callback.call(this);+ }+ };++ var withinClickDistance = function (event) {+ var targetX = event.clientX;+ var targetY = event.clientY;+ var distance = Math.sqrt(Math.pow(startX - targetX, 2) ++ Math.pow(startY - targetY, 2));+ return distance <= self.sensitivity;+ };++ init();+ return self;+ },+ wheel: function (element, ui, options) {+ options = options || {};++ var self = {};+ self.timeout = options.timeout || 100; // milliseconds+ self.threshold = options.threshold || 100; // delta units+ self.events = util.events();++ var delta = 0;+ var zoomed = false;+ var timer = null;+ var timeout = false;+ var lastEvent = null;++ var init = function () {+ element.on('zoom', wheel);+ };++ self.change = function (listener) {+ self.events.on('change', listener);+ return self;+ };++ var wheel = function () {+ var evt = d3.event.sourceEvent;+ if ((Math.abs(evt.deltaX) <= Math.abs(evt.deltaY)) && timeout === false) {+ lastEvent = evt;+ delta += evt.deltaY;+ if (!timer) {+ zoomed = false;+ }+ clearTimeout(timer);+ timer = setTimeout(end, self.timeout);+ update(evt);+ } else if ((Math.abs(evt.deltaX) >= Math.abs(evt.deltaY))) {+ if (ui.timeline.isCropped) {+ ui.timeline.pan.axis(d3.event);+ timeout = true;+ clearTimeout(timer);+ timer = setTimeout(function () {+ timeout = false;+ }, 500);+ }+ } else {+ if (ui.timeline.isCropped) {+ ui.timeline.pan.axis();+ }+ }+ };++ var update = function (event) {+ var change = Math.floor(Math.abs(delta) / self.threshold);+ if (change >= 1) {+ var sign = delta ? delta < 0 ? -1 : 1 : 0;+ self.events.trigger('change', sign * change, event);+ delta = delta % self.threshold;+ zoomed = true;+ }+ };++ var end = function () {+ timer = null;+ if (!zoomed) {+ var sign = delta ? delta < 0 ? -1 : 1 : 0;+ self.events.trigger('change', sign, lastEvent);+ }+ lastEvent = null;+ delta = 0;+ zoomed = false;+ };++ init();+ return self;+ }+};diff --git a/web/js/ui/ui.js b/web/js/ui/ui.jsindex d965ff407..99db788c6 100644--- a/web/js/ui/ui.js+++ b/web/js/ui/ui.js@@ -1,2 +1,259 @@-/* This file is just a stub to test the browserify build task */-export function wvui () {};+import $ from 'jquery';+import indicator from './indicator.js';+import info from './info.js';+import mouse from './mouse.js';++export const wvui = (function (self) {+ // Export other ui methods+ self.indicator = indicator;+ self.info = info;+ self.mouse = mouse;+ /**+ * 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 {Exception} cause The exception object that caused the error+ */+ self.error = function () {+ console.error.apply(console, arguments);++ self.notify(+ '<div class=\'error-header\'>' ++ '<i class=\'error-icon fa fa-exclamation-triangle fa-3x\'></i>' ++ 'An unexpected error has occurred' ++ '</div>' ++ '<div class=\'error-body\'>Please reload the page and try ' ++ 'again. If you continue to have problems, contact us at ' ++ '<a href=\'mailto:@MAIL@\'>' ++ '@MAIL@</a>' ++ '</div>', 'Error'+ );+ };++ /**+ * Displays a message to the end user in a dialog box.+ *+ * @method notify+ * @static+ *+ * @param {string} message The message to display to the user.+ *+ * @param [title="Notice"] {string} Title for the dialog box.+ */+ self.notify = function (message, title, width, callback) {+ var $dialog = self.getDialog();+ title = title || 'Notice';+ width = width || 300;+ $dialog.html(message)+ .dialog({+ title: title,+ width: width,+ minHeight: 1,+ height: 'auto',+ show: {+ effect: 'fade',+ duration: 400+ },+ hide: {+ effect: 'fade',+ duration: 200+ }+ })+ .on('dialogclose', function () {+ $(this)+ .off('dialogclose');+ if (callback) {+ callback();+ }+ });+ };++ self.alert = function (body, title, size, glyph, closeFn) {+ var $message = $('<span/>', {class: 'notify-message'});+ var $icon = $('<i/>', { class: 'fa fa-' + glyph + ' fa-1x', title: title });+ var $messageWrapper = $('<div/>').click(function () {+ self.notify(body, title, size);+ }).append($icon).append($message);+ var $close = $('<i/>', {class: 'fa fa-times fa-1x'}).click(closeFn);+ var $alert = $('<div/>').append($close).append($messageWrapper).dialog({+ autoOpen: false,+ resizable: false,+ height: 40,+ width: 420,+ draggable: false,+ show: {+ effect: 'fade',+ duration: 400+ },+ hide: {+ effect: 'fade',+ duration: 200+ },+ dialogClass: 'no-titlebar notify-alert'+ });+ $message.empty().append(title);+ return $alert;+ };++ /**+ * Asks the end user a yes or no question in a dialog box.+ *+ * @method ask+ * @static+ *+ * @param [spec.header="Notice"] {string} Header text to be displayed in+ * the dialog box.+ *+ * @param [spec.message="Are you sure?"] {string} Message text to be+ * displayed in the dialog box.+ *+ * @param [spec.okButton="OK"] {string} Text to be used in the no button.+ *+ * @param [spec.cancelButton="Cancel"] {string} Text to be used in the yes+ * button.+ *+ * @param [spec.onOk] {function} Function to execute when the OK button is+ * pressed. If not specified, the dialog box simply closes.+ *+ * @parma [spec.onCancel] {function} Function to execute when the Cancel+ * button is pressed. If not specified, the dialog box simply closes.+ */+ self.ask = function (spec) {+ var $dialog = self.getDialog('wv-dialog-ask');+ var cancelText = spec.cancelButton || 'Cancel';+ var okText = spec.okButton || 'OK';+ var buttons = {};+ buttons[cancelText] = function () {+ $(this)+ .dialog('close');+ if (spec.onCancel) {+ spec.onCancel();+ }+ };+ buttons[okText] = function () {+ $(this)+ .dialog('close');+ if (spec.onOk) {+ spec.onOk();+ }+ };+ $dialog.dialog({+ title: spec.header || 'Notice',+ resizable: false,+ modal: true,+ buttons: buttons+ })+ .html(spec.message)+ .on('dialogclose', function () {+ if (spec.onCancel) {+ spec.onCancel();+ }+ });+ };++ /**+ * Displays a message to the end user that the feature is not supported+ * in this web browser.+ *+ * @method unsupported+ * @static+ *+ * @param {String} [featureName] If specified, the message will state+ * "The <featureName> feature is not supported...". Otherwise it will+ * state "This feature..."+ */+ self.unsupported = function (featureName) {+ var prefix;+ if (!featureName) {+ prefix = 'This feature';+ } else {+ prefix = 'The ' + featureName + ' feature';+ }+ self.notify(prefix + ' is not supported with your web ' ++ 'browser. Upgrade or try again in a different browser.');+ };++ var getComponent = function (marker) {+ var $element = $('<div></div>')+ .addClass(marker);+ $('body')+ .append($element);+ return $element;+ };++ var closeComponent = function (marker, fnClose) {+ var selector = '.' + marker;+ var $element = $(selector);+ if ($element.length !== 0) {+ fnClose($element);+ }+ };++ var closeDialog = function ($element) {+ if ($element.length !== 0) {+ if ($element.dialog) {+ $element.dialog('close');+ }+ $element.remove();+ }+ };++ var closeMenu = function ($element) {+ if ($element.length !== 0) {+ $element.remove();+ }+ };++ self.close = function () {+ closeComponent('wv-dialog', closeDialog);+ closeComponent('wv-menu', closeMenu);+ };++ self.getDialog = function (marker) {+ self.close(marker);+ return getComponent(marker || 'wv-dialog', closeDialog);+ };++ self.getMenu = function (marker) {+ self.close();+ return getComponent(marker || 'wv-menu', closeMenu);+ };++ self.closeDialog = function () {+ self.close();+ };++ self.positionMenu = function ($menuItems, pos) {+ var position = function () {+ $menuItems.menu()+ .position(pos);+ };+ position();+ $(window)+ .resize(position);+ $menuItems.on('hide', function () {+ $(window)+ .off('resize', position);+ });+ };++ self.positionDialog = function ($dialog, pos) {+ var position = function () {+ $dialog.dialog('option', 'position', pos);+ };+ position();+ $(window)+ .resize(position);+ $dialog.on('dialogclose', function () {+ $(window)+ .off('resize', position);+ });+ };++ return self;+})();diff --git a/web/js/ui/wv.ui.js b/web/js/ui/wv.ui.jsdeleted file mode 100644index 5c41d4e38..000000000--- a/web/js/ui/wv.ui.js+++ /dev/null@@ -1,261 +0,0 @@-/**- * @module wv.ui- */-var wv = wv || {};--/**- * UI utilities- *- * @class wv.ui- * @static- */-wv.ui = (function (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 {Exception} cause The exception object that caused the error- */- self.error = function () {- console.error.apply(console, arguments);-- self.notify(- '<div class=\'error-header\'>' +- '<i class=\'error-icon fa fa-exclamation-triangle fa-3x\'></i>' +- 'An unexpected error has occurred' +- '</div>' +- '<div class=\'error-body\'>Please reload the page and try ' +- 'again. If you continue to have problems, contact us at ' +- '<a href=\'mailto:@MAIL@\'>' +- '@MAIL@</a>' +- '</div>', 'Error'- );- };-- /**- * Displays a message to the end user in a dialog box.- *- * @method notify- * @static- *- * @param {string} message The message to display to the user.- *- * @param [title="Notice"] {string} Title for the dialog box.- */- self.notify = function (message, title, width, callback) {- var $dialog = self.getDialog();- title = title || 'Notice';- width = width || 300;- $dialog.html(message)- .dialog({- title: title,- width: width,- minHeight: 1,- height: 'auto',- show: {- effect: 'fade',- duration: 400- },- hide: {- effect: 'fade',- duration: 200- }- })- .on('dialogclose', function () {- $(this)- .off('dialogclose');- if (callback) {- callback();- }- });- };-- self.alert = function (body, title, size, glyph, closeFn) {- var $message = $('<span/>', {class: 'notify-message'});- var $icon = $('<i/>', { class: 'fa fa-' + glyph + ' fa-1x', title: title });- var $messageWrapper = $('<div/>').click(function () {- self.notify(body, title, size);- }).append($icon).append($message);- var $close = $('<i/>', {class: 'fa fa-times fa-1x'}).click(closeFn);- var $alert = $('<div/>').append($close).append($messageWrapper).dialog({- autoOpen: false,- resizable: false,- height: 40,- width: 420,- draggable: false,- show: {- effect: 'fade',- duration: 400- },- hide: {- effect: 'fade',- duration: 200- },- dialogClass: 'no-titlebar notify-alert'- });- $message.empty().append(title);- return $alert;- };-- /**- * Asks the end user a yes or no question in a dialog box.- *- * @method ask- * @static- *- * @param [spec.header="Notice"] {string} Header text to be displayed in- * the dialog box.- *- * @param [spec.message="Are you sure?"] {string} Message text to be- * displayed in the dialog box.- *- * @param [spec.okButton="OK"] {string} Text to be used in the no button.- *- * @param [spec.cancelButton="Cancel"] {string} Text to be used in the yes- * button.- *- * @param [spec.onOk] {function} Function to execute when the OK button is- * pressed. If not specified, the dialog box simply closes.- *- * @parma [spec.onCancel] {function} Function to execute when the Cancel- * button is pressed. If not specified, the dialog box simply closes.- */- self.ask = function (spec) {- var $dialog = self.getDialog('wv-dialog-ask');- var cancelText = spec.cancelButton || 'Cancel';- var okText = spec.okButton || 'OK';- var buttons = {};- buttons[cancelText] = function () {- $(this)- .dialog('close');- if (spec.onCancel) {- spec.onCancel();- }- };- buttons[okText] = function () {- $(this)- .dialog('close');- if (spec.onOk) {- spec.onOk();- }- };- $dialog.dialog({- title: spec.header || 'Notice',- resizable: false,- modal: true,- buttons: buttons- })- .html(spec.message)- .on('dialogclose', function () {- if (spec.onCancel) {- spec.onCancel();- }- });- };-- /**- * Displays a message to the end user that the feature is not supported- * in this web browser.- *- * @method unsupported- * @static- *- * @param {String} [featureName] If specified, the message will state- * "The <featureName> feature is not supported...". Otherwise it will- * state "This feature..."- */- self.unsupported = function (featureName) {- var prefix;- if (!featureName) {- prefix = 'This feature';- } else {- prefix = 'The ' + featureName + ' feature';- }- wv.ui.notify(prefix + ' is not supported with your web ' +- 'browser. Upgrade or try again in a different browser.');- };-- var getComponent = function (marker) {- var $element = $('<div></div>')- .addClass(marker);- $('body')- .append($element);- return $element;- };-- var closeComponent = function (marker, fnClose) {- var selector = '.' + marker;- var $element = $(selector);- if ($element.length !== 0) {- fnClose($element);- }- };-- var closeDialog = function ($element) {- if ($element.length !== 0) {- if ($element.dialog) {- $element.dialog('close');- }- $element.remove();- }- };-- var closeMenu = function ($element) {- if ($element.length !== 0) {- $element.remove();- }- };-- self.close = function () {- closeComponent('wv-dialog', closeDialog);- closeComponent('wv-menu', closeMenu);- };-- self.getDialog = function (marker) {- self.close(marker);- return getComponent(marker || 'wv-dialog', closeDialog);- };-- self.getMenu = function (marker) {- self.close();- return getComponent(marker || 'wv-menu', closeMenu);- };-- self.closeDialog = function () {- self.close();- };-- self.positionMenu = function ($menuItems, pos) {- var position = function () {- $menuItems.menu()- .position(pos);- };- position();- $(window)- .resize(position);- $menuItems.on('hide', function () {- $(window)- .off('resize', position);- });- };-- self.positionDialog = function ($dialog, pos) {- var position = function () {- $dialog.dialog('option', 'position', pos);- };- position();- $(window)- .resize(position);- $dialog.on('dialogclose', function () {- $(window)- .off('resize', position);- });- };-- return self;-})(wv.ui = wv.ui || {});diff --git a/web/js/ui/wv.ui.mouse.js b/web/js/ui/wv.ui.mouse.jsdeleted file mode 100644index 38a3c72c0..000000000--- a/web/js/ui/wv.ui.mouse.js+++ /dev/null@@ -1,112 +0,0 @@-var wv = wv || {};-wv.ui = wv.ui || {};-wv.ui.mouse = wv.ui.mouse || {};--wv.ui.mouse.click = wv.ui.mouse.click || function ($element, callback) {- var self = {};- self.sensitivity = 5; // pixels-- var startX, startY;-- var init = function () {- $element.mousedown(mousedown);- $element.mouseup(mouseup);- };-- var mousedown = function (event) {- startX = event.clientX;- startY = event.clientY;- };-- var mouseup = function (event) {- if (withinClickDistance(event)) {- callback.call(this);- }- };-- var withinClickDistance = function (event) {- var targetX = event.clientX;- var targetY = event.clientY;- var distance = Math.sqrt(Math.pow(startX - targetX, 2) +- Math.pow(startY - targetY, 2));- return distance <= self.sensitivity;- };-- init();- return self;-};--wv.ui.mouse.wheel = wv.ui.mouse.wheel || function (element, ui, options) {- options = options || {};-- var self = {};- self.timeout = options.timeout || 100; // milliseconds- self.threshold = options.threshold || 100; // delta units- self.events = wv.util.events();-- var delta = 0;- var zoomed = false;- var timer = null;- var timeout = false;- var lastEvent = null;-- var init = function () {- element.on('zoom', wheel);- };-- self.change = function (listener) {- self.events.on('change', listener);- return self;- };-- var wheel = function () {- var evt = d3.event.sourceEvent;- if ((Math.abs(evt.deltaX) <= Math.abs(evt.deltaY)) && timeout === false) {- lastEvent = evt;- delta += evt.deltaY;- if (!timer) {- zoomed = false;- }- clearTimeout(timer);- timer = setTimeout(end, self.timeout);- update(evt);- } else if ((Math.abs(evt.deltaX) >= Math.abs(evt.deltaY))) {- if (ui.timeline.isCropped) {- ui.timeline.pan.axis(d3.event);- timeout = true;- clearTimeout(timer);- timer = setTimeout(function () {- timeout = false;- }, 500);- }- } else {- if (ui.timeline.isCropped) {- ui.timeline.pan.axis();- }- }- };-- var update = function (event) {- var change = Math.floor(Math.abs(delta) / self.threshold);- if (change >= 1) {- var sign = delta ? delta < 0 ? -1 : 1 : 0;- self.events.trigger('change', sign * change, event);- delta = delta % self.threshold;- zoomed = true;- }- };-- var end = function () {- timer = null;- if (!zoomed) {- var sign = delta ? delta < 0 ? -1 : 1 : 0;- self.events.trigger('change', sign, lastEvent);- }- lastEvent = null;- delta = 0;- zoomed = false;- };-- init();- return self;-};diff --git a/web/js/util/util.js b/web/js/util/util.jsindex 5690cd9d3..e4d627860 100644--- a/web/js/util/util.js+++ b/web/js/util/util.js@@ -1,5 +1,10 @@/* global ntptEventTag */+/* The ntptEventTag global variable is defined by+ * https://earthdata.nasa.gov/lib/ntpagetag.js+ * which is included via config.scripts in web/config/wv.json+*/+import $ from 'jquery';import each from 'lodash/each';import isNull from 'lodash/isNull';