@vizzuality/layer-manager-plugin-mapboxgl
Version:
## Requirements Install the next peer dependencies: ```sh # with Yarn yarn add deck.gl@7.3.6 luma.gl@7.3.2
62 lines • 3.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVectorStyleLayers = void 0;
const layer_manager_utils_1 = require("@vizzuality/layer-manager-utils");
/**
* Parsed vector style layers that follow the mapbox style layer spec
* and returns mountable layers with ids and opacity applied
* @param {Array} vectorLayers
* @param {Object} layerModel
*/
const getVectorStyleLayers = (vectorLayers, layerModel) => {
const { id, params, sqlParams } = layerModel;
if (vectorLayers && vectorLayers.length) {
const vectorLayersParsed = JSON.parse((0, layer_manager_utils_1.replace)(JSON.stringify(vectorLayers), params, sqlParams));
return (vectorLayersParsed
&& vectorLayersParsed.map((vectorLayer, i) => {
const PAINT_STYLE_NAMES = {
symbol: ['icon', 'text'],
circle: ['circle', 'circle-stroke'],
};
// Select the paint property from the original layer
const { paint = {} } = vectorLayer;
// Select the style to change depending on the type of layer
const opacityPaintNames = PAINT_STYLE_NAMES[vectorLayer.type] || [vectorLayer.type];
const opacityPaintStyles = opacityPaintNames.reduce((obj, name) => {
const currentProperty = paint[`${name}-opacity`];
let paintOpacity = 0.99 * layerModel.opacity;
if (currentProperty !== undefined && currentProperty !== null) {
if (typeof currentProperty === 'number') {
paintOpacity = currentProperty * layerModel.opacity * 0.99;
}
if (Array.isArray(currentProperty)) {
paintOpacity = currentProperty.map((j) => {
if (typeof j === 'number') {
return j * layerModel.opacity * 0.99;
}
return j;
});
}
}
return Object.assign(Object.assign({}, obj), { [`${name}-opacity`]: paintOpacity });
}, {});
// if paint properties are null are passed it breaks interaction
// on mapbox. We need to remove these
const filteredPaintProperties = vectorLayer.paint
&& Object.entries(vectorLayer.paint).reduce((obj, [key, value]) => (Object.assign(Object.assign({}, obj), (!!value && {
[key]: value,
}))), {});
return Object.assign(Object.assign(Object.assign({
// id: This will avoid having issues with any layer,
// but you should specify an id when you create it.
// If you don't set an id in the definition, and you set a fill-opacity, it won't work
id: `${id}-${vectorLayer.type}-${i}` }, vectorLayer), { source: id }), (vectorLayer.paint && {
paint: Object.assign(Object.assign({}, filteredPaintProperties), opacityPaintStyles),
}));
}));
}
return false;
};
exports.getVectorStyleLayers = getVectorStyleLayers;
exports.default = { getVectorStyleLayers: exports.getVectorStyleLayers };
//# sourceMappingURL=vector-style-layers.js.map