-
Notifications
You must be signed in to change notification settings - Fork 988
/
timer.js
303 lines (275 loc) · 9.83 KB
/
timer.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* @file Script for timer
* @copyright LimeSurvey <http://www.limesurvey.org>
* @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
*/
var TimerConstructor = function(options){
"use strict";
/* ##### public methods ##### */
/**
* Starts the timer
* Sts the interval to visualize the timer and the timeouts for the warnings.
*/
var startTimer = function(){
if(timeLeft == 0){
finishTimer();
return;
}
_setTimerToLocalStorage(timeLeft);
_disableNavigation();
_setInterval();
},
/**
* Finishing action
* Unsets all timers and intervals and then triggers the defined action.
* Either redirect, invalidate or warn before redirect
*/
finishTimer = function(){
timerLogger.log('Timer has ended or was ended');
_unsetInterval();
_enableNavigation();
_bindUnsetToSubmit();
switch(option.action)
{
case 3: //Just warn, don't move on
_showExpiredNotice();
_disableInput();
break;
case 2: //Just move on, no warning
_redirectOut();
break;
case 1: //fallthrough
default: //Warn and move on
_showExpiredNotice();
_warnBeforeRedirection();
break;
}
};
/* ##### private methods ##### */
/**
* Parses the options to default values if not set
* @param Object options
* @return Object
*/
var _parseOptions = function(option) {
return {
questionid : option.questionid || null,
timer : option.timer || 0,
action : option.action || 1,
warning : option.warning || 0,
warning2 : option.warning2 || 0,
warninghide : option.warninghide || 0,
warning2hide : option.warning2hide || 0,
disabledElement : option.disabledElement || null,
}
},
/**
* Takes a duration in seconds and creates an object containing the duration in hours, minutes and seconds
* @param int seconds The duration in seconds
* @return Object Contains hours, minutes and seconds
*/
_parseTimeToObject = function(secLeft, asStrings){
asStrings = asStrings || false;
var oDuration = moment.duration(secLeft, 'seconds');
var sHours = String(oDuration.hours()),
sMinutes = String(oDuration.minutes()),
sSeconds = String(oDuration.seconds());
return {
hours : asStrings ? (sHours.length == 1 ? '0'+sHours : sHours) : parseInt(sHours),
minutes : asStrings ? (sMinutes.length == 1 ? '0'+sMinutes : sMinutes) : parseInt(sMinutes),
seconds : asStrings ? (sSeconds.length == 1 ? '0'+sSeconds : sSeconds) : parseInt(sSecond)
};
},
/**
* The actions done on each step and the trigger to the finishing action
*/
_intervalStep = function(){
var currentTimeLeft = _getTimerFromLocalStorage();
currentTimeLeft = currentTimeLeft-1 ;
timerLogger.log('Interval emitted | seconds left:', currentTimeLeft);
if(currentTimeLeft <= 0){
finishTimer();
}
_checkForWarning(currentTimeLeft);
_setTimerToLocalStorage(currentTimeLeft);
_setTimer(currentTimeLeft);
},
/**
* Set the interval to update the timer visuals
*/
_setInterval = function(){
_setTimer(option.timer);
intervalObject = setInterval(_intervalStep, 1000);
},
/**
* Unset the timer;
*/
_unsetInterval = function(){
clearInterval(intervalObject);
intervalObject = null;
},
/**
* Sets the timer to the display element
*/
_setTimer = function(currentTimeLeft){
var timeObject = _parseTimeToObject(currentTimeLeft, true);
$timerDisplayElement
.css({display: 'flex'})
.html($countDownMessageElement.html()+" <div class='ls-timer-time'>"+ timeObject.hours +':'+ timeObject.minutes +':'+ timeObject.seconds +"</div>");
},
/**
* Checks if a warning should be shown relative to the interval
* @param int currentTime The current amount of seconds gone
*/
_checkForWarning = function(currentTime){
if(currentTime == option.warning){
_showWarning();
}
if(currentTime == option.warning2){
_showWarning2();
}
},
/**
* Shows the warning and fades it out after the set amount of time
*/
_showWarning = function(){
timerLogger.log('Warning called!');
$warningDisplayElement.removeClass('hidden').css({opacity: 0}).animate({'opacity': 1}, 200);
setTimeout( function(){
timerLogger.log('Warning ended!');
$warningDisplayElement.animate({opacity: 0}, 200, function(){
$(this).addClass('hidden');
})
}, 1000*option.warninghide );
},
/**
* Shows the warning2 and fades it out after the set amount of time
*/
_showWarning2 = function(){
timerLogger.log('Warning2 called!');
$warning2DisplayElement.removeClass('hidden').css({opacity: 0}).animate({'opacity': 1}, 200);
setTimeout(function(){
timerLogger.log('Warning2 ended!');
$warning2DisplayElement.animate({opacity: 0}, 200, function(){
$(this).addClass('hidden');
})
}, 1000*option.warning2hide );
},
/**
* Disables the navigation buttons if necessary
*/
_disableNavigation = function(){
timerLogger.log('Disabling navigation');
$('.ls-move-previous-btn').each(function(){
$(this).prop('disabled',(disable_prev==1));
});
$('.ls-move-next-btn,.ls-move-submit-btn').each(function(){
$(this).prop('disabled',(disable_next==1));
});
},
/**
* Enables the navigation buttons
*/
_enableNavigation = function(){
$('.ls-move-previous-btn').each(function(){
$(this).prop('disabled',false);
});
$('.ls-move-next-btn,.ls-move-submit-btn').each(function(){
$(this).prop('disabled',false);
});
},
/**
* Gets the current timer from the localStorage
*/
_getTimerFromLocalStorage = function(){
return window.localStorage.getItem('limesurvey_timers_'+timersessionname);
},
/**
* Sets the current timer to localStorage
*/
_setTimerToLocalStorage = function(timerValue){
window.localStorage.setItem('limesurvey_timers_'+timersessionname, timerValue);
},
/**
* Unsets the timer in localStorage
*/
_unsetTimerInLocalStorage = function(){
window.localStorage.removeItem('limesurvey_timers_'+timersessionname);
},
/**
* Finalize Method to show a warning and then redirect
*/
_warnBeforeRedirection = function(){
_disableInput();
setTimeout(_redirectOut, redirectWarnTime);
},
/**
* Finalize method to just diable the input
*/
_disableInput = function(){
$toBeDisabledElement.prop('readonly',true);
},
/**
* Show the notice that the time is up and the input is expired
*/
_showExpiredNotice = function(){
$timerExpiredElement.removeClass('hidden');
},
/**
* redirect to the next page
*/
_redirectOut = function(){
$('.action--ls-button-submit').trigger('click');
},
/**
* Binds the reset of the localStorage as soon as the participant has submitted the form
*/
_bindUnsetToSubmit = function(){
$('#limesurvey').on('submit', function(){
_unsetTimerInLocalStorage();
});
};
/* ##### define state and closure vars ##### */
var option = _parseOptions(options),
timerWarning = null,
timerWarning2 = null,
timerLogger = new ConsoleShim('TIMER#'+options.questionid, !window.debugState.frontend),
intervalObject = null,
warning = 0,
timersessionname='timer_question_'+option.questionid,
timeLeft = _getTimerFromLocalStorage() || option.timer,
disable_next = $("#disablenext-"+timersessionname).val(),
disable_prev = $("#disableprev-"+timersessionname).val(),
//jQuery Elements
$timerDisplayElement = $('#LS_question'+option.questionid+'_Timer'),
$timerExpiredElement = $('#question'+option.questionid+'_timer'),
$warningTimeDisplayElement = $('#LS_question'+option.questionid+'_Warning'),
$warningDisplayElement = $('#LS_question'+option.questionid+'_warning'),
$warning2TimeDisplayElement=$('#LS_question'+option.questionid+'_Warning_2'),
$warning2DisplayElement=$('#LS_question'+option.questionid+'_warning_2'),
$countDownMessageElement = $("#countdown-message-"+timersessionname),
redirectWarnTime=$('#message-delay-'+timersessionname).val(),
$toBeDisabledElement=$('#'+option.disabledElement);
timerLogger.log('Options set:', option);
return {
startTimer : startTimer,
finishTimer : finishTimer
};
};
function countdown(questionid,timer,action,warning,warning2,warninghide,warning2hide,disable)
{
window.timerObjectSpace = window.timerObjectSpace || {};
window.timerObjectSpace[questionid] = new TimerConstructor(
{
questionid : questionid,
timer : timer,
action : action,
warning : warning,
warning2 : warning2,
warninghide : warninghide,
warning2hide : warning2hide,
disabledElement : disable
}
);
window.timerObjectSpace[questionid].startTimer();
}