123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- import AssociativeArray from "../Core/AssociativeArray.js";
- import createGuid from "../Core/createGuid.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import Event from "../Core/Event.js";
- import Iso8601 from "../Core/Iso8601.js";
- import JulianDate from "../Core/JulianDate.js";
- import RuntimeError from "../Core/RuntimeError.js";
- import TimeInterval from "../Core/TimeInterval.js";
- import Entity from "./Entity.js";
- var entityOptionsScratch = {
- id: undefined,
- };
- function fireChangedEvent(collection) {
- if (collection._firing) {
- collection._refire = true;
- return;
- }
- if (collection._suspendCount === 0) {
- var added = collection._addedEntities;
- var removed = collection._removedEntities;
- var changed = collection._changedEntities;
- if (changed.length !== 0 || added.length !== 0 || removed.length !== 0) {
- collection._firing = true;
- do {
- collection._refire = false;
- var addedArray = added.values.slice(0);
- var removedArray = removed.values.slice(0);
- var changedArray = changed.values.slice(0);
- added.removeAll();
- removed.removeAll();
- changed.removeAll();
- collection._collectionChanged.raiseEvent(
- collection,
- addedArray,
- removedArray,
- changedArray
- );
- } while (collection._refire);
- collection._firing = false;
- }
- }
- }
- function EntityCollection(owner) {
- this._owner = owner;
- this._entities = new AssociativeArray();
- this._addedEntities = new AssociativeArray();
- this._removedEntities = new AssociativeArray();
- this._changedEntities = new AssociativeArray();
- this._suspendCount = 0;
- this._collectionChanged = new Event();
- this._id = createGuid();
- this._show = true;
- this._firing = false;
- this._refire = false;
- }
- EntityCollection.prototype.suspendEvents = function () {
- this._suspendCount++;
- };
- EntityCollection.prototype.resumeEvents = function () {
-
- if (this._suspendCount === 0) {
- throw new DeveloperError(
- "resumeEvents can not be called before suspendEvents."
- );
- }
-
- this._suspendCount--;
- fireChangedEvent(this);
- };
- EntityCollection.collectionChangedEventCallback = undefined;
- Object.defineProperties(EntityCollection.prototype, {
-
- collectionChanged: {
- get: function () {
- return this._collectionChanged;
- },
- },
-
- id: {
- get: function () {
- return this._id;
- },
- },
-
- values: {
- get: function () {
- return this._entities.values;
- },
- },
-
- show: {
- get: function () {
- return this._show;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- if (value === this._show) {
- return;
- }
-
-
-
-
- this.suspendEvents();
- var i;
- var oldShows = [];
- var entities = this._entities.values;
- var entitiesLength = entities.length;
- for (i = 0; i < entitiesLength; i++) {
- oldShows.push(entities[i].isShowing);
- }
- this._show = value;
- for (i = 0; i < entitiesLength; i++) {
- var oldShow = oldShows[i];
- var entity = entities[i];
- if (oldShow !== entity.isShowing) {
- entity.definitionChanged.raiseEvent(
- entity,
- "isShowing",
- entity.isShowing,
- oldShow
- );
- }
- }
- this.resumeEvents();
- },
- },
-
- owner: {
- get: function () {
- return this._owner;
- },
- },
- });
- EntityCollection.prototype.computeAvailability = function () {
- var startTime = Iso8601.MAXIMUM_VALUE;
- var stopTime = Iso8601.MINIMUM_VALUE;
- var entities = this._entities.values;
- for (var i = 0, len = entities.length; i < len; i++) {
- var entity = entities[i];
- var availability = entity.availability;
- if (defined(availability)) {
- var start = availability.start;
- var stop = availability.stop;
- if (
- JulianDate.lessThan(start, startTime) &&
- !start.equals(Iso8601.MINIMUM_VALUE)
- ) {
- startTime = start;
- }
- if (
- JulianDate.greaterThan(stop, stopTime) &&
- !stop.equals(Iso8601.MAXIMUM_VALUE)
- ) {
- stopTime = stop;
- }
- }
- }
- if (Iso8601.MAXIMUM_VALUE.equals(startTime)) {
- startTime = Iso8601.MINIMUM_VALUE;
- }
- if (Iso8601.MINIMUM_VALUE.equals(stopTime)) {
- stopTime = Iso8601.MAXIMUM_VALUE;
- }
- return new TimeInterval({
- start: startTime,
- stop: stopTime,
- });
- };
- EntityCollection.prototype.add = function (entity) {
-
- if (!defined(entity)) {
- throw new DeveloperError("entity is required.");
- }
-
- if (!(entity instanceof Entity)) {
- entity = new Entity(entity);
- }
- var id = entity.id;
- var entities = this._entities;
- if (entities.contains(id)) {
- throw new RuntimeError(
- "An entity with id " + id + " already exists in this collection."
- );
- }
- entity.entityCollection = this;
- entities.set(id, entity);
- if (!this._removedEntities.remove(id)) {
- this._addedEntities.set(id, entity);
- }
- entity.definitionChanged.addEventListener(
- EntityCollection.prototype._onEntityDefinitionChanged,
- this
- );
- fireChangedEvent(this);
- return entity;
- };
- EntityCollection.prototype.remove = function (entity) {
- if (!defined(entity)) {
- return false;
- }
- return this.removeById(entity.id);
- };
- EntityCollection.prototype.contains = function (entity) {
-
- if (!defined(entity)) {
- throw new DeveloperError("entity is required");
- }
-
- return this._entities.get(entity.id) === entity;
- };
- EntityCollection.prototype.removeById = function (id) {
- if (!defined(id)) {
- return false;
- }
- var entities = this._entities;
- var entity = entities.get(id);
- if (!this._entities.remove(id)) {
- return false;
- }
- if (!this._addedEntities.remove(id)) {
- this._removedEntities.set(id, entity);
- this._changedEntities.remove(id);
- }
- this._entities.remove(id);
- entity.definitionChanged.removeEventListener(
- EntityCollection.prototype._onEntityDefinitionChanged,
- this
- );
- fireChangedEvent(this);
- return true;
- };
- EntityCollection.prototype.removeAll = function () {
-
-
- var entities = this._entities;
- var entitiesLength = entities.length;
- var array = entities.values;
- var addedEntities = this._addedEntities;
- var removed = this._removedEntities;
- for (var i = 0; i < entitiesLength; i++) {
- var existingItem = array[i];
- var existingItemId = existingItem.id;
- var addedItem = addedEntities.get(existingItemId);
- if (!defined(addedItem)) {
- existingItem.definitionChanged.removeEventListener(
- EntityCollection.prototype._onEntityDefinitionChanged,
- this
- );
- removed.set(existingItemId, existingItem);
- }
- }
- entities.removeAll();
- addedEntities.removeAll();
- this._changedEntities.removeAll();
- fireChangedEvent(this);
- };
- EntityCollection.prototype.getById = function (id) {
-
- if (!defined(id)) {
- throw new DeveloperError("id is required.");
- }
-
- return this._entities.get(id);
- };
- EntityCollection.prototype.getOrCreateEntity = function (id) {
-
- if (!defined(id)) {
- throw new DeveloperError("id is required.");
- }
-
- var entity = this._entities.get(id);
- if (!defined(entity)) {
- entityOptionsScratch.id = id;
- entity = new Entity(entityOptionsScratch);
- this.add(entity);
- }
- return entity;
- };
- EntityCollection.prototype._onEntityDefinitionChanged = function (entity) {
- var id = entity.id;
- if (!this._addedEntities.contains(id)) {
- this._changedEntities.set(id, entity);
- }
- fireChangedEvent(this);
- };
- export default EntityCollection;
|