PropertyArray.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Property from './Property.js';
  7. /**
  8. * A {@link Property} whose value is an array whose items are the computed value
  9. * of other property instances.
  10. *
  11. * @alias PropertyArray
  12. * @constructor
  13. *
  14. * @param {Property[]} [value] An array of Property instances.
  15. */
  16. function PropertyArray(value) {
  17. this._value = undefined;
  18. this._definitionChanged = new Event();
  19. this._eventHelper = new EventHelper();
  20. this.setValue(value);
  21. }
  22. defineProperties(PropertyArray.prototype, {
  23. /**
  24. * Gets a value indicating if this property is constant. This property
  25. * is considered constant if all property items in the array are constant.
  26. * @memberof PropertyArray.prototype
  27. *
  28. * @type {Boolean}
  29. * @readonly
  30. */
  31. isConstant : {
  32. get : function() {
  33. var value = this._value;
  34. if (!defined(value)) {
  35. return true;
  36. }
  37. var length = value.length;
  38. for (var i = 0; i < length; i++) {
  39. if (!Property.isConstant(value[i])) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. },
  46. /**
  47. * Gets the event that is raised whenever the definition of this property changes.
  48. * The definition is changed whenever setValue is called with data different
  49. * than the current value or one of the properties in the array also changes.
  50. * @memberof PropertyArray.prototype
  51. *
  52. * @type {Event}
  53. * @readonly
  54. */
  55. definitionChanged : {
  56. get : function() {
  57. return this._definitionChanged;
  58. }
  59. }
  60. });
  61. /**
  62. * Gets the value of the property.
  63. *
  64. * @param {JulianDate} time The time for which to retrieve the value.
  65. * @param {Object[]} [result] The object to store the value into, if omitted, a new instance is created and returned.
  66. * @returns {Object[]} The modified result parameter, which is an array of values produced by evaluating each of the contained properties at the given time or a new instance if the result parameter was not supplied.
  67. */
  68. PropertyArray.prototype.getValue = function(time, result) {
  69. //>>includeStart('debug', pragmas.debug);
  70. if (!defined(time)) {
  71. throw new DeveloperError('time is required.');
  72. }
  73. //>>includeEnd('debug');
  74. var value = this._value;
  75. if (!defined(value)) {
  76. return undefined;
  77. }
  78. var length = value.length;
  79. if (!defined(result)) {
  80. result = new Array(length);
  81. }
  82. var i = 0;
  83. var x = 0;
  84. while (i < length) {
  85. var property = this._value[i];
  86. var itemValue = property.getValue(time, result[i]);
  87. if (defined(itemValue)) {
  88. result[x] = itemValue;
  89. x++;
  90. }
  91. i++;
  92. }
  93. result.length = x;
  94. return result;
  95. };
  96. /**
  97. * Sets the value of the property.
  98. *
  99. * @param {Property[]} value An array of Property instances.
  100. */
  101. PropertyArray.prototype.setValue = function(value) {
  102. var eventHelper = this._eventHelper;
  103. eventHelper.removeAll();
  104. if (defined(value)) {
  105. this._value = value.slice();
  106. var length = value.length;
  107. for (var i = 0; i < length; i++) {
  108. var property = value[i];
  109. if (defined(property)) {
  110. eventHelper.add(property.definitionChanged, PropertyArray.prototype._raiseDefinitionChanged, this);
  111. }
  112. }
  113. } else {
  114. this._value = undefined;
  115. }
  116. this._definitionChanged.raiseEvent(this);
  117. };
  118. /**
  119. * Compares this property to the provided property and returns
  120. * <code>true</code> if they are equal, <code>false</code> otherwise.
  121. *
  122. * @param {Property} [other] The other property.
  123. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  124. */
  125. PropertyArray.prototype.equals = function(other) {
  126. return this === other || //
  127. (other instanceof PropertyArray && //
  128. Property.arrayEquals(this._value, other._value));
  129. };
  130. PropertyArray.prototype._raiseDefinitionChanged = function() {
  131. this._definitionChanged.raiseEvent(this);
  132. };
  133. export default PropertyArray;