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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 Property from "./Property.js";
  6. function resolve(that) {
  7. let targetProperty = that._targetProperty;
  8. if (!defined(targetProperty)) {
  9. let targetEntity = that._targetEntity;
  10. if (!defined(targetEntity)) {
  11. targetEntity = that._targetCollection.getById(that._targetId);
  12. if (!defined(targetEntity)) {
  13. // target entity not found
  14. that._targetEntity = that._targetProperty = undefined;
  15. return;
  16. }
  17. // target entity was found. listen for changes to entity definition
  18. targetEntity.definitionChanged.addEventListener(
  19. ReferenceProperty.prototype._onTargetEntityDefinitionChanged,
  20. that,
  21. );
  22. that._targetEntity = targetEntity;
  23. }
  24. // walk the list of property names and resolve properties
  25. const targetPropertyNames = that._targetPropertyNames;
  26. targetProperty = that._targetEntity;
  27. for (
  28. let i = 0, len = targetPropertyNames.length;
  29. i < len && defined(targetProperty);
  30. ++i
  31. ) {
  32. targetProperty = targetProperty[targetPropertyNames[i]];
  33. }
  34. // target property may or may not be defined, depending on if it was found
  35. that._targetProperty = targetProperty;
  36. }
  37. return targetProperty;
  38. }
  39. /**
  40. * A {@link Property} which transparently links to another property on a provided object.
  41. *
  42. * @alias ReferenceProperty
  43. * @constructor
  44. *
  45. * @param {EntityCollection} targetCollection The entity collection which will be used to resolve the reference.
  46. * @param {string} targetId The id of the entity which is being referenced.
  47. * @param {string[]} targetPropertyNames The names of the property on the target entity which we will use.
  48. *
  49. * @example
  50. * const collection = new Cesium.EntityCollection();
  51. *
  52. * //Create a new entity and assign a billboard scale.
  53. * const object1 = new Cesium.Entity({id:'object1'});
  54. * object1.billboard = new Cesium.BillboardGraphics();
  55. * object1.billboard.scale = new Cesium.ConstantProperty(2.0);
  56. * collection.add(object1);
  57. *
  58. * //Create a second entity and reference the scale from the first one.
  59. * const object2 = new Cesium.Entity({id:'object2'});
  60. * object2.model = new Cesium.ModelGraphics();
  61. * object2.model.scale = new Cesium.ReferenceProperty(collection, 'object1', ['billboard', 'scale']);
  62. * collection.add(object2);
  63. *
  64. * //Create a third object, but use the fromString helper function.
  65. * const object3 = new Cesium.Entity({id:'object3'});
  66. * object3.billboard = new Cesium.BillboardGraphics();
  67. * object3.billboard.scale = Cesium.ReferenceProperty.fromString(collection, 'object1#billboard.scale');
  68. * collection.add(object3);
  69. *
  70. * //You can refer to an entity with a # or . in id and property names by escaping them.
  71. * const object4 = new Cesium.Entity({id:'#object.4'});
  72. * object4.billboard = new Cesium.BillboardGraphics();
  73. * object4.billboard.scale = new Cesium.ConstantProperty(2.0);
  74. * collection.add(object4);
  75. *
  76. * const object5 = new Cesium.Entity({id:'object5'});
  77. * object5.billboard = new Cesium.BillboardGraphics();
  78. * object5.billboard.scale = Cesium.ReferenceProperty.fromString(collection, '\\#object\\.4#billboard.scale');
  79. * collection.add(object5);
  80. */
  81. function ReferenceProperty(targetCollection, targetId, targetPropertyNames) {
  82. //>>includeStart('debug', pragmas.debug);
  83. if (!defined(targetCollection)) {
  84. throw new DeveloperError("targetCollection is required.");
  85. }
  86. if (!defined(targetId) || targetId === "") {
  87. throw new DeveloperError("targetId is required.");
  88. }
  89. if (!defined(targetPropertyNames) || targetPropertyNames.length === 0) {
  90. throw new DeveloperError("targetPropertyNames is required.");
  91. }
  92. for (let i = 0; i < targetPropertyNames.length; i++) {
  93. const item = targetPropertyNames[i];
  94. if (!defined(item) || item === "") {
  95. throw new DeveloperError("reference contains invalid properties.");
  96. }
  97. }
  98. //>>includeEnd('debug');
  99. this._targetCollection = targetCollection;
  100. this._targetId = targetId;
  101. this._targetPropertyNames = targetPropertyNames;
  102. this._targetProperty = undefined;
  103. this._targetEntity = undefined;
  104. this._definitionChanged = new Event();
  105. targetCollection.collectionChanged.addEventListener(
  106. ReferenceProperty.prototype._onCollectionChanged,
  107. this,
  108. );
  109. }
  110. Object.defineProperties(ReferenceProperty.prototype, {
  111. /**
  112. * Gets a value indicating if this property is constant.
  113. * @memberof ReferenceProperty.prototype
  114. * @type {boolean}
  115. * @readonly
  116. */
  117. isConstant: {
  118. get: function () {
  119. return Property.isConstant(resolve(this));
  120. },
  121. },
  122. /**
  123. * Gets the event that is raised whenever the definition of this property changes.
  124. * The definition is changed whenever the referenced property's definition is changed.
  125. * @memberof ReferenceProperty.prototype
  126. * @type {Event}
  127. * @readonly
  128. */
  129. definitionChanged: {
  130. get: function () {
  131. return this._definitionChanged;
  132. },
  133. },
  134. /**
  135. * Gets the reference frame that the position is defined in.
  136. * This property is only valid if the referenced property is a {@link PositionProperty}.
  137. * @memberof ReferenceProperty.prototype
  138. * @type {ReferenceFrame}
  139. * @readonly
  140. */
  141. referenceFrame: {
  142. get: function () {
  143. const target = resolve(this);
  144. return defined(target) ? target.referenceFrame : undefined;
  145. },
  146. },
  147. /**
  148. * Gets the id of the entity being referenced.
  149. * @memberof ReferenceProperty.prototype
  150. * @type {string}
  151. * @readonly
  152. */
  153. targetId: {
  154. get: function () {
  155. return this._targetId;
  156. },
  157. },
  158. /**
  159. * Gets the collection containing the entity being referenced.
  160. * @memberof ReferenceProperty.prototype
  161. * @type {EntityCollection}
  162. * @readonly
  163. */
  164. targetCollection: {
  165. get: function () {
  166. return this._targetCollection;
  167. },
  168. },
  169. /**
  170. * Gets the array of property names used to retrieve the referenced property.
  171. * @memberof ReferenceProperty.prototype
  172. * @type {}
  173. * @readonly
  174. */
  175. targetPropertyNames: {
  176. get: function () {
  177. return this._targetPropertyNames;
  178. },
  179. },
  180. /**
  181. * Gets the resolved instance of the underlying referenced property.
  182. * @memberof ReferenceProperty.prototype
  183. * @type {Property|undefined}
  184. * @readonly
  185. */
  186. resolvedProperty: {
  187. get: function () {
  188. return resolve(this);
  189. },
  190. },
  191. });
  192. /**
  193. * Creates a new instance given the entity collection that will
  194. * be used to resolve it and a string indicating the target entity id and property.
  195. * The format of the string is "objectId#foo.bar", where # separates the id from
  196. * property path and . separates sub-properties. If the reference identifier or
  197. * or any sub-properties contains a # . or \ they must be escaped.
  198. *
  199. * @param {EntityCollection} targetCollection
  200. * @param {string} referenceString
  201. * @returns {ReferenceProperty} A new instance of ReferenceProperty.
  202. *
  203. * @exception {DeveloperError} invalid referenceString.
  204. */
  205. ReferenceProperty.fromString = function (targetCollection, referenceString) {
  206. //>>includeStart('debug', pragmas.debug);
  207. if (!defined(targetCollection)) {
  208. throw new DeveloperError("targetCollection is required.");
  209. }
  210. if (!defined(referenceString)) {
  211. throw new DeveloperError("referenceString is required.");
  212. }
  213. //>>includeEnd('debug');
  214. let identifier;
  215. const values = [];
  216. let inIdentifier = true;
  217. let isEscaped = false;
  218. let token = "";
  219. for (let i = 0; i < referenceString.length; ++i) {
  220. const c = referenceString.charAt(i);
  221. if (isEscaped) {
  222. token += c;
  223. isEscaped = false;
  224. } else if (c === "\\") {
  225. isEscaped = true;
  226. } else if (inIdentifier && c === "#") {
  227. identifier = token;
  228. inIdentifier = false;
  229. token = "";
  230. } else if (!inIdentifier && c === ".") {
  231. values.push(token);
  232. token = "";
  233. } else {
  234. token += c;
  235. }
  236. }
  237. values.push(token);
  238. return new ReferenceProperty(targetCollection, identifier, values);
  239. };
  240. const timeScratch = new JulianDate();
  241. /**
  242. * Gets the value of the property at the provided time.
  243. *
  244. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  245. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  246. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied.
  247. */
  248. ReferenceProperty.prototype.getValue = function (time, result) {
  249. const target = resolve(this);
  250. if (!defined(time)) {
  251. time = JulianDate.now(timeScratch);
  252. }
  253. return defined(target) ? target.getValue(time, result) : undefined;
  254. };
  255. /**
  256. * Gets the value of the property at the provided time and in the provided reference frame.
  257. * This method is only valid if the property being referenced is a {@link PositionProperty}.
  258. *
  259. * @param {JulianDate} time The time for which to retrieve the value.
  260. * @param {ReferenceFrame} referenceFrame The desired referenceFrame of the result.
  261. * @param {Cartesian3} [result] The object to store the value into, if omitted, a new instance is created and returned.
  262. * @returns {Cartesian3} The modified result parameter or a new instance if the result parameter was not supplied.
  263. */
  264. ReferenceProperty.prototype.getValueInReferenceFrame = function (
  265. time,
  266. referenceFrame,
  267. result,
  268. ) {
  269. const target = resolve(this);
  270. return defined(target)
  271. ? target.getValueInReferenceFrame(time, referenceFrame, result)
  272. : undefined;
  273. };
  274. /**
  275. * Gets the {@link Material} type at the provided time.
  276. * This method is only valid if the property being referenced is a {@link MaterialProperty}.
  277. *
  278. * @param {JulianDate} time The time for which to retrieve the type.
  279. * @returns {string} The type of material.
  280. */
  281. ReferenceProperty.prototype.getType = function (time) {
  282. const target = resolve(this);
  283. return defined(target) ? target.getType(time) : undefined;
  284. };
  285. /**
  286. * Compares this property to the provided property and returns
  287. * <code>true</code> if they are equal, <code>false</code> otherwise.
  288. *
  289. * @param {Property} [other] The other property.
  290. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  291. */
  292. ReferenceProperty.prototype.equals = function (other) {
  293. if (this === other) {
  294. return true;
  295. }
  296. const names = this._targetPropertyNames;
  297. const otherNames = other._targetPropertyNames;
  298. if (
  299. this._targetCollection !== other._targetCollection || //
  300. this._targetId !== other._targetId || //
  301. names.length !== otherNames.length
  302. ) {
  303. return false;
  304. }
  305. const length = this._targetPropertyNames.length;
  306. for (let i = 0; i < length; i++) {
  307. if (names[i] !== otherNames[i]) {
  308. return false;
  309. }
  310. }
  311. return true;
  312. };
  313. ReferenceProperty.prototype._onTargetEntityDefinitionChanged = function (
  314. targetEntity,
  315. name,
  316. value,
  317. oldValue,
  318. ) {
  319. if (defined(this._targetProperty) && this._targetPropertyNames[0] === name) {
  320. this._targetProperty = undefined;
  321. this._definitionChanged.raiseEvent(this);
  322. }
  323. };
  324. ReferenceProperty.prototype._onCollectionChanged = function (
  325. collection,
  326. added,
  327. removed,
  328. ) {
  329. let targetEntity = this._targetEntity;
  330. if (defined(targetEntity) && removed.indexOf(targetEntity) !== -1) {
  331. targetEntity.definitionChanged.removeEventListener(
  332. ReferenceProperty.prototype._onTargetEntityDefinitionChanged,
  333. this,
  334. );
  335. this._targetEntity = this._targetProperty = undefined;
  336. } else if (!defined(targetEntity)) {
  337. targetEntity = resolve(this);
  338. if (defined(targetEntity)) {
  339. this._definitionChanged.raiseEvent(this);
  340. }
  341. }
  342. };
  343. export default ReferenceProperty;