TimeIntervalCollectionProperty.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*global define*/
  2. define([
  3. '../Core/defined',
  4. '../Core/defineProperties',
  5. '../Core/DeveloperError',
  6. '../Core/Event',
  7. '../Core/isArray',
  8. '../Core/TimeIntervalCollection',
  9. './Property'
  10. ], function(
  11. defined,
  12. defineProperties,
  13. DeveloperError,
  14. Event,
  15. isArray,
  16. TimeIntervalCollection,
  17. Property) {
  18. "use strict";
  19. /**
  20. * A {@link Property} which is defined by a {@link TimeIntervalCollection}, where the
  21. * data property of each {@link TimeInterval} represents the value at time.
  22. *
  23. * @alias TimeIntervalCollectionProperty
  24. * @constructor
  25. *
  26. * @example
  27. * //Create a Cartesian2 interval property which contains data on August 1st, 2012
  28. * //and uses a different value every 6 hours.
  29. * var composite = new Cesium.TimeIntervalCollectionProperty();
  30. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  31. * iso8601 : '2012-08-01T00:00:00.00Z/2012-08-01T06:00:00.00Z',
  32. * isStartIncluded : true,
  33. * isStopIncluded : false,
  34. * data : new Cesium.Cartesian2(2.0, 3.4)
  35. * }));
  36. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  37. * iso8601 : '2012-08-01T06:00:00.00Z/2012-08-01T12:00:00.00Z',
  38. * isStartIncluded : true,
  39. * isStopIncluded : false,
  40. * data : new Cesium.Cartesian2(12.0, 2.7)
  41. * }));
  42. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  43. * iso8601 : '2012-08-01T12:00:00.00Z/2012-08-01T18:00:00.00Z',
  44. * isStartIncluded : true,
  45. * isStopIncluded : false,
  46. * data : new Cesium.Cartesian2(5.0, 12.4)
  47. * }));
  48. * composite.intervals.addInterval(Cesium.TimeInterval.fromIso8601({
  49. * iso8601 : '2012-08-01T18:00:00.00Z/2012-08-02T00:00:00.00Z',
  50. * isStartIncluded : true,
  51. * isStopIncluded : true,
  52. * data : new Cesium.Cartesian2(85.0, 4.1)
  53. * }));
  54. */
  55. var TimeIntervalCollectionProperty = function() {
  56. this._definitionChanged = new Event();
  57. this._intervals = new TimeIntervalCollection();
  58. this._intervals.changedEvent.addEventListener(TimeIntervalCollectionProperty.prototype._intervalsChanged, this);
  59. };
  60. defineProperties(TimeIntervalCollectionProperty.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 TimeIntervalCollectionProperty.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 TimeIntervalCollectionProperty.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 TimeIntervalCollectionProperty.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. * @exception {DeveloperError} This value requires a clone function be specified for the TimeIntervalCollectionProperty constructor.
  108. */
  109. TimeIntervalCollectionProperty.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. var value = this._intervals.findDataForIntervalContainingDate(time);
  116. if (defined(value) && (typeof value === 'object' && !isArray(value))) {
  117. return value.clone(result);
  118. }
  119. return value;
  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. TimeIntervalCollectionProperty.prototype.equals = function(other) {
  129. return this === other || //
  130. (other instanceof TimeIntervalCollectionProperty && //
  131. this._intervals.equals(other._intervals, Property.equals));
  132. };
  133. /**
  134. * @private
  135. */
  136. TimeIntervalCollectionProperty.prototype._intervalsChanged = function() {
  137. this._definitionChanged.raiseEvent(this);
  138. };
  139. return TimeIntervalCollectionProperty;
  140. });