Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Custom palettes now working

Mike McGann4aeb9d0a5e68
diff --git a/etc/deploy/wv.js.json b/etc/deploy/wv.js.json
index 8439dcdfe..5f2df9d39 100644
--- a/etc/deploy/wv.js.json
+++ b/etc/deploy/wv.js.json
@@ -30,6 +30,7 @@
"src/js/layers/wv.layers.add.js",
"src/js/palettes/wv.palettes.js",
+ "src/js/palettes/wv.palettes.custom.js",
"src/js/palettes/wv.palettes.legend.js",
"src/js/palettes/wv.palettes.model.js",
@@ -39,6 +40,7 @@
"src/js/legacy/wv.legacy.switch_.js",
"src/js/legacy/wv.legacy.bank.js",
"src/js/legacy/wv.legacy.date.js",
+ "src/js/legacy/wv.legacy.palettes.js",
"src/js/debug/wv.debug.gibs.js",
@@ -49,11 +51,6 @@
"src/js/Worldview/Indicator.js",
"src/js/Worldview/AjaxJoin.js",
- "src/js/Worldview/Palette.js",
- "src/js/Worldview/Palette/ColorBar.js",
- "src/js/Worldview/Palette/Palette.js",
- "src/js/Worldview/Palette/PaletteSelector.js",
-
"src/js/Worldview/Map.js",
"src/js/Worldview/Map/CanvasTile.js",
"src/js/Worldview/Map/MapSet.js",
@@ -118,7 +115,6 @@
"src/js/Worldview/DataDownload/BulkDownloadPage.js",
"src/js/Worldview/Widget/Map.js",
- "src/js/Worldview/Widget/Palette.js",
"src/js/Worldview/Widget/WorldviewMap.js",
"src/js/Worldview/Widget/CRS.js",
"src/js/Worldview/Widget/DataDownload.js",
diff --git a/src/js/Worldview/Palette.js b/src/js/Worldview/Palette.js
deleted file mode 100644
index 13ec64d77..000000000
--- a/src/js/Worldview/Palette.js
+++ /dev/null
@@ -1,469 +0,0 @@
-/*
- * 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 United States Government as represented by the
- * Administrator of the National Aeronautics and Space Administration.
- * All Rights Reserved.
- */
-
-/**
- * Namespace: Worldview.Palette
- * Visualization of science data.
- */
-Worldview.namespace("Palette");
-
-$(function() {
-
- // This namespace
- var ns = Worldview.Palette;
-
- //-------------------------------------------------------------------------
- // Public
- //-------------------------------------------------------------------------
-
- /**
- * Property: checkerboard
- * A canvas pattern of a gray checkerboard used to denote transparency.
- */
- ns.checkerboard = null;
-
- /**
- * Function: toIndexedLookup
- * Converts a <Palette> to a <IndexedLookup>. The lookup table is generated
- * for a certain number of equally spaced bins. Each bin is assigned
- * a distance along the range and its color is determined by the stop
- * locations in the palette. The first bin is always at 0% and the last
- * bin is always at 100%.
- *
- * If the type is gradient, the color value will be interpolated from the
- * stops that it fits within. If it is before the first stop, it will be
- * the color of the first stop. If it is after the last stop, it will be
- * the color of the last stop. Colors can be interpolated in the HSL
- * color space (by default or if type is "hsl") or in the RGB color space
- * (if the type is "rgb"). If the type is "solid", the color will be equal
- * to the stop it rounds down to.
- *
- * Alpha values in the stop definitions are ignored. If the bin is not
- * within the min and max values, the alpha is set to zero, otherwise the
- * alpha is set to 0xff.
- *
- * Parameters:
- * bins - Number of entries that the output lookup table should contain
- * palette - The <Palette> to convert
- *
- * Returns:
- * A <IndexedLookup>.
- */
- ns.toIndexedLookup = function(bins, palette, binStops) {
- var stops = palette.stops;
- var lut = [];
-
- // Each bin in the sequence must be at the current stop or later.
- // Save the current index and not iterate through each time.
- var currentStop = 0;
-
- var min = palette.min || 0;
- var max = ( palette.max === undefined ) ? 1 : palette.max;
-
- for ( var bin = 0; bin < bins; bin++ ) {
- // Percentange this bin is located at along the range.
- var distance;
- if ( binStops ) {
- // Select the middle of the bin when trying to fit this in
- var nextStop = ( bin < bins - 1 ) ? binStops[bin + 1] : 1.0;
- distance = binStops[bin] + ((nextStop - binStops[bin]) / 2.0);
- } else {
- distance = bin / (bins - 1);
- }
-
- // If the current distance is greater than the current stop,
- // keep advancing until that is not true.
- for ( var i = currentStop; i < stops.length; i++ ) {
- if ( distance <= stops[i].at ) {
- break;
- }
- }
- currentStop = i;
-
- // The bin is between these two stops
- var end = stops[Worldview.clampIndex(stops, i)];
- var begin = stops[Worldview.clampIndex(stops, i-1)];
-
- // Find out how far this bin in between the stops
- var segmentLength = end.at - begin.at;
- var segmentDistance = (segmentLength !== 0) ?
- (distance - begin.at) / segmentLength : 0;
-
- // Within the cutoffs?
- if ( distance < min || distance > max ) {
- lut.push({r: 0, b: 0, g: 0, a: 0});
- } else if ( palette.type === "solid" ) {
- // For solid colors, always pick the color of the beginning
- // stop unless we are at the very end.
- if ( segmentDistance < 1 ) {
- lut.push({
- r: begin.r,
- g: begin.g,
- b: begin.b,
- a: 0xff
- });
- } else {
- lut.push({
- r: end.r,
- g: end.g,
- b: end.b,
- a: 0xff
- });
- }
- } else if ( palette.interpolate === "rgb" ) {
- lut.push(ns.rgbInterpolate(segmentDistance, begin, end));
- } else { // interpolate === "hsl"
- lut.push(ns.hslInterpolate(segmentDistance, begin, end));
- }
- }
- return lut;
- };
-
- /**
- * Function: toColorLookup
- * Maps an indexed lookup to a color-to-color lookup. This is used to take
- * the palette definition of a rendered product to create a lookup table
- * to map the original colors to a new set of colors.
- *
- * Parameters:
- * indexed - An <IndexedLookup>
- * stops - The original color values as an array of <StopRBGA>
- *
- * Returns:
- * A <ColorLookup>
- */
- ns.toColorLookup = function(indexed, stops) {
- map = {};
- $.each(stops, function(index, stop) {
- var key = stop.r + "," + stop.g + "," + stop.b + "," + stop.a;
- map[key] = indexed[index];
- });
- return map;
- };
-
- /**
- * Function: rgbInterpolate
- * Interpolates a color value between two other colors via the RGB color
- * space. Alpha values are ignored and are always set to 0xff.
- *
- * Parameters:
- * percent - The distance between the two colors as a percentage in the
- * range of [0.0, 1.0].
- * begin - The beginning <ColorRGBA>
- * end - The ending <ColorRGBA>
- *
- * Returns:
- * The interpolated color as a <ColorRGBA>.
- */
- ns.rgbInterpolate = function(percent, begin, end) {
- return {
- r: Math.round(begin.r + (percent * (end.r - begin.r))),
- g: Math.round(begin.g + (percent * (end.g - begin.g))),
- b: Math.round(begin.b + (percent * (end.b - begin.b))),
- a: 0xff
- };
- };
-
- /**
- * Function: hslInterpolate
- * Interpolates a color value between two other colors via the HSL
- * color space. Alpha values are ignored and are always set to 0xff.
- *
- * Parameters:
- * percent - The distance between the two colors as a percentage in the
- * range of [0.0, 1.0].
- * rgbBegin - The beginning <ColorRGBA>
- * rgbEnd - The ending <ColorRGBA>
- *
- * Returns:
- * The interpoalted color as a <ColorRGBA>.
- */
- ns.hslInterpolate = function(percent, rgbBegin, rgbEnd) {
- var hslBegin = ns.rgb2hsl(rgbBegin);
- var hslEnd = ns.rgb2hsl(rgbEnd);
-
- // Hue is a circle. Traverse the shortest route. If the distance
- // between the two endpoints is more than half the circle, add one
- // to the smallest value.
- var distance = Math.abs(hslBegin.h - hslEnd.h);
- if ( distance > 0.5 ) {
- if ( hslBegin.h < hslEnd.h ) {
- hslBegin.h += 1.0;
- } else {
- hslEnd.h += 1.0;
- }
- }
- var h = hslBegin.h + (percent * (hslEnd.h - hslBegin.h));
- var s = hslBegin.s + (percent * (hslEnd.s - hslBegin.s));
- var l = hslBegin.l + (percent * (hslEnd.l - hslBegin.l));
-
- // Normalize back to the range of 0 - 1
- if ( h > 1.0 ) {
- h -= 1.0;
- }
- return ns.hsl2rgb({h: h, s: s, l: l});
- };
-
- /**
- * Function: rgb2hsl
- * Converts an RGB color value to HSL. Conversion formula
- * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
- *
- * See Also:
- * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
- *
- * Parameters:
- * color - The <ColorRGBA> value to convert.
- *
- * Returns:
- * The converted <ColorHSL> value.
- *
- * Example:
- * (begin code)
- * >>> Worldview.Palette.rgb2hsl({r: 10, g: 20, b: 30})
- * Object { h=0.5833333333333334, s=0.5, l=0.0784313725490196}
- * (end code)
- */
- ns.rgb2hsl = function(color) {
- var r = color.r;
- var g = color.g;
- var b = color.b;
-
- r /= 255, g /= 255, b /= 255;
- var max = Math.max(r, g, b), min = Math.min(r, g, b);
- var h, s, l = (max + min) / 2;
-
- if ( max == min ) {
- h = s = 0; // achromatic
- } else {
- var d = max - min;
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
- switch ( max ) {
- case r: h = (g - b) / d + (g < b ? 6 : 0); break;
- case g: h = (b - r) / d + 2; break;
- case b: h = (r - g) / d + 4; break;
- }
- h /= 6;
- }
-
- return {h: h, s: s, l: l};
- };
-
- /**
- * Function: hsl2rgb
- * Converts an HSL color value to RGB. Conversion formula
- * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
- *
- * See Also:
- * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
- *
- * Parameters:
- * color - The <ColorHSL> to convert.
- *
- * Returns:
- * The converted <ColorRGBA> value.
- *
- * Example:
- * (begin code)
- * >>> Worldview.Palette.hsl2rgb({h: 0.5833, s: 0.5, l: 0.078})
- * Object { r=10, g=20, b=30 }
- * (end code)
- */
- ns.hsl2rgb = function(color) {
- var h = color.h;
- var s = color.s;
- var l = color.l;
-
- var r, g, b;
-
- if ( s == 0 ) {
- r = g = b = l; // achromatic
- } else {
- function hue2rgb(p, q, t) {
- if(t < 0) t += 1;
- if(t > 1) t -= 1;
- if(t < 1/6) return p + (q - p) * 6 * t;
- if(t < 1/2) return q;
- if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
- return p;
- }
-
- var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
- var p = 2 * l - q;
- r = hue2rgb(p, q, h + 1/3);
- g = hue2rgb(p, q, h);
- b = hue2rgb(p, q, h - 1/3);
- }
-
- return {
- r: Math.round(r * 255),
- g: Math.round(g * 255),
- b: Math.round(b * 255),
- a: 0xff
- };
- };
-
-
- //-------------------------------------------------------------------------
- // Private
- //-------------------------------------------------------------------------
-
- // Draws the default checkboard pattern
- var drawCheckerboard = function() {
- var size = 2;
-
- var canvas = document.createElement("canvas");
- canvas.width = size * 2;
- canvas.height = size * 2;
-
- var g = canvas.getContext("2d");
-
- g.fillStyle = "rgb(102, 102, 102)";
- g.fillRect(0, 0, size, size);
- g.fillRect(size, size, size, size);
-
- g.fillStyle = "rgb(153, 153, 153)";
- g.fillRect(0, size, size, size);
- g.fillRect(size, 0, size, size);
-
- ns.checkerboard = g.createPattern(canvas, "repeat");
- };
-
- // Static initialization
- var init = function() {
- drawCheckerboard();
- };
-
- init();
-});
-
-/**
- * Class: Worldview.Palette.ColorRGBA
- *
- * Represents a color in the RGBA color space.
- *
- * Example:
- * For a yellow color
- *
- * (begin code)
- * var color = { r: 0xff, g: 0xff, b: 0x00, a: 0xff }
- * (end code)
- *
- * Property: r
- * The red color value in the range of [0, 0xff].
- *
- * Property: g
- * The green color value in the range of [0, 0xff].
- *
- * Property: b
- * The blue color value in the range of [0, 0xff].
- *
- * Property: a
- * The alpha color value in the range of [0, 0xff]. Zero is transparent, 0xff
- * is opaque.
- */
-
-/**
- * Class: Worldview.Palette.ColorHSL
- *
- * Represents a color in the HSL color space.
- *
- * Example:
- * For a red color
- *
- * (begin code)
- * var color = { h: 0x00, s: 0xff, l: 0xff }
- * (end code)
- *
- * Property: h
- * The hue value in the range of [0.0, 1.0].
- *
- * Property: s
- * The saturation value in the range of [0.0, 1.0].
- *
- * Property: l
- * The lightness value in the range of [0.0, 1.0].
- */
-
-/**
- * Class: Worldview.Palette.IndexedLookup
- *
- * Indexed based color lookup table.
- *
- * Example:
- * (begin code)
- * var lookup = {
- * id: "my_lookup",
- * name: "My Lookup",
- * table: [
- * { r: 0x01, g: 0x02, b: 0x03, a: 0x04 },
- * { r: 0x05, g: 0x06, b: 0x07, a: 0x08 }
- * ]
- * };
- * (end code)
- *
- * Property: id
- * Identifier for this lookup (optional).
- *
- * Property: name
- * Descriptive name for this lookup (optional).
- *
- * Property: table
- * Array that maps index values to <ColorRGBA> objects. For example, a value of
- * 2 should use the color found at table[2].
- */
-
-/**
- * Class: Worldview.Palette.ColorLookup
- *
- * Lookup from one color value to another color value.
- *
- * Example:
- * (begin code)
- * var lookup = {
- * "0,0,255,255": { r: 0xff, g: 0x00, b: 0x00, a: 0xff }
- *
- * (end code)
- *
- * Each property in the object is a string containing integer red, green,
- * blue, and alpha values separated by commas. Each property contains contains
- * a <ColorRGBA> value to map.
- */
-
-/**
- * Class: Worldview.Palette.StopRGBA
- *
- * Defines a color value at a percentage along a color range.
- *
- * Example:
- * For a red color stop at 40% along the range
- *
- * (begin code)
- * var stop = { at: 0.4, r: 0xff, g: 0x00, b: 0x00, a:0x00 };
- * (end code)
- *
- * Property: at
- * The percentage along the color range where this stop is located in the range
- * of [0.0, 1.0].
- *
- * Property: r
- * The red color value in the range of [0, 0xff].
- *
- * Property: g
- * The green color value in the range of [0, 0xff].
- *
- * Property: b
- * The blue color value in the range of [0, 0xff].
- *
- * Property: a
- * a - The alpha color value in the range of [0, 0xff]. Zero is transparent,
- * 0xff is opaque.
- */
diff --git a/src/js/Worldview/Palette/ColorBar.js b/src/js/Worldview/Palette/ColorBar.js
deleted file mode 100644
index 9ee85755e..000000000
--- a/src/js/Worldview/Palette/ColorBar.js
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * 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 United States Government as represented by the
- * Administrator of the National Aeronautics and Space Administration.
- * All Rights Reserved.
- */
-
-Worldview.namespace("Palette");
-
-/**
- * Class: Worldview.Palette.ColorBar
- * Renders a <Palette> in a canvas. Color transitions are placed along the
- * x axis.
- *
- * Example:
- * (begin code)
- * var colorBar = Worldview.Palette.ColorBar({
- * selector: "#colorBarCanvas",
- * bins: 10,
- * palette: {
- * stops: [
- * { at: 0.0, r: 0x00, g: 0x00, b: 0x00 },
- * { at: 1.0, r: 0x00, g: 0x00, b: 0x00 }
- * ]
- * }
- * });
- * (end code)
- *
- * will render in the canvas as shown in the following image
- * (see black_to_white_color_bar.png)
- *
- */
-Worldview.Palette.ColorBar = function(spec) {
-
- // This namespace
- var ns = Worldview.Palette;
-
- // The canvas to render in as a jQuery object
- var $canvas = null;
-
- // The canvas to render in as a canvas object
- var canvas = null;
-
- // Graphics drawing context for the canvas
- var g = null;
-
- //-------------------------------------------------------------------------
- // Public
- //-------------------------------------------------------------------------
- var self = {};
-
- /**
- * Property: background
- *
- * Canvas pattern to use as the background. If not specified,
- * <checkerboard> is used.
- */
- self.background = spec.background || ns.checkerboard;
-
- /**
- * Property: bins
- *
- * Maximum number of discrete color values. For example, if this value
- * is ten, only ten color values will be shown, evenly spaced through
- * the canvas. If not specified, this value is 255.
- */
- self.bins = spec.bins || 255;
-
- self.stops = spec.stops || null;
-
- /**
- * Property: palette
- *
- * The <Palette> or <IndexedLookup> to render in the canvas. If not specified, this
- * value is null and no palette will be rendered.
- */
- self.palette = spec.palette || null;
-
- /**
- * Constructor: ColorBar
- * Creates a new instance.
- *
- * Parameters:
- * spec - Accepts all properties as an associative array.
- * spec.selector - jQuery selector for the canvas to render in.
- *
- * Throws:
- * An error if the canvas element could not be found.
- */
- var init = function() {
- if ( spec.canvas ) {
- canvas = spec.canvas;
- } else if ( spec.selector ) {
- $canvas = $(spec.selector);
- if ( $canvas.length === 0 ) {
- throw new Error("No such canvas element (" + spec.selector +
- ") for the ColorBar");
- }
- canvas = $canvas.get(0);
-
- // The canavs dimensions does not equal the element dimensions! Make
- // them the same or scaling will occur.
-
- // tj, 5/16/13, edit: the element client width and height will be 0 in the new
- // product picker design because the bank is not visible when colorbars are rendered
- /*canvas.width = $canvas.width();
- canvas.height = $canvas.height();*/
- } else {
- canvas = docuemnt.createElement("canvas");
- canvas.width = spec.width;
- canvas.height = spec.height;
- }
-
- g = canvas.getContext("2d");
- self.redraw();
- };
-
- /**
- * Method: redraw
- * Redraws the palette in the canvas. If palette is set to null, this
- * method only draws the background.
- */
- self.redraw = function() {
- drawBackground();
- if ( !self.palette ) {
- return;
- }
-
- // If a lookup is provided, just use that. Otherwise, convert the
- // palette definition to a lookup table.
- var lut = ( self.palette.table )
- ? self.palette.table
- : ns.toIndexedLookup(self.bins, self.palette, self.stops);
-
- for ( var bin = 0; bin < self.bins; bin++ ) {
- var left;
- var width;
-
- // If the bins are and not evenly spaced out, use the provided
- // stops.
- if ( self.stops ) {
- var nextStop = ( bin < self.bins - 1 )
- ? self.stops[bin + 1] : 1.0;
- left = Math.floor(self.stops[bin] * canvas.width);
- width = Math.ceil(canvas.width * (nextStop - self.stops[bin]));
- } else {
- left = Math.floor(bin * (canvas.width / self.bins));
- width = Math.ceil(canvas.width / self.bins);
- }
- if ( width < 1 ) {
- width = 1;
- }
- // Only draw if there is a lookup entry and it is not completely
- // transparent
- if ( lut[bin] && lut[bin].a !== 0 ) {
- // Always draw with no transparency
- g.fillStyle = "rgba(" +
- [lut[bin].r, lut[bin].g, lut[bin].b, 0xff].join() + ")";
- g.fillRect(left, 0, width, canvas.height);
- }
- }
- };
-
- self.toImage = function() {
- return canvas.toDataURL("image/png");
- };
-
- //-------------------------------------------------------------------------
- // Private
- //-------------------------------------------------------------------------
- var drawBackground = function() {
- g.fillStyle = self.background;
- g.fillRect(0, 0, canvas.width, canvas.height);
- };
-
- // Call constructor
- init();
-
- // Return public variables
- return self;
-};
diff --git a/src/js/Worldview/Palette/Palette.js b/src/js/Worldview/Palette/Palette.js
deleted file mode 100644
index d407dc582..000000000
--- a/src/js/Worldview/Palette/Palette.js
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * 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 United States Government as represented by the
- * Administrator of the National Aeronautics and Space Administration.
- * All Rights Reserved.
- */
-
-Worldview.namespace("Palette");
-
-/**
- * Class: Worldview.Palette.Palette
- * Color palette defined by color stops. Each stop is located along the
- * available range as a percentage between zero and one.
- *
- * A gradient is applied between each stop. To use solid colors instead, set
- * the type property to solid. Gradients usually traverse through the HSL color
- * space. Set the interpolate property to rgb to go through the RGB color space
- * instead.
- *
- * Example:
- * To define a palette that is a gradient from black to white:
- * (begin code)
- * var palette = Worldview.Palette.Palette({
- * stops: [
- * { at: 0.0, r: 0x00, g: 0x00, b: 0x00 },
- * { at: 1.0, r: 0xff, g: 0xff, b: 0xff },
- * ]
- * });
- * (end code)
- *
- * To define a palette that is a gradient from red to green but passing
- * through blue half way through:
- * (begin code)
- * var palette = Worldview.Palette.Palette({
- * stops: [
- * { at: 0.0, r: 0xff, g: 0x00, b: 0x00 },
- * { at: 0.5, r: 0x00, g: 0xff, b: 0xff },
- * { at: 1.0, r: 0x00, g: 0xff, b: 0x00 },
- * ]
- * });
- * (end code)
- *
- * Constructor: Palette
- * Creates a new instance.
- *
- * Parameters:
- * spec - Accepts all properties as an associative array.
- */
-Worldview.Palette.Palette = function(spec) {
-
- return {
- /**
- * Property: id
- * Identifier for this palette.
- */
- id: spec.id || null,
-
- /**
- * Property: name
- * Name for this palette.
- */
- name: spec.name || null,
-
- /**
- * Property: min
- * Minimum display value. Values below this percentage will be
- * transparent. Valid range is [0.0, 1.0].
- */
- min: spec.min || 0.0,
-
- /**
- * Property: max
- * Maximum display value. Values above this percentage will be
- * transparent. Valid range is [0.0, 1.0].
- */
- max: ( spec.max === undefined ) ? 1.0 : spec.max,
-
- /**
- * Property: type
- * Method used to compute colors between stops. Either "gradient" or
- * "solid". If set to "gradient", colors between each stop will
- * gradually shift color. If set to "solid", color changes
- * are absolute at each stop. If not specified, this value is set to
- * "gradient".
- */
- type: spec.type || "gradient",
-
- /**
- * Property: interpolate
- * Interpolation used for gradients. If set to "hsl", interpolation is
- * done in the HSL color space. If set to "rbg", interpolation is done
- * in the RGB color space. If not specified, this valueis set to "hsl".
- * This property has no effect if the type is not set to "gradient".
- */
- interpolate: spec.interpolate || "hsl",
-
- source: spec.source || "stock",
-
- /**
- * Property: stops
- * Array of color stops of type <StopRGBA>. If not specified, this
- * value is set to an emtpy array.
- */
- stops: spec.stops || [],
- };
-
-};
-
diff --git a/src/js/Worldview/Palette/PaletteSelector.js b/src/js/Worldview/Palette/PaletteSelector.js
deleted file mode 100644
index 8d79aa778..000000000
--- a/src/js/Worldview/Palette/PaletteSelector.js
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * 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 United States Government as represented by the
- * Administrator of the National Aeronautics and Space Administration.
- * All Rights Reserved.
- */
-
-Worldview.namespace("Palette");
-
-/**
- * Class: Worldview.Palette.PaletteSelector
- * Combo box to select a color palette. This uses
- * <msDropDown at http://www.marghoobsuleman.com/jquery-image-dropdown> to
- * render the combo box.
- *
- * Constructor: PaletteSelector
- * Creates a new instance.
- *
- * Parameters:
- * spec.selector - The jQuery selector of the container to render in.
- * spec.palettes - Array of <Palettes> to display in the combo box.
- */
-Worldview.Palette.PaletteSelector = function(spec) {
-
- // This namespace
- var ns = Worldview.Palette;
-
- // Public exports
- var self = {};
-
- // The palettes to display in the combo box
- var palettes = spec.palettes;
-
- // Container to render in
- var $container;
-
- var dropDown;
-
- // Listeners for selection events
- var listeners = [];
-
- //-------------------------------------------------------------------------
- // Public
- //-------------------------------------------------------------------------
-
- var init = function() {
- $container = $(spec.selector);
- if ( $container.length === 0 ) {
- throw new Error("Container not found for PaletteSelector: " +
- spec.selector);
- }
-
- var items = [];
- for ( var i = 0; i < palettes.length; i++ ) {
- var palette = palettes[i];
- var item = {
- text: palette.name || "&nbsp;",
- description: palette.description || "",
- value: i,
- image: palette.image
- };
- items.push(item);
- }
-
- dropDown = $container.msDropDown({
- byJson: {
- name: "Palettes",
- data: items,
- width: 225,
- },
- visibleRows: 5,
- rowHeight: 17
- }).data("dd");
-
- $container.on("change", function() {
- fireSelectionEvent(palettes[dropDown.selectedIndex]);
- });
- };
-
- /**
- * Method: select
- * Selects an item in the selector by index. A selection event is fired
- * after the value changes.
- *
- * Parameters:
- * index - The index of the value to set.
- */
- self.select = function(index) {
- dropDown.set("selectedIndex", index);
-
- // Change event not fired on set, do it manually
- fireSelectionEvent(palettes[dropDown.selectedIndex]);
- };
-
- /**
- * Method: addSelectionListener
- * Adds a listener for selection events.
- *
- * Parameters:
- * listener - Function to call when an item is selected. The function is
- * called with one parameter, the <Palette> that was selected. If no
- * palette was selected, the passed in parameter is undefined.
- */
- self.addSelectionListener = function(listener) {
- listeners.push(listener);
- };
-
- //-------------------------------------------------------------------------
- // Private
- //-------------------------------------------------------------------------
-
- // Notifies all listeners on a selection event.
- var fireSelectionEvent = function(item) {
- for ( var i = 0; i < listeners.length; i++ ) {
- var listener = listeners[i];
- listener(item);
- }
- };
-
- init();
- return self;
-};
-
-
-
diff --git a/src/js/Worldview/Widget/Palette.js b/src/js/Worldview/Widget/Palette.js
deleted file mode 100644
index c8093baa9..000000000
--- a/src/js/Worldview/Widget/Palette.js
+++ /dev/null
@@ -1,299 +0,0 @@
-/*
- * 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 United States Government as represented by the
- * Administrator of the National Aeronautics and Space Administration.
- * All Rights Reserved.
- */
-
-Worldview.namespace("Widget");
-
-Worldview.Widget.Palette = function(config, model, spec) {
-
- var self = {};
- var containerId = "palettes";
- var value = "";
- var dialog = null;
- var dialogForLayer = null;
-
- self.config = config;
- self.active = {};
- self.inactive = {};
- self.alignTo = spec.alignTo;
- self.noRestore = false;
-
- var init = function() {
- //Logging.debug("Worldview.Widget.Palette");
- if ( REGISTRY ) {
- REGISTRY.register(containerId, self);
- } else {
- throw new Error("Cannot register paletteWidget, REGISTRY " +
- "not found");
- }
- REGISTRY.markComponentReady(containerId);
- };
-
- self.getPalette = function(layer) {
- var name = self.active[layer];
- if ( !name || !config.palettes[name] ) {
- name = config.layers[layer].rendered;
- }
- if ( !name ) {
- return null;
- }
- return config.palettes[name];
- };
-
- self.setValue = function(v) {
- if ( v === undefined ) {
- return;
- }
- var wasActive = self.active;
- self.active = {};
- if ( v !== "" ) {
- var parts = v.split("~");
- $.each(parts, function(index, part) {
- var segments = part.split(",");
- var layerName = segments[0];
- var paletteName = segments[1];
- self.active[layerName] = paletteName;
- self.inactive[layerName] = paletteName;
- model.events.trigger("palette", self.getPalette(layerName),
- layerName);
- });
- } else {
- $.each(wasActive, function(layerName) {
- model.events.trigger("palette", self.getPalette(layerName),
- layerName);
- });
- }
- REGISTRY.fire(self);
- };
-
- self.getValue = function() {
- try {
- var parts = [];
- $.each(self.active, function(product, palette) {
- if ( palette ) {
- parts.push(product + "," + palette);
- }
- });
- var qs = "";
- if ( parts.length > 0 ) {
- qs = containerId + "=" + parts.join("~");
- }
- return qs;
- } catch ( error ) {
- Worldview.error("Unable to update", error);
- }
- };
-
- self.updateComponent = function(queryString) {
- if ( REGISTRY.isLoadingQuery ) {
- return;
- }
- queryString = queryString || "";
- var changed = false;
- try {
- var state = REGISTRY.getState(queryString);
- $.each(self.active, function(layer, palette) {
- if ( $.inArray(layer, state.layers) < 0 ) {
- delete self.active[layer];
- changed = true;
- }
- });
- $.each(self.inactive, function(layer, palette) {
- if ( !self.noRestore &&
- $.inArray(layer, state.layers) >= 0 &&
- !self.active[layer] ) {
- self.active[layer] = self.inactive[layer];
- changed = true;
- }
- });
- if ( dialogForLayer &&
- $.inArray(dialogForLayer, state.layers) < 0 ) {
- dialog.hide();
- }
- if ( changed ) {
- REGISTRY.fire(self);
- }
- } catch ( error ) {
- Worldview.error("Unable to update state", error);
- }
- };
-
- self.loadFromQuery = function(queryString) {
- var query = Worldview.queryStringToObject(queryString);
- if ( query.palettes && !wv.palette.supported ) {
- wv.ui.unsupported("custom palette");
- } else {
- self.setValue(query.palettes);
- }
- };
-
- self.displaySelector = function(layer) {
- if ( dialog ) {
- dialog.hide();
- setTimeout(function() {
- showSelector(layer);
- }, 6);
- } else {
- showSelector(layer);
- }
- };
-
- self.parse = function(queryString, object) {
- object.palettes = {};
- palettes = Worldview.extractFromQuery("palettes", queryString);
- object.palettesString = palettes;
-
- if ( !palettes ) {
- return object;
- }
- var definitions = palettes.split("~");
- $.each(definitions, function(index, definition) {
- var items = definition.split(",");
- var layer = items[0];
- var palette = items[1];
- object.palettes[layer] = palette;
- });
- return object;
- };
-
- var showSelector = function(layer) {
- var layerConfig = self.config.layers[layer];
- var properties = {
- width: "245px",
- height: "265px",
- visible: false,
- autofillheight: "body",
- constraintoviewport: true ,
- zindex: 210
- };
- if ( self.alignTo ) {
- var $element = $(self.alignTo);
- properties.x = Math.ceil($element.offset().left +
- $element.width() + 20);
- properties.y = Math.ceil($element.offset().top);
- }
-
- dialog = new YAHOO.widget.Panel("palette-selector-dialog",
- properties);
- dialogForLayer = layer;
- dialog.setHeader("Select palette");
- dialog.setBody([
- "<div class='product-name'>" + layerConfig.name + "</div>",
- "<div class='product-description'>" +
- layerConfig.description +
- "</div>" +
- "<div id='palette-selector'></div>"
- ].join("\n"));
- dialog.hideEvent.subscribe(function(i) {
- setTimeout(function() {
- dialog.destroy();
- dialog = null;
- dialogForLayer = null;
- }, 5);
- });
- dialog.render(document.body);
-
- var palettes = [];
- var canvas = document.createElement("canvas");
- canvas.width = 100;
- canvas.height = 14;
-
- // The default palette the layer is rendered in
- var renderedName = layerConfig.rendered;
-
- var renderedPalette = $.extend(true, {},
- config.palettes[renderedName]);
- var renderedColorBar = Worldview.Palette.ColorBar({
- canvas: canvas,
- palette: renderedPalette,
- bins: layerConfig.bins,
- stops: layerConfig.stops
- });
- renderedPalette.name = "Default";
- renderedPalette.image = renderedColorBar.toImage();
- palettes.push(renderedPalette);
-
- var activePalette = self.active[layer];
- var selected = null;
-
- // Palettes for the drop down, place the recommended ones first
- if ( layerConfig.recommendedPalettes ) {
- $.each(layerConfig.recommendedPalettes, function(index, name) {
- var palette = config.palettes[name];
- var colorBar = Worldview.Palette.ColorBar({
- canvas: canvas,
- palette: palette,
- bins: layerConfig.bins,
- stops: layerConfig.stops
- });
- palette.image = colorBar.toImage();
- palettes.push(palette);
- });
- }
-
- $.each(self.config.paletteOrder, function(index, name) {
- if ( $.inArray(name, layerConfig.recommendedPalettes) >= 0 ) {
- return;
- }
- var p = self.config.palettes[name];
-
- // Skip this palette if configuration says to exclude
- if ( $.inArray(name, layerConfig.excludePalettes) >= 0 ) {
- return;
- }
- if ( !p ) {
- console.error("No such palette: " + name);
- return;
- }
- if ( p.source === "stock" ) {
- var palette = $.extend(true, {}, p);
- var colorBar = Worldview.Palette.ColorBar({
- canvas: canvas,
- palette: palette,
- bins: layerConfig.bins,
- stops: layerConfig.stops
- });
- palette.image = colorBar.toImage();
- palettes.push(palette);
- }
- });
-
- $.each(palettes, function(index, palette) {
- if ( palette.id === activePalette ) {
- selected = index;
- }
- });
-
- var paletteSelector = Worldview.Palette.PaletteSelector({
- selector: "#palette-selector",
- palettes: palettes
- });
- if ( selected !== null ) {
- paletteSelector.select(selected);
- }
-
- paletteSelector.addSelectionListener(function(palette) {
- if ( palette.source === "rendered" ) {
- delete self.active[layer];
- delete self.inactive[layer];
- } else {
- self.active[layer] = palette.id;
- self.inactive[layer] = palette.id;
- }
- model.events.trigger("palette", palette, layer);
- REGISTRY.fire(self);
- });
-
- dialog.show();
- };
-
- init();
- return self;
-};
diff --git a/src/js/layers/wv.layers.active.js b/src/js/layers/wv.layers.active.js
index 00bf86c0d..b3cd595cb 100644
--- a/src/js/layers/wv.layers.active.js
+++ b/src/js/layers/wv.layers.active.js
@@ -171,11 +171,11 @@ wv.layers.active = wv.layers.active || function(models, config) {
var selector = ".wv-palette[data-layer='" +
encodeURIComponent(layer.id) + "']";
wv.palettes.legend({
- selector: selector,
- config: config,
- models: models,
- layer: layer
- });
+ selector: selector,
+ config: config,
+ models: models,
+ layer: layer
+ });
};
var adjustCategoryHeights = function() {
diff --git a/src/js/main.js b/src/js/main.js
index 61bc661d4..a7e6728eb 100644
--- a/src/js/main.js
+++ b/src/js/main.js
@@ -10,7 +10,16 @@ $(function() {// Initialize "static" vars
var storageEngine;
+ // FIXME: Make this more elegant
var init = function(config) {
+ try {
+ _init(config);
+ } catch ( error ) {
+ wv.util.error(error);
+ }
+ };
+
+ var _init = function(config) {
wv.config = config;
// Convert all parameters found in the query string to an object,
@@ -121,126 +130,133 @@ $(function() {// Initialize "static" vars
models.link.load();
models.proj.change = wv.proj.change(models);
- // Create widgets
- var ui = {};
- ui.proj = wv.proj.ui(models);
-
- ui.sidebar = wv.layers.sidebar(models);
- ui.activeLayers = wv.layers.active(models, config);
- ui.addLayers = wv.layers.add(models, config);
- ui.dateSliders = wv.date.sliders(models, config);
- ui.dateLabel = wv.date.label(models);
- ui.dateWheels = wv.date.wheels(models, config);
- var rubberBand = new SOTE.widget.RubberBand("camera", {
- icon: "images/camera.png",
- onicon: "images/cameraon.png",
- cropee: "map",
- mapWidget: map
- });
- var imageDownload = new SOTE.widget.ImageDownload("imagedownload", {
- baseLayer: "MODIS_Terra_CorrectedReflectance_TrueColor",
- alignTo: rubberBand,
- m: map,
- config: config
- });
- var crs = new Worldview.Widget.CRS(config);
-
- // collapse events if worldview is being loaded via permalink
- if(window.location.search) {
- eventsCollapsed = true;
- }
- /*
- var events = new SOTE.widget.Events("eventsHolder", {
- config: config,
- models: models,
- maps: map.maps,
- shouldCollapse: eventsCollapsed,
- lastVisit: lastVisitObj
- });
- */
- var dataDownload = Worldview.Widget.DataDownload(config, {
- selector: "#DataDownload",
- model: dataDownloadModel,
- maps: map.maps,
- });
- dataDownload.render();
-
- ui.link = wv.link.ui(models);
-
- $(window).resize(function() {
- if ($(window).width() < 720) {
- $('#productsHoldertabs li.first a').trigger('click');
- }
- });
-
- document.activeElement.blur();
- $("input").blur();
- $("#eventsHolder").hide();
- // Wirings
- ui.sidebar.events
- .on("dataDownloadSelect", function() {
- dataDownloadModel.activate();
- })
- .on("dataDownloadUnselect", function() {
- dataDownloadModel.deactivate();
- });
- dataDownloadModel.events
- .on("activate", function() {
- ui.sidebar.selectTab("download");
+ // FIXME: This is a hack for now. Will be cleaned up in 0.7
+ wv.palettes.startup(models, config).done(function() {
+
+ // FIXME: Hack to get the map to read palette info
+ map.updateComponent(models.link.toQueryString());
+
+ // Create widgets
+ var ui = {};
+ ui.proj = wv.proj.ui(models);
+
+ ui.sidebar = wv.layers.sidebar(models);
+ ui.activeLayers = wv.layers.active(models, config);
+ ui.addLayers = wv.layers.add(models, config);
+ ui.dateSliders = wv.date.sliders(models, config);
+ ui.dateLabel = wv.date.label(models);
+ ui.dateWheels = wv.date.wheels(models, config);
+ var rubberBand = new SOTE.widget.RubberBand("camera", {
+ icon: "images/camera.png",
+ onicon: "images/cameraon.png",
+ cropee: "map",
+ mapWidget: map
});
- map.maps.events
- .on("moveEnd", function(map) {
- dataDownload.onViewChange(map);
- })
- .on("zoomEnd", function(map) {
- dataDownload.onViewChange(map);
+ var imageDownload = new SOTE.widget.ImageDownload("imagedownload", {
+ baseLayer: "MODIS_Terra_CorrectedReflectance_TrueColor",
+ alignTo: rubberBand,
+ m: map,
+ config: config
});
- dataDownloadModel.events
- .on("queryResults", function() {
- dataDownload.onViewChange(map.maps.map);
+ var crs = new Worldview.Widget.CRS(config);
+
+ // collapse events if worldview is being loaded via permalink
+ if(window.location.search) {
+ eventsCollapsed = true;
+ }
+ /*
+ var events = new SOTE.widget.Events("eventsHolder", {
+ config: config,
+ models: models,
+ maps: map.maps,
+ shouldCollapse: eventsCollapsed,
+ lastVisit: lastVisitObj
+ });
+ */
+ var dataDownload = Worldview.Widget.DataDownload(config, {
+ selector: "#DataDownload",
+ model: dataDownloadModel,
+ maps: map.maps,
});
+ dataDownload.render();
- // Register event listeners
- REGISTRY.addEventListener("map", "imagedownload");
- REGISTRY.addEventListener("time",
- "map", "imagedownload", crs.containerId,
- dataDownload.containerId);
- REGISTRY.addEventListener("switch",
- "map", "products", "selectorbox", "imagedownload", "camera",
- crs.containerId, dataDownload.containerId);
- REGISTRY.addEventListener("products",
- "map", "selectorbox", "imagedownload", "palettes",
- dataDownload.containerId);
- REGISTRY.addEventListener("selectorbox","products");
- REGISTRY.addEventListener("camera","imagedownload");
- REGISTRY.addEventListener("palettes","map","camera","products");
- REGISTRY.addEventListener(crs.containerId, "imagedownload");
-
-
- // Console notifications
- if ( wv.brand.release() ) {
- console.info(wv.brand.NAME + " - Version " + wv.brand.VERSION +
- " - " + wv.brand.BUILD_TIMESTAMP);
- } else {
- console.warn("Development version");
- }
+ ui.link = wv.link.ui(models);
- // Do not start the tour if coming in via permalink or if local
- // storage is not available
- if ( !window.location.search && storageEngine ) {
- Worldview.Tour.start(storageEngine, hideSplash, false);
- }
+ $(window).resize(function() {
+ if ($(window).width() < 720) {
+ $('#productsHoldertabs li.first a').trigger('click');
+ }
+ });
- if ( config.parameters.debugGIBS ) {
- wv.debug.gibs(ui, models, config);
- };
+ document.activeElement.blur();
+ $("input").blur();
+ $("#eventsHolder").hide();
+ // Wirings
+ ui.sidebar.events
+ .on("dataDownloadSelect", function() {
+ dataDownloadModel.activate();
+ })
+ .on("dataDownloadUnselect", function() {
+ dataDownloadModel.deactivate();
+ });
+ dataDownloadModel.events
+ .on("activate", function() {
+ ui.sidebar.selectTab("download");
+ });
+ map.maps.events
+ .on("moveEnd", function(map) {
+ dataDownload.onViewChange(map);
+ })
+ .on("zoomEnd", function(map) {
+ dataDownload.onViewChange(map);
+ });
+ dataDownloadModel.events
+ .on("queryResults", function() {
+ dataDownload.onViewChange(map.maps.map);
+ });
+
+ // Register event listeners
+ REGISTRY.addEventListener("map", "imagedownload");
+ REGISTRY.addEventListener("time",
+ "map", "imagedownload", crs.containerId,
+ dataDownload.containerId);
+ REGISTRY.addEventListener("switch",
+ "map", "products", "selectorbox", "imagedownload", "camera",
+ crs.containerId, dataDownload.containerId);
+ REGISTRY.addEventListener("products",
+ "map", "selectorbox", "imagedownload", "palettes",
+ dataDownload.containerId);
+ REGISTRY.addEventListener("selectorbox","products");
+ REGISTRY.addEventListener("camera","imagedownload");
+ REGISTRY.addEventListener("palettes","map","camera","products");
+ REGISTRY.addEventListener(crs.containerId, "imagedownload");
+
+
+ // Console notifications
+ if ( wv.brand.release() ) {
+ console.info(wv.brand.NAME + " - Version " + wv.brand.VERSION +
+ " - " + wv.brand.BUILD_TIMESTAMP);
+ } else {
+ console.warn("Development version");
+ }
+
+ // Do not start the tour if coming in via permalink or if local
+ // storage is not available
+ if ( !window.location.search && storageEngine ) {
+ Worldview.Tour.start(storageEngine, hideSplash, false);
+ }
- window.onbeforeunload = function() {
- var events = events || {};
- if ( storageEngine && events ) {
- storageEngine.setItem('eventsCollapsed', events.isCollapsed);
- }
- };
+ if ( config.parameters.debugGIBS ) {
+ wv.debug.gibs(ui, models, config);
+ };
+
+ window.onbeforeunload = function() {
+ var events = events || {};
+ if ( storageEngine && events ) {
+ storageEngine.setItem('eventsCollapsed', events.isCollapsed);
+ }
+ };
+ }).fail(wv.util.error);
};
var debuggingFeatures = function(config) {
diff --git a/src/js/palettes/wv.palettes.custom.js b/src/js/palettes/wv.palettes.custom.js
index 5e3b7900b..f579ab656 100644
--- a/src/js/palettes/wv.palettes.custom.js
+++ b/src/js/palettes/wv.palettes.custom.js
@@ -25,10 +25,7 @@ wv.palettes.custom = wv.palettes.custom || function(config, models, layer) {
canvas = document.createElement("canvas");
canvas.width = 100;
canvas.height = 14;
-
- var url = wv.brand.url("conf/wv.palettes.json");
- var promise = wv.util.load(config.palettes, "custom", url);
- promise.done(loaded).fail(wv.util.error);
+ var promise = wv.palettes.loadCustom(config).done(loaded);
wv.ui.indicator.delayed(promise);
};
@@ -42,6 +39,12 @@ wv.palettes.custom = wv.palettes.custom || function(config, models, layer) {
zindex: 210
};
+ // FIXME: SUPER HACK
+ if ( wv.dialogs && wv.dialogs.customPalette ) {
+ wv.dialogs.customPalette.destroy();
+ wv.dialogs.customPalette = null;
+ }
+
var $element = $(alignTo);
properties.x = Math.ceil($element.offset().left +
$element.width() + 20);
@@ -58,18 +61,26 @@ wv.palettes.custom = wv.palettes.custom || function(config, models, layer) {
"<div id='palette-selector'></div>"
].join("\n"));
dialog.hideEvent.subscribe(function(i) {
+ if ( wv.dialogs.customPalette === dialog ) {
+ wv.dialogs.customPalette = null;
+ }
setTimeout(function() {
dialog.destroy();
- dialog = null;
}, 5);
});
dialog.render(document.body);
palettes = [];
palettes.push(defaultPalette());
- _.each(config.paletteOrder, function(id) {
+ var recommended = layer.palette.recommended || [];
+ _.each(recommended, function(id) {
palettes.push(customPalette(id));
});
+ _.each(config.paletteOrder, function(id) {
+ if ( _.indexOf(recommended, id) < 0 ) {
+ palettes.push(customPalette(id));
+ }
+ });
$dropDown = $("#palette-selector").msDropDown({
@@ -84,94 +95,15 @@ wv.palettes.custom = wv.palettes.custom || function(config, models, layer) {
$dropDown.on("change", selectPalette);
- /*
- // The default palette the layer is rendered in
- var renderedName = layerConfig.rendered;
-
- var renderedPalette = $.extend(true, {},
- config.palettes[renderedName]);
- var renderedColorBar = Worldview.Palette.ColorBar({
- canvas: canvas,
- palette: renderedPalette,
- bins: layerConfig.bins,
- stops: layerConfig.stops
- });
- renderedPalette.name = "Default";
- renderedPalette.image = renderedColorBar.toImage();
- palettes.push(renderedPalette);
-
- var activePalette = self.active[layer];
- var selected = null;
-
- // Palettes for the drop down, place the recommended ones first
- if ( layerConfig.recommendedPalettes ) {
- $.each(layerConfig.recommendedPalettes, function(index, name) {
- var palette = config.palettes[name];
- var colorBar = Worldview.Palette.ColorBar({
- canvas: canvas,
- palette: palette,
- bins: layerConfig.bins,
- stops: layerConfig.stops
- });
- palette.image = colorBar.toImage();
- palettes.push(palette);
- });
+ if ( models.palettes.active[layer.id] ) {
+ var paletteId = models.palettes.active[layer.id];
+ var index = _.findIndex(palettes, { id: paletteId });
+ $dropDown.set("selectedIndex", index);
}
-
- $.each(self.config.paletteOrder, function(index, name) {
- if ( $.inArray(name, layerConfig.recommendedPalettes) >= 0 ) {
- return;
- }
- var p = self.config.palettes[name];
-
- // Skip this palette if configuration says to exclude
- if ( $.inArray(name, layerConfig.excludePalettes) >= 0 ) {
- return;
- }
- if ( !p ) {
- console.error("No such palette: " + name);
- return;
- }
- if ( p.source === "stock" ) {
- var palette = $.extend(true, {}, p);
- var colorBar = Worldview.Palette.ColorBar({
- canvas: canvas,
- palette: palette,
- bins: layerConfig.bins,
- stops: layerConfig.stops
- });
- palette.image = colorBar.toImage();
- palettes.push(palette);
- }
- });
-
- $.each(palettes, function(index, palette) {
- if ( palette.id === activePalette ) {
- selected = index;
- }
- });
-
- var paletteSelector = Worldview.Palette.PaletteSelector({
- selector: "#palette-selector",
- palettes: palettes
- });
- if ( selected !== null ) {
- paletteSelector.select(selected);
- }
-
- paletteSelector.addSelectionListener(function(palette) {
- if ( palette.source === "rendered" ) {
- delete self.active[layer];
- delete self.inactive[layer];
- } else {
- self.active[layer] = palette.id;
- self.inactive[layer] = palette.id;
- }
- model.events.trigger("palette", palette, layer);
- REGISTRY.fire(self);
- });
- */
dialog.show();
+ // FIXME: MAJOR HACK
+ wv.dialogs = wv.dialogs || {};
+ wv.dialogs.customPalette = dialog;
};
var defaultPalette = function() {
diff --git a/src/js/palettes/wv.palettes.js b/src/js/palettes/wv.palettes.js
index 71322e961..3bb233289 100644
--- a/src/js/palettes/wv.palettes.js
+++ b/src/js/palettes/wv.palettes.js
@@ -112,6 +112,34 @@ wv.palettes = (function(self) {
return lookup;
};
+ self.loadCustom = function(config) {
+ return wv.util.load(config.palettes, "custom", "conf/wv.palettes.json");
+ };
+
+ self.loadRendered = function(config, layerId) {
+ var layer = config.layers[layerId];
+ return wv.util.load(config.palettes.rendered, layer.palette.id,
+ "conf/wv.palettes/" + layer.palette.id + ".json");
+ };
+
+ self.startup = function(models, config) {
+ var promises = [];
+ if ( models.palettes.active ) {
+ promises.push(self.loadCustom(config));
+ }
+ _.each(models.layers.active.overlays, function(layer) {
+ if ( layer.palette ) {
+ promises.push(self.loadRendered(config, layer.id));
+ }
+ });
+ var loaded = {};
+ if ( promises.length > 0 ) {
+ loaded = $.Deferred();
+ $.when.apply(null, promises).then(loaded.resolve);
+ }
+ return loaded;
+ };
+
init();
return self;
diff --git a/src/js/palettes/wv.palettes.legend.js b/src/js/palettes/wv.palettes.legend.js
index b48755516..a350c67e5 100644
--- a/src/js/palettes/wv.palettes.legend.js
+++ b/src/js/palettes/wv.palettes.legend.js
@@ -25,7 +25,6 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {
var self = {};
var $colorbar;
- var palette;
var init = function() {
var $parent = $(selector);
@@ -66,21 +65,20 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {
showCustomSelector();
});
wv.palettes.colorbar(selector + " .wv-palettes-colorbar");
- updateLegend();
model.events
.on("add", updateLegend)
.on("remove", updateLegend);
+ wv.palettes.loadRendered(config, layer.palette.id).done(updateLegend);
};
var updateLegend = function() {
- model.forLayer(layer.id).done(function(p) {
- palette = p;
- wv.palettes.colorbar(selector + " .wv-palettes-colorbar", palette);
- showUnitRange();
- });
+ var palette = model.forLayer(layer.id);
+ wv.palettes.colorbar(selector + " .wv-palettes-colorbar", palette);
+ showUnitRange();
};
var showUnitRange = function() {
+ var palette = model.forLayer(layer.id);
if (layer.palette.single) {
$(selector + " .wv-palettes-center").html(palette.values[0]);
} else if (layer.palette.classified) {
@@ -95,9 +93,7 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {
};
var showUnitHover = function(event) {
- if ( !palette ) {
- return;
- }
+ var palette = model.forLayer(layer.id);
var x = event.pageX - $colorbar.offset().left;
var width = $colorbar.width();
var percent = x / width;
diff --git a/src/js/palettes/wv.palettes.model.js b/src/js/palettes/wv.palettes.model.js
index 0aabdaeb4..e83c16271 100644
--- a/src/js/palettes/wv.palettes.model.js
+++ b/src/js/palettes/wv.palettes.model.js
@@ -30,57 +30,9 @@ wv.palettes.model = wv.palettes.model || function(models, config) {
self.active = {};
- self.loadCustom = function() {
- var result = $.Deferred();
- if ( _.size(config.palettes.custom) > 0 ) {
- result.resolve(config.palettes.custom);
- } else {
- $.getJSON("conf/wv.palettes.json")
- .done(function(data) {
- config.palettes.custom = data;
- result.resolve(data);
- }).fail(function() {
- self.events.trigger("error", "Unable to load custom " +
- "palettes", arguments);
- result.reject();
- });
- }
- return result;
- };
-
- self.loadRendered = function(layerId) {
- var paletteId = config.layers[layerId].palette.id;
- if ( !paletteId ) {
- throw new Error("Layer " + layerId + " does not have a palette");
- }
- var result = $.Deferred();
- if ( config.palettes.rendered[paletteId] ) {
- result.resolve(config.palettes.rendered[paletteId]);
- } else {
- $.getJSON("conf/wv.palettes/" + paletteId + ".json")
- .done(function(data) {
- config.palettes.rendered[paletteId] = data;
- result.resolve(data);
- }).fail(function() {
- self.events.trigger("error", "Unable to load palette for " +
- "layer " + layerId, arguments);
- result.reject();
- });
- }
- return result;
- };
-
self.add = function(layerId, paletteId) {
- var c = self.loadCustom;
- var r = self.loadRendered;
- $.when(c(), r(layerId)).then(function(customs, sourcePalette) {
- var targetPalette = customs[paletteId];
- self.active[layerId] = wv.palettes.translate(sourcePalette, targetPalette);
- self.events.trigger("add", layerId, self.active[layerId]);
- }).fail(function() {
- self.events.trigger("error", "Unable to set palette " + paletteId +
- " for layer " + layerId);
- });
+ self.active[layerId] = paletteId;
+ self.events.trigger("add", layerId, self.active[layerId]);
};
self.remove = function(layerId) {
@@ -91,21 +43,23 @@ wv.palettes.model = wv.palettes.model || function(models, config) {
};
self.forLayer = function(layerId) {
- var result = $.Deferred();
+ var sourcePaletteId = config.layers[layerId].palette.id;
+ var sourcePalette = config.palettes.rendered[sourcePaletteId];
if ( self.active[layerId] ) {
- result.resolve(self.active[layerId]);
+ var targetPaletteId = self.active[layerId];
+ var targetPalette = config.palettes.custom[targetPaletteId];
+ return wv.palettes.translate(sourcePalette, targetPalette);
} else {
- result = self.loadRendered(layerId);
+ return sourcePalette;
}
- return result;
};
self.toPermalink = function() {
var parts = [];
- _.each(self.active, function(palette, layerId) {
- parts.push(layerId + "," + palette.id);
+ _.each(self.active, function(paletteId, layerId) {
+ parts.push(layerId + "," + paletteId);
});
- return ( parts ) ? "palettes=" + parts.join("~") : "";
+ return ( parts.length > 0 ) ? "palettes=" + parts.join("~") : "";
};
self.fromPermalink = function(queryString) {
diff --git a/src/js/util/wv.util.js b/src/js/util/wv.util.js
index ac2ada652..41bc3924c 100644
--- a/src/js/util/wv.util.js
+++ b/src/js/util/wv.util.js
@@ -347,11 +347,12 @@ wv.util = (function(self) {
if ( root[attr] && _.size(root[attr]) > 0 ) {
promise.resolve(root[attr]);
} else {
- promise = $.getJSON(url);
+ promise = $.getJSON(wv.brand.url(url));
promise.done(function(result) {
root[attr] = result;
});
}
+ promise.fail(wv.util.error);
return promise;
};
diff --git a/test/palettes/wv.palettes-test.js b/test/palettes/wv.palettes-test.js
new file mode 100644
index 000000000..142f9f9f0
--- /dev/null
+++ b/test/palettes/wv.palettes-test.js
@@ -0,0 +1,55 @@
+/*
+ * 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.palettes", {
+
+ "Translate one to one with custom palette": function() {
+ var source = {
+ colors: ["a", "b", "c"]
+ };
+ var target = {
+ colors: ["1", "2", "3"]
+ };
+ var p = wv.palettes.translate(source, target);
+ buster.assert.equals(p.colors[0], "1");
+ buster.assert.equals(p.colors[1], "2");
+ buster.assert.equals(p.colors[2], "3");
+ },
+
+ "Translate by compressing color range": function() {
+ var source = {
+ colors: ["a", "b", "c"]
+ };
+ var target = {
+ colors: ["1", "2", "3", "4", "5", "6"]
+ };
+ var p = wv.palettes.translate(source, target);
+ buster.assert.equals(p.colors[0], "1");
+ buster.assert.equals(p.colors[1], "3");
+ buster.assert.equals(p.colors[2], "5");
+ },
+
+ "Translate by expanding color range": function() {
+ var source = {
+ colors: ["a", "b", "c", "e", "f", "g"]
+ };
+ var target = {
+ colors: ["1", "2", "3"]
+ };
+ var p = wv.palettes.translate(source, target);
+ buster.assert.equals(p.colors[0], "1");
+ buster.assert.equals(p.colors[1], "1");
+ buster.assert.equals(p.colors[2], "2");
+ buster.assert.equals(p.colors[3], "2");
+ buster.assert.equals(p.colors[4], "3");
+ buster.assert.equals(p.colors[5], "3");
+ }
+});
\ No newline at end of file
diff --git a/test/palettes/wv.palettes.model-test.js b/test/palettes/wv.palettes.model-test.js
index 4c4ff695a..125cc94fc 100644
--- a/test/palettes/wv.palettes.model-test.js
+++ b/test/palettes/wv.palettes.model-test.js
@@ -9,7 +9,7 @@
* All Rights Reserved.
*/
-buster.testCase("wv.palette.model", {
+buster.testCase("wv.palettes.model", {
config: null,
models: null,
@@ -36,112 +36,6 @@ buster.testCase("wv.palette.model", {
this.stub($, "getJSON");
},
- "Loads palette for layer": function(done) {
- var result = $.Deferred();
- result.resolve({ id: "palette1" });
- $.getJSON.returns(result);
- var self = this;
- this.model.forLayer("layer1").done(function(palette) {
- buster.assert.equals(palette.id, "palette1");
- buster.assert.equals(self.config.palettes.rendered.palette1.id,
- "palette1");
- done();
- });
- },
-
- "Only loads palette once": function(done) {
- // Stuff configuration with existing palette
- this.config.palettes.rendered.palette1 = { id: "palette1" };
- this.model.forLayer("layer1").done(function(palette) {
- buster.assert.equals(palette.id, "palette1");
- buster.refute.called($.getJSON);
- done();
- });
- },
-
- "Error triggered if palette for layer unavailable": function(done) {
- var result = $.Deferred();
- result.reject();
- $.getJSON.returns(result);
- var trigger = this.stub(this.model.events, "trigger");
- this.model.forLayer("layer1").fail(function() {
- buster.assert.calledWith(trigger, "error");
- done();
- });
- },
-
- "Loads custom palettes": function(done) {
- var result = $.Deferred();
- result.resolve({ answer: "yes" });
- $.getJSON.returns(result);
- this.model.loadCustom().done(function(data) {
- buster.assert.equals(data.answer, "yes");
- done();
- });
- },
-
- "Only loads custom palettes once": function(done) {
- this.config.palettes.custom = { id: "custom1" };
- this.model.loadCustom().done(function(data) {
- buster.assert.equals(data.id, "custom1");
- buster.refute.called($.getJSON);
- done();
- });
- },
-
- "Error triggered if custom palettes are unavailable": function(done) {
- var result = $.Deferred();
- result.reject();
- $.getJSON.returns(result);
- var trigger = this.stub(this.model.events, "trigger");
- this.model.loadCustom().fail(function() {
- buster.assert.calledWith(trigger, "error");
- done();
- });
- },
-
- "Translate one to one with custom palette": function() {
- var source = {
- colors: ["a", "b", "c"]
- };
- var target = {
- colors: ["1", "2", "3"]
- };
- var p = this.model.translate(source, target);
- buster.assert.equals(p.colors[0], "1");
- buster.assert.equals(p.colors[1], "2");
- buster.assert.equals(p.colors[2], "3");
- },
-
- "Translate by compressing color range": function() {
- var source = {
- colors: ["a", "b", "c"]
- };
- var target = {
- colors: ["1", "2", "3", "4", "5", "6"]
- };
- var p = this.model.translate(source, target);
- buster.assert.equals(p.colors[0], "1");
- buster.assert.equals(p.colors[1], "3");
- buster.assert.equals(p.colors[2], "5");
- },
-
- "Translate by expanding color range": function() {
- var source = {
- colors: ["a", "b", "c", "e", "f", "g"]
- };
- var target = {
- colors: ["1", "2", "3"]
- };
- var p = this.model.translate(source, target);
- buster.assert.equals(p.colors[0], "1");
- buster.assert.equals(p.colors[1], "1");
- buster.assert.equals(p.colors[2], "2");
- buster.assert.equals(p.colors[3], "2");
- buster.assert.equals(p.colors[4], "3");
- buster.assert.equals(p.colors[5], "3");
- },
-
"Adds a custom palette": function(done) {
this.config.palettes.rendered = {
"palette1": {
@@ -156,7 +50,9 @@ buster.testCase("wv.palette.model", {
colors: ["x1"]
}
};
- this.model.events.on("add", function(layerId, palette) {
+ var self = this;
+ this.model.events.on("add", function(layerId) {
+ var palette = self.model.forLayer(layerId);
buster.assert.equals(palette.colors[0], "x1");
buster.assert.equals(palette.values[0], "v1");
done();
@@ -179,12 +75,11 @@ buster.testCase("wv.palette.model", {
}
};
var self = this;
- this.model.events.on("remove", function(layerId, palette) {
- self.model.forLayer(layerId).done(function(palette) {
- buster.assert.equals(palette.colors[0], "c1");
- buster.assert.equals(palette.values[0], "v1");
- done();
- });
+ this.model.events.on("remove", function(layerId) {
+ var palette = self.model.forLayer(layerId);
+ buster.assert.equals(palette.colors[0], "c1");
+ buster.assert.equals(palette.values[0], "v1");
+ done();
});
this.model.add("layer1", "custom1");
this.model.remove("layer1");
@@ -192,13 +87,17 @@ buster.testCase("wv.palette.model", {
"To permalink": function() {
this.model.active = {
- "layer1": { id: "custom1" },
- "layer2": { id: "custom2" }
+ "layer1": "custom1",
+ "layer2": "custom2"
};
buster.assert.equals(this.model.toPermalink(),
"palettes=layer1,custom1~layer2,custom2");
},
+ "Empty permalink when no palettes are active": function() {
+ buster.assert.equals(this.model.toPermalink(), "");
+ },
+
"From permalink": function() {
this.stub(this.model, "add");
this.model.fromPermalink("palettes=layer1,custom1~layer2,custom2");