tchen-vuelayers
Version:
Web map Vue components with the power of OpenLayers
326 lines (279 loc) • 6.25 kB
JavaScript
/**
* 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 _Object$create from '@babel/runtime-corejs2/core-js/object/create';
import _classCallCheck from '@babel/runtime-corejs2/helpers/esm/classCallCheck';
import _createClass from '@babel/runtime-corejs2/helpers/esm/createClass';
import { forEach } from '../util/minilo';
/**
* Wraps OpenLayers collection to provide indexed access to elements.
*/
var IndexedCollectionAdapter =
/*#__PURE__*/
function () {
/**
* @param {Collection} collection
* @param {function} getElementKey
*/
function IndexedCollectionAdapter(collection, getElementKey) {
var _this = this;
_classCallCheck(this, IndexedCollectionAdapter);
/**
* @type {Collection}
* @private
*/
this._adaptee = collection;
/**
* @type {Function}
* @private
*/
this._getElementKey = getElementKey;
/**
* @type {Object<mixed, number>}
* @private
*/
this._index = _Object$create(null);
this._adaptee.forEach(function (element) {
return _this._addToIndex(_this._getElementKey(element), element);
});
}
/**
* @return {Collection}
*/
_createClass(IndexedCollectionAdapter, [{
key: "forEach",
/**
* @param {function} iteratee
*/
value: function forEach$$1(iteratee) {
forEach(this.elements, iteratee);
}
/**
* @param {*} element
*/
}, {
key: "add",
value: function add(element) {
var key = this._getElementKey(element);
if (key != null) {
this._adaptee.push(element);
this._index[key] = element;
}
}
/**
* @param {*} element
*/
}, {
key: "remove",
value: function remove(element) {
if (this._adaptee.remove(element)) {
this._removeFromIndex(this._getElementKey(element));
}
}
/**
* @param {*} element
* @return {boolean}
*/
}, {
key: "has",
value: function has(element) {
return !!this.findByKey(this._getElementKey(element));
}
/**
* @return {void}
*/
}, {
key: "clear",
value: function clear() {
this._adaptee.clear();
this._resetIndex();
}
/**
* @param {*} key
* @return {*|undefined}
*/
}, {
key: "findByKey",
value: function findByKey(key) {
return key && this._index[key];
}
/**
* @param {function} sorter
*/
}, {
key: "sort",
value: function sort(sorter) {
this.elements.sort(sorter);
}
/**
* @private
*/
}, {
key: "_resetIndex",
value: function _resetIndex() {
this._index = _Object$create(null);
}
/**
* @param {string} key
* @param {*} element
* @private
*/
}, {
key: "_addToIndex",
value: function _addToIndex(key, element) {
if (key == null) {
return false;
}
this._index[key] = element;
element.on('propertychange', this._handleElementChange, this);
return true;
}
/**
* @param {string} key
* @private
*/
}, {
key: "_removeFromIndex",
value: function _removeFromIndex(key) {
var element = this.findByKey(key);
if (element) {
element.un('propertychange', this._handleElementChange, this);
delete this._index[key];
}
return !!element;
}
/**
* Updates index
* @param {*} target
* @private
*/
}, {
key: "_handleElementChange",
value: function _handleElementChange(_ref) {
var target = _ref.target;
var key = this._getElementKey(target); // remove by old key
if (this.findByKey(key) !== target) {
for (var k in this._index) {
if (this._index[k] === target) {
this._removeFromIndex(k);
break;
}
}
}
this._addToIndex(key, target);
}
}, {
key: "adaptee",
get: function get() {
return this._adaptee;
}
}, {
key: "elements",
get: function get() {
return this._adaptee.getArray();
}
}]);
return IndexedCollectionAdapter;
}();
/**
* Wraps vector source to provide collection like API.
*/
var SourceCollectionAdapter =
/*#__PURE__*/
function () {
/**
* @param {Vector} source
*/
function SourceCollectionAdapter(source) {
_classCallCheck(this, SourceCollectionAdapter);
/**
* @type {Vector}
* @private
*/
this._adaptee = source;
}
/**
* @return {Vector}
*/
_createClass(SourceCollectionAdapter, [{
key: "forEach",
/**
* @param {function} iteratee
*/
value: function forEach$$1(iteratee) {
this.elements.forEach(iteratee);
}
/**
* @param {Feature} feature
*/
}, {
key: "add",
value: function add(feature) {
this._adaptee.addFeature(feature);
}
/**
* @param {Feature} feature
*/
}, {
key: "remove",
value: function remove(feature) {
this._adaptee.removeFeature(feature);
}
/**
* @param {Feature} feature
* @return {boolean}
*/
}, {
key: "has",
value: function has(feature) {
return !!this.findByKey(feature.getId());
}
/**
* @return {void}
*/
}, {
key: "clear",
value: function clear() {
this._adaptee.clear();
}
/**
* @param {*} key
* @return {Feature|undefined}
*/
}, {
key: "findByKey",
value: function findByKey(key) {
return this._adaptee.getFeatureById(key);
}
/**
* @param {function} sorter
*/
}, {
key: "sort",
value: function sort(sorter) {
throw new Error('Not supported');
}
}, {
key: "adaptee",
get: function get() {
return this._adaptee;
}
/**
* @return {Array<Feature>}
*/
}, {
key: "elements",
get: function get() {
return this._adaptee.getFeatures();
}
}]);
return SourceCollectionAdapter;
}();
export { IndexedCollectionAdapter, SourceCollectionAdapter };