@vizzuality/layer-manager
Version:
## Installation ```sh # with Yarn yarn add @vizzuality/layer-manager
203 lines • 7.03 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cancelable_promise_1 = require("cancelable-promise");
const layer_manager_utils_1 = require("@vizzuality/layer-manager-utils");
const layer_model_1 = __importDefault(require("./layer-model"));
const defaultLayerOptions = {
opacity: 1,
visibility: true,
zIndex: 0,
};
class LayerManager {
constructor(plugin) {
this._layers = [];
this._promises = {};
this._plugin = plugin;
this._plugin.setOptions({ getLayers: this.getLayers.bind(this) });
}
/**
* Add new layer
* @param {Array} layers
* @param {Object} layerOptions
* @param {Function} onAfterAdd
*/
add(layer, layerOptions = defaultLayerOptions, onAfterAdd) {
if (!layer)
throw new Error('layer param is required');
const layerModel = new layer_model_1.default(Object.assign(Object.assign({}, layer), layerOptions));
const layerWasAlreadyAdded = this._layers.find(({ id }) => id === layerModel.id);
// Only add a new layer if it was not added before
if (!layerWasAlreadyAdded) {
this._layers.push(layerModel);
}
this.requestLayer(layerModel, onAfterAdd); // TO-DO: review
return this._layers;
}
/**
* Updating a specific layer by ID
* @param {String} id
* @param {Object} newLayerSpec
*/
update(id, newLayerSpec) {
const layerModel = this.getLayerModel(id);
if (!layerModel || !layerModel.mapLayer)
return;
layerModel.update(newLayerSpec);
const { opacity, visibility, zIndex, source, render, params, sqlParams, deck, } = newLayerSpec;
if (typeof opacity !== 'undefined') {
this._plugin.setOpacity(layerModel, opacity);
}
if (typeof visibility !== 'undefined') {
this._plugin.setVisibility(layerModel, visibility);
}
if (typeof zIndex !== 'undefined') {
this._plugin.setZIndex(layerModel, zIndex);
}
if (!(0, layer_manager_utils_1.isEmpty)(source)) {
this._plugin.setSource(layerModel);
}
if (!(0, layer_manager_utils_1.isEmpty)(render)) {
this._plugin.setRender(layerModel);
}
if (!(0, layer_manager_utils_1.isEmpty)(params)) {
this._plugin.setParams(layerModel);
}
if (!(0, layer_manager_utils_1.isEmpty)(sqlParams)) {
this._plugin.setSQLParams(layerModel);
}
if (!(0, layer_manager_utils_1.isEmpty)(deck)) {
this._plugin.setDeck(layerModel);
}
}
/**
* Remove a layer giving a Layer ID
* @param {String} id
* @param {Function} onAfterRemove
*/
remove(id, onAfterRemove) {
const layers = this._layers.slice(0);
this.requestCancel(id);
const layerModel = this.getLayerModel(id);
if (layerModel) {
this._plugin.remove(layerModel);
onAfterRemove(layerModel);
}
this._layers = layers.filter((l) => l.id !== id);
}
/**
* Same as layers getter
* NOTE: This method will be DEPRECATED
* @returns []
*/
getLayers() {
return this._layers;
}
getLayerModel(id) {
return this._layers.find((layerModel) => layerModel.id === id);
}
/**
* A namespace to set opacity on selected layer
* @param {String} id
* @param {Number} opacity
*/
setOpacity(id, opacity) {
const layerModel = this.getLayerModel(id);
if (layerModel)
this._plugin.setOpacity(layerModel, opacity);
}
/**
* A namespace to hide or show a selected layer
* @param {String} id
* @param {Boolean} visibility
*/
setVisibility(id, visibility) {
const layerModel = this.getLayerModel(id);
if (layerModel)
this._plugin.setVisibility(layerModel, visibility);
}
/**
* A namespace to set z-index on selected layer
* @param {String} id
* @param {Number} zIndex
*/
setZIndex(id, zIndex) {
const layerModel = this.getLayerModel(id);
if (layerModel)
this._plugin.setZIndex(layerModel, zIndex);
}
requestLayer(layerModel, onAfterAdd) {
const { id, type } = layerModel;
const method = this._plugin.getLayerByType(type);
if (!method) {
this._promises[id] = cancelable_promise_1.CancelablePromise.reject(new Error(`${type} type is not yet supported.`));
}
else {
// Cancel previous/existing request
this.requestCancel(layerModel.id);
// every request method returns a promise that we store in the array
// to control when all layers are fetched.
this._promises[layerModel.id] = method
.call(this, layerModel, LayerManager.providers)
.then((layer) => {
const { isCanceled } = this._promises[layerModel.id];
if (!isCanceled()) {
layerModel.setMapLayer(layer);
this._plugin.add(layerModel, this._layers);
if (layerModel.zIndex || layerModel.zIndex === 0) {
this._plugin.setZIndex(layerModel, layerModel.zIndex);
}
if (layerModel.opacity || layerModel.opacity === 0) {
this._plugin.setOpacity(layerModel, layerModel.opacity);
}
if (layerModel.visibility) {
this._plugin.setVisibility(layerModel, layerModel.visibility);
}
this._plugin.setRender(layerModel);
onAfterAdd(layerModel);
}
});
}
}
/**
* Cancel previous/existing request
*/
requestCancel(id) {
if (this._promises[id]) {
this._promises[id].cancel();
}
}
/**
* Util for React component.
* NOTE: This method will be DEPRECATED
*/
unmount() {
this._plugin.map = null;
this._plugin.unmount();
}
/**
* Access to map instance, provided by the plugin.
* NOTE: This method will be DEPRECATED
*/
get map() {
return this._plugin.map;
}
/**
* Access to layer collection instances
*/
get layers() {
return this._layers;
}
/**
* Method to register a new provider
* @param provider Instance of ProviderMaker
*/
static registerProvider(provider) {
LayerManager.providers = Object.assign(Object.assign({}, LayerManager.providers), { [provider.name]: provider.handleData });
}
}
LayerManager.providers = {};
exports.default = LayerManager;
//# sourceMappingURL=layer-manager.js.map