All files / widgets/Source ClockViewModel.js

100% Statements 61/61
100% Branches 2/2
100% Functions 13/13
100% Lines 61/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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