1
1
2
2
3
3
var lock = false ;
4
+ var responses = { } ;
4
5
5
- function displayQuizItem ( question ) {
6
+ function displayQuestion ( question ) {
6
7
var questionElem = document . getElementById ( "question" ) ;
7
8
removeChildren ( questionElem ) ;
8
9
var answersElem = document . getElementById ( "answers" ) ;
@@ -14,6 +15,9 @@ function displayQuizItem(question) {
14
15
console . log ( "Inputs: " + question . inputs ) ;
15
16
}
16
17
displayHeader ( question . text ) ;
18
+ if ( question . southText !== undefined ) {
19
+ displaySouthText ( question . southText ) ;
20
+ }
17
21
18
22
if ( question . response . type === "singleChoice" || question . response . type === "multiChoice" ) {
19
23
displayResponseAlternatives ( question ) ;
@@ -32,12 +36,18 @@ function displayHeader(text) {
32
36
questionElem . innerHTML = textNode ;
33
37
}
34
38
39
+ function displaySouthText ( text ) {
40
+ var southTextElem = document . getElementById ( "southtext" ) ;
41
+ southTextElem . innerHTML = text ;
42
+ }
43
+
35
44
function displayResponseAlternatives ( question ) {
36
45
var answersElem = document . getElementById ( "answers" ) ;
37
46
for ( var i = 0 ; i < question . response . alternatives . length ; i ++ ) {
38
47
var responseAlternatives = question . response . alternatives [ i ] ;
39
48
var questionNoText = document . createTextNode ( responseAlternatives . value ) ;
40
- var answerText = document . createTextNode ( " " + responseAlternatives . text ) ;
49
+ var answerText = document . createElement ( "span" ) ; //document.createTextNode(" " + responseAlternatives.text);
50
+ answerText . innerHTML = " " + responseAlternatives . text ;
41
51
var button = document . createElement ( "input" ) ;
42
52
43
53
button . id = "answerButton" + responseAlternatives . value ;
@@ -52,11 +62,20 @@ function displayResponseAlternatives(question) {
52
62
button . name = "answerButton" ;
53
63
button . value = responseAlternatives . value ;
54
64
55
- if ( $ . inArray ( responseAlternatives . value , question . inputs ) !== - 1 ) {
65
+ if ( ( question . response . type === "multiChoice" && $ . inArray ( responseAlternatives . value , question . inputs ) !== - 1 ) ||
66
+ question . response . type === "singleChoice" && responseAlternatives . value === question . inputs ) {
56
67
button . checked = true ;
57
- }
58
- answersElem . appendChild ( ulWrapper ( questionNoText , button , answerText ) ) ;
59
68
}
69
+ answersElem . appendChild ( ulWrapper ( questionNoText , button , answerText ) ) ;
70
+ }
71
+ }
72
+
73
+ function ulWrapper ( ) {
74
+ var ulElem = document . createElement ( "ul" ) ;
75
+ for ( var i = 0 ; i < arguments . length ; i ++ ) {
76
+ ulElem . appendChild ( arguments [ i ] ) ;
77
+ }
78
+ return ulElem ;
60
79
}
61
80
62
81
function displayTextField ( question ) {
@@ -83,15 +102,27 @@ function nextQuestion() {
83
102
return ;
84
103
}
85
104
if ( Questionnaire . currentQuestion ( ) . response . type === QuestionTypes . textField ) { // && Questionnaire.currentQuestion().regex) {
86
- var regExp = new RegExp ( Questionnaire . currentQuestion ( ) . response . regex ) ;
87
- var matchResult = Questionnaire . currentQuestion ( ) . inputs . match ( regExp ) ;
88
- console . log ( "Validation: " + matchResult ) ;
89
- if ( matchResult && Questionnaire . currentQuestion ( ) . inputs === matchResult [ 0 ] ) {
90
- console . log ( "MATCH!!!" ) ;
91
- }
105
+ checkRegExp ( Questionnaire . currentQuestion ( ) ) ;
92
106
}
93
- if ( Questionnaire . currentQuestion ( ) . inputs && Questionnaire . currentQuestion ( ) . inputs . length !== 0 && Questionnaire . currentQuestion ( ) !== Questionnaire . lastQuestion ( ) ) {
94
- displayQuizItem ( Questionnaire . nextQuestion ( ) ) ;
107
+ if ( Questionnaire . hasResponse ( Questionnaire . currentQuestion ( ) ) ) {
108
+ responses [ Questionnaire . currentQuestion ( ) . variableName ] = Questionnaire . currentQuestion ( ) . inputs ;
109
+ displayQuestion ( Questionnaire . nextQuestion ( ) ) ;
110
+ }
111
+ }
112
+
113
+ function checkRegExp ( question ) {
114
+ if ( ! question . response . regexp ) {
115
+ return true ;
116
+ }
117
+ var regExp = new RegExp ( question . response . regex ) ;
118
+ var matchResult = question . inputs . match ( regExp ) ;
119
+ console . log ( "Validation: " + matchResult ) ;
120
+ if ( matchResult && question . inputs === matchResult [ 0 ] ) {
121
+ console . log ( "MATCH!!!" ) ;
122
+ return true ;
123
+ }
124
+ else {
125
+ return false ;
95
126
}
96
127
}
97
128
@@ -100,7 +131,7 @@ function previousQuestion() {
100
131
return ;
101
132
}
102
133
if ( Questionnaire . currentQuestion ( ) !== Questionnaire . firstQuestion ( ) ) {
103
- displayQuizItem ( Questionnaire . previousQuestion ( ) ) ;
134
+ displayQuestion ( Questionnaire . previousQuestion ( ) ) ;
104
135
}
105
136
}
106
137
@@ -109,49 +140,49 @@ function currentAnswers() {
109
140
var answerValues = [ ] ;
110
141
for ( var i = 0 ; i < answerButtons . length ; i ++ ) {
111
142
if ( answerButtons [ i ] . checked ) {
112
- answerValues . push ( answerButtons [ i ] . value ) ;
143
+ if ( answerButtons [ i ] . type === "checkbox" ) {
144
+ answerValues . push ( parseInt ( answerButtons [ i ] . value ) ) ;
145
+ }
146
+ else if ( answerButtons [ i ] . type === "radio" ) {
147
+ answerValues = parseInt ( answerButtons [ i ] . value ) ;
148
+ return answerValues ;
149
+ }
113
150
}
114
151
}
115
152
return answerValues ;
116
153
}
117
154
118
155
119
- function removeChildren ( element ) {
120
- while ( element . firstChild ) {
121
- element . removeChild ( element . firstChild ) ;
122
- }
123
- }
156
+ function removeChildren ( element ) {
157
+ while ( element . firstChild ) {
158
+ element . removeChild ( element . firstChild ) ;
159
+ }
160
+ }
124
161
125
162
126
- function radioButtonClicked ( ) {
127
- if ( lock === true ) {
128
- return ;
129
- }
163
+ function radioButtonClicked ( ) {
164
+ if ( lock === true ) {
165
+ return ;
166
+ }
167
+
168
+ lock = true ;
169
+ storeSelectedResponseAlternatives ( ) ;
130
170
131
- lock = true ;
132
- storeSelectedResponseAlternatives ( ) ;
171
+ document . getElementById ( "next" ) . disabled = false ;
172
+ setTimeout ( function ( ) {
173
+ lock = false ;
174
+ nextQuestion ( ) ;
175
+ } , 200 ) ;
176
+ }
133
177
134
- document . getElementById ( "next" ) . disabled = false ;
135
- setTimeout ( function ( ) {
136
- lock = false ;
137
- nextQuestion ( ) ;
138
- } , 200 ) ;
139
- }
178
+ function storeSelectedResponseAlternatives ( ) {
179
+ Questionnaire . currentQuestion ( ) . inputs = currentAnswers ( ) ;
180
+ }
140
181
141
- function storeSelectedResponseAlternatives ( ) {
142
- Questionnaire . currentQuestion ( ) . inputs = currentAnswers ( ) ;
143
- }
144
182
145
- function ulWrapper ( ) {
146
- var ulElem = document . createElement ( "ul" ) ;
147
- for ( var i = 0 ; i < arguments . length ; i ++ ) {
148
- ulElem . appendChild ( arguments [ i ] ) ;
149
- }
150
- return ulElem ;
151
- }
152
183
153
- document . getElementById ( "next" ) . addEventListener ( "click" , nextQuestion , false ) ;
154
- document . getElementById ( "previous" ) . addEventListener ( "click" , previousQuestion , false ) ;
184
+ document . getElementById ( "next" ) . addEventListener ( "click" , nextQuestion , false ) ;
185
+ document . getElementById ( "previous" ) . addEventListener ( "click" , previousQuestion , false ) ;
155
186
156
187
157
188
// Keyboard listener
@@ -201,12 +232,12 @@ window.onkeyup = function(event) {
201
232
202
233
function leftArrowIsApplicable ( ) {
203
234
return ( responseType === "singleChoice" || responseType === "multiChoice" || responseType === "info" ) ||
204
- ( responseType === "textField" && document . getElementById ( "textField" ) !== document . activeElement ) ;
235
+ ( responseType === "textField" && document . getElementById ( "textField" ) !== document . activeElement ) ;
205
236
} ;
206
237
207
238
function rightArrowIsApplicable ( ) {
208
239
return ( responseType === "singleChoice" || responseType === "multiChoice" ) ||
209
- ( responseType === "textField" && document . getElementById ( "textField" ) !== document . activeElement ) ;
240
+ ( responseType === "textField" && document . getElementById ( "textField" ) !== document . activeElement ) ;
210
241
} ;
211
242
212
243
function enterIsApplicable ( ) {
@@ -218,7 +249,7 @@ window.onkeyup = function(event) {
218
249
answerButton . checked = true ;
219
250
radioButtonClicked ( ) ;
220
251
}
221
- if ( answerButton . type === "checkbox" ) {
252
+ else if ( answerButton . type === "checkbox" ) {
222
253
answerButton . checked = ! answerButton . checked ;
223
254
storeSelectedResponseAlternatives ( ) ;
224
255
}
@@ -230,6 +261,7 @@ window.onkeyup = function(event) {
230
261
var Questionnaire = ( function ( ) {
231
262
var questions ;
232
263
var currentQuestionNo = 0 ;
264
+ var questionHistory = [ ] ;
233
265
234
266
235
267
return {
@@ -240,10 +272,22 @@ var Questionnaire = (function() {
240
272
return questions [ currentQuestionNo ] ;
241
273
} ,
242
274
nextQuestion : function ( ) {
243
- return questions [ ++ currentQuestionNo ] ;
275
+ questionHistory . push ( {
276
+ question : questions [ currentQuestionNo ] ,
277
+ questionNo : currentQuestionNo
278
+ } ) ;
279
+ responses [ questions [ currentQuestionNo ] . variableName ] = questions [ currentQuestionNo ] . inputs ;
280
+ currentQuestionNo ++ ;
281
+ while ( questions [ currentQuestionNo ] . condition !== undefined && ! evaluate ( questions [ currentQuestionNo ] . condition ) ) {
282
+ console . log ( "Condition: " + evaluate ( questions [ currentQuestionNo ] . condition ) ) ;
283
+ currentQuestionNo ++ ;
284
+ }
285
+ return questions [ currentQuestionNo ] ;
244
286
} ,
245
287
previousQuestion : function ( ) {
246
- return questions [ -- currentQuestionNo ] ;
288
+ var historyItem = questionHistory . pop ( ) ;
289
+ currentQuestionNo = historyItem . questionNo ;
290
+ return historyItem . question ; //questions[--currentQuestionNo];
247
291
} ,
248
292
firstQuestion : function ( ) {
249
293
return questions [ 0 ] ;
@@ -253,8 +297,53 @@ var Questionnaire = (function() {
253
297
} ,
254
298
length : function ( ) {
255
299
return questions . length ;
300
+ } ,
301
+ hasResponse : function ( question ) {
302
+ if ( questions . inputs !== undefined ) {
303
+ return false ;
304
+ }
305
+ var singleChoiceResponse = question . response . type === QuestionTypes . singleChoice && question . inputs !== null ;
306
+ var multiChoiceResponse = question . response . type === QuestionTypes . multiChoice && question . inputs . length > 0 ;
307
+ var textFieldResponse = question . response . type === QuestionTypes . textField && question . inputs !== null && question . inputs . trim ( ) !== "" ;
308
+ return singleChoiceResponse || multiChoiceResponse || textFieldResponse ;
256
309
}
257
- } ;
310
+ }
311
+
312
+ function evaluate ( element ) {
313
+ if ( element === true ) {
314
+ return true ;
315
+ }
316
+ else if ( element === false ) {
317
+ return false ;
318
+ }
319
+ else {
320
+ if ( element . op === "AND" ) {
321
+ return evaluate ( element . left ) && evaluate ( element . right ) ;
322
+ }
323
+ else if ( element . op === "OR" ) {
324
+ return evaluate ( element . left ) || evaluate ( element . right ) ;
325
+ }
326
+ else if ( element . op === "NOT" ) {
327
+ return ! evaluate ( element . right ) ;
328
+ }
329
+ else if ( element . op === "==" ) {
330
+ return checkEquality ( element . left , element . right ) ;
331
+ }
332
+ else if ( element . op === "!=" ) {
333
+ return ! checkEquality ( element . left , element . right ) ;
334
+ }
335
+ }
336
+ }
337
+
338
+ function checkEquality ( left , right ) {
339
+ if ( left . variableReference ) {
340
+ left = responses [ left . variableReference ] ;
341
+ }
342
+ if ( right . variableReference ) {
343
+ right = responses [ right . variableReference ] ;
344
+ }
345
+ return left === right ;
346
+ }
258
347
} ) ( ) ;
259
348
260
349
var QuestionTypes = {
@@ -265,18 +354,20 @@ var QuestionTypes = {
265
354
} ;
266
355
267
356
357
+
268
358
// Init
269
359
$ ( document ) . ready ( function ( ) {
270
360
//window.onbeforeunload = function() { return "You work will be lost."; };
271
361
$ . getJSON ( "http://localhost:8000/questions.json" , function ( data ) {
272
362
console . log ( "Loading JSON questionnaire file." ) ;
363
+ console . log ( data ) ;
273
364
Questionnaire . init ( data ) ;
274
- displayQuizItem ( Questionnaire . firstQuestion ( ) ) ;
365
+ displayQuestion ( Questionnaire . firstQuestion ( ) ) ;
275
366
} ) ;
276
367
} ) ;
277
368
278
369
jQuery . extend ( jQuery . expr [ ':' ] , {
279
- focus : "a == document.activeElement"
370
+ focus : "a == document.activeElement"
280
371
} ) ;
281
372
282
373
0 commit comments