ClockViewModel.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {
  2. Clock,
  3. defined,
  4. destroyObject,
  5. EventHelper,
  6. JulianDate,
  7. } from "@cesium/engine";
  8. import knockout from "./ThirdParty/knockout.js";
  9. /**
  10. * A view model which exposes a {@link Clock} for user interfaces.
  11. * @alias ClockViewModel
  12. * @constructor
  13. *
  14. * @param {Clock} [clock] The clock object wrapped by this view model, if undefined a new instance will be created.
  15. *
  16. * @see Clock
  17. */
  18. function ClockViewModel(clock) {
  19. if (!defined(clock)) {
  20. clock = new Clock();
  21. }
  22. this._clock = clock;
  23. this._eventHelper = new EventHelper();
  24. this._eventHelper.add(clock.onTick, this.synchronize, this);
  25. /**
  26. * Gets the current system time.
  27. * This property is observable.
  28. * @type {JulianDate}
  29. */
  30. this.systemTime = knockout.observable(JulianDate.now());
  31. this.systemTime.equalityComparer = JulianDate.equals;
  32. /**
  33. * Gets or sets the start time of the clock.
  34. * See {@link Clock#startTime}.
  35. * This property is observable.
  36. * @type {JulianDate}
  37. */
  38. this.startTime = knockout.observable(clock.startTime);
  39. this.startTime.equalityComparer = JulianDate.equals;
  40. this.startTime.subscribe(function (value) {
  41. clock.startTime = value;
  42. this.synchronize();
  43. }, this);
  44. /**
  45. * Gets or sets the stop time of the clock.
  46. * See {@link Clock#stopTime}.
  47. * This property is observable.
  48. * @type {JulianDate}
  49. */
  50. this.stopTime = knockout.observable(clock.stopTime);
  51. this.stopTime.equalityComparer = JulianDate.equals;
  52. this.stopTime.subscribe(function (value) {
  53. clock.stopTime = value;
  54. this.synchronize();
  55. }, this);
  56. /**
  57. * Gets or sets the current time.
  58. * See {@link Clock#currentTime}.
  59. * This property is observable.
  60. * @type {JulianDate}
  61. */
  62. this.currentTime = knockout.observable(clock.currentTime);
  63. this.currentTime.equalityComparer = JulianDate.equals;
  64. this.currentTime.subscribe(function (value) {
  65. clock.currentTime = value;
  66. this.synchronize();
  67. }, this);
  68. /**
  69. * Gets or sets the clock multiplier.
  70. * See {@link Clock#multiplier}.
  71. * This property is observable.
  72. * @type {number}
  73. */
  74. this.multiplier = knockout.observable(clock.multiplier);
  75. this.multiplier.subscribe(function (value) {
  76. clock.multiplier = value;
  77. this.synchronize();
  78. }, this);
  79. /**
  80. * Gets or sets the clock step setting.
  81. * See {@link Clock#clockStep}.
  82. * This property is observable.
  83. * @type {ClockStep}
  84. */
  85. this.clockStep = knockout.observable(clock.clockStep);
  86. this.clockStep.subscribe(function (value) {
  87. clock.clockStep = value;
  88. this.synchronize();
  89. }, this);
  90. /**
  91. * Gets or sets the clock range setting.
  92. * See {@link Clock#clockRange}.
  93. * This property is observable.
  94. * @type {ClockRange}
  95. */
  96. this.clockRange = knockout.observable(clock.clockRange);
  97. this.clockRange.subscribe(function (value) {
  98. clock.clockRange = value;
  99. this.synchronize();
  100. }, this);
  101. /**
  102. * Gets or sets whether the clock can animate.
  103. * See {@link Clock#canAnimate}.
  104. * This property is observable.
  105. * @type {boolean}
  106. */
  107. this.canAnimate = knockout.observable(clock.canAnimate);
  108. this.canAnimate.subscribe(function (value) {
  109. clock.canAnimate = value;
  110. this.synchronize();
  111. }, this);
  112. /**
  113. * Gets or sets whether the clock should animate.
  114. * See {@link Clock#shouldAnimate}.
  115. * This property is observable.
  116. * @type {boolean}
  117. */
  118. this.shouldAnimate = knockout.observable(clock.shouldAnimate);
  119. this.shouldAnimate.subscribe(function (value) {
  120. clock.shouldAnimate = value;
  121. this.synchronize();
  122. }, this);
  123. knockout.track(this, [
  124. "systemTime",
  125. "startTime",
  126. "stopTime",
  127. "currentTime",
  128. "multiplier",
  129. "clockStep",
  130. "clockRange",
  131. "canAnimate",
  132. "shouldAnimate",
  133. ]);
  134. }
  135. Object.defineProperties(ClockViewModel.prototype, {
  136. /**
  137. * Gets the underlying Clock.
  138. * @memberof ClockViewModel.prototype
  139. * @type {Clock}
  140. */
  141. clock: {
  142. get: function () {
  143. return this._clock;
  144. },
  145. },
  146. });
  147. /**
  148. * Updates the view model with the contents of the underlying clock.
  149. * Can be called to force an update of the viewModel if the underlying
  150. * clock has changed and <code>Clock.tick</code> has not yet been called.
  151. */
  152. ClockViewModel.prototype.synchronize = function () {
  153. const clock = this._clock;
  154. this.systemTime = JulianDate.now();
  155. this.startTime = clock.startTime;
  156. this.stopTime = clock.stopTime;
  157. this.currentTime = clock.currentTime;
  158. this.multiplier = clock.multiplier;
  159. this.clockStep = clock.clockStep;
  160. this.clockRange = clock.clockRange;
  161. this.canAnimate = clock.canAnimate;
  162. this.shouldAnimate = clock.shouldAnimate;
  163. };
  164. /**
  165. * @returns {boolean} true if the object has been destroyed, false otherwise.
  166. */
  167. ClockViewModel.prototype.isDestroyed = function () {
  168. return false;
  169. };
  170. /**
  171. * Destroys the view model. Should be called to
  172. * properly clean up the view model when it is no longer needed.
  173. */
  174. ClockViewModel.prototype.destroy = function () {
  175. this._eventHelper.removeAll();
  176. destroyObject(this);
  177. };
  178. export default ClockViewModel;