PropertyArray.js 4.0 KB

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