1 /** 2 * @fileOverview a scoreboard widget 3 * @author Bryan Berry <bryan@olenepal.org> 4 * uses MIT License 5 */ 6 7 8 9 (function($){ 10 11 // This is a dummy function, just here as placeholder to 12 // to make the jsdoc tool happy 13 /** @name $.ui.kFooter 14 * @namespace KFooter widget 15 * @example Emits the event kFooterWinGame when the maxScore is reached <br /> 16 * Emits the event kFooterRestart when game restarted <br /> 17 * Start button emits kFooterStart event when clicked <br /> 18 * Restart button emits kFooterRestart event when clicked <br /> 19 * Pause button emits the kFooterPause event when clicked <br /> 20 */ 21 $.ui.kFooter = function(){}; 22 23 $.widget('ui.kFooter', 24 /** @lends $.ui.kFooter.prototype */ 25 { 26 /** Gets the current score 27 * @returns {Number} current score 28 */ 29 getScore : function(){ 30 return this._getData('score'); 31 }, 32 /** Sets the current score 33 * @param {Number} newScore new score 34 */ 35 setScore : function(newScore){ 36 this._setData('score', parseInt(newScore)); 37 this._refresh(); 38 }, 39 /** Gets the current total 40 * @returns {Number} current total 41 */ 42 getTotal : function(){ 43 return this._getData('total'); 44 }, 45 /** Sets the current total 46 * @param {Number} newTotal new total 47 */ 48 setTotal : function(newTotal){ 49 this._setData('total', parseInt(newTotal)); 50 this._refresh(); 51 }, 52 /** 53 * Resets the score and total to initial values and triggers 54 * the "kFooterRestart" event 55 */ 56 restart : function(){ 57 this.element.trigger('kFooterRestart'); 58 this._setData('score', this._getData('initialScore')); 59 this._setData('total', this._getData('initialTotal')); 60 this._refresh(); 61 }, 62 /** Increments the score by 1 or by the supplied numeric argument 63 * @param {Number} [val] increment value 64 */ 65 inc : function(val){ 66 var incVal = parseInt(val) || 1; 67 this._setData('score', this._getData('score') + incVal); 68 this._refresh(); 69 if(this._getData('winScore') === this._getData('score')){ 70 this.element.trigger('kFooterWinGame'); 71 } 72 }, 73 /** Increments the total by 1 or by the supplied numeric argument 74 * @param {Number} [val] increment value 75 */ 76 incTotal : function(val){ 77 var incVal = parseInt(val) || 1; 78 this._setData('total', this._getData('total') + incVal); 79 this._refresh(); 80 }, 81 /** Decrements the score by 1 or by the supplied numeric argument 82 * @param {Number} [val] decrement value 83 */ 84 dec : function(val){ 85 var decVal = parseInt(val) || 1; 86 this._setData('score', this._getData('score') - decVal); 87 this._refresh(); 88 }, 89 /** Decrements the total by 1 or by the supplied numeric argument 90 * @param {Number} [val] decrement value 91 */ 92 decTotal : function(val){ 93 var decVal = parseInt(val) || 1; 94 this._setData('total', this._getData('total') - decVal); 95 this._refresh(); 96 }, 97 _ : function(val, loc){ 98 var self = this; 99 var locale = self._getData('locale') || loc; 100 var convertNumLocale = function(num){ 101 //48 is the base for western numerals 102 var convertDigit = function(digit){ 103 104 var numBase = 48; 105 var prefix = "u00"; 106 107 if (self._getData('locale') === "ne"){ 108 prefix = "u0"; 109 numBase = 2406; 110 } 111 112 return '\\' + prefix + 113 (numBase + parseInt(digit)).toString(16); 114 }; 115 116 var charArray = num.toString().split("").map(convertDigit); 117 return eval('"' + charArray.join('') + '"'); 118 }; 119 120 var convertStringLocale = function (str){ 121 if (self._getData('locale') === "ne"){ 122 switch(str){ 123 case "Score": 124 return "अङ्क"; 125 case "Total": 126 return "जम्मा"; 127 case "Play Again": 128 return "फेरी खेलौ"; 129 case "Pause": 130 return "खेल रोकौ"; 131 case "Start": 132 return "सुरु गरौ"; 133 default: 134 return "string not translated"; 135 } 136 } 137 return "String really not translated"; 138 }; 139 140 141 142 if (typeof val === "number"){ 143 return convertNumLocale(val); 144 } 145 146 if (locale !== "en"){ 147 return convertStringLocale(val); 148 }else { 149 return val; 150 } 151 152 153 }, 154 _init : function(){ 155 156 var divDisplay = "inline"; 157 var score = this.options.score; 158 var total = this.options.total; 159 var self = this; 160 161 var options = $.extend({}, $.ui.kFooter.defaults, this.options); 162 163 this._setData('initialScore', parseInt(options.score)); 164 this._setData('initialTotal', parseInt(options.total)); 165 this._setData('score', parseInt(options.score)); 166 this._setData('total', parseInt(options.total)); 167 this._setData('winScore', parseInt(options.winningScore)); 168 this._setData('locale', options.locale); 169 170 171 this.element.addClass('ui-widget ui-widget-content ' + 172 ' ui-kFooter-container'); 173 174 var $parent = $('<div>') 175 .addClass('ui-kFooter-spacing'); 176 177 this._scoreText = $('<div><span>' + this._("Score") + 178 "</span></div>") 179 .addClass('ui-kFooter-spacing ui-kFooter-left ui-kFooter-text') 180 .appendTo($parent); 181 182 this._score = $("<div><span>" + this._(score) + "</span></div>") 183 .addClass('ui-kFooter-spacing ui-kFooter-left' + 184 ' ui-kFooter-text ui-kFooter-number') 185 .find('span') 186 .addClass('ui-corner-all') 187 .end() 188 .appendTo($parent) 189 .find('span:first'); 190 191 192 $("<div><span>" + this._("Total") + "</span></div>") 193 .addClass('ui-kFooter-spacing ui-kFooter-left' + 194 ' ui-corner-all ui-kFooter-text') 195 .appendTo($parent); 196 197 this._total = $("<div><span>" + this._(total) + "</span></div>") 198 .addClass('ui-kFooter-spacing ui-kFooter-left' + 199 ' ui-kFooter-text ui-kFooter-number') 200 .find('span') 201 .addClass('ui-corner-all') 202 .end() 203 .appendTo($parent) 204 .find('span:first'); 205 206 207 var $templateBtn = $('<button></button>') 208 .addClass('ui-kFooter-spacing ui-corner-all ' + 209 ' ui-state-default ui-kFooter-right ui-kFooter-button') 210 .append( 211 $('<span></span>') 212 .addClass('ui-icon ui-kFooter-icon') 213 ) 214 .append( 215 $('<span></span>') 216 .addClass('ui-kFooter-button-text') 217 ); 218 219 if(options.restartButton){ 220 var $restartBtn = $templateBtn.clone() 221 .find('span:first') 222 .addClass('ui-icon-arrowrefresh-1-w') 223 .end() 224 .find('span:last') 225 .text(this._('Play Again')) 226 .end() 227 .click(function(){ self.restart();}) 228 .appendTo($parent); 229 } 230 231 232 if(options.pauseButton){ 233 234 var $pauseBtn = $templateBtn.clone() 235 .find('span:first') // 236 .removeClass('ui-icon-arrowrefresh-1-w') 237 .addClass('ui-icon-pause') 238 .end() 239 .find('span:last') 240 .text(this._('Pause')) 241 .end() 242 .click(function(){ 243 self.element.trigger('kFooterPause'); 244 }) 245 .appendTo($parent); 246 } 247 248 if(options.startButton){ 249 var $startBtn = $templateBtn.clone() 250 .find('span:first') 251 .addClass('ui-icon-play') 252 .end() 253 .find('span:last') 254 .text(this._('Start')) 255 .end() 256 .click(function(){ 257 self.element.trigger('kFooterStart'); 258 }) 259 .appendTo($parent); 260 } 261 262 263 $parent.find('button').hover( 264 function(){ 265 $(this).addClass("ui-state-hover"); 266 }, 267 function(){ 268 $(this).removeClass("ui-state-hover"); 269 }); 270 271 this.element.append($parent); 272 273 }, 274 _refresh : function(){ 275 this._score.text(this._(this._getData('score'))); 276 this._total.text(this._(this._getData('total'))); 277 }, 278 /** Removes the kFooter widget and all related data from the DOM */ 279 destroy : function(){ 280 this.element.remove(); 281 $.widget.prototype.destroy.apply(this, arguments); 282 } 283 284 285 }); 286 287 $.ui.kFooter.getter = ['getScore', 'getTotal', '_convertNumLocale']; 288 289 /** Default settings for the kFooter widget 290 * @namespace Default settings for the kFooter widget 291 * @extends $.ui.kFooter 292 */ 293 $.ui.kFooter.defaults = { 294 /** Initial score 295 * @type Number 296 * @default 0 297 */ 298 score: 0, 299 /** Initial total 300 * @type Number 301 * @default 0 302 */ 303 total: 0, 304 /** The score that will win the game 305 * @type Number 306 * @default 0 307 */ 308 winningScore: 0, 309 /** Default locale, valid options are "en" and "ne" 310 * @type String 311 * @default "en" 312 */ 313 locale: "ne", 314 /** Display the Start Button 315 * @type boolean 316 * @default false 317 */ 318 startButton: true, 319 /** Display the Retart Button 320 * @type boolean 321 * @default true 322 */ 323 restartButton: true, 324 /** Display the Pause Button 325 * @type boolean 326 * @default false 327 */ 328 pauseButton: true 329 }; 330 331 })(jQuery);