/
ClockViewModel.js
255 lines (228 loc) · 8.09 KB
/
ClockViewModel.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*global define*/
define([
'../Core/Clock',
'../Core/defined',
'../Core/defineProperties',
'../Core/destroyObject',
'../Core/EventHelper',
'../Core/JulianDate',
'../ThirdParty/knockout'
], function(
Clock,
defined,
defineProperties,
destroyObject,
EventHelper,
JulianDate,
knockout) {
"use strict";
/**
* 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
*/
var ClockViewModel = function(clock) {
if (!defined(clock)) {
clock = new Clock();
}
this._clock = clock;
this._eventHelper = new EventHelper();
this._eventHelper.add(clock.onTick, this.synchronize, this);
var startTime = knockout.observable(clock.startTime);
startTime.equalityComparer = JulianDate.equals;
/**
* Gets the current system time. This property is observable.
* @type {JulianDate}
* @default JulianDate()
*/
this.systemTime = knockout.observable(JulianDate.now());
this.systemTime.equalityComparer = JulianDate.equals;
knockout.track(this, ['systemTime']);
/**
* Gets or sets the start time of the clock. This property is observable.
* @type {JulianDate}
* @default undefined
*/
this.startTime = undefined;
knockout.defineProperty(this, 'startTime', {
get : startTime,
set : function(value) {
startTime(value);
clock.startTime = value;
}
});
var stopTime = knockout.observable(clock.stopTime);
stopTime.equalityComparer = JulianDate.equals;
/**
* Gets or sets the stop time of the clock. This property is observable.
* @type {JulianDate}
* @default undefined
*/
this.stopTime = undefined;
knockout.defineProperty(this, 'stopTime', {
get : stopTime,
set : function(value) {
clock.stopTime = value;
stopTime(value);
}
});
var currentTime = knockout.observable(clock.currentTime);
currentTime.equalityComparer = JulianDate.equals;
/**
* Gets or sets the current time. This property is observable.
* @type {JulianDate}
* @default undefined
*/
this.currentTime = undefined;
knockout.defineProperty(this, 'currentTime', {
get : currentTime,
set : function(value) {
clock.currentTime = value;
currentTime(value);
}
});
var multiplier = knockout.observable(clock.multiplier);
/**
* Gets or sets how much time advances when tick is called, negative values allow for advancing backwards.
* If <code>clockStep</code> is set to ClockStep.TICK_DEPENDENT this is the number of seconds to advance.
* If <code>clockStep</code> is set to ClockStep.SYSTEM_CLOCK_MULTIPLIER this value is multiplied by the
* elapsed system time since the last call to tick. This property is observable.
* @type {Number}
* @default undefined
*/
this.multiplier = undefined;
knockout.defineProperty(this, 'multiplier', {
get : multiplier,
set : function(value) {
clock.multiplier = value;
multiplier(value);
}
});
var clockStep = knockout.observable(clock.clockStep);
clockStep.equalityComparer = function(a, b) {
return a === b;
};
/**
* Gets or sets whether calls to <code>Clock.tick</code> are frame dependent or system clock dependent.
* This property is observable.
* @type {ClockStep}
* @default undefined
*/
this.clockStep = undefined;
knockout.defineProperty(this, 'clockStep', {
get : clockStep,
set : function(value) {
clockStep(value);
clock.clockStep = value;
}
});
var clockRange = knockout.observable(clock.clockRange);
clockRange.equalityComparer = function(a, b) {
return a === b;
};
/**
* Gets or sets how tick should behave when <code>startTime</code> or <code>stopTime</code> is reached.
* This property is observable.
* @type {ClockRange}
* @default undefined
*/
this.clockRange = undefined;
knockout.defineProperty(this, 'clockRange', {
get : clockRange,
set : function(value) {
clockRange(value);
clock.clockRange = value;
}
});
var canAnimate = knockout.observable(clock.canAnimate);
/**
* Gets or sets whether or not <code>Clock.tick</code> can advance time.
* This could be false if data is being buffered, for example.
* The clock will only tick when both <code>canAnimate</code> and <code>shouldAnimate</code> are true.
* This property is observable.
* @type {Boolean}
* @default undefined
*/
this.canAnimate = undefined;
knockout.defineProperty(this, 'canAnimate', {
get : canAnimate,
set : function(value) {
canAnimate(value);
clock.canAnimate = value;
}
});
var shouldAnimate = knockout.observable(clock.shouldAnimate);
/**
* Gets or sets whether or not <code>Clock.tick</code> should attempt to advance time.
* The clock will only tick when both <code>canAnimate</code> and <code>shouldAnimate</code> are true.
* This property is observable.
* @type {Boolean}
* @default undefined
*/
this.shouldAnimate = undefined;
knockout.defineProperty(this, 'shouldAnimate', {
get : shouldAnimate,
set : function(value) {
shouldAnimate(value);
clock.shouldAnimate = value;
}
});
};
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() {
var clock = this._clock;
var startTime = clock.startTime;
var stopTime = clock.stopTime;
var currentTime = clock.currentTime;
var multiplier = clock.multiplier;
var clockStep = clock.clockStep;
var clockRange = clock.clockRange;
var canAnimate = clock.canAnimate;
var shouldAnimate = clock.shouldAnimate;
this.systemTime = JulianDate.now();
this.startTime = startTime;
this.stopTime = stopTime;
this.currentTime = currentTime;
this.multiplier = multiplier;
this.clockStep = clockStep;
this.clockRange = clockRange;
this.canAnimate = canAnimate;
this.shouldAnimate = 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);
};
return ClockViewModel;
});