UNPKG

tchen-vuelayers

Version:

Web map Vue components with the power of OpenLayers

132 lines (115 loc) 2.95 kB
/** * VueLayers * Web map Vue components with the power of OpenLayers * * @package vuelayers * @author Vladimir Vershinin <ghettovoice@gmail.com> * @version 0.11.1 * @license MIT * @copyright (c) 2017-2019, Vladimir Vershinin <ghettovoice@gmail.com> */ import Feature from 'ol/Feature'; import Vue from 'vue'; import { initFeature } from '../ol-ext/feature'; import { instanceOf } from '../util/assert'; import { isPlainObject } from '../util/minilo'; import projTransforms from './proj-transforms'; var methods = { /** * @return {IndexedCollectionAdapter|SourceCollectionAdapter} * @protected */ getFeaturesTarget: function getFeaturesTarget() { throw new Error('Not implemented method'); }, /** * @param {Array<(Feature|Vue|Object)>} features * @return {void} */ addFeatures: function addFeatures(features) { features.forEach(this.addFeature.bind(this)); }, /** * @param {Feature|Vue|Object} feature * @return {void} */ addFeature: function addFeature(feature) { if (feature instanceof Vue) { feature = feature.$feature; } else if (isPlainObject(feature)) { feature = this.readFeatureInDataProj(feature); } instanceOf(feature, Feature); this.prepareFeature(feature); if (!this.getFeaturesTarget().has(feature)) { this.getFeaturesTarget().add(feature); } }, /** * @param {Array<(Feature|Vue|Object)>} features * @return {void} */ removeFeatures: function removeFeatures(features) { features.forEach(this.removeFeature.bind(this)); }, /** * @param {Feature|Vue|Object} feature * @return {void} */ removeFeature: function removeFeature(feature) { if (feature instanceof Vue) { feature = feature.$feature; } else if (isPlainObject(feature)) { // feature = this._features[feature.id] feature = this.getFeatureById(feature.id); } if (!feature) return; if (this.getFeaturesTarget().has(feature)) { this.getFeaturesTarget().remove(feature); } }, /** * @return {void} */ clearFeatures: function clearFeatures() { this.getFeaturesTarget().clear(); }, /** * @param {string|number} id * @return {Feature|undefined} */ getFeatureById: function getFeatureById(id) { return this.getFeaturesTarget().findByKey(id); }, /** * @return {Feature[]} */ getFeatures: function getFeatures() { return this.getFeaturesTarget().elements; }, /** * @returns {Object} * @protected */ getServices: function getServices() { var vm = this; return { get featuresContainer() { return vm; } }; }, /** * @param {Feature} feature * @return {Feature} * @protected */ prepareFeature: function prepareFeature(feature) { return initFeature(feature); } }; var featuresContainer = { mixins: [projTransforms], methods: methods }; export default featuresContainer;