ClockViewModel.js 5.1 KB

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