PropertyBag.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defined from '../Core/defined.js';
  3. import defineProperties from '../Core/defineProperties.js';
  4. import DeveloperError from '../Core/DeveloperError.js';
  5. import Event from '../Core/Event.js';
  6. import ConstantProperty from './ConstantProperty.js';
  7. import createPropertyDescriptor from './createPropertyDescriptor.js';
  8. import Property from './Property.js';
  9. /**
  10. * A {@link Property} whose value is a key-value mapping of property names to the computed value of other properties.
  11. *
  12. * @alias PropertyBag
  13. * @constructor
  14. *
  15. * @param {Object} [value] An object, containing key-value mapping of property names to properties.
  16. * @param {Function} [createPropertyCallback] A function that will be called when the value of any of the properties in value are not a Property.
  17. */
  18. var PropertyBag = function(value, createPropertyCallback) {
  19. this._propertyNames = [];
  20. this._definitionChanged = new Event();
  21. if (defined(value)) {
  22. this.merge(value, createPropertyCallback);
  23. }
  24. };
  25. defineProperties(PropertyBag.prototype, {
  26. /**
  27. * Gets the names of all properties registered on this instance.
  28. * @memberof PropertyBag.prototype
  29. * @type {Array}
  30. */
  31. propertyNames : {
  32. get : function() {
  33. return this._propertyNames;
  34. }
  35. },
  36. /**
  37. * Gets a value indicating if this property is constant. This property
  38. * is considered constant if all property items in this object are constant.
  39. * @memberof PropertyBag.prototype
  40. *
  41. * @type {Boolean}
  42. * @readonly
  43. */
  44. isConstant : {
  45. get : function() {
  46. var propertyNames = this._propertyNames;
  47. for (var i = 0, len = propertyNames.length; i < len; i++) {
  48. if (!Property.isConstant(this[propertyNames[i]])) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. },
  55. /**
  56. * Gets the event that is raised whenever the set of properties contained in this
  57. * object changes, or one of the properties itself changes.
  58. *
  59. * @memberof PropertyBag.prototype
  60. *
  61. * @type {Event}
  62. * @readonly
  63. */
  64. definitionChanged : {
  65. get : function() {
  66. return this._definitionChanged;
  67. }
  68. }
  69. });
  70. /**
  71. * Determines if this object has defined a property with the given name.
  72. *
  73. * @param {String} propertyName The name of the property to check for.
  74. *
  75. * @returns {Boolean} True if this object has defined a property with the given name, false otherwise.
  76. */
  77. PropertyBag.prototype.hasProperty = function(propertyName) {
  78. return this._propertyNames.indexOf(propertyName) !== -1;
  79. };
  80. function createConstantProperty(value) {
  81. return new ConstantProperty(value);
  82. }
  83. /**
  84. * Adds a property to this object.
  85. *
  86. * @param {String} propertyName The name of the property to add.
  87. * @param {*} [value] The value of the new property, if provided.
  88. * @param {Function} [createPropertyCallback] A function that will be called when the value of this new property is set to a value that is not a Property.
  89. *
  90. * @exception {DeveloperError} "propertyName" is already a registered property.
  91. */
  92. PropertyBag.prototype.addProperty = function(propertyName, value, createPropertyCallback) {
  93. var propertyNames = this._propertyNames;
  94. //>>includeStart('debug', pragmas.debug);
  95. if (!defined(propertyName)) {
  96. throw new DeveloperError('propertyName is required.');
  97. }
  98. if (propertyNames.indexOf(propertyName) !== -1) {
  99. throw new DeveloperError(propertyName + ' is already a registered property.');
  100. }
  101. //>>includeEnd('debug');
  102. propertyNames.push(propertyName);
  103. Object.defineProperty(this, propertyName, createPropertyDescriptor(propertyName, true, defaultValue(createPropertyCallback, createConstantProperty)));
  104. if (defined(value)) {
  105. this[propertyName] = value;
  106. }
  107. this._definitionChanged.raiseEvent(this);
  108. };
  109. /**
  110. * Removed a property previously added with addProperty.
  111. *
  112. * @param {String} propertyName The name of the property to remove.
  113. *
  114. * @exception {DeveloperError} "propertyName" is not a registered property.
  115. */
  116. PropertyBag.prototype.removeProperty = function(propertyName) {
  117. var propertyNames = this._propertyNames;
  118. var index = propertyNames.indexOf(propertyName);
  119. //>>includeStart('debug', pragmas.debug);
  120. if (!defined(propertyName)) {
  121. throw new DeveloperError('propertyName is required.');
  122. }
  123. if (index === -1) {
  124. throw new DeveloperError(propertyName + ' is not a registered property.');
  125. }
  126. //>>includeEnd('debug');
  127. this._propertyNames.splice(index, 1);
  128. delete this[propertyName];
  129. this._definitionChanged.raiseEvent(this);
  130. };
  131. /**
  132. * Gets the value of this property. Each contained property will be evaluated at the given time, and the overall
  133. * result will be an object, mapping property names to those values.
  134. *
  135. * @param {JulianDate} time The time for which to retrieve the value.
  136. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  137. * Note that any properties in result which are not part of this PropertyBag will be left as-is.
  138. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied.
  139. */
  140. PropertyBag.prototype.getValue = function(time, result) {
  141. //>>includeStart('debug', pragmas.debug);
  142. if (!defined(time)) {
  143. throw new DeveloperError('time is required.');
  144. }
  145. //>>includeEnd('debug');
  146. if (!defined(result)) {
  147. result = {};
  148. }
  149. var propertyNames = this._propertyNames;
  150. for (var i = 0, len = propertyNames.length; i < len; i++) {
  151. var propertyName = propertyNames[i];
  152. result[propertyName] = Property.getValueOrUndefined(this[propertyName], time, result[propertyName]);
  153. }
  154. return result;
  155. };
  156. /**
  157. * Assigns each unassigned property on this object to the value
  158. * of the same property on the provided source object.
  159. *
  160. * @param {Object} source The object to be merged into this object.
  161. * @param {Function} [createPropertyCallback] A function that will be called when the value of any of the properties in value are not a Property.
  162. */
  163. PropertyBag.prototype.merge = function(source, createPropertyCallback) {
  164. //>>includeStart('debug', pragmas.debug);
  165. if (!defined(source)) {
  166. throw new DeveloperError('source is required.');
  167. }
  168. //>>includeEnd('debug');
  169. var propertyNames = this._propertyNames;
  170. var sourcePropertyNames = defined(source._propertyNames) ? source._propertyNames : Object.keys(source);
  171. for (var i = 0, len = sourcePropertyNames.length; i < len; i++) {
  172. var name = sourcePropertyNames[i];
  173. var targetProperty = this[name];
  174. var sourceProperty = source[name];
  175. //Custom properties that are registered on the source must also be added to this.
  176. if (targetProperty === undefined && propertyNames.indexOf(name) === -1) {
  177. this.addProperty(name, undefined, createPropertyCallback);
  178. }
  179. if (sourceProperty !== undefined) {
  180. if (targetProperty !== undefined) {
  181. if (defined(targetProperty) && defined(targetProperty.merge)) {
  182. targetProperty.merge(sourceProperty);
  183. }
  184. } else if (defined(sourceProperty) && defined(sourceProperty.merge) && defined(sourceProperty.clone)) {
  185. this[name] = sourceProperty.clone();
  186. } else {
  187. this[name] = sourceProperty;
  188. }
  189. }
  190. }
  191. };
  192. function propertiesEqual(a, b) {
  193. var aPropertyNames = a._propertyNames;
  194. var bPropertyNames = b._propertyNames;
  195. var len = aPropertyNames.length;
  196. if (len !== bPropertyNames.length) {
  197. return false;
  198. }
  199. for (var aIndex = 0; aIndex < len; ++aIndex) {
  200. var name = aPropertyNames[aIndex];
  201. var bIndex = bPropertyNames.indexOf(name);
  202. if (bIndex === -1) {
  203. return false;
  204. }
  205. if (!Property.equals(a[name], b[name])) {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. /**
  212. * Compares this property to the provided property and returns
  213. * <code>true</code> if they are equal, <code>false</code> otherwise.
  214. *
  215. * @param {Property} [other] The other property.
  216. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  217. */
  218. PropertyBag.prototype.equals = function(other) {
  219. return this === other || //
  220. (other instanceof PropertyBag && //
  221. propertiesEqual(this, other));
  222. };
  223. export default PropertyBag;