CompositeProperty.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import defined from "../Core/defined.js";
  2. import DeveloperError from "../Core/DeveloperError.js";
  3. import Event from "../Core/Event.js";
  4. import EventHelper from "../Core/EventHelper.js";
  5. import TimeIntervalCollection from "../Core/TimeIntervalCollection.js";
  6. import Property from "./Property.js";
  7. function subscribeAll(property, eventHelper, definitionChanged, intervals) {
  8. function callback() {
  9. definitionChanged.raiseEvent(property);
  10. }
  11. const items = [];
  12. eventHelper.removeAll();
  13. const length = intervals.length;
  14. for (let i = 0; i < length; i++) {
  15. const interval = intervals.get(i);
  16. if (defined(interval.data) && items.indexOf(interval.data) === -1) {
  17. eventHelper.add(interval.data.definitionChanged, callback);
  18. }
  19. }
  20. }
  21. /**
  22. * A {@link Property} which is defined by a {@link TimeIntervalCollection}, where the
  23. * data property of each {@link TimeInterval} is another Property instance which is
  24. * evaluated at the provided time.
  25. *
  26. * @alias CompositeProperty
  27. * @constructor
  28. *
  29. *
  30. * @example
  31. * const constantProperty = ...;
  32. * const sampledProperty = ...;
  33. *
  34. * //Create a composite property from two previously defined properties
  35. * //where the property is valid on August 1st, 2012 and uses a constant
  36. * //property for the first half of the day and a sampled property for the
  37. * //remaining half.
  38. * const composite = new Cesium.CompositeProperty();
  39. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  40. * iso8601 : '2012-08-01T00:00:00.00Z/2012-08-01T12:00:00.00Z',
  41. * data : constantProperty
  42. * }));
  43. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  44. * iso8601 : '2012-08-01T12:00:00.00Z/2012-08-02T00:00:00.00Z',
  45. * isStartIncluded : false,
  46. * isStopIncluded : false,
  47. * data : sampledProperty
  48. * }));
  49. *
  50. * @see CompositeMaterialProperty
  51. * @see CompositePositionProperty
  52. */
  53. function CompositeProperty() {
  54. this._eventHelper = new EventHelper();
  55. this._definitionChanged = new Event();
  56. this._intervals = new TimeIntervalCollection();
  57. this._intervals.changedEvent.addEventListener(
  58. CompositeProperty.prototype._intervalsChanged,
  59. this
  60. );
  61. }
  62. Object.defineProperties(CompositeProperty.prototype, {
  63. /**
  64. * Gets a value indicating if this property is constant. A property is considered
  65. * constant if getValue always returns the same result for the current definition.
  66. * @memberof CompositeProperty.prototype
  67. *
  68. * @type {boolean}
  69. * @readonly
  70. */
  71. isConstant: {
  72. get: function () {
  73. return this._intervals.isEmpty;
  74. },
  75. },
  76. /**
  77. * Gets the event that is raised whenever the definition of this property changes.
  78. * The definition is changed whenever setValue is called with data different
  79. * than the current value.
  80. * @memberof CompositeProperty.prototype
  81. *
  82. * @type {Event}
  83. * @readonly
  84. */
  85. definitionChanged: {
  86. get: function () {
  87. return this._definitionChanged;
  88. },
  89. },
  90. /**
  91. * Gets the interval collection.
  92. * @memberof CompositeProperty.prototype
  93. *
  94. * @type {TimeIntervalCollection}
  95. */
  96. intervals: {
  97. get: function () {
  98. return this._intervals;
  99. },
  100. },
  101. });
  102. /**
  103. * Gets the value of the property at the provided time.
  104. *
  105. * @param {JulianDate} time The time for which to retrieve the value.
  106. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  107. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied.
  108. */
  109. CompositeProperty.prototype.getValue = function (time, result) {
  110. //>>includeStart('debug', pragmas.debug);
  111. if (!defined(time)) {
  112. throw new DeveloperError("time is required");
  113. }
  114. //>>includeEnd('debug');
  115. const innerProperty = this._intervals.findDataForIntervalContainingDate(time);
  116. if (defined(innerProperty)) {
  117. return innerProperty.getValue(time, result);
  118. }
  119. return undefined;
  120. };
  121. /**
  122. * Compares this property to the provided property and returns
  123. * <code>true</code> if they are equal, <code>false</code> otherwise.
  124. *
  125. * @param {Property} [other] The other property.
  126. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  127. */
  128. CompositeProperty.prototype.equals = function (other) {
  129. return (
  130. this === other || //
  131. (other instanceof CompositeProperty && //
  132. this._intervals.equals(other._intervals, Property.equals))
  133. );
  134. };
  135. /**
  136. * @private
  137. */
  138. CompositeProperty.prototype._intervalsChanged = function () {
  139. subscribeAll(
  140. this,
  141. this._eventHelper,
  142. this._definitionChanged,
  143. this._intervals
  144. );
  145. this._definitionChanged.raiseEvent(this);
  146. };
  147. export default CompositeProperty;