智慧水务管理系统 - 精河县供水工程综合管理平台

DataSourceCollection.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import defined from "../Core/defined.js";
  2. import destroyObject from "../Core/destroyObject.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Event from "../Core/Event.js";
  5. import CesiumMath from "../Core/Math.js";
  6. /**
  7. * A collection of {@link DataSource} instances.
  8. * @alias DataSourceCollection
  9. * @constructor
  10. */
  11. function DataSourceCollection() {
  12. this._dataSources = [];
  13. this._dataSourceAdded = new Event();
  14. this._dataSourceRemoved = new Event();
  15. this._dataSourceMoved = new Event();
  16. }
  17. Object.defineProperties(DataSourceCollection.prototype, {
  18. /**
  19. * Gets the number of data sources in this collection.
  20. * @memberof DataSourceCollection.prototype
  21. * @type {number}
  22. * @readonly
  23. */
  24. length: {
  25. get: function () {
  26. return this._dataSources.length;
  27. },
  28. },
  29. /**
  30. * An event that is raised when a data source is added to the collection.
  31. * Event handlers are passed the data source that was added.
  32. * @memberof DataSourceCollection.prototype
  33. * @type {Event}
  34. * @readonly
  35. */
  36. dataSourceAdded: {
  37. get: function () {
  38. return this._dataSourceAdded;
  39. },
  40. },
  41. /**
  42. * An event that is raised when a data source is removed from the collection.
  43. * Event handlers are passed the data source that was removed.
  44. * @memberof DataSourceCollection.prototype
  45. * @type {Event}
  46. * @readonly
  47. */
  48. dataSourceRemoved: {
  49. get: function () {
  50. return this._dataSourceRemoved;
  51. },
  52. },
  53. /**
  54. * An event that is raised when a data source changes position in the collection. Event handlers are passed the data source
  55. * that was moved, its new index after the move, and its old index prior to the move.
  56. * @memberof DataSourceCollection.prototype
  57. * @type {Event}
  58. * @readonly
  59. */
  60. dataSourceMoved: {
  61. get: function () {
  62. return this._dataSourceMoved;
  63. },
  64. },
  65. });
  66. /**
  67. * Adds a data source to the collection.
  68. *
  69. * @param {DataSource|Promise<DataSource>} dataSource A data source or a promise to a data source to add to the collection.
  70. * When passing a promise, the data source will not actually be added
  71. * to the collection until the promise resolves successfully.
  72. * @returns {Promise<DataSource>} A Promise that resolves once the data source has been added to the collection.
  73. */
  74. DataSourceCollection.prototype.add = function (dataSource) {
  75. //>>includeStart('debug', pragmas.debug);
  76. if (!defined(dataSource)) {
  77. throw new DeveloperError("dataSource is required.");
  78. }
  79. //>>includeEnd('debug');
  80. const that = this;
  81. const dataSources = this._dataSources;
  82. return Promise.resolve(dataSource).then(function (value) {
  83. //Only add the data source if removeAll has not been called
  84. //Since it was added.
  85. if (dataSources === that._dataSources) {
  86. that._dataSources.push(value);
  87. that._dataSourceAdded.raiseEvent(that, value);
  88. }
  89. return value;
  90. });
  91. };
  92. /**
  93. * Removes a data source from this collection, if present.
  94. *
  95. * @param {DataSource} dataSource The data source to remove.
  96. * @param {boolean} [destroy=false] Whether to destroy the data source in addition to removing it.
  97. * @returns {boolean} true if the data source was in the collection and was removed,
  98. * false if the data source was not in the collection.
  99. */
  100. DataSourceCollection.prototype.remove = function (dataSource, destroy) {
  101. destroy = destroy ?? false;
  102. const index = this._dataSources.indexOf(dataSource);
  103. if (index !== -1) {
  104. this._dataSources.splice(index, 1);
  105. this._dataSourceRemoved.raiseEvent(this, dataSource);
  106. if (destroy && typeof dataSource.destroy === "function") {
  107. dataSource.destroy();
  108. }
  109. return true;
  110. }
  111. return false;
  112. };
  113. /**
  114. * Removes all data sources from this collection.
  115. *
  116. * @param {boolean} [destroy=false] whether to destroy the data sources in addition to removing them.
  117. */
  118. DataSourceCollection.prototype.removeAll = function (destroy) {
  119. destroy = destroy ?? false;
  120. const dataSources = this._dataSources;
  121. for (let i = 0, len = dataSources.length; i < len; ++i) {
  122. const dataSource = dataSources[i];
  123. this._dataSourceRemoved.raiseEvent(this, dataSource);
  124. if (destroy && typeof dataSource.destroy === "function") {
  125. dataSource.destroy();
  126. }
  127. }
  128. this._dataSources = [];
  129. };
  130. /**
  131. * Checks to see if the collection contains a given data source.
  132. *
  133. * @param {DataSource} dataSource The data source to check for.
  134. * @returns {boolean} true if the collection contains the data source, false otherwise.
  135. */
  136. DataSourceCollection.prototype.contains = function (dataSource) {
  137. return this.indexOf(dataSource) !== -1;
  138. };
  139. /**
  140. * Determines the index of a given data source in the collection.
  141. *
  142. * @param {DataSource} dataSource The data source to find the index of.
  143. * @returns {number} The index of the data source in the collection, or -1 if the data source does not exist in the collection.
  144. */
  145. DataSourceCollection.prototype.indexOf = function (dataSource) {
  146. return this._dataSources.indexOf(dataSource);
  147. };
  148. /**
  149. * Gets a data source by index from the collection.
  150. *
  151. * @param {number} index the index to retrieve.
  152. * @returns {DataSource} The data source at the specified index.
  153. */
  154. DataSourceCollection.prototype.get = function (index) {
  155. //>>includeStart('debug', pragmas.debug);
  156. if (!defined(index)) {
  157. throw new DeveloperError("index is required.");
  158. }
  159. //>>includeEnd('debug');
  160. return this._dataSources[index];
  161. };
  162. /**
  163. * Gets a data source by name from the collection.
  164. *
  165. * @param {string} name The name to retrieve.
  166. * @returns {DataSource[]} A list of all data sources matching the provided name.
  167. */
  168. DataSourceCollection.prototype.getByName = function (name) {
  169. //>>includeStart('debug', pragmas.debug);
  170. if (!defined(name)) {
  171. throw new DeveloperError("name is required.");
  172. }
  173. //>>includeEnd('debug');
  174. return this._dataSources.filter(function (dataSource) {
  175. return dataSource.name === name;
  176. });
  177. };
  178. function getIndex(dataSources, dataSource) {
  179. //>>includeStart('debug', pragmas.debug);
  180. if (!defined(dataSource)) {
  181. throw new DeveloperError("dataSource is required.");
  182. }
  183. //>>includeEnd('debug');
  184. const index = dataSources.indexOf(dataSource);
  185. //>>includeStart('debug', pragmas.debug);
  186. if (index === -1) {
  187. throw new DeveloperError("dataSource is not in this collection.");
  188. }
  189. //>>includeEnd('debug');
  190. return index;
  191. }
  192. function swapDataSources(collection, i, j) {
  193. const arr = collection._dataSources;
  194. const length = arr.length - 1;
  195. i = CesiumMath.clamp(i, 0, length);
  196. j = CesiumMath.clamp(j, 0, length);
  197. if (i === j) {
  198. return;
  199. }
  200. const temp = arr[i];
  201. arr[i] = arr[j];
  202. arr[j] = temp;
  203. collection.dataSourceMoved.raiseEvent(temp, j, i);
  204. }
  205. /**
  206. * Raises a data source up one position in the collection.
  207. *
  208. * @param {DataSource} dataSource The data source to move.
  209. *
  210. * @exception {DeveloperError} dataSource is not in this collection.
  211. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  212. */
  213. DataSourceCollection.prototype.raise = function (dataSource) {
  214. const index = getIndex(this._dataSources, dataSource);
  215. swapDataSources(this, index, index + 1);
  216. };
  217. /**
  218. * Lowers a data source down one position in the collection.
  219. *
  220. * @param {DataSource} dataSource The data source to move.
  221. *
  222. * @exception {DeveloperError} dataSource is not in this collection.
  223. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  224. */
  225. DataSourceCollection.prototype.lower = function (dataSource) {
  226. const index = getIndex(this._dataSources, dataSource);
  227. swapDataSources(this, index, index - 1);
  228. };
  229. /**
  230. * Raises a data source to the top of the collection.
  231. *
  232. * @param {DataSource} dataSource The data source to move.
  233. *
  234. * @exception {DeveloperError} dataSource is not in this collection.
  235. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  236. */
  237. DataSourceCollection.prototype.raiseToTop = function (dataSource) {
  238. const index = getIndex(this._dataSources, dataSource);
  239. if (index === this._dataSources.length - 1) {
  240. return;
  241. }
  242. this._dataSources.splice(index, 1);
  243. this._dataSources.push(dataSource);
  244. this.dataSourceMoved.raiseEvent(
  245. dataSource,
  246. this._dataSources.length - 1,
  247. index,
  248. );
  249. };
  250. /**
  251. * Lowers a data source to the bottom of the collection.
  252. *
  253. * @param {DataSource} dataSource The data source to move.
  254. *
  255. * @exception {DeveloperError} dataSource is not in this collection.
  256. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  257. */
  258. DataSourceCollection.prototype.lowerToBottom = function (dataSource) {
  259. const index = getIndex(this._dataSources, dataSource);
  260. if (index === 0) {
  261. return;
  262. }
  263. this._dataSources.splice(index, 1);
  264. this._dataSources.splice(0, 0, dataSource);
  265. this.dataSourceMoved.raiseEvent(dataSource, 0, index);
  266. };
  267. /**
  268. * Returns true if this object was destroyed; otherwise, false.
  269. * If this object was destroyed, it should not be used; calling any function other than
  270. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  271. *
  272. * @returns {boolean} true if this object was destroyed; otherwise, false.
  273. *
  274. * @see DataSourceCollection#destroy
  275. */
  276. DataSourceCollection.prototype.isDestroyed = function () {
  277. return false;
  278. };
  279. /**
  280. * Destroys the resources held by all data sources in this collection. Explicitly destroying this
  281. * object allows for deterministic release of WebGL resources, instead of relying on the garbage
  282. * collector. Once this object is destroyed, it should not be used; calling any function other than
  283. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  284. * assign the return value (<code>undefined</code>) to the object as done in the example.
  285. *
  286. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  287. *
  288. *
  289. * @example
  290. * dataSourceCollection = dataSourceCollection && dataSourceCollection.destroy();
  291. *
  292. * @see DataSourceCollection#isDestroyed
  293. */
  294. DataSourceCollection.prototype.destroy = function () {
  295. this.removeAll(true);
  296. return destroyObject(this);
  297. };
  298. export default DataSourceCollection;