ReferenceProperty.js 11 KB

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