Project

General

Profile

1
/*
2
 SlickQuiz jQuery Plugin
3
 http://github.com/jewlofthelotus/SlickQuiz
4

    
5
 @updated October 25, 2014
6
 @version 1.5.20
7

    
8
 @author Julie Cameron - http://www.juliecameron.com
9
 @copyright (c) 2013 Quicken Loans - http://www.quickenloans.com
10
 @license MIT
11
*/
12
(function(b){b.slickQuiz=function(C,c){var a=this,y=b(C),e="#"+y.attr("id"),z="nextQuestion",v="."+z,F=e+" .startQuiz",h=e+" .quizName",I=e+" .quizArea",J=e+" .quizResults",K=e+" .quizResultsCopy",L=e+" .quizHeader",D=e+" .quizScore",A=e+" .quizLevel",q=b(F),G=b(h),E=b(I),B=b(J),H=b(K),M=b(L);b(D);var N=b(A),h="";c&&"undefined"!=typeof c.disableNext&&("undefined"==typeof c.preventUnanswered&&(c.preventUnanswered=c.disableNext),h+="The 'disableNext' option has been deprecated, please use 'preventUnanswered' in it's place.\n\n");
13
c&&"undefined"!=typeof c.disableResponseMessaging&&("undefined"==typeof c.preventUnanswered&&(c.perQuestionResponseMessaging=c.disableResponseMessaging),h+="The 'disableResponseMessaging' option has been deprecated, please use 'perQuestionResponseMessaging' and 'completionResponseMessaging' in it's place.\n\n");c&&"undefined"!=typeof c.randomSort&&("undefined"==typeof c.randomSortQuestions&&(c.randomSortQuestions=c.randomSort),"undefined"==typeof c.randomSortAnswers&&(c.randomSortAnswers=c.randomSort),
14
h+="The 'randomSort' option has been deprecated, please use 'randomSortQuestions' and 'randomSortAnswers' in it's place.\n\n");""!==h&&("undefined"!=typeof console?console.warn(h):alert(h));a.config=b.extend({checkAnswerText:"Check My Answer!",nextQuestionText:'Next <i class="material-icons">&#xE5CC;</i>',backButtonText:"",completeQuizText:"",tryAgainText:"",questionCountText:"Question %current of %total",preventUnansweredText:"You must select at least one answer.",questionTemplateText:"%count. %text",
15
scoreTemplateText:"%score / %total",nameTemplateText:"<span>Quiz: </span>%name",skipStartButton:!1,numberOfQuestions:null,randomSortQuestions:!1,randomSortAnswers:!1,preventUnanswered:!1,disableScore:!1,disableRanking:!1,scoreAsPercentage:!1,perQuestionResponseMessaging:!0,perQuestionResponseAnswers:!1,completionResponseMessaging:!1,displayQuestionCount:!0,displayQuestionNumber:!0,animationCallbacks:{setupQuiz:function(){},startQuiz:function(){},resetQuiz:function(){},checkAnswer:function(){},nextQuestion:function(){},
16
backToQuestion:function(){},completeQuiz:function(){}},events:{onStartQuiz:function(a){},onCompleteQuiz:function(a){}}},c);var p=a.config.json?a.config.json:"undefined"!=typeof quizJSON?quizJSON:null,r=a.config.randomSortQuestions?p.questions.sort(function(){return Math.round(Math.random())-.5}):p.questions,t=r.length;a.config.numberOfQuestions&&t>=a.config.numberOfQuestions&&(r=r.slice(0,a.config.numberOfQuestions),t=r.length);var k={getKey:function(a){var d=[];for(i=0;i<a;i++)d[i]=b.Deferred();
17
return d},turnKeyAndGo:function(a,d){b.when.apply(null,a).then(function(){d()})},getKeyNotch:function(a,d){return function(){a[d-1].resolve()}}};a.method={setupQuiz:function(m){var d=k.getKey(3);var l=k.getKeyNotch;G.hide().html(a.config.nameTemplateText.replace("%name",p.info.name)).fadeIn(1E3,l(d,1));M.hide().prepend(b('<div class="quizDescription">'+p.info.main+"</div>")).fadeIn(1E3,l(d,2));H.append(p.info.results);a.config.tryAgainText&&""!==a.config.tryAgainText&&H.append('<p><a class="button tryAgain" href="#">'+
18
a.config.tryAgainText+"</a></p>");var e=b('<ol class="questions"></ol>'),f=1;for(i in r)if(r.hasOwnProperty(i)){var g=r[i],c=b('<li class="question" id="question'+(f-1)+'"></li>');a.config.displayQuestionCount&&c.append('<div class="questionCount">'+a.config.questionCountText.replace("%current",'<span class="current">'+f+"</span>").replace("%total",'<span class="total">'+t+"</span>")+"</div>");var n="",n=a.config.displayQuestionNumber?a.config.questionTemplateText.replace("%count",f).replace("%text",
19
g.q):g.q;c.append("<h3>"+n+"</h3>");var w=0;for(i in g.a)g.a.hasOwnProperty(i)&&(answer=g.a[i],answer.correct&&w++);var n=b('<ul class="answers"></ul>'),h=a.config.randomSortAnswers?g.a.sort(function(){return Math.round(Math.random())-.5}):g.a,x=g.select_any?g.select_any:!1,u=g.force_checkbox?g.force_checkbox:!1,x=1<w&&!x||u,w=y.attr("id")+"_question"+(f-1),x=x?"checkbox":"radio";f==p.questions.length&&(z+=" lastQuestion");for(i in h)if(h.hasOwnProperty(i)){answer=h[i];optionId=w+"_"+i.toString();
20
var u='<input id="'+optionId+'" name="'+w+'" type="'+x+'" /> ',v='<label for="'+optionId+'">'+answer.option+"</label>",u=b("<li></li>").append(u).append(v);n.append(u)}c.append(n);if(a.config.perQuestionResponseMessaging||a.config.completionResponseMessaging)n=b('<ul class="responses"></ul>'),n.append('<li class="correct">'+g.correct+"</li>"),n.append('<li class="incorrect">'+g.incorrect+"</li>"),c.append(n);a.config.backButtonText&&""!==a.config.backButtonText&&c.append('<a href="#" class="md-btn md-btn-large backToQuestion">'+
21
a.config.backButtonText+"</a>");g=a.config.nextQuestionText;a.config.completeQuizText&&f==t&&(g=a.config.completeQuizText);a.config.perQuestionResponseMessaging?(c.append('<a href="#" class="md-btn md-btn-large '+z+'">'+g+"</a>"),c.append('<a href="#" class="md-btn md-btn-large checkAnswer">'+a.config.checkAnswerText+"</a>")):c.append('<a href="#" class="md-btn md-btn-large '+z+' checkAnswer">'+g+"</a>");e.append(c);f++}E.append(e);a.config.skipStartButton||0==q.length?(q.hide(),a.method.startQuiz.apply(this,
22
[{callback:a.config.animationCallbacks.startQuiz}]),l(d,3).apply(null,[])):q.fadeIn(500,l(d,3)).css("display","inline-block");k.turnKeyAndGo(d,m&&m.callback?m.callback:function(){})},startQuiz:function(m){function d(a){var d=b(e+" .questions li").first();d.length&&d.fadeIn(500,function(){a&&a.callback&&a.callback()})}var l=k.getKey(1);var c=k.getKeyNotch;a.config.skipStartButton||0==q.length?d({callback:c(l,1)}):q.fadeOut(300,function(){d({callback:c(l,1)})});k.turnKeyAndGo(l,m&&m.callback?m.callback:
23
function(){});a.config.events&&a.config.events.onStartQuiz&&a.config.events.onStartQuiz.apply(null,[])},resetQuiz:function(m,d){var l=k.getKey(1);var c=k.getKeyNotch;B.fadeOut(300,function(){b(e+" input").prop("checked",!1).prop("disabled",!1);N.attr("class","quizLevel");b(e+" .question").removeClass("correctResponse").removeClass("incorrectResponse").remove("complete");b(e+" .answers li").removeClass("correct").removeClass("incorrect");b(e+" .question,"+e+" .responses,"+e+" .responses li,"+e+" "+
24
v+","+e+" .backToQuestion").hide();b(e+" .questionCount,"+e+" .answers,"+e+" .checkAnswer").show();E.append(b(e+" .questions")).show();c(l,1).apply(null,[]);a.method.startQuiz({callback:a.config.animationCallbacks.startQuiz},B)});k.turnKeyAndGo(l,d&&d.callback?d.callback:function(){})},checkAnswer:function(c,d){var l=k.getKey(2);var e=k.getKeyNotch;var f=b(b(c).parents(".question")[0]),g=f.find(".answers li"),m=g.find("input:checked"),n=parseInt(f.attr("id").replace(/(question)/,""),10),h=r[n].a,
25
n=r[n].select_any?r[n].select_any:!1;g.addClass("incorrect");var p=[];for(i in h)if(h.hasOwnProperty(i)){var t=h[i],u=parseInt(i,10);t.correct&&(p.push(u),g.eq(u).removeClass("incorrect").addClass("correct"))}var q=[];m.each(function(){var a=b(this).attr("id");q.push(parseInt(a.replace(/(.*\_question\d{1,}_)/,""),10))});if(a.config.preventUnanswered&&0===q.length)return alert(a.config.preventUnansweredText),!1;(g=a.method.compareAnswers(p,q,n))?f.addClass("correctResponse"):f.addClass("incorrectResponse");
26
f.find(g?".correct":".incorrect").show();a.config.perQuestionResponseMessaging?(b(c).hide(),a.config.perQuestionResponseAnswers?f.addClass("complete"):f.find(".answers").hide({duration:0,complete:function(){f.addClass("complete")}}),f.find("input").prop("disabled",!0),f.find(".responses").show(),f.find(v).fadeIn(300,e(l,1)).css("display","inline-block"),f.find(".backToQuestion").fadeIn(300,e(l,2)).css("display","inline-block"),f.find(".backToQuestion").length||e(l,2).apply(null,[])):(e(l,1).apply(null,
27
[]),e(l,2).apply(null,[]));k.turnKeyAndGo(l,d&&d.callback?d.callback:function(){})},nextQuestion:function(e,d){var c=k.getKey(1);var m=k.getKeyNotch;var f=b(b(e).parents(".question")[0]),g=f.next(".question"),h=f.find("input:checked");if(a.config.preventUnanswered&&0===h.length)return!1;g.length?f.fadeOut(300,function(){g.find(".backToQuestion").show().end().fadeIn(500,m(c,1));g.find(".backToQuestion").show().end().length||m(c,1).apply(null,[])}):(m(c,1).apply(null,[]),a.method.completeQuiz({callback:a.config.animationCallbacks.completeQuiz}));
28
k.turnKeyAndGo(c,d&&d.callback?d.callback:function(){})},backToQuestion:function(a,d){var c=k.getKey(2);var e=k.getKeyNotch;var f=b(b(a).parents(".question")[0]);if("block"===f.find(".responses").css("display"))f.find(".responses").fadeOut(300,function(){f.removeClass("correctResponse").removeClass("incorrectResponse").removeClass("complete");f.find(".responses, .responses li").hide();f.find(".answers").show();f.find(".answers li").removeClass("correct").removeClass("incorrect");f.find("input").prop("disabled",
29
!1);f.find(".answers").fadeIn(500,e(c,1));f.find(".checkAnswer").fadeIn(500,e(c,2));f.find(v).hide();"question0"!=f.attr("id")?f.find(".backToQuestion").show():f.find(".backToQuestion").hide()});else{var g=f.prev(".question");f.fadeOut(300,function(){g.removeClass("correctResponse").removeClass("incorrectResponse").removeClass("complete");g.find(".responses, .responses li").hide();g.find(".answers").show();g.find(".answers li").removeClass("correct").removeClass("incorrect");g.find("input").prop("disabled",
30
!1);g.find(v).hide();g.find(".checkAnswer").show();"question0"!=g.attr("id")?g.find(".backToQuestion").show():g.find(".backToQuestion").hide();g.fadeIn(500,e(c,1));e(c,2).apply(null,[])})}k.turnKeyAndGo(c,d&&d.callback?d.callback:function(){})},completeQuiz:function(c){var d=k.getKey(1);var m=k.getKeyNotch;var h=b(e+" .correctResponse").length,f=h;a.config.scoreAsPercentage&&(f=100*(h/t).toFixed(2)+"%");a.config.disableScore?b(D).remove():b(D+" span").html(a.config.scoreTemplateText.replace("%score",
31
f).replace("%total",t));if(a.config.disableRanking)b(A).remove();else{var g=[p.info.level1,p.info.level2,p.info.level3,p.info.level4,p.info.level5],f=a.method.calculateLevel(h),g=b.isNumeric(f)?g[f]:"";b(A+" span").html(g);b(A).addClass("level"+f)}E.fadeOut(300,function(){a.config.completionResponseMessaging?(b(e+" .button:not(.tryAgain), "+e+" .questionCount").hide(),b(e+" .question, "+e+" .answers, "+e+" .responses").show(),B.append(b(e+" .questions")).fadeIn(500,m(d,1))):B.fadeIn(500,m(d,1))});
32
k.turnKeyAndGo(d,c&&c.callback?c.callback:function(){});a.config.events&&a.config.events.onCompleteQuiz&&a.config.events.onCompleteQuiz.apply(null,[{questionCount:t,score:h}])},compareAnswers:function(a,d,c){return c?-1<b.inArray(d[0],a):0===b(a).not(d).length&&0===b(d).not(a).length},calculateLevel:function(b){b=(b/t).toFixed(2);var d=null;a.method.inRange(0,.2,b)?d=4:a.method.inRange(.21,.4,b)?d=3:a.method.inRange(.41,.6,b)?d=2:a.method.inRange(.61,.8,b)?d=1:a.method.inRange(.81,1,b)&&(d=0);return d},
33
inRange:function(a,b,c){return c>=a&&c<=b}};a.init=function(){a.method.setupQuiz.apply(null,[{callback:a.config.animationCallbacks.setupQuiz}]);q.on("click",function(c){c.preventDefault();this.disabled||b(this).hasClass("disabled")||a.method.startQuiz.apply(null,[{callback:a.config.animationCallbacks.startQuiz}])});b(e+" .tryAgain").on("click",function(b){b.preventDefault();a.method.resetQuiz(this,{callback:a.config.animationCallbacks.resetQuiz})});b(e+" .checkAnswer").on("click",function(b){b.preventDefault();
34
a.method.checkAnswer(this,{callback:a.config.animationCallbacks.checkAnswer})});b(e+" .backToQuestion").on("click",function(b){b.preventDefault();a.method.backToQuestion(this,{callback:a.config.animationCallbacks.backToQuestion})});b(e+" "+v).on("click",function(b){b.preventDefault();a.method.nextQuestion(this,{callback:a.config.animationCallbacks.nextQuestion})});var c=y.attr("id")+"-name";G.attr("id",c);y.attr({"aria-labelledby":c,"aria-live":"polite","aria-relevant":"additions",role:"form"});b(F+
35
', [href = "#"]').attr("role","button")};a.init()};b.fn.slickQuiz=function(C){return this.each(function(){if(void 0===b(this).data("slickQuiz")){var c=new b.slickQuiz(this,C);b(this).data("slickQuiz",c)}})}})(jQuery);
(2-2/2)