CompositeProperty.js 5.3 KB

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