Also apply the __toString fix to UserInfo
[cacert-mgr.git] / external / ZendFramework-1.9.5 / externals / dojo / demos / flashCards / src / MathFlashCard.js
1 dojo.provide("demos.flashCards.src.MathFlashCard");
2
3 dojo.require("dijit._Widget");
4 dojo.require("dijit._Templated");
5
6 dojo.declare("dojofun.widget.MathFlashCard",
7 [dijit._Widget, dijit._Templated],
8 {
9 numberProblems: 20,
10 minInt: 0,
11 maxInt: 10,
12 timer: 15,
13 betweenTime: 5,
14 problemType: "addition",
15 currentProblem: 0,
16 correctAnswers: 0,
17 incorrectAnswers: 0,
18 problemSet: null,
19 started: false,
20 tooSlow: 0,
21
22 templatePath: dojo.moduleUrl("demos.flashCards","src/MathFlashCard.html"),
23
24 generateProblemSet: function(){
25 var problems=[];
26
27 if (this.problemType=="addition"){
28 for (var i=0; i<this.numberProblems; i++){
29 problems.push({
30 x: Math.floor(Math.random()*(this.maxInt+1)),
31 y: Math.floor(Math.random()*(this.maxInt+1)),
32 type: "addition"
33 });
34 }
35 }
36
37 return problems;
38 },
39
40 restart: function(){
41 this.problemSet = this.generateProblemSet();
42 dojo.forEach(["currentProblem","correctAnswers","incorrectAnswers","tooSlow"],
43 function(attr){
44 this[attr] = 0;
45 }, this)
46 this.renderProblem(0);
47 this.getStarted();
48 },
49
50 renderProblem: function(prob){
51 this.topR.innerHTML = this.problemSet[prob].x;
52 this.bottomR.innerHTML = this.problemSet[prob].y;
53 this.answerInput.value = "";
54 if (this.problemSet[prob].type == "addition"){
55 this.bottomL.innerHTML = "+";
56 }
57
58 if (this.started){
59 this.getStarted();
60 }
61
62 },
63
64 getStarted: function(){
65 dojo.style(this.domNode, "opacity", 100);
66 this.fadeOp = dojo.fadeOut({
67 node: this.domNode,
68 duration: (this.timer * 1000),
69 onEnd: dojo.hitch(this, "onTooSlow")
70 }).play();
71
72 if (!this.started){
73 this.started = true;
74 var func = dojo.hitch(dijit,"focus", this.answerInput);
75 this.connect(this.answerInput,"onblur", function(){
76 setTimeout(func, 10);
77 });
78 setTimeout(func, 10);
79 this.listenForKeyEvents();
80 }
81 },
82
83 listenForKeyEvents: function(){
84
85 this.onKeyDownEvent = this.connect(dojo.doc,"onkeydown", function(e){
86 var key = e.keyCode || e.charCode;
87 var k = dojo.keys;
88 switch(key){
89 case k.ENTER:
90 if(this.checkAnswer()){
91 this.onCorrect();
92 }else{
93 this.onIncorrect();
94 }
95 break;
96 }
97 });
98 },
99
100 stopListening: function(){
101 this.disconnect(this.onKeyDownEvent);
102 delete this.onKeyDownEvent;
103 },
104
105 checkAnswer: function(){
106 var prob = this.problemSet[this.currentProblem];
107
108 if(prob.type=="addition"){
109 var correct = prob.x + prob.y;
110 }
111 console.log(this.answerInput.value,"value");
112 if (correct && this.answerInput.value==correct){
113 return true;
114 }
115 },
116
117 onCorrect: function(){
118 this.correctAnswers++;
119 this.fadeOp.stop();
120 delete this.fadeOp;
121 if ((this.currentProblem+1)<this.problemSet.length){
122 this.renderProblem(++this.currentProblem);
123 } else {
124 this.onEnd(this.correctAnswers,this.problemSet.length,this.tooSlow);
125 }
126
127 },
128
129 onIncorrect: function(){
130 this.incorrectAnswers++;
131 this.fadeOp.stop();
132 delete this.fadeOp;
133 if ((this.currentProblem+1)<this.problemSet.length){
134 this.renderProblem(++this.currentProblem);
135 } else {
136 this.onEnd(this.correctAnswers,this.problemSet.length,this.tooSlow);
137 }
138 },
139
140 onTooSlow: function(){
141 this.tooSlow++;
142 if ((this.currentProblem+1)<this.problemSet.length){
143 this.renderProblem(++this.currentProblem);
144 } else {
145 this.onEnd(this.correctAnswers,this.problemSet.length,this.tooSlow);
146 }
147 },
148
149 onEnd: function(correct, set, tooSlow){
150 this.stopListening();
151 this.started=false;
152 },
153
154 postCreate:function(){
155 this.problemSet = this.generateProblemSet();
156 this.renderProblem(0);
157 }
158 }
159 );