PropertyArray.js 4.8 KB

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