ClockViewModel.js 6.0 KB

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