Interactive interface for browsing global, full-resolution satellite imagery
Exported palettes
ZachTRice6198a5acb54c
diff --git a/web/js/palettes/palettes.js b/web/js/palettes/palettes.jsindex a9dd4dcfd..7d7cbdf78 100644--- a/web/js/palettes/palettes.js+++ b/web/js/palettes/palettes.js@@ -1 +1,187 @@-export default function () {};+import $ from 'jquery';+import each from 'lodash/each';+import find from 'lodash/find';+import util from '../util/util';+import wvui from '../ui/ui';++export const palettes = (function (self) {+ var checkerboard;++ self.supported = true;++ var init = function () {+ var browser = util.browser;+ if (browser.ie || !browser.webWorkers || !browser.cors) {+ self.supported = false;+ } else {+ drawCheckerboard();+ }+ };++ self.colorbar = function (target, colors) {+ var canvas;+ if (target.length) {+ canvas = $(target)+ .get(0);+ } else {+ canvas = target;+ }+ if (!canvas) {+ return;+ }+ var g = canvas.getContext('2d');++ g.fillStyle = checkerboard;+ g.fillRect(0, 0, canvas.width, canvas.height);+ if (colors) {+ var bins = colors.length;+ var binWidth = canvas.width / bins;+ var drawWidth = Math.ceil(binWidth);+ each(colors, function (color, i) {+ g.fillStyle = util.hexToRGBA(color);+ g.fillRect(Math.floor(binWidth * i), 0, drawWidth,+ canvas.height);+ });+ }+ };++ 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.fillStyle = 'rgb(200, 200, 200)';+ g.fillRect(0, 0, size, size);+ g.fillRect(size, size, size, size);++ // g.fillStyle = "rgb(153, 153, 153)";+ g.fillStyle = 'rgb(240, 240, 240)';+ g.fillRect(0, size, size, size);+ g.fillRect(size, 0, size, size);++ checkerboard = g.createPattern(canvas, 'repeat');+ };++ self.translate = function (source, target) {+ var translation = [];+ each(source, function (color, index) {+ var sourcePercent = index / source.length;+ var targetIndex = Math.floor(sourcePercent * target.length);+ translation.push(target[targetIndex]);+ });+ return translation;+ };++ self.lookup = function (sourcePalette, targetPalette) {+ var lookup = {};+ each(sourcePalette.colors, function (sourceColor, index) {+ var source =+ parseInt(sourceColor.substring(0, 2), 16) + ',' ++ parseInt(sourceColor.substring(2, 4), 16) + ',' ++ parseInt(sourceColor.substring(4, 6), 16) + ',' ++ '255';+ var targetColor = targetPalette.colors[index];+ var target = {+ r: parseInt(targetColor.substring(0, 2), 16),+ g: parseInt(targetColor.substring(2, 4), 16),+ b: parseInt(targetColor.substring(4, 6), 16),+ a: 255+ };+ lookup[source] = target;+ });+ return lookup;+ };++ self.loadCustom = function (config) {+ return util.load.config(config.palettes,+ 'custom', 'config/palettes-custom.json');+ };++ self.loadRendered = function (config, layerId) {+ var layer = config.layers[layerId];+ return util.load.config(config.palettes.rendered,+ layer.palette.id, 'config/palettes/' + layer.palette.id + '.json');+ };++ self.requirements = function (state, config) {+ var promises = [];+ config.palettes = {+ rendered: {},+ custom: {}+ };+ each(state.l, function (qsLayer) {+ var layerId = qsLayer.id;+ if (config.layers[layerId] && config.layers[layerId].palette) {+ promises.push(self.loadRendered(config, layerId));+ }+ var custom = find(qsLayer.attributes, {+ id: 'palette'+ });+ if (custom) {+ promises.push(self.loadCustom(config));+ }+ });+ if (promises.length > 0) {+ var promise = $.Deferred();+ $.when.apply(null, promises)+ .then(promise.resolve)+ .fail(promise.reject);+ return promise;+ }+ };++ // Only for permalink 1.1 support+ self.parse = function (state, errors, config) {+ if (state.palettes) {+ if (!palettes.supported) {+ // FIXME: This should go in errors+ delete state.palettes;+ wvui.notify('The custom palette feature is not supported ' ++ 'with your web browser. Upgrade or try again in a ' ++ 'different browser');+ return;+ }+ var parts = state.palettes.split('~');+ each(parts, function (part) {+ var items = part.split(',');+ var layerId = items[0];+ var paletteId = items[1];+ if (!config.layers[layerId]) {+ errors.push({+ message: 'Invalid layer for palette ' ++ paletteId + ': ' + layerId+ });+ } else if (!config.layers[layerId].palette) {+ errors.push({+ message: 'Layer ' + layerId + ' does not ' ++ 'support palettes'+ });+ } else {+ var layer = find(state.l, {+ id: layerId+ });+ if (layer) {+ layer.attributes.push({+ id: 'palette',+ value: paletteId+ });+ } else {+ errors.push({+ message: 'Layer ' + layerId + ' is not ' ++ 'active'+ });+ }+ }+ });+ delete state.paletes;+ }+ };++ init();+ return self;+})();diff --git a/web/js/palettes/palettes.legend.js b/web/js/palettes/palettes.legend.jsindex a9dd4dcfd..990842a36 100644--- a/web/js/palettes/palettes.legend.js+++ b/web/js/palettes/palettes.legend.js@@ -1 +1,289 @@-export default function () {};+import $ from 'jquery';+import each from 'lodash/each';+import first from 'lodash/first';+import last from 'lodash/last';+import util from '../util/util';+import palettes from '../palettes/palettes';++export function palettesLegend(spec) {+ var selector = spec.selector;+ var config = spec.config;+ var models = spec.models;+ var model = spec.models.palettes;+ var ui = spec.ui;+ var layer = spec.layer;+ var loaded = false;+ var rendered = false;++ var self = {};++ var init = function () {+ var paletteId = layer.palette.id;+ if (config.palettes.rendered[paletteId]) {+ loaded = true;+ render();+ } else {+ palettes.loadRendered(config, layer.id)+ .done(function () {+ if (!loaded) {+ loaded = true;+ render();+ if (spec.onLoad) {+ spec.onLoad();+ }+ }+ });+ }+ };++ var render = function () {+ var $parent = $(selector);+ var paletteId = layer.palette.id;+ var palette = config.palettes.rendered[paletteId];++ var $legendPanel = $('<div></div>')+ .addClass('wv-palettes-panel')+ .attr('data-layer', layer.id);+ $parent.append($legendPanel);+ var legends = model.getLegends(layer.id);+ each(legends, function (legend, index) {+ if ((legend.type === 'continuous') ||+ (legend.type === 'discrete')) {+ renderScale($legendPanel, legend, index, layer.id);+ }+ if (legend.type === 'classification') {+ renderClasses($legendPanel, legend, index);+ }+ });+ self.update();+ };++ var renderScale = function ($legendPanel, legend, index, layerId) {+ $container = $('<div></div>')+ .addClass('wv-palettes-legend')+ .attr('data-index', index);+ $colorbar = $('<canvas></canvas>')+ .addClass('wv-palettes-colorbar')+ .attr('id', legend.id)+ .attr('data-index', index);+ // set fixed canvas dimensions+ $colorbar[0].width = 235;+ $colorbar[0].height = 12;++ $container.append($colorbar);++ var $runningDataPointBar = $('<div></div>')+ .addClass('wv-running-bar');+ var $runningDataPointLabel = $('<span></span>')+ .addClass('wv-running-label');++ var $ranges = $('<div></div>')+ .addClass('wv-palettes-ranges');+ var $min = $('<div></div>')+ .addClass('wv-palettes-min');+ var $max = $('<div></div>')+ .addClass('wv-palettes-max');+ var $title = $('<div></div>')+ .addClass('wv-palettes-title');+ $container.prepend($title);+ $ranges+ .append($min)+ .append($max)+ .append($runningDataPointLabel);+ $container+ .append($ranges)+ .append($runningDataPointBar);++ $colorbar.on('mousemove', function (e) {+ showUnitHover(e, index);+ });+ $colorbar.on('mouseout', hideUnitsOnMouseOut);+ $legendPanel.append($container);+ palettes.colorbar(selector + ' ' ++ '[data-index=\'' + index + '\'] canvas', legend.colors);+ };+ var renderClasses = function ($legendPanel, legend, index) {+ // var $runningDataPointLabel = $("<span></span>")+ // .addClass("wv-running-category-label");+ var $panel = $('<div></div>')+ .addClass('wv-palettes-legend')+ .addClass('wv-palettes-classes')+ // .append($runningDataPointLabel)+ .attr('data-index', index);+ $legendPanel+ .attr('id', legend.id)+ .append($panel);+ };++ var updateClasses = function (legend, index) {+ var $panel = $(selector + ' [data-index=\'' + index + '\']');+ $panel.empty();+ each(legend.colors, function (color, classIndex) {+ var $colorBox;+ var $runningDataPointLabel = $('<span></span>')+ .addClass('wv-running-category-label');+ $colorBox = $('<span></span>')+ .attr('data-index', index)+ .attr('data-class-index', classIndex)+ .attr('data-hex', color)+ .addClass('wv-palettes-class')+ .html(' ')+ .css('background-color', util.hexToRGB(color))+ .hover(highlightClass, unhighlightClass);+ $panel.append($colorBox);+ $panel.append($runningDataPointLabel);+ // Calls running data+ $colorBox.on('mouseenter', showClassUnitHover);+ $colorBox.on('mouseout', hideUnitsOnMouseOut);+ });+ var $detailPanel = $('<div></div>');+ each(legend.colors, function (color, classIndex) {+ var label = legend.tooltips[classIndex];+ label = (legend.units) ? label + ' ' + legend.units : label;+ var $row = $('<div></div>')+ .addClass('wv-palettes-class-detail')+ .attr('data-class-index', classIndex);+ $colorBox =+ $row.append(+ $('<span></span>')+ .addClass('wv-palettes-class')+ .html(' ')+ .css('background-color', util.hexToRGB(color)))+ .append($('<span></span>')+ .addClass('wv-palettes-class-label')+ .attr('data-index', index)+ .attr('data-class-index', classIndex)+ .html(label));+ $detailPanel.append($row);+ });+ if (!rendered) {+ rendered = true;+ }+ };++ self.update = function () {+ if (!loaded) {+ return;+ }+ var legends = model.getLegends(layer.id);+ each(legends, function (legend, index) {+ if ((legend.type === 'continuous') ||+ (legend.type === 'discrete')) {+ palettes.colorbar(selector + ' ' ++ '[data-index=\'' + index + '\'] canvas', legend.colors);+ showUnitRange(index);+ } else if (legend.type === 'classification') {+ updateClasses(legend, index);+ }+ });+ };++ var showUnitRange = function (index) {+ if (!loaded) {+ return;+ }+ var legends = model.getLegends(layer.id, index);+ var entries = model.get(layer.id, index)+ .entries;+ each(legends, function (legend, index) {+ var min = legend.minLabel || first(legend.tooltips);+ var max = legend.maxLabel || last(legend.tooltips);+ min = (legend.units) ? min + ' ' + legend.units : min;+ max = (legend.units) ? max + ' ' + legend.units : max;+ $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-min')+ .html(min);+ $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-max')+ .html(max);+ var title = legend.title || ' ';+ if (legends.length === 1) {+ $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-title')+ .hide();+ } else {+ $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-title')+ .html(title);+ }+ });+ };+ /**+ * get color from canvas bar and+ * send it to data processing+ *+ * @method showUnitHover+ * @static+ * @param {MouseEvent} e+ * @return {void}+ */+ var showUnitHover = function (e, index) {+ var rgba;+ var pos;+ var x;+ var y;+ var id;+ var legends;+ if (!loaded) {+ return;+ }+ legends = model.getLegends(layer.id)[index];+ offset = $(e.currentTarget)+ .offset();+ x = e.pageX - offset.left;+ y = e.pageY - offset.top;+ rgba = util.getCanvasPixelData(e.currentTarget, x, y);+ hex = util.rgbaToHex(rgba[0], rgba[1], rgba[2]);+ ui.map.runningdata.newLegend(legends, hex);+ };++ /**+ * get color from data attr and+ * send it to data processing and+ * render legend+ *+ * @method showClassUnitHover+ * @static+ * @param {MouseEvent} e+ * @return {void}+ */+ var showClassUnitHover = function (e) {+ var hex = $(this)+ .data('hex');+ var legends = model.getLegends(layer.id)[0];+ ui.map.runningdata.newLegend(legends, hex);+ };++ /**+ * get color from data attr and+ * send it to data processing and+ * render legend+ *+ * @method showClassUnitHover+ * @static+ * @param {MouseEvent} e+ * @return {void}+ */+ var hideUnitsOnMouseOut = function () {+ ui.map.runningdata.clearAll();+ };++ var highlightClass = function () {+ legendIndex = $(this)+ .attr('data-index');+ classIndex = $(this)+ .attr('data-class-index');+ $('.wv-palettes-class-label[data-index=\'' + legendIndex + '\']' ++ '[data-class-index=\'' + classIndex + '\']')+ .addClass('wv-palettes-class-highlight');+ };++ var unhighlightClass = function () {+ legendIndex = $(this)+ .attr('data-index');+ classIndex = $(this)+ .attr('data-class-index');+ $('.wv-palettes-class-label[data-index=\'' + legendIndex + '\']' ++ '[data-class-index=\'' + classIndex + '\']')+ .removeClass('wv-palettes-class-highlight');+ };++ init();+ return self;+};diff --git a/web/js/palettes/wv.palettes.model.js b/web/js/palettes/palettes.model.jssimilarity index 84%rename from web/js/palettes/wv.palettes.model.jsrename to web/js/palettes/palettes.model.jsindex b7e0c15ed..02d0c8fff 100644--- a/web/js/palettes/wv.palettes.model.js+++ b/web/js/palettes/palettes.model.js@@ -1,26 +1,26 @@-/**- * @module wv.palette- */-var wv = wv || {};-wv.palettes = wv.palettes || {};--/**- * @class wv.palette.model- */-wv.palettes.model = wv.palettes.model || function (models, config) {+import each from 'lodash/each';+import find from 'lodash/find';+import cloneDeep from 'lodash/cloneDeep';+import isNaN from 'lodash/isNaN';+import isUndefined from 'lodash/isUndefined';+import parseInt from 'lodash/parseInt';+import util from '../util/util';+import palettes from '../palettes/palettes';++export function palettesModel(models, config) {config.palettes = config.palettes || {rendered: {},custom: {}};var self = {};- self.events = wv.util.events();+ self.events = util.events();self.active = {};self.getRendered = function (layerId, index) {var name = config.layers[layerId].palette.id;var palette = config.palettes.rendered[name];- if (!_.isUndefined(index)) {+ if (!isUndefined(index)) {palette = palette.maps[index];}return palette;@@ -38,17 +38,17 @@ wv.palettes.model = wv.palettes.model || function (models, config) {self.active[layerId] = self.active[layerId] || {};var active = self.active[layerId];active.maps = active.maps || [];- _.each(self.getRendered(layerId)+ each(self.getRendered(layerId).maps,function (palette, index) {if (!active.maps[index]) {- active.maps[index] = _.cloneDeep(palette);+ active.maps[index] = cloneDeep(palette);}});};self.allowed = function (layerId) {- if (!wv.palettes.supported) {+ if (!palettes.supported) {return false;}return config.layers[layerId].palette;@@ -59,7 +59,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {throw new Error('Invalid layer: ' + layerId);}prepare(layerId);- index = (_.isUndefined(index)) ? 0 : index;+ index = (isUndefined(index)) ? 0 : index;var active = self.active[layerId];var palette = active.maps[index];if (palette.custom === paletteId) {@@ -72,7 +72,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {};self.clearCustom = function (layerId, index) {- index = (_.isUndefined(index)) ? 0 : index;+ index = (isUndefined(index)) ? 0 : index;var active = self.active[layerId];if (!active) {return;@@ -89,7 +89,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {self.setRange = function (layerId, min, max, squash, index) {prepare(layerId);- index = (_.isUndefined(index)) ? 0 : index;+ index = (isUndefined(index)) ? 0 : index;var palette = self.active[layerId].maps[index];if (min === 0) {min = undefined;@@ -122,7 +122,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {* @return {object} object including the entries and legend*/self.get = function (layerId, index) {- index = (_.isUndefined(index)) ? 0 : index;+ index = (isUndefined(index)) ? 0 : index;if (self.active[layerId]) {return self.active[layerId].maps[index];}@@ -200,8 +200,8 @@ wv.palettes.model = wv.palettes.model || function (models, config) {if (self.inUse() && !state.l) {throw new Error('No layers in state');}- _.each(self.active, function (def, layerId) {- if (!_.find(models.layers.get(), {+ each(self.active, function (def, layerId) {+ if (!find(models.layers.get(), {id: layerId})) {return;@@ -215,7 +215,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {};self.saveSingle = function (state, layerId) {- var attr = _.find(state.l, {+ var attr = find(state.l, {id: layerId}).attributes;@@ -288,7 +288,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {}}- var attr = _.find(state.l, {+ var attr = find(state.l, {id: layerId}).attributes;@@ -340,22 +340,22 @@ wv.palettes.model = wv.palettes.model || function (models, config) {};self.load = function (state, errors) {- if (!wv.palettes.supported) {+ if (!palettes.supported) {return;}- _.each(state.l, function (layerDef) {+ each(state.l, function (layerDef) {var layerId = layerDef.id;var minValue, maxValue;var min = [],max = [];var squash = [];var count = 0;- _.each(layerDef.attributes, function (attr) {+ each(layerDef.attributes, function (attr) {if (attr.id === 'palette') {count = self.getCount(layerId);- values = wv.util.toArray(attr.value.split(';'));- _.each(values, function (value, index) {+ values = util.toArray(attr.value.split(';'));+ each(values, function (value, index) {try {self.setCustom(layerId, value, index);} catch (error) {@@ -365,14 +365,14 @@ wv.palettes.model = wv.palettes.model || function (models, config) {}if (attr.id === 'min') {count = self.getCount(layerId);- values = wv.util.toArray(attr.value.split(';'));- _.each(values, function (value, index) {+ values = util.toArray(attr.value.split(';'));+ each(values, function (value, index) {if (value === '') {min.push(undefined);return;}minValue = parseFloat(value);- if (_.isNaN(minValue)) {+ if (isNaN(minValue)) {errors.push('Invalid min value: ' + value);} else {min.push(findIndex(layerId, 'min', minValue, index));@@ -381,14 +381,14 @@ wv.palettes.model = wv.palettes.model || function (models, config) {}if (attr.id === 'max') {count = self.getCount(layerId);- values = wv.util.toArray(attr.value.split(';'));- _.each(values, function (value, index) {+ values = util.toArray(attr.value.split(';'));+ each(values, function (value, index) {if (value === '') {max.push(undefined);return;}maxValue = parseFloat(value);- if (_.isNaN(maxValue)) {+ if (isNaN(maxValue)) {errors.push('Invalid max value: ' + value);} else {max.push(findIndex(layerId, 'max', maxValue, index));@@ -400,8 +400,8 @@ wv.palettes.model = wv.palettes.model || function (models, config) {if (attr.value === true) {squash[0] = true;} else {- values = wv.util.toArray(attr.value.split(';'));- _.each(values, function (value) {+ values = util.toArray(attr.value.split(';'));+ each(values, function (value) {squash.push(value === 'true');});}@@ -423,7 +423,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {var values = self.get(layerId, index).entries.values;var result;- _.each(values, function (check, index) {+ each(values, function (check, index) {var min = getMinValue(check);var max = getMaxValue(check);if (type === 'min' && value === min) {@@ -443,7 +443,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {self.inUse = function () {var layers = models.layers.get();var found = false;- _.each(layers, function (layer) {+ each(layers, function (layer) {if (self.active[layer.id]) {found = true;return false;@@ -456,7 +456,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {var use = false;var active = self.active[layerId].maps;- _.each(active, function (palette, index) {+ each(active, function (palette, index) {if (palette.custom) {use = true;return false;@@ -469,7 +469,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {if (palette.max >= rendered.entries.values.length) {delete palette.max;}- if (!_.isUndefined(palette.min) || !_.isUndefined(palette.max)) {+ if (!isUndefined(palette.min) || !isUndefined(palette.max)) {use = true;return false;}@@ -485,7 +485,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {}var active = self.active[layerId].maps;var lookup = {};- _.each(active, function (palette, index) {+ each(active, function (palette, index) {oldLegend = palette.legend;entries = palette.entries;legend = {@@ -511,7 +511,7 @@ wv.palettes.model = wv.palettes.model || function (models, config) {var targetCount = target.length;var indexCount = max - min;- _.each(source, function (color, index) {+ each(source, function (color, index) {var newColor;if (index < min || index > max) {targetColor = '00000000';@@ -534,15 +534,15 @@ wv.palettes.model = wv.palettes.model || function (models, config) {}legend.colors.push(targetColor);var lookupSource =- _.parseInt(color.substring(0, 2), 16) + ',' +- _.parseInt(color.substring(2, 4), 16) + ',' +- _.parseInt(color.substring(4, 6), 16) + ',' +- _.parseInt(color.substring(6, 8), 16);+ parseInt(color.substring(0, 2), 16) + ',' ++ parseInt(color.substring(2, 4), 16) + ',' ++ parseInt(color.substring(4, 6), 16) + ',' ++ parseInt(color.substring(6, 8), 16);var lookupTarget = {- r: _.parseInt(targetColor.substring(0, 2), 16),- g: _.parseInt(targetColor.substring(2, 4), 16),- b: _.parseInt(targetColor.substring(4, 6), 16),- a: _.parseInt(targetColor.substring(6, 8), 16)+ r: parseInt(targetColor.substring(0, 2), 16),+ g: parseInt(targetColor.substring(2, 4), 16),+ b: parseInt(targetColor.substring(4, 6), 16),+ a: parseInt(targetColor.substring(6, 8), 16)};lookup[lookupSource] = lookupTarget;});diff --git a/web/js/palettes/wv.palettes.js b/web/js/palettes/wv.palettes.jsdeleted file mode 100644index 81b64ba3c..000000000--- a/web/js/palettes/wv.palettes.js+++ /dev/null@@ -1,186 +0,0 @@-/**- * @module wv.palettes- */-var wv = wv || {};--wv.palettes = (function (self) {- var checkerboard;-- self.supported = true;-- var init = function () {- var browser = wv.util.browser;- if (browser.ie || !browser.webWorkers || !browser.cors) {- self.supported = false;- } else {- drawCheckerboard();- }- };-- self.colorbar = function (target, colors) {- var canvas;- if (target.length) {- canvas = $(target)- .get(0);- } else {- canvas = target;- }- if (!canvas) {- return;- }- var g = canvas.getContext('2d');-- g.fillStyle = checkerboard;- g.fillRect(0, 0, canvas.width, canvas.height);- if (colors) {- var bins = colors.length;- var binWidth = canvas.width / bins;- var drawWidth = Math.ceil(binWidth);- _.each(colors, function (color, i) {- g.fillStyle = wv.util.hexToRGBA(color);- g.fillRect(Math.floor(binWidth * i), 0, drawWidth,- canvas.height);- });- }- };-- 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.fillStyle = 'rgb(200, 200, 200)';- g.fillRect(0, 0, size, size);- g.fillRect(size, size, size, size);-- // g.fillStyle = "rgb(153, 153, 153)";- g.fillStyle = 'rgb(240, 240, 240)';- g.fillRect(0, size, size, size);- g.fillRect(size, 0, size, size);-- checkerboard = g.createPattern(canvas, 'repeat');- };-- self.translate = function (source, target) {- var translation = [];- _.each(source, function (color, index) {- var sourcePercent = index / source.length;- var targetIndex = Math.floor(sourcePercent * target.length);- translation.push(target[targetIndex]);- });- return translation;- };-- self.lookup = function (sourcePalette, targetPalette) {- var lookup = {};- _.each(sourcePalette.colors, function (sourceColor, index) {- var source =- parseInt(sourceColor.substring(0, 2), 16) + ',' +- parseInt(sourceColor.substring(2, 4), 16) + ',' +- parseInt(sourceColor.substring(4, 6), 16) + ',' +- '255';- var targetColor = targetPalette.colors[index];- var target = {- r: parseInt(targetColor.substring(0, 2), 16),- g: parseInt(targetColor.substring(2, 4), 16),- b: parseInt(targetColor.substring(4, 6), 16),- a: 255- };- lookup[source] = target;- });- return lookup;- };-- self.loadCustom = function (config) {- return wv.util.load.config(config.palettes,- 'custom', 'config/palettes-custom.json');- };-- self.loadRendered = function (config, layerId) {- var layer = config.layers[layerId];- return wv.util.load.config(config.palettes.rendered,- layer.palette.id, 'config/palettes/' + layer.palette.id + '.json');- };-- self.requirements = function (state, config) {- var promises = [];- config.palettes = {- rendered: {},- custom: {}- };- _.each(state.l, function (qsLayer) {- var layerId = qsLayer.id;- if (config.layers[layerId] && config.layers[layerId].palette) {- promises.push(self.loadRendered(config, layerId));- }- var custom = _.find(qsLayer.attributes, {- id: 'palette'- });- if (custom) {- promises.push(self.loadCustom(config));- }- });- if (promises.length > 0) {- var promise = $.Deferred();- $.when.apply(null, promises)- .then(promise.resolve)- .fail(promise.reject);- return promise;- }- };-- // Only for permalink 1.1 support- self.parse = function (state, errors, config) {- if (state.palettes) {- if (!wv.palettes.supported) {- // FIXME: This should go in errors- delete state.palettes;- wv.ui.notify('The custom palette feature is not supported ' +- 'with your web browser. Upgrade or try again in a ' +- 'different browser');- return;- }- var parts = state.palettes.split('~');- _.each(parts, function (part) {- var items = part.split(',');- var layerId = items[0];- var paletteId = items[1];- if (!config.layers[layerId]) {- errors.push({- message: 'Invalid layer for palette ' +- paletteId + ': ' + layerId- });- } else if (!config.layers[layerId].palette) {- errors.push({- message: 'Layer ' + layerId + ' does not ' +- 'support palettes'- });- } else {- var layer = _.find(state.l, {- id: layerId- });- if (layer) {- layer.attributes.push({- id: 'palette',- value: paletteId- });- } else {- errors.push({- message: 'Layer ' + layerId + ' is not ' +- 'active'- });- }- }- });- delete state.paletes;- }- };-- init();- return self;-})(wv.palettes || {});diff --git a/web/js/palettes/wv.palettes.legend.js b/web/js/palettes/wv.palettes.legend.jsdeleted file mode 100644index ff8f6a85c..000000000--- a/web/js/palettes/wv.palettes.legend.js+++ /dev/null@@ -1,288 +0,0 @@-/**- * @module wv.palettes- */-var wv = wv || {};-wv.palettes = wv.palettes || {};--wv.palettes.legend = wv.palettes.legend || function (spec) {- var selector = spec.selector;- var config = spec.config;- var models = spec.models;- var model = spec.models.palettes;- var ui = spec.ui;- var layer = spec.layer;- var loaded = false;- var rendered = false;-- var self = {};-- var init = function () {- var paletteId = layer.palette.id;- if (config.palettes.rendered[paletteId]) {- loaded = true;- render();- } else {- wv.palettes.loadRendered(config, layer.id)- .done(function () {- if (!loaded) {- loaded = true;- render();- if (spec.onLoad) {- spec.onLoad();- }- }- });- }- };-- var render = function () {- var $parent = $(selector);- var paletteId = layer.palette.id;- var palette = config.palettes.rendered[paletteId];-- var $legendPanel = $('<div></div>')- .addClass('wv-palettes-panel')- .attr('data-layer', layer.id);- $parent.append($legendPanel);- var legends = model.getLegends(layer.id);- _.each(legends, function (legend, index) {- if ((legend.type === 'continuous') ||- (legend.type === 'discrete')) {- renderScale($legendPanel, legend, index, layer.id);- }- if (legend.type === 'classification') {- renderClasses($legendPanel, legend, index);- }- });- self.update();- };-- var renderScale = function ($legendPanel, legend, index, layerId) {- $container = $('<div></div>')- .addClass('wv-palettes-legend')- .attr('data-index', index);- $colorbar = $('<canvas></canvas>')- .addClass('wv-palettes-colorbar')- .attr('id', legend.id)- .attr('data-index', index);- // set fixed canvas dimensions- $colorbar[0].width = 235;- $colorbar[0].height = 12;-- $container.append($colorbar);-- var $runningDataPointBar = $('<div></div>')- .addClass('wv-running-bar');- var $runningDataPointLabel = $('<span></span>')- .addClass('wv-running-label');-- var $ranges = $('<div></div>')- .addClass('wv-palettes-ranges');- var $min = $('<div></div>')- .addClass('wv-palettes-min');- var $max = $('<div></div>')- .addClass('wv-palettes-max');- var $title = $('<div></div>')- .addClass('wv-palettes-title');- $container.prepend($title);- $ranges- .append($min)- .append($max)- .append($runningDataPointLabel);- $container- .append($ranges)- .append($runningDataPointBar);-- $colorbar.on('mousemove', function (e) {- showUnitHover(e, index);- });- $colorbar.on('mouseout', hideUnitsOnMouseOut);- $legendPanel.append($container);- wv.palettes.colorbar(selector + ' ' +- '[data-index=\'' + index + '\'] canvas', legend.colors);- };- var renderClasses = function ($legendPanel, legend, index) {- // var $runningDataPointLabel = $("<span></span>")- // .addClass("wv-running-category-label");- var $panel = $('<div></div>')- .addClass('wv-palettes-legend')- .addClass('wv-palettes-classes')- // .append($runningDataPointLabel)- .attr('data-index', index);- $legendPanel- .attr('id', legend.id)- .append($panel);- };-- var updateClasses = function (legend, index) {- var $panel = $(selector + ' [data-index=\'' + index + '\']');- $panel.empty();- _.each(legend.colors, function (color, classIndex) {- var $colorBox;- var $runningDataPointLabel = $('<span></span>')- .addClass('wv-running-category-label');- $colorBox = $('<span></span>')- .attr('data-index', index)- .attr('data-class-index', classIndex)- .attr('data-hex', color)- .addClass('wv-palettes-class')- .html(' ')- .css('background-color', wv.util.hexToRGB(color))- .hover(highlightClass, unhighlightClass);- $panel.append($colorBox);- $panel.append($runningDataPointLabel);- // Calls running data- $colorBox.on('mouseenter', showClassUnitHover);- $colorBox.on('mouseout', hideUnitsOnMouseOut);- });- var $detailPanel = $('<div></div>');- _.each(legend.colors, function (color, classIndex) {- var label = legend.tooltips[classIndex];- label = (legend.units) ? label + ' ' + legend.units : label;- var $row = $('<div></div>')- .addClass('wv-palettes-class-detail')- .attr('data-class-index', classIndex);- $colorBox =- $row.append(- $('<span></span>')- .addClass('wv-palettes-class')- .html(' ')- .css('background-color', wv.util.hexToRGB(color)))- .append($('<span></span>')- .addClass('wv-palettes-class-label')- .attr('data-index', index)- .attr('data-class-index', classIndex)- .html(label));- $detailPanel.append($row);- });- if (!rendered) {- rendered = true;- }- };-- self.update = function () {- if (!loaded) {- return;- }- var legends = model.getLegends(layer.id);- _.each(legends, function (legend, index) {- if ((legend.type === 'continuous') ||- (legend.type === 'discrete')) {- wv.palettes.colorbar(selector + ' ' +- '[data-index=\'' + index + '\'] canvas', legend.colors);- showUnitRange(index);- } else if (legend.type === 'classification') {- updateClasses(legend, index);- }- });- };-- var showUnitRange = function (index) {- if (!loaded) {- return;- }- var legends = model.getLegends(layer.id, index);- var entries = model.get(layer.id, index)- .entries;- _.each(legends, function (legend, index) {- var min = legend.minLabel || _.first(legend.tooltips);- var max = legend.maxLabel || _.last(legend.tooltips);- min = (legend.units) ? min + ' ' + legend.units : min;- max = (legend.units) ? max + ' ' + legend.units : max;- $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-min')- .html(min);- $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-max')- .html(max);- var title = legend.title || ' ';- if (legends.length === 1) {- $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-title')- .hide();- } else {- $(selector + ' [data-index=\'' + index + '\'] .wv-palettes-title')- .html(title);- }- });- };- /**- * get color from canvas bar and- * send it to data processing- *- * @method showUnitHover- * @static- * @param {MouseEvent} e- * @return {void}- */- var showUnitHover = function (e, index) {- var rgba;- var pos;- var x;- var y;- var id;- var legends;- if (!loaded) {- return;- }- legends = model.getLegends(layer.id)[index];- offset = $(e.currentTarget)- .offset();- x = e.pageX - offset.left;- y = e.pageY - offset.top;- rgba = wv.util.getCanvasPixelData(e.currentTarget, x, y);- hex = wv.util.rgbaToHex(rgba[0], rgba[1], rgba[2]);- ui.map.runningdata.newLegend(legends, hex);- };-- /**- * get color from data attr and- * send it to data processing and- * render legend- *- * @method showClassUnitHover- * @static- * @param {MouseEvent} e- * @return {void}- */- var showClassUnitHover = function (e) {- var hex = $(this)- .data('hex');- var legends = model.getLegends(layer.id)[0];- ui.map.runningdata.newLegend(legends, hex);- };-- /**- * get color from data attr and- * send it to data processing and- * render legend- *- * @method showClassUnitHover- * @static- * @param {MouseEvent} e- * @return {void}- */- var hideUnitsOnMouseOut = function () {- ui.map.runningdata.clearAll();- };-- var highlightClass = function () {- legendIndex = $(this)- .attr('data-index');- classIndex = $(this)- .attr('data-class-index');- $('.wv-palettes-class-label[data-index=\'' + legendIndex + '\']' +- '[data-class-index=\'' + classIndex + '\']')- .addClass('wv-palettes-class-highlight');- };-- var unhighlightClass = function () {- legendIndex = $(this)- .attr('data-index');- classIndex = $(this)- .attr('data-class-index');- $('.wv-palettes-class-label[data-index=\'' + legendIndex + '\']' +- '[data-class-index=\'' + classIndex + '\']')- .removeClass('wv-palettes-class-highlight');- };-- init();- return self;-};diff --git a/web/js/stub.js b/web/js/stub.jsindex fd6815bfd..53c11b9a5 100644--- a/web/js/stub.js+++ b/web/js/stub.js@@ -14,7 +14,8 @@ import layersOptions from './layers/layers.options';import layersSidebar from './layers/layers.sidebar';import palettes from './palettes/palettes';import palettesLegend from './palettes/palettes.legend';+import palettesModel from './palettes/palettes.model';export default function () {- console.log($, GA, linkUi, linkModel, layerParser, layerValidate, layersAdd, layersActive, layersInfo, layersModal, layersModel, layersOptions, layersSidebar, palettes, palettesLegend);+ console.log($, GA, linkUi, linkModel, layerParser, layerValidate, layersAdd, layersActive, layersInfo, layersModal, layersModel, layersOptions, layersSidebar, palettes, palettesLegend, palettesModel);};