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
|
(function($){
$.widget('ui.scoreboard',
{
getScore : function(){
return this._getData('score');
},
setScore : function(newScore){
this._setData('score', parseInt(newScore));
this._refresh();
},
getTotal : function(){
return this._getData('total');
},
setTotal : function(newTotal){
this._setData('total', parseInt(newTotal));
this._refresh();
},
reset : function(){
this._setData('score', this._getData('initialScore'));
this._setData('total', this._getData('initialTotal'));
this._refresh();
},
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('winGame');
}
},
incTotal : function(val){
var incVal = parseInt(val) || 1;
this._setData('total', this._getData('total') + incVal);
this._refresh();
},
dec : function(val){
var decVal = parseInt(val) || 1;
this._setData('score', this._getData('score') - decVal);
this._refresh();
},
decTotal : function(val){
var decVal = parseInt(val) || 1;
this._setData('total', this._getData('total') - decVal);
this._refresh();
},
_init : function(){
var divDisplay = "inline";
var score = this.options.score;
var total = this.options.total;
var layoutId = "h";
var self = this;
this._setData('initialScore', parseInt(this.options.score));
this._setData('initialTotal', parseInt(this.options.total));
this._setData('score', parseInt(this.options.score));
this._setData('total', parseInt(this.options.total));
this._setData('winScore', parseInt(this.options.winningScore) || 0);
if(this.options.layout === "vertical"){
layoutId = "v";
}
this.evWinGame = document.createEvent('Events');
this.element.addClass('ui-scoreboard-container-' + layoutId +
' ui-widget ui-widget-content ui-corner-all');
var clone = $('<div>')
.addClass('ui-scoreboard-spacing-' + layoutId);
this._scoreText = $("<div>Score</div>")
.addClass('ui-scoreboard-spacing-'+ layoutId +
' ui-corner-all ui-scoreboard-text')
.appendTo(this.element);
this._score = $("<div>" + score + "</div>")
.addClass('ui-scoreboard-spacing-' + layoutId +
' ui-scoreboard-number-' + layoutId)
.appendTo(this.element);
$("<div>Total</div>")
.addClass('ui-scoreboard-spacing-' + layoutId +
' ui-corner-all ' +
'ui-scoreboard-text')
.appendTo(this.element);
this._total = $("<div>" + total + "</div>")
.addClass('ui-scoreboard-spacing-' + layoutId +
' ui-scoreboard-number-' + layoutId)
.appendTo(this.element);
},
_refresh : function(){
this._score.text(this._getData('score'));
this._total.text(this._getData('total'));
},
destroy : function(){
this.element.remove();
$.widget.prototype.destroy.apply(this, arguments);
}
});
$.ui.scoreboard.getter = ['getScore', 'getTotal'];
$.ui.scoreboard.defaults = {
score: 0, total: 0, layout: "horizontal", winningScore: 0
};
})(jQuery);
|