diff options
Diffstat (limited to 'js/ui.kHeader.js~')
-rwxr-xr-x | js/ui.kHeader.js~ | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/js/ui.kHeader.js~ b/js/ui.kHeader.js~ new file mode 100755 index 0000000..e915764 --- /dev/null +++ b/js/ui.kHeader.js~ @@ -0,0 +1,310 @@ +/** +* @fileOverview a scoreboard 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(); + }, + _ : function(val, loc){ + var self = this; + var locale = self._getData('locale') || loc; + var convertNumLocale = function(num){ + //48 is the base for western numerals + var convertDigit = function(digit){ + + var numBase = 48; + var prefix = "u00"; + + if (self._getData('locale') === "ne"){ + prefix = "u0"; + numBase = 2406; + } + + return '\\' + prefix + + (numBase + parseInt(digit)).toString(16); + }; + + var charArray = num.toString().split("").map(convertDigit); + return eval('"' + charArray.join('') + '"'); + }; + + var convertStringLocale = function (str){ + if (self._getData('locale') === "ne"){ + switch(str){ + case "Score": + return "अङ्क"; + case "Total": + return "जम्मा"; + case "Play Again": + return "फेरी खेलौ"; + case "Pause": + return "खेल रोकौ"; + case "Start": + return "सुरु गरौ"; + default: + return "string not translated"; + } + } + return "String really not translated"; + }; + + + + if (typeof val === "number"){ + return convertNumLocale(val); + } + + if (locale !== "en"){ + return convertStringLocale(val); + }else { + return val; + } + + + }, + _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-container'); + + 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._(score) + "</span></li>" + + "<li class='left'>" + this._("Total") + "</li>" + + "<li class='left'><span id='kFooterTotal' " + + "class='ui-corner-all number'>" + + this._(total) + "</span></li>"); + + this._score = $('#kFooterScore', $scoreboard); + this._total = $('#kFooterTotal', $scoreboard); + + $kFooter.append($scoreboard); + } + + //need timer stuff + //if options.timer === true + + //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.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.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.element.trigger('kFooterStart'); + }) + .appendTo($kFooter); + } + + $('button', $kFooter).hover( + function(){ + $(this).addClass("ui-state-hover"); + }, + function(){ + $(this).removeClass("ui-state-hover"); + }); + + + this.element.append($kFooter); + + }, + _refresh : function(){ + this._score.text(this._(this._getData('score'))); + this._total.text(this._(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', '_convertNumLocale']; + + /** 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: true, + /** Display the Retart Button + * @type boolean + * @default true + */ + restartButton: true, + /** Display the Pause Button + * @type boolean + * @default false + */ + pauseButton: true + }; + + })(jQuery);
\ No newline at end of file |