Skip to content

Commit 796473d

Browse files
committed
Now with conditional logic
1 parent 7de9ac2 commit 796473d

File tree

4 files changed

+288
-81
lines changed

4 files changed

+288
-81
lines changed

apprunner.html

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<h1>CAPItan <em>Barbarossa</em></h1>
1313
<p id="question"></p>
1414
<p id="answers"></p>
15+
<p id="southtext"></p>
1516
<input type="button" id="previous" value="Previous">
1617
<input type="button" id="next" value="Next">
1718
<script type="text/javascript" src="nyrequest.js"></script>

nyrequest.js

+143-52
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

22

33
var lock = false;
4+
var responses = {};
45

5-
function displayQuizItem(question) {
6+
function displayQuestion(question) {
67
var questionElem = document.getElementById("question");
78
removeChildren(questionElem);
89
var answersElem = document.getElementById("answers");
@@ -14,6 +15,9 @@ function displayQuizItem(question) {
1415
console.log("Inputs: " + question.inputs);
1516
}
1617
displayHeader(question.text);
18+
if (question.southText !== undefined) {
19+
displaySouthText(question.southText);
20+
}
1721

1822
if (question.response.type === "singleChoice" || question.response.type === "multiChoice") {
1923
displayResponseAlternatives(question);
@@ -32,12 +36,18 @@ function displayHeader(text) {
3236
questionElem.innerHTML = textNode;
3337
}
3438

39+
function displaySouthText(text) {
40+
var southTextElem = document.getElementById("southtext");
41+
southTextElem.innerHTML = text;
42+
}
43+
3544
function displayResponseAlternatives(question) {
3645
var answersElem = document.getElementById("answers");
3746
for (var i = 0; i < question.response.alternatives.length; i++) {
3847
var responseAlternatives = question.response.alternatives[i];
3948
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;
4151
var button = document.createElement("input");
4252

4353
button.id = "answerButton" + responseAlternatives.value;
@@ -52,11 +62,20 @@ function displayResponseAlternatives(question) {
5262
button.name = "answerButton";
5363
button.value = responseAlternatives.value;
5464

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) {
5667
button.checked = true;
57-
}
58-
answersElem.appendChild(ulWrapper(questionNoText, button, answerText));
5968
}
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;
6079
}
6180

6281
function displayTextField(question) {
@@ -83,15 +102,27 @@ function nextQuestion() {
83102
return;
84103
}
85104
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());
92106
}
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;
95126
}
96127
}
97128

@@ -100,7 +131,7 @@ function previousQuestion() {
100131
return;
101132
}
102133
if (Questionnaire.currentQuestion() !== Questionnaire.firstQuestion()) {
103-
displayQuizItem(Questionnaire.previousQuestion());
134+
displayQuestion(Questionnaire.previousQuestion());
104135
}
105136
}
106137

@@ -109,49 +140,49 @@ function currentAnswers() {
109140
var answerValues = [];
110141
for (var i = 0; i < answerButtons.length; i++) {
111142
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+
}
113150
}
114151
}
115152
return answerValues;
116153
}
117154

118155

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+
}
124161

125162

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();
130170

131-
lock = true;
132-
storeSelectedResponseAlternatives();
171+
document.getElementById("next").disabled = false;
172+
setTimeout(function() {
173+
lock = false;
174+
nextQuestion();
175+
}, 200);
176+
}
133177

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+
}
140181

141-
function storeSelectedResponseAlternatives() {
142-
Questionnaire.currentQuestion().inputs = currentAnswers();
143-
}
144182

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-
}
152183

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);
155186

156187

157188
// Keyboard listener
@@ -201,12 +232,12 @@ window.onkeyup = function(event) {
201232

202233
function leftArrowIsApplicable() {
203234
return (responseType === "singleChoice" || responseType === "multiChoice" || responseType === "info") ||
204-
(responseType === "textField" && document.getElementById("textField") !== document.activeElement);
235+
(responseType === "textField" && document.getElementById("textField") !== document.activeElement);
205236
};
206237

207238
function rightArrowIsApplicable() {
208239
return (responseType === "singleChoice" || responseType === "multiChoice") ||
209-
(responseType === "textField" && document.getElementById("textField") !== document.activeElement);
240+
(responseType === "textField" && document.getElementById("textField") !== document.activeElement);
210241
};
211242

212243
function enterIsApplicable() {
@@ -218,7 +249,7 @@ window.onkeyup = function(event) {
218249
answerButton.checked = true;
219250
radioButtonClicked();
220251
}
221-
if (answerButton.type === "checkbox") {
252+
else if (answerButton.type === "checkbox") {
222253
answerButton.checked = !answerButton.checked;
223254
storeSelectedResponseAlternatives();
224255
}
@@ -230,6 +261,7 @@ window.onkeyup = function(event) {
230261
var Questionnaire = (function() {
231262
var questions;
232263
var currentQuestionNo = 0;
264+
var questionHistory = [];
233265

234266

235267
return {
@@ -240,10 +272,22 @@ var Questionnaire = (function() {
240272
return questions[currentQuestionNo];
241273
},
242274
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];
244286
},
245287
previousQuestion: function() {
246-
return questions[--currentQuestionNo];
288+
var historyItem = questionHistory.pop();
289+
currentQuestionNo = historyItem.questionNo;
290+
return historyItem.question; //questions[--currentQuestionNo];
247291
},
248292
firstQuestion: function() {
249293
return questions[0];
@@ -253,8 +297,53 @@ var Questionnaire = (function() {
253297
},
254298
length: function() {
255299
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;
256309
}
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+
}
258347
})();
259348

260349
var QuestionTypes = {
@@ -265,18 +354,20 @@ var QuestionTypes = {
265354
};
266355

267356

357+
268358
// Init
269359
$(document).ready(function () {
270360
//window.onbeforeunload = function() { return "You work will be lost."; };
271361
$.getJSON("http://localhost:8000/questions.json", function(data) {
272362
console.log("Loading JSON questionnaire file.");
363+
console.log(data);
273364
Questionnaire.init(data);
274-
displayQuizItem(Questionnaire.firstQuestion());
365+
displayQuestion(Questionnaire.firstQuestion());
275366
});
276367
});
277368

278369
jQuery.extend(jQuery.expr[':'], {
279-
focus: "a == document.activeElement"
370+
focus: "a == document.activeElement"
280371
});
281372

282373

0 commit comments

Comments
 (0)