123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import destroyObject from "../Core/destroyObject.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import Event from "../Core/Event.js";
- import CesiumMath from "../Core/Math.js";
- import when from "../ThirdParty/when.js";
- function DataSourceCollection() {
- this._dataSources = [];
- this._dataSourceAdded = new Event();
- this._dataSourceRemoved = new Event();
- this._dataSourceMoved = new Event();
- }
- Object.defineProperties(DataSourceCollection.prototype, {
-
- length: {
- get: function () {
- return this._dataSources.length;
- },
- },
-
- dataSourceAdded: {
- get: function () {
- return this._dataSourceAdded;
- },
- },
-
- dataSourceRemoved: {
- get: function () {
- return this._dataSourceRemoved;
- },
- },
-
- dataSourceMoved: {
- get: function () {
- return this._dataSourceMoved;
- },
- },
- });
- DataSourceCollection.prototype.add = function (dataSource) {
-
- if (!defined(dataSource)) {
- throw new DeveloperError("dataSource is required.");
- }
-
- var that = this;
- var dataSources = this._dataSources;
- return when(dataSource, function (value) {
-
-
- if (dataSources === that._dataSources) {
- that._dataSources.push(value);
- that._dataSourceAdded.raiseEvent(that, value);
- }
- return value;
- });
- };
- DataSourceCollection.prototype.remove = function (dataSource, destroy) {
- destroy = defaultValue(destroy, false);
- var index = this._dataSources.indexOf(dataSource);
- if (index !== -1) {
- this._dataSources.splice(index, 1);
- this._dataSourceRemoved.raiseEvent(this, dataSource);
- if (destroy && typeof dataSource.destroy === "function") {
- dataSource.destroy();
- }
- return true;
- }
- return false;
- };
- DataSourceCollection.prototype.removeAll = function (destroy) {
- destroy = defaultValue(destroy, false);
- var dataSources = this._dataSources;
- for (var i = 0, len = dataSources.length; i < len; ++i) {
- var dataSource = dataSources[i];
- this._dataSourceRemoved.raiseEvent(this, dataSource);
- if (destroy && typeof dataSource.destroy === "function") {
- dataSource.destroy();
- }
- }
- this._dataSources = [];
- };
- DataSourceCollection.prototype.contains = function (dataSource) {
- return this.indexOf(dataSource) !== -1;
- };
- DataSourceCollection.prototype.indexOf = function (dataSource) {
- return this._dataSources.indexOf(dataSource);
- };
- DataSourceCollection.prototype.get = function (index) {
-
- if (!defined(index)) {
- throw new DeveloperError("index is required.");
- }
-
- return this._dataSources[index];
- };
- DataSourceCollection.prototype.getByName = function (name) {
-
- if (!defined(name)) {
- throw new DeveloperError("name is required.");
- }
-
- return this._dataSources.filter(function (dataSource) {
- return dataSource.name === name;
- });
- };
- function getIndex(dataSources, dataSource) {
-
- if (!defined(dataSource)) {
- throw new DeveloperError("dataSource is required.");
- }
-
- var index = dataSources.indexOf(dataSource);
-
- if (index === -1) {
- throw new DeveloperError("dataSource is not in this collection.");
- }
-
- return index;
- }
- function swapDataSources(collection, i, j) {
- var arr = collection._dataSources;
- var length = arr.length - 1;
- i = CesiumMath.clamp(i, 0, length);
- j = CesiumMath.clamp(j, 0, length);
- if (i === j) {
- return;
- }
- var temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- collection.dataSourceMoved.raiseEvent(temp, j, i);
- }
- DataSourceCollection.prototype.raise = function (dataSource) {
- var index = getIndex(this._dataSources, dataSource);
- swapDataSources(this, index, index + 1);
- };
- DataSourceCollection.prototype.lower = function (dataSource) {
- var index = getIndex(this._dataSources, dataSource);
- swapDataSources(this, index, index - 1);
- };
- DataSourceCollection.prototype.raiseToTop = function (dataSource) {
- var index = getIndex(this._dataSources, dataSource);
- if (index === this._dataSources.length - 1) {
- return;
- }
- this._dataSources.splice(index, 1);
- this._dataSources.push(dataSource);
- this.dataSourceMoved.raiseEvent(
- dataSource,
- this._dataSources.length - 1,
- index
- );
- };
- DataSourceCollection.prototype.lowerToBottom = function (dataSource) {
- var index = getIndex(this._dataSources, dataSource);
- if (index === 0) {
- return;
- }
- this._dataSources.splice(index, 1);
- this._dataSources.splice(0, 0, dataSource);
- this.dataSourceMoved.raiseEvent(dataSource, 0, index);
- };
- DataSourceCollection.prototype.isDestroyed = function () {
- return false;
- };
- DataSourceCollection.prototype.destroy = function () {
- this.removeAll(true);
- return destroyObject(this);
- };
- export default DataSourceCollection;
|