ConstantProperty.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defineProperties',
  5. '../Core/DeveloperError',
  6. '../Core/Event',
  7. '../Core/isArray'
  8. ], function(
  9. defaultValue,
  10. defineProperties,
  11. DeveloperError,
  12. Event,
  13. isArray) {
  14. "use strict";
  15. /**
  16. * A {@link Property} whose value does not change with respect to simulation time.
  17. * If the value is not a number, string, array, or HTMLElement then it must provide clone and equals functions.
  18. *
  19. * @alias ConstantProperty
  20. * @constructor
  21. *
  22. * @param {Object} [value] The property value.
  23. *
  24. * @see ConstantPositionProperty
  25. *
  26. * @exception {DeveloperError} value.clone is a required function.
  27. * @exception {DeveloperError} value.equals is a required function.
  28. */
  29. var ConstantProperty = function(value) {
  30. this._value = undefined;
  31. this._simple = true;
  32. this._definitionChanged = new Event();
  33. this.setValue(value);
  34. };
  35. defineProperties(ConstantProperty.prototype, {
  36. /**
  37. * Gets a value indicating if this property is constant.
  38. * This property always returns <code>true</code>.
  39. * @memberof ConstantProperty.prototype
  40. *
  41. * @type {Boolean}
  42. * @readonly
  43. */
  44. isConstant : {
  45. value : true
  46. },
  47. /**
  48. * Gets the event that is raised whenever the definition of this property changes.
  49. * The definition is changed whenever setValue is called with data different
  50. * than the current value.
  51. * @memberof ConstantProperty.prototype
  52. *
  53. * @type {Event}
  54. * @readonly
  55. */
  56. definitionChanged : {
  57. get : function() {
  58. return this._definitionChanged;
  59. }
  60. }
  61. });
  62. /**
  63. * Gets the value of the property.
  64. *
  65. * @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.
  66. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  67. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied.
  68. */
  69. ConstantProperty.prototype.getValue = function(time, result) {
  70. return this._simple ? this._value : this._value.clone(result);
  71. };
  72. /**
  73. * Sets the value of the property.
  74. * If the value is not a number, string, array, or HTMLElement then it must provide clone and equals functions.
  75. *
  76. * @param {Object} value The property value.
  77. *
  78. * @exception {DeveloperError} value.clone is a required function.
  79. * @exception {DeveloperError} value.equals is a required function.
  80. */
  81. ConstantProperty.prototype.setValue = function(value) {
  82. var oldValue = this._value;
  83. var simple = this._simple;
  84. if ((simple && oldValue !== value) || (!simple && !oldValue.equals(value))) {
  85. simple = typeof value !== 'object' || value instanceof HTMLElement || isArray(value);
  86. //>>includeStart('debug', pragmas.debug);
  87. if (!simple) {
  88. if (typeof value.clone !== 'function') {
  89. throw new DeveloperError('clone is a required function.');
  90. }
  91. if (typeof value.equals !== 'function') {
  92. throw new DeveloperError('equals is a required function.');
  93. }
  94. }
  95. //>>includeEnd('debug');
  96. this._value = simple ? value : value.clone();
  97. this._simple = simple;
  98. this._definitionChanged.raiseEvent(this);
  99. }
  100. };
  101. /**
  102. * Compares this property to the provided property and returns
  103. * <code>true</code> if they are equal, <code>false</code> otherwise.
  104. *
  105. * @param {Property} [other] The other property.
  106. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  107. */
  108. ConstantProperty.prototype.equals = function(other) {
  109. return this === other || //
  110. (other instanceof ConstantProperty && //
  111. ((this._simple && (this._value === other._value)) || //
  112. (!this._simple && this._value.equals(other._value))));
  113. };
  114. return ConstantProperty;
  115. });