@carto/airship-bridge
Version:
Airship bridge to other libs (CARTO VL, CARTO.js)
168 lines (167 loc) • 6.18 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { select } from '../../util/Utils';
import { rgbaToString, rgbToHex } from '../utils/color';
import { waitUntilLoaded } from '../utils/layers';
var FALLBACK_WIDTH = 16;
var FALLBACK_COLOR = '#000';
function _getLegendData(ramp, options) {
if (options === void 0) { options = {}; }
var legendData = ramp.getLegendData(options.config);
var index = Math.floor(legendData.data.length / 2);
return legendData.data[index].value;
}
function _getColorValue(viz, propName, options) {
if (options === void 0) { options = {}; }
var prop = viz[propName];
if (prop.expressionName === 'ramp' || prop.expressionName === 'opacity') {
return rgbaToString(_getLegendData(prop, options));
}
if (prop.type === 'color') {
return rgbaToString(prop.value);
}
return FALLBACK_COLOR;
}
function _getNumberValue(viz, propName, options) {
if (options === void 0) { options = {}; }
var prop = viz[propName];
if (prop.expressionName === 'ramp') {
return options ? _getLegendData(prop, options) : FALLBACK_WIDTH;
}
if (prop.type === 'number') {
return prop.value;
}
return FALLBACK_WIDTH;
}
function _getSymbolValue(viz, value, options) {
if (value === void 0) { value = null; }
if (options === void 0) { options = {}; }
var prop = value && viz.variables[value]
? viz.variables[value]
: viz.symbol;
if (prop.expressionName === 'ramp') {
return _getLegendData(prop, options);
}
return prop.value;
}
/**
* Get Airship Legends Style object from a VL Layer's Viz
* @param layerWithProps An object with: layer and props properties.
*/
function _styleFromLayer(layerWithProps, options) {
var layer = layerWithProps.layer, props = layerWithProps.props;
var viz = layer.viz;
if (!viz) {
return {};
}
return __assign({ color: _getColorValue(viz, 'color', options), label: props.label, marker: viz.symbol.default ? undefined : _getSymbolValue(viz, options), strokeColor: _getColorValue(viz, 'strokeColor', options), strokeStyle: _getNumberValue(viz, 'strokeWidth', options) === 0 ? 'hidden' : undefined, type: layer.metadata.geomType, width: _getNumberValue(viz, 'width', options) }, props);
}
/**
* Converts a VL layer into the object required by _styleFromLayer, setting
* some default values.
*
* If the correct format is passed, it will just return the value.
*
* @param layer VL Layer or object with layer and props.
*/
function _parseLayer(layer) {
if (layer.props) {
return layer;
}
return {
layer: layer,
props: {
label: layer.id
}
};
}
function _formatRangeValue(value) {
var first = value[0], second = value[1];
if (first === -Infinity) {
return "< " + second.toFixed(2);
}
if (second === Infinity) {
return "> " + first.toFixed(2);
}
return first.toFixed(2) + " - " + second.toFixed(2);
}
function _formatLegendKey(key) {
if (Array.isArray(key)) {
return _formatRangeValue(key);
}
if (key.toFixed) {
return key.toFixed(2);
}
return key;
}
function _formatProp(prop, value) {
if (prop.type === 'color') {
return rgbToHex(value);
}
return value;
}
var Legends = /** @class */ (function () {
function Legends() {
}
Legends.layersLegend = function (widget, layers, options) {
var _this = this;
if (options === void 0) { options = {}; }
widget = select(widget);
var parsedLayers = layers.map(_parseLayer);
parsedLayers.forEach(function (layerWithOpts, index, arr) {
waitUntilLoaded(layerWithOpts.layer, function () {
var data = parsedLayers.map(_styleFromLayer.bind(_this, options));
if (options.format) {
data.label = options.format(data.label, index, arr);
}
widget.data = data;
if (options.onLoad) {
// Fire onLoad on the next cycle, to let the widget paint
setTimeout(options.onLoad, 0);
}
}, options.dynamic);
});
};
Legends.rampLegend = function (widget, layer, prop, options) {
if (options === void 0) { options = {}; }
widget = select(widget);
var parsedLayer = _parseLayer(layer);
waitUntilLoaded(parsedLayer.layer, function () {
var baseStyle = _styleFromLayer(parsedLayer, options);
var vizProp = parsedLayer.layer.viz[prop];
var config = options.config;
var dataProp = options.config.variable || prop;
var data = parsedLayer.layer.viz.variables[dataProp]
? parsedLayer.layer.viz.variables[dataProp]
: parsedLayer.layer.viz[dataProp];
var legendData = data.getLegendData(config).data;
var parsedData = legendData.map(function (legend) {
var _a;
return __assign({}, baseStyle, (_a = {}, _a[prop] = _formatProp(vizProp, legend.value), _a.label = options.format ? options.format(legend.key) : _formatLegendKey(data.key), _a));
});
if (Array.isArray(parsedLayer.props) && parsedLayer.props.length === legendData.length) {
widget.data = parsedData.map(function (d, i) {
return __assign({}, d, parsedLayer.props[i]);
});
}
else {
widget.data = parsedData;
}
if (options.onLoad) {
// Fire onLoad on the next cycle, to let the widget paint
setTimeout(options.onLoad, 0);
}
}, options.dynamic);
};
return Legends;
}());
export default Legends;