Team:Cambridge-JIC/Assets/Quiz js
quiz = function(elem){ this.elem = $(elem); this.steps = [];
this.step = function(question, answers) { this.question = question; this.answers = {}; this.bind = function(target, keys) { if (typeof keys === "undefined") { keys = Object.keys(this.answers); }
for (var i=0; i<keys.length; i++) { key = keys[i]; this.answers[key].target = target; } }; this.elem = function(quiz, index) {elem = $('
');
elem.append($('').text(this.question));
for (key in this.answers) {
elem.append($('
').text(this.question));
for (key in this.answers) {
elem.append($('')
.text(this.answers[key].answer)
.data("key", key)
.data("step", this)
.data("quiz", quiz)
.data("index", index)
.click(function(){
key = $(this).data("key");
step = $(this).data("step");
quiz = $(this).data("quiz");
index = $(this).data("index");
ref = quiz.steps[index];
ref.elem.find('.cam-quiz-selected').removeClass('cam-quiz-selected');
$(this).addClass('cam-quiz-selected');
for (var i=quiz.steps.length; --i>index;) {
quiz.steps[i].elem.remove();
quiz.steps.pop();
}
quiz.steps[index].option = key;
nstep = step.answers[key].target;
switch (typeof nstep) {
case "object":
var nindex = quiz.steps.length;
var nref = {
'step': nstep,
'elem': nstep.elem(quiz, nindex),
'option': undefined
};
quiz.steps.push(nref);
quiz.elem.append(nref.elem);
break;
case "function":
options = [];
for (var i=0; i<quiz.steps.length; i++) {
options.push(quiz.steps[i].option);
}
var nref = {
'step': undefined,
'elem': $('')
.append(nstep(options)),
'option': undefined
};
quiz.steps.push(nref);
quiz.elem.append(nref.elem);
break;
default:
alert("Quiz error! Please contact the site maintainers....");
break;
}
}));
}
return elem;
};
for (key in answers) {
answer = answers[key];
this.answers[key] = {
'answer': answer,
'target': undefined
};
}
};
this.start = function(step) {
index = this.steps.length;
ref = {
'step': step,
'elem': step.elem(this, index),
'option': undefined
};
this.steps.push(ref);
this.elem.append(ref.elem);
};
};
.text(this.answers[key].answer) .data("key", key) .data("step", this) .data("quiz", quiz) .data("index", index) .click(function(){ key = $(this).data("key"); step = $(this).data("step"); quiz = $(this).data("quiz"); index = $(this).data("index"); ref = quiz.steps[index]; ref.elem.find('.cam-quiz-selected').removeClass('cam-quiz-selected'); $(this).addClass('cam-quiz-selected');
for (var i=quiz.steps.length; --i>index;) { quiz.steps[i].elem.remove(); quiz.steps.pop(); }
quiz.steps[index].option = key; nstep = step.answers[key].target; switch (typeof nstep) { case "object": var nindex = quiz.steps.length; var nref = { 'step': nstep, 'elem': nstep.elem(quiz, nindex), 'option': undefined }; quiz.steps.push(nref); quiz.elem.append(nref.elem); break; case "function": options = []; for (var i=0; i<quiz.steps.length; i++) { options.push(quiz.steps[i].option); } var nref = { 'step': undefined,'elem': $('
')
.append(nstep(options)), 'option': undefined }; quiz.steps.push(nref); quiz.elem.append(nref.elem); break; default: alert("Quiz error! Please contact the site maintainers...."); break; } })); } return elem; };
for (key in answers) { answer = answers[key]; this.answers[key] = { 'answer': answer, 'target': undefined }; } };
this.start = function(step) { index = this.steps.length; ref = { 'step': step, 'elem': step.elem(this, index), 'option': undefined }; this.steps.push(ref); this.elem.append(ref.elem); };};