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

EntityCollection.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import AssociativeArray from "../Core/AssociativeArray.js";
  2. import createGuid from "../Core/createGuid.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import Event from "../Core/Event.js";
  6. import Iso8601 from "../Core/Iso8601.js";
  7. import JulianDate from "../Core/JulianDate.js";
  8. import TimeInterval from "../Core/TimeInterval.js";
  9. import Entity from "./Entity.js";
  10. const entityOptionsScratch = {
  11. id: undefined,
  12. };
  13. function fireChangedEvent(collection) {
  14. if (collection._firing) {
  15. collection._refire = true;
  16. return;
  17. }
  18. if (collection._suspendCount === 0) {
  19. const added = collection._addedEntities;
  20. const removed = collection._removedEntities;
  21. const changed = collection._changedEntities;
  22. if (changed.length !== 0 || added.length !== 0 || removed.length !== 0) {
  23. collection._firing = true;
  24. do {
  25. collection._refire = false;
  26. const addedArray = added.values.slice(0);
  27. const removedArray = removed.values.slice(0);
  28. const changedArray = changed.values.slice(0);
  29. added.removeAll();
  30. removed.removeAll();
  31. changed.removeAll();
  32. collection._collectionChanged.raiseEvent(
  33. collection,
  34. addedArray,
  35. removedArray,
  36. changedArray,
  37. );
  38. } while (collection._refire);
  39. collection._firing = false;
  40. }
  41. }
  42. }
  43. /**
  44. * An observable collection of {@link Entity} instances where each entity has a unique id.
  45. * @alias EntityCollection
  46. * @constructor
  47. *
  48. * @param {DataSource|CompositeEntityCollection} [owner] The data source (or composite entity collection) which created this collection.
  49. */
  50. function EntityCollection(owner) {
  51. this._owner = owner;
  52. this._entities = new AssociativeArray();
  53. this._addedEntities = new AssociativeArray();
  54. this._removedEntities = new AssociativeArray();
  55. this._changedEntities = new AssociativeArray();
  56. this._suspendCount = 0;
  57. this._collectionChanged = new Event();
  58. this._id = createGuid();
  59. this._show = true;
  60. this._firing = false;
  61. this._refire = false;
  62. }
  63. /**
  64. * Prevents {@link EntityCollection#collectionChanged} events from being raised
  65. * until a corresponding call is made to {@link EntityCollection#resumeEvents}, at which
  66. * point a single event will be raised that covers all suspended operations.
  67. * This allows for many items to be added and removed efficiently.
  68. * This function can be safely called multiple times as long as there
  69. * are corresponding calls to {@link EntityCollection#resumeEvents}.
  70. */
  71. EntityCollection.prototype.suspendEvents = function () {
  72. this._suspendCount++;
  73. };
  74. /**
  75. * Resumes raising {@link EntityCollection#collectionChanged} events immediately
  76. * when an item is added or removed. Any modifications made while while events were suspended
  77. * will be triggered as a single event when this function is called.
  78. * This function is reference counted and can safely be called multiple times as long as there
  79. * are corresponding calls to {@link EntityCollection#resumeEvents}.
  80. *
  81. * @exception {DeveloperError} resumeEvents can not be called before suspendEvents.
  82. */
  83. EntityCollection.prototype.resumeEvents = function () {
  84. //>>includeStart('debug', pragmas.debug);
  85. if (this._suspendCount === 0) {
  86. throw new DeveloperError(
  87. "resumeEvents can not be called before suspendEvents.",
  88. );
  89. }
  90. //>>includeEnd('debug');
  91. this._suspendCount--;
  92. fireChangedEvent(this);
  93. };
  94. /**
  95. * The signature of the event generated by {@link EntityCollection#collectionChanged}.
  96. * @callback EntityCollection.CollectionChangedEventCallback
  97. *
  98. * @param {EntityCollection} collection The collection that triggered the event.
  99. * @param {Entity[]} added The array of {@link Entity} instances that have been added to the collection.
  100. * @param {Entity[]} removed The array of {@link Entity} instances that have been removed from the collection.
  101. * @param {Entity[]} changed The array of {@link Entity} instances that have been modified.
  102. */
  103. Object.defineProperties(EntityCollection.prototype, {
  104. /**
  105. * Gets the event that is fired when entities are added or removed from the collection.
  106. * The generated event is a {@link EntityCollection.CollectionChangedEventCallback}.
  107. * @memberof EntityCollection.prototype
  108. * @readonly
  109. * @type {Event<EntityCollection.CollectionChangedEventCallback>}
  110. */
  111. collectionChanged: {
  112. get: function () {
  113. return this._collectionChanged;
  114. },
  115. },
  116. /**
  117. * Gets a globally unique identifier for this collection.
  118. * @memberof EntityCollection.prototype
  119. * @readonly
  120. * @type {string}
  121. */
  122. id: {
  123. get: function () {
  124. return this._id;
  125. },
  126. },
  127. /**
  128. * Gets the array of Entity instances in the collection.
  129. * This array should not be modified directly.
  130. * @memberof EntityCollection.prototype
  131. * @readonly
  132. * @type {Entity[]}
  133. */
  134. values: {
  135. get: function () {
  136. return this._entities.values;
  137. },
  138. },
  139. /**
  140. * Gets whether or not this entity collection should be
  141. * displayed. When true, each entity is only displayed if
  142. * its own show property is also true.
  143. * @memberof EntityCollection.prototype
  144. * @type {boolean}
  145. */
  146. show: {
  147. get: function () {
  148. return this._show;
  149. },
  150. set: function (value) {
  151. //>>includeStart('debug', pragmas.debug);
  152. if (!defined(value)) {
  153. throw new DeveloperError("value is required.");
  154. }
  155. //>>includeEnd('debug');
  156. if (value === this._show) {
  157. return;
  158. }
  159. //Since entity.isShowing includes the EntityCollection.show state
  160. //in its calculation, we need to loop over the entities array
  161. //twice, once to get the old showing value and a second time
  162. //to raise the changed event.
  163. this.suspendEvents();
  164. let i;
  165. const oldShows = [];
  166. const entities = this._entities.values;
  167. const entitiesLength = entities.length;
  168. for (i = 0; i < entitiesLength; i++) {
  169. oldShows.push(entities[i].isShowing);
  170. }
  171. this._show = value;
  172. for (i = 0; i < entitiesLength; i++) {
  173. const oldShow = oldShows[i];
  174. const entity = entities[i];
  175. if (oldShow !== entity.isShowing) {
  176. entity.definitionChanged.raiseEvent(
  177. entity,
  178. "isShowing",
  179. entity.isShowing,
  180. oldShow,
  181. );
  182. }
  183. }
  184. this.resumeEvents();
  185. },
  186. },
  187. /**
  188. * Gets the owner of this entity collection, ie. the data source or composite entity collection which created it.
  189. * @memberof EntityCollection.prototype
  190. * @readonly
  191. * @type {DataSource|CompositeEntityCollection}
  192. */
  193. owner: {
  194. get: function () {
  195. return this._owner;
  196. },
  197. },
  198. });
  199. /**
  200. * Computes the maximum availability of the entities in the collection.
  201. * If the collection contains a mix of infinitely available data and non-infinite data,
  202. * it will return the interval pertaining to the non-infinite data only. If all
  203. * data is infinite, an infinite interval will be returned.
  204. *
  205. * @returns {TimeInterval} The availability of entities in the collection.
  206. */
  207. EntityCollection.prototype.computeAvailability = function () {
  208. let startTime = Iso8601.MAXIMUM_VALUE;
  209. let stopTime = Iso8601.MINIMUM_VALUE;
  210. const entities = this._entities.values;
  211. for (let i = 0, len = entities.length; i < len; i++) {
  212. const entity = entities[i];
  213. const availability = entity.availability;
  214. if (defined(availability)) {
  215. const start = availability.start;
  216. const stop = availability.stop;
  217. if (
  218. JulianDate.lessThan(start, startTime) &&
  219. !start.equals(Iso8601.MINIMUM_VALUE)
  220. ) {
  221. startTime = start;
  222. }
  223. if (
  224. JulianDate.greaterThan(stop, stopTime) &&
  225. !stop.equals(Iso8601.MAXIMUM_VALUE)
  226. ) {
  227. stopTime = stop;
  228. }
  229. }
  230. }
  231. if (Iso8601.MAXIMUM_VALUE.equals(startTime)) {
  232. startTime = Iso8601.MINIMUM_VALUE;
  233. }
  234. if (Iso8601.MINIMUM_VALUE.equals(stopTime)) {
  235. stopTime = Iso8601.MAXIMUM_VALUE;
  236. }
  237. return new TimeInterval({
  238. start: startTime,
  239. stop: stopTime,
  240. });
  241. };
  242. /**
  243. * Add an entity to the collection.
  244. *
  245. * @param {Entity | Entity.ConstructorOptions} entity The entity to be added.
  246. * @returns {Entity} The entity that was added.
  247. * @exception {DeveloperError} An entity with <entity.id> already exists in this collection.
  248. */
  249. EntityCollection.prototype.add = function (entity) {
  250. //>>includeStart('debug', pragmas.debug);
  251. if (!defined(entity)) {
  252. throw new DeveloperError("entity is required.");
  253. }
  254. //>>includeEnd('debug');
  255. if (!(entity instanceof Entity)) {
  256. entity = new Entity(entity);
  257. }
  258. const id = entity.id;
  259. const entities = this._entities;
  260. if (entities.contains(id)) {
  261. throw new DeveloperError(
  262. `An entity with id ${id} already exists in this collection.`,
  263. );
  264. }
  265. entity.entityCollection = this;
  266. entities.set(id, entity);
  267. if (!this._removedEntities.remove(id)) {
  268. this._addedEntities.set(id, entity);
  269. }
  270. entity.definitionChanged.addEventListener(
  271. EntityCollection.prototype._onEntityDefinitionChanged,
  272. this,
  273. );
  274. fireChangedEvent(this);
  275. return entity;
  276. };
  277. /**
  278. * Removes an entity from the collection.
  279. *
  280. * @param {Entity} entity The entity to be removed.
  281. * @returns {boolean} true if the item was removed, false if it did not exist in the collection.
  282. */
  283. EntityCollection.prototype.remove = function (entity) {
  284. if (!defined(entity)) {
  285. return false;
  286. }
  287. return this.removeById(entity.id);
  288. };
  289. /**
  290. * Returns true if the provided entity is in this collection, false otherwise.
  291. *
  292. * @param {Entity} entity The entity.
  293. * @returns {boolean} true if the provided entity is in this collection, false otherwise.
  294. */
  295. EntityCollection.prototype.contains = function (entity) {
  296. //>>includeStart('debug', pragmas.debug);
  297. if (!defined(entity)) {
  298. throw new DeveloperError("entity is required");
  299. }
  300. //>>includeEnd('debug');
  301. return this._entities.get(entity.id) === entity;
  302. };
  303. /**
  304. * Removes an entity with the provided id from the collection.
  305. *
  306. * @param {string} id The id of the entity to remove.
  307. * @returns {boolean} true if the item was removed, false if no item with the provided id existed in the collection.
  308. */
  309. EntityCollection.prototype.removeById = function (id) {
  310. if (!defined(id)) {
  311. return false;
  312. }
  313. const entities = this._entities;
  314. const entity = entities.get(id);
  315. if (!this._entities.remove(id)) {
  316. return false;
  317. }
  318. if (!this._addedEntities.remove(id)) {
  319. this._removedEntities.set(id, entity);
  320. this._changedEntities.remove(id);
  321. }
  322. this._entities.remove(id);
  323. entity.definitionChanged.removeEventListener(
  324. EntityCollection.prototype._onEntityDefinitionChanged,
  325. this,
  326. );
  327. fireChangedEvent(this);
  328. return true;
  329. };
  330. /**
  331. * Removes all Entities from the collection.
  332. */
  333. EntityCollection.prototype.removeAll = function () {
  334. //The event should only contain items added before events were suspended
  335. //and the contents of the collection.
  336. const entities = this._entities;
  337. const entitiesLength = entities.length;
  338. const array = entities.values;
  339. const addedEntities = this._addedEntities;
  340. const removed = this._removedEntities;
  341. for (let i = 0; i < entitiesLength; i++) {
  342. const existingItem = array[i];
  343. const existingItemId = existingItem.id;
  344. const addedItem = addedEntities.get(existingItemId);
  345. if (!defined(addedItem)) {
  346. existingItem.definitionChanged.removeEventListener(
  347. EntityCollection.prototype._onEntityDefinitionChanged,
  348. this,
  349. );
  350. removed.set(existingItemId, existingItem);
  351. }
  352. }
  353. entities.removeAll();
  354. addedEntities.removeAll();
  355. this._changedEntities.removeAll();
  356. fireChangedEvent(this);
  357. };
  358. /**
  359. * Gets an entity with the specified id.
  360. *
  361. * @param {string} id The id of the entity to retrieve.
  362. * @returns {Entity|undefined} The entity with the provided id or undefined if the id did not exist in the collection.
  363. */
  364. EntityCollection.prototype.getById = function (id) {
  365. //>>includeStart('debug', pragmas.debug);
  366. if (!defined(id)) {
  367. throw new DeveloperError("id is required.");
  368. }
  369. //>>includeEnd('debug');
  370. return this._entities.get(id);
  371. };
  372. /**
  373. * Gets an entity with the specified id or creates it and adds it to the collection if it does not exist.
  374. *
  375. * @param {string} id The id of the entity to retrieve or create.
  376. * @returns {Entity} The new or existing object.
  377. */
  378. EntityCollection.prototype.getOrCreateEntity = function (id) {
  379. //>>includeStart('debug', pragmas.debug);
  380. if (!defined(id)) {
  381. throw new DeveloperError("id is required.");
  382. }
  383. //>>includeEnd('debug');
  384. let entity = this._entities.get(id);
  385. if (!defined(entity)) {
  386. entityOptionsScratch.id = id;
  387. entity = new Entity(entityOptionsScratch);
  388. this.add(entity);
  389. }
  390. return entity;
  391. };
  392. EntityCollection.prototype._onEntityDefinitionChanged = function (entity) {
  393. const id = entity.id;
  394. if (!this._addedEntities.contains(id)) {
  395. this._changedEntities.set(id, entity);
  396. }
  397. fireChangedEvent(this);
  398. };
  399. export default EntityCollection;