CompositeProperty.js 5.4 KB

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