forked from chikamichi/jquery.pietimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.pietimer.js
111 lines (104 loc) · 3.26 KB
/
jquery.pietimer.js
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
/**
* jQuery PieTimer Plugin v0.1
* Authors: Kus (http://blakek.us/css3-pie-graph-timer-with-jquery/)
* Connor Doyle (jQuery plugin)
*
* http://github.com/chikamichi/jquery.pietimer
*
* Licensed under the MIT licenses:
* http://www.opensource.org/licenses/mit-license.php
*/
(function($){
var methods = {
init: function(options) {
var state = {
timer: null,
timerSeconds: 10,
callback: function() {},
timerCurrent: 0,
showPercentage: false,
fill: false,
color: '#CCC'
};
state = $.extend(state, options);
return this.each(function() {
var $this = $(this);
var data = $this.data('pietimer');
if (!data) {
$this.addClass('pietimer');
$this.css({fontSize: $this.width()});
$this.data('pietimer', state);
if (state.showPercentage) {
$this.find('.percent').show();
}
if (state.fill) {
$this.addClass('fill');
}
$this.pietimer('start');
}
});
},
stopWatch: function() {
var data = $(this).data('pietimer');
if (data) {
var seconds = (data.timerFinish-(new Date().getTime()))/1000;
if (seconds <= 0) {
clearInterval(data.timer);
$(this).pietimer('drawTimer', 100);
data.callback();
} else {
var percent = 100-((seconds/(data.timerSeconds))*100);
$(this).pietimer('drawTimer', percent);
}
}
},
drawTimer: function(percent) {
$this = $(this);
var data = $this.data('pietimer');
if (data) {
$this.html('<div class="percent"></div><div class="slice'+(percent > 50?' gt50"':'"')+'><div class="pie"></div>'+(percent > 50?'<div class="pie fill"></div>':'')+'</div>');
var deg = 360/100*percent;
$this.find('.slice .pie').css({
'-moz-transform':'rotate('+deg+'deg)',
'-webkit-transform':'rotate('+deg+'deg)',
'-o-transform':'rotate('+deg+'deg)',
'transform':'rotate('+deg+'deg)'
});
$this.find('.percent').html(Math.round(percent)+'%');
if (data.showPercentage) {
$this.find('.percent').show();
}
if ($this.hasClass('fill')) {
$this.find('.slice .pie').css({backgroundColor: data.color});
}
else {
$this.find('.slice .pie').css({borderColor: data.color});
}
}
},
start: function() {
var data = $(this).data('pietimer');
if (data) {
data.timerFinish = new Date().getTime()+(data.timerSeconds*1000);
$(this).pietimer('drawTimer', 0);
data.timer = setInterval("$this.pietimer('stopWatch')", 50);
}
},
reset: function() {
var data = $(this).data('pietimer');
if (data) {
clearInterval(data.timer);
$(this).pietimer('drawTimer', 0);
}
}
};
$.fn.pietimer = function(method) {
if (methods[method]) {
return methods[method].apply( this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.pietimer');
}
};
})(jQuery);