Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/js/ui.kFooter.js
blob: 02d7937cad19df58d61d4852f27130387379d8c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* @fileOverview a footer widget 
* @author Bryan Berry <bryan@olenepal.org> 
*  uses MIT License
*/



(function($){

     // This is a dummy function, just here as placeholder to
     // to make the jsdoc tool happy
     /** @name $.ui.kFooter
      * @namespace kFooter widget 
      * @example Emits the event kFooterWinGame when the maxScore is reached <br />
      * Emits the event kFooterRestart when game restarted <br />
      * Start button emits kFooterStart event when clicked <br />
      * Restart button emits kFooterRestart event when clicked <br />
      * Pause button emits the kFooterPause event when clicked <br />
      */
     $.ui.kFooter = function(){};

     $.widget('ui.kFooter',
	      /** @lends $.ui.kFooter.prototype */
	      {
		  /** Gets the current score
		   * @returns {Number} current score
		   */
		  getScore : function(){
		      return this._getData('score');
		  },
		   /** Sets the current score
		   * @param {Number} newScore new score
		   */
		  setScore : function(newScore){
		      this._setData('score', parseInt(newScore));
		      this._refresh();
		  },
		  /** Gets the current total
		   * @returns {Number} current total
		   */
		  getTotal : function(){
		      return this._getData('total');
		  },
		  /** Sets the current total
		   * @param {Number} newTotal new total
		   */
		  setTotal : function(newTotal){
		      this._setData('total', parseInt(newTotal));
		      this._refresh();  
		  },
		  /**
		   * Resets the score and total to initial values and triggers 
		   * the "kFooterRestart" event
		   */
		  restart : function(){
		      this.element.trigger('kFooterRestart');
		      this._setData('score', this._getData('initialScore'));
		      this._setData('total', this._getData('initialTotal'));
		      this._refresh();
		  },
		  /** Increments the score by 1 or by the supplied numeric argument
		   * @param {Number} [val] increment value
		   */
		  inc : function(val){
		      var incVal = parseInt(val) || 1;
		      this._setData('score',  this._getData('score') + incVal);
		      this._refresh();
		      if(this._getData('winScore') === this._getData('score')){
			  this.element.trigger('kFooterWinGame');
		      }
		  },
		   /** Increments the total by 1 or by the supplied numeric argument
		   * @param {Number} [val] increment value
		   */
		  incTotal : function(val){
		      var incVal = parseInt(val) || 1;
		      this._setData('total',  this._getData('total') + incVal);
		      this._refresh();
		  },
		   /** Decrements the score by 1 or by the supplied numeric argument
		   * @param {Number} [val] decrement value
		   */
		  dec : function(val){
		      var decVal = parseInt(val) || 1;
		      this._setData('score',  this._getData('score') - decVal);
		      this._refresh();
		  },
		   /** Decrements the total by 1 or by the supplied numeric argument
		   * @param {Number} [val] decrement value
		   */
		  decTotal : function(val){
		      var decVal = parseInt(val) || 1;
		      this._setData('total',  this._getData('total') - decVal);
		      this._refresh();
		  },
		  /** Start the timer, defaults to 0:00 if no arguments supplied
		   * @param {Number} [minutes] value for minutes, default to 0
		   * @param {Number} [seconds]  value for seconds, default to 0 
		   */ 
		  startTimer : function(minutes, seconds){
		      var timerRunning = this._getData('timerRunning')|| false;

		      if (this._$timer && timerRunning === false){
			  var mins = minutes || 0;
			  var secs = seconds || 0;
			  var timerId = null;
			  var self = this;
		      
		      
			  this._setData('mins', mins);
			  this._setData('secs', secs);

			  var addLeadingZero = function(num){
			      if(''.concat(num).length === 1){
				  return "0".concat(num);
			      } else {
				  return num;
			      }
			    
			  };

			  var increaseTimer = function(){
			      if (self._getData('timerRunning') === false){
				  return;
			      }

			      var s = self._getData('secs') + 1;
			      var m = null;
			      var timerId = null;
					  
			      if (s < 60) {
				  self._setData('secs', s);
				  self._$timerSecs.text(self._n(addLeadingZero(s)));
			      } else {
				  s = 0;
				  m = self._getData('mins') + 1;
				  self._$timerSecs.text(self._n(addLeadingZero(s)));
				  self._$timerMins.text(self._n(addLeadingZero(m)));
				  self._setData('secs', s);
				  self._setData('mins', m);
			      }
				      
			      timerId = setTimeout(increaseTimer, 1000);
			      self._setData('timerId', timerId);

			  };
			  
			  timerId = setTimeout(increaseTimer , 1000);

			  this._setData('timerRunning', true);
			  this._setData('timerId', timerId);
		      }
		  },
		  /** Stop the timer
		   */
		  stopTimer : function(){
		      this._setData('timerRunning', false);
		  },
		  _ : function(val, loc){
		      return $.i18n.call($.ui.kFooter, val, loc);
		  },
		  _n : function(val, loc){
		      return $._n(val, loc);
		  },
		  _init : function(){

		      var divDisplay = "inline";
		      var score = this.options.score;
		      var total = this.options.total;
		      var self = this;
		      
		      var options = $.extend({}, $.ui.kFooter.defaults, this.options);

		      this._setData('initialScore', parseInt(options.score));
		      this._setData('initialTotal', parseInt(options.total));
		      this._setData('score', parseInt(options.score));
		      this._setData('total', parseInt(options.total)); 
		      this._setData('winScore', parseInt(options.winningScore)); 
		      this._setData('locale', options.locale);

		     
		      this.element.addClass('ui-widget ui-widget-content ' +
			      ' ui-kFooter');
		      
		      
		      var $kFooter = $("<ul></ul>");
		     
		      
		      if(options.scoreboard === true){
			  
			  var $scoreboard = $("<li class='left'>" + this._("Score") + 
			      "</li>" + "<li class='left'>" +
			      "<span id='kFooterScore' class='ui-corner-all number'>" + 
			      this._n(score) + "</span></li>" +
			      "<li class='left'>" + this._("Total") + "</li>" +
			      "<li class='left'><span id='kFooterTotal' " +
			      "class='ui-corner-all number'>" + 
			  this._n(total) + "</span></li>")
			      .appendTo($kFooter);
			 
			  this._score = $('#kFooterScore', $scoreboard);
			  this._total = $('#kFooterTotal', $scoreboard);

		      }

		      if(options.timer === true){
			  this._$timer = $("<li class='left'>" + this._("Timer") + 
			      "</li>" +
			      "<li class='left'><span id='kFooterMins'" + 
			      "class='ui-corner-all" +
			      " number timer'>" + this._n("00") + 
			      "</span></li>" +
			      "<li class='left'><span id='kFooterSecs'" + 
			      "class='ui-corner-all " +
			      "number timer'>"+ this._n("00") + 
			      "</span></li>")
			      .appendTo($kFooter);

			  this._$timerMins = $('#kFooterMins', this._$timer);
			  this._$timerSecs = $('#kFooterSecs', this._$timer);		     
		      }

		      //if options.checkAnswerBtn === true
			 
		      if (options.restartButton === true){
			  var $restartButton = $("<li class='right'><button " +
			      "class='ui-corner-all ui-state-default'>" +
 			      "<span class='ui-icon ui-icon-arrowrefresh-1-w'>" +
			      "</span>" + 
			      "<span class='text left'>" + this._('Play Again') + 
			      "</span></button></li>")
			      .click(function(){ 
					 self.startTimer();
					 self.restart();
				     })
			      .appendTo($kFooter);
		      }
		      
		       if (options.pauseButton === true){
			  var $pauseButton = $("<li class='right'><button " +
			      "class='ui-corner-all ui-state-default'>" +
 			      "<span class='ui-icon ui-icon-pause'>" +
			      "</span>" + 
			      "<span class='text left'>" + this._('Pause') + 
			      "</span></button></li>")
			      .click(function(){ 
					 self.stopTimer();
					 self.element.trigger('kFooterPause'); 
				     })
			      .appendTo($kFooter);
		      }

		      if (options.startButton === true){
			  var $startButton = $("<li class='right'><button " +
			      "class='ui-corner-all ui-state-default'>" +
 			      "<span class='ui-icon ui-icon-play'>" +
			      "</span>" + 
			      "<span class='text left'>" + this._('Start') + 
			      "</span></button></li>")
			      .click(function(){ 
				      self.startTimer();
				      self.element.trigger('kFooterStart'); 
				     })
			      .appendTo($kFooter);
		      }
		  
		      $('button', $kFooter).hover(
			      function(){ 
				  $(this).addClass("ui-state-hover"); 
			      },
			      function(){ 
				  $(this).removeClass("ui-state-hover"); 
			      });
		      
		      
		      // Check if any html w/in this.element, if so wrap it in <li> </li>
		      // and add to $kFooter later
		      var $userHtml = this.element
			  .children()
			  .appendTo($kFooter);
			  

		      $userHtml.wrap('<li class="left"></li>');

		      //get rid of userHtml
		      this.element.empty();
		      
		      this.element.append($kFooter);
		      
		  },
		  _refresh : function(){
		      this._score.text(this._n(this._getData('score')));
		      this._total.text(this._n(this._getData('total')));
		  },
		  /** Removes the kFooter widget and all related data from the DOM */
		  destroy : function(){
		      this.element.remove();
		      $.widget.prototype.destroy.apply(this, arguments);
		  }

		  
	      });

	      $.ui.kFooter.getter = ['getScore', 'getTotal', '_n', '_' ];

	      $.ui.kFooter.i18n = {};

		
		/** Default settings for the kFooter widget
		 * @namespace Default settings for the kFooter widget
		 * @extends $.ui.kFooter
		 */			   
	      $.ui.kFooter.defaults = {
                  /** Initial score
		   * @type Number 
		   * @default 0
		   */
		  score: 0, 
		  /** Initial total
		   * @type Number
		   * @default 0
		   */
		  total: 0, 
		  /** The score that will win the game
		   * @type Number
		   * @default 0
		   */
		  winningScore: 0,
		  /** Default locale, valid options are "en" and "ne" 
		   * @type String
		   * @default "en"
		   */
		  locale: "ne",
		  /** Display the scoreboard
		   * @type boolean
		   * @default true
		   */
		  scoreboard: true,
		  /** Display the Start Button
		   * @type boolean
		   * @default false
		   */
		  startButton: false,
		  /** Display the Retart Button
		   * @type boolean
		   * @default true
		   */
		  restartButton: true,
		  /** Display the Pause Button
		   * @type boolean
		   * @default false
		   */
		  pauseButton: false,
		  /** Display the timer
		   * @type boolean
		   * @default false
		   */
		  timer: false
	      };

 })(jQuery);