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

PropertyBag.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import defined from "../Core/defined.js";
  2. import DeveloperError from "../Core/DeveloperError.js";
  3. import Event from "../Core/Event.js";
  4. import JulianDate from "../Core/JulianDate.js";
  5. import ConstantProperty from "./ConstantProperty.js";
  6. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  7. import Property from "./Property.js";
  8. /**
  9. * A {@link Property} whose value is a key-value mapping of property names to the computed value of other properties.
  10. *
  11. * @alias PropertyBag
  12. * @implements Record<string, any>
  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. function PropertyBag(value, createPropertyCallback) {
  19. this._propertyNames = [];
  20. this._definitionChanged = new Event();
  21. if (defined(value)) {
  22. this.merge(value, createPropertyCallback);
  23. }
  24. }
  25. Object.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. const propertyNames = this._propertyNames;
  47. for (let 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 (
  93. propertyName,
  94. value,
  95. createPropertyCallback,
  96. ) {
  97. const propertyNames = this._propertyNames;
  98. //>>includeStart('debug', pragmas.debug);
  99. if (!defined(propertyName)) {
  100. throw new DeveloperError("propertyName is required.");
  101. }
  102. if (propertyNames.indexOf(propertyName) !== -1) {
  103. throw new DeveloperError(
  104. `${propertyName} is already a registered property.`,
  105. );
  106. }
  107. //>>includeEnd('debug');
  108. propertyNames.push(propertyName);
  109. Object.defineProperty(
  110. this,
  111. propertyName,
  112. createPropertyDescriptor(
  113. propertyName,
  114. true,
  115. createPropertyCallback ?? createConstantProperty,
  116. ),
  117. );
  118. if (defined(value)) {
  119. this[propertyName] = value;
  120. }
  121. this._definitionChanged.raiseEvent(this);
  122. };
  123. /**
  124. * Removed a property previously added with addProperty.
  125. *
  126. * @param {string} propertyName The name of the property to remove.
  127. *
  128. * @exception {DeveloperError} "propertyName" is not a registered property.
  129. */
  130. PropertyBag.prototype.removeProperty = function (propertyName) {
  131. const propertyNames = this._propertyNames;
  132. const index = propertyNames.indexOf(propertyName);
  133. //>>includeStart('debug', pragmas.debug);
  134. if (!defined(propertyName)) {
  135. throw new DeveloperError("propertyName is required.");
  136. }
  137. if (index === -1) {
  138. throw new DeveloperError(`${propertyName} is not a registered property.`);
  139. }
  140. //>>includeEnd('debug');
  141. this._propertyNames.splice(index, 1);
  142. delete this[propertyName];
  143. this._definitionChanged.raiseEvent(this);
  144. };
  145. const timeScratch = new JulianDate();
  146. /**
  147. * Gets the value of this property. Each contained property will be evaluated at the given time, and the overall
  148. * result will be an object, mapping property names to those values.
  149. *
  150. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  151. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  152. * Note that any properties in result which are not part of this PropertyBag will be left as-is.
  153. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied.
  154. */
  155. PropertyBag.prototype.getValue = function (time, result) {
  156. if (!defined(time)) {
  157. time = JulianDate.now(timeScratch);
  158. }
  159. if (!defined(result)) {
  160. result = {};
  161. }
  162. const propertyNames = this._propertyNames;
  163. for (let i = 0, len = propertyNames.length; i < len; i++) {
  164. const propertyName = propertyNames[i];
  165. result[propertyName] = Property.getValueOrUndefined(
  166. this[propertyName],
  167. time,
  168. result[propertyName],
  169. );
  170. }
  171. return result;
  172. };
  173. /**
  174. * Assigns each unassigned property on this object to the value
  175. * of the same property on the provided source object.
  176. *
  177. * @param {object} source The object to be merged into this object.
  178. * @param {Function} [createPropertyCallback] A function that will be called when the value of any of the properties in value are not a Property.
  179. */
  180. PropertyBag.prototype.merge = function (source, createPropertyCallback) {
  181. //>>includeStart('debug', pragmas.debug);
  182. if (!defined(source)) {
  183. throw new DeveloperError("source is required.");
  184. }
  185. //>>includeEnd('debug');
  186. const propertyNames = this._propertyNames;
  187. const sourcePropertyNames = defined(source._propertyNames)
  188. ? source._propertyNames
  189. : Object.keys(source);
  190. for (let i = 0, len = sourcePropertyNames.length; i < len; i++) {
  191. const name = sourcePropertyNames[i];
  192. const targetProperty = this[name];
  193. const sourceProperty = source[name];
  194. //Custom properties that are registered on the source must also be added to this.
  195. if (targetProperty === undefined && propertyNames.indexOf(name) === -1) {
  196. this.addProperty(name, undefined, createPropertyCallback);
  197. }
  198. if (sourceProperty !== undefined) {
  199. if (targetProperty !== undefined) {
  200. if (defined(targetProperty) && defined(targetProperty.merge)) {
  201. targetProperty.merge(sourceProperty);
  202. }
  203. } else if (
  204. defined(sourceProperty) &&
  205. defined(sourceProperty.merge) &&
  206. defined(sourceProperty.clone)
  207. ) {
  208. this[name] = sourceProperty.clone();
  209. } else {
  210. this[name] = sourceProperty;
  211. }
  212. }
  213. }
  214. };
  215. function propertiesEqual(a, b) {
  216. const aPropertyNames = a._propertyNames;
  217. const bPropertyNames = b._propertyNames;
  218. const len = aPropertyNames.length;
  219. if (len !== bPropertyNames.length) {
  220. return false;
  221. }
  222. for (let aIndex = 0; aIndex < len; ++aIndex) {
  223. const name = aPropertyNames[aIndex];
  224. const bIndex = bPropertyNames.indexOf(name);
  225. if (bIndex === -1) {
  226. return false;
  227. }
  228. if (!Property.equals(a[name], b[name])) {
  229. return false;
  230. }
  231. }
  232. return true;
  233. }
  234. /**
  235. * Compares this property to the provided property and returns
  236. * <code>true</code> if they are equal, <code>false</code> otherwise.
  237. *
  238. * @param {Property} [other] The other property.
  239. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  240. */
  241. PropertyBag.prototype.equals = function (other) {
  242. return (
  243. this === other || //
  244. (other instanceof PropertyBag && //
  245. propertiesEqual(this, other))
  246. );
  247. };
  248. export default PropertyBag;