智慧水务管理系统 - 精河县供水工程综合管理平台

PropertyArray.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import defined from "../Core/defined.js";
  2. import Event from "../Core/Event.js";
  3. import EventHelper from "../Core/EventHelper.js";
  4. import JulianDate from "../Core/JulianDate.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. const timeScratch = new JulianDate();
  61. /**
  62. * Gets the value of the property.
  63. *
  64. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  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. if (!defined(time)) {
  70. time = JulianDate.now(timeScratch);
  71. }
  72. const value = this._value;
  73. if (!defined(value)) {
  74. return undefined;
  75. }
  76. const length = value.length;
  77. if (!defined(result)) {
  78. result = new Array(length);
  79. }
  80. let i = 0;
  81. let x = 0;
  82. while (i < length) {
  83. const property = this._value[i];
  84. const itemValue = property.getValue(time, result[i]);
  85. if (defined(itemValue)) {
  86. result[x] = itemValue;
  87. x++;
  88. }
  89. i++;
  90. }
  91. result.length = x;
  92. return result;
  93. };
  94. /**
  95. * Sets the value of the property.
  96. *
  97. * @param {Property[]} value An array of Property instances.
  98. */
  99. PropertyArray.prototype.setValue = function (value) {
  100. const eventHelper = this._eventHelper;
  101. eventHelper.removeAll();
  102. if (defined(value)) {
  103. this._value = value.slice();
  104. const length = value.length;
  105. for (let i = 0; i < length; i++) {
  106. const property = value[i];
  107. if (defined(property)) {
  108. eventHelper.add(
  109. property.definitionChanged,
  110. PropertyArray.prototype._raiseDefinitionChanged,
  111. this,
  112. );
  113. }
  114. }
  115. } else {
  116. this._value = undefined;
  117. }
  118. this._definitionChanged.raiseEvent(this);
  119. };
  120. /**
  121. * Compares this property to the provided property and returns
  122. * <code>true</code> if they are equal, <code>false</code> otherwise.
  123. *
  124. * @param {Property} [other] The other property.
  125. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  126. */
  127. PropertyArray.prototype.equals = function (other) {
  128. return (
  129. this === other || //
  130. (other instanceof PropertyArray && //
  131. Property.arrayEquals(this._value, other._value))
  132. );
  133. };
  134. PropertyArray.prototype._raiseDefinitionChanged = function () {
  135. this._definitionChanged.raiseEvent(this);
  136. };
  137. export default PropertyArray;