-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpercircle.js
More file actions
210 lines (176 loc) · 8.55 KB
/
Copy pathpercircle.js
File metadata and controls
210 lines (176 loc) · 8.55 KB
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
(function (factory) {
"use strict";
if (typeof define === 'function' && define.amd) { // AMD
define(['jquery'], factory);
}
else if (typeof exports == "object" && typeof module == "object") { // CommonJS
module.exports = factory(require('jquery'));
}
else { // Browser
factory(jQuery);
}
}) (function($, undefined) {
"use strict";
$.fn.percircle = function(options) {
// default options
var defaultOptions = {
animate: true
};
// extend with any provided options
if (!options) options = {};
$.extend(options, defaultOptions);
var rotationMultiplier = 3.6;
// for each element matching selector
return this.each(function(){
/*
If it is about dynamic value update, ensure the filling of the bar will start from 0 degrees.
Without this line, if it as about a dynamic update from a value > 50 to a value < 50, the bar
filling will start from 180 degrees.
However, this fix is not the best one, UX-wise, since user loses the gradual degredation of the bar.
The way this should be best implemented is to perform a smooth animation from > 50deg back to the
requested value, instead of first moving to 0deg and then "drawing" again.
*/
if ($(this).hasClass('gt50')) $(this).removeClass('gt50');
var percircle = $(this);
var progressBarColor = '';
// When user tries adding a custom progress bar color, the text color should be updated as well.
var changeTextColor = function (context, color) {
// Change color text the same with progress bar color
percircle.on('mouseover', function(){
context.children('span').css('color', color);
});
percircle.on('mouseleave', function(){
context.children('span').attr('style', '');
});
};
// add percircle class for styling
if (!percircle.hasClass('percircle')) percircle.addClass('percircle');
// apply options
if (typeof(percircle.attr('data-animate')) !== 'undefined') options.animate = percircle.attr('data-animate') == 'true';
if (options.animate) percircle.addClass('animate');
if (typeof(percircle.attr('data-progressBarColor')) !== 'undefined') {
options.progressBarColor = percircle.attr('data-progressBarColor');
progressBarColor = "style='border-color: "+ options.progressBarColor +"'";
changeTextColor($(this), options.progressBarColor);
} else {
if (typeof options.progressBarColor !== 'undefined'){
progressBarColor = "style='border-color: "+ options.progressBarColor +"'";
changeTextColor($(this), options.progressBarColor);
}
}
var percent = percircle.attr('data-percent') || options.percent || 0;
var perclock = percircle.attr('data-perclock') || options.perclock || 0;
var perdown = percircle.attr('data-perdown') || options.perdown || 0;
if (percent) {
if (percent > 50) percircle.addClass('gt50');
var text = percircle.attr('data-text') || options.text || percent + '%';
percircle.html('<span>'+text+'</span>');
// add divs for structure
$('<div class="slice"><div class="bar" '+progressBarColor+'></div><div class="fill" '+progressBarColor+'></div></div>').appendTo(percircle);
if (percent > 50)
$('.bar', percircle).css({
'-webkit-transform' : 'rotate(180deg)',
'-moz-transform' : 'rotate(180deg)',
'-ms-transform' : 'rotate(180deg)',
'-o-transform' : 'rotate(180deg)',
'transform' : 'rotate(180deg)'
});
var rotationDegrees = rotationMultiplier * percent;
// set timeout causes the animation to be visible on load
setTimeout(function(){
$('.bar', percircle).css({
'-webkit-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-moz-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-ms-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-o-transform' : 'rotate(' + rotationDegrees + 'deg)',
'transform' : 'rotate(' + rotationDegrees + 'deg)'
});
}, 0);
} else if(perclock){
if (!percircle.hasClass('perclock')) percircle.addClass('perclock');
setInterval(function(){
var d = new Date(); // without params it defaults to "now"
var text = getPadded(d.getHours()) + ":" + getPadded(d.getMinutes()) + ":" + getPadded(d.getSeconds());
percircle.html('<span>'+text+'</span>');
// add divs for structure
$('<div class="slice"><div class="bar" '+progressBarColor+'></div><div class="fill" '+progressBarColor+'></div></div>').appendTo(percircle);
var seconds = d.getSeconds();
if (seconds === 0) percircle.removeClass('gt50');
if (seconds > 30){
percircle.addClass('gt50');
$('.bar', percircle).css({
'-webkit-transform' : 'rotate(180deg);scale(1,3)',
'-moz-transform' : 'rotate(180deg);scale(1,3)',
'-ms-transform' : 'rotate(180deg);scale(1,3)',
'-o-transform' : 'rotate(180deg);scale(1,3)',
'transform' : 'rotate(180deg);scale(1,3)'
});
}
var rotationDegrees = 6 * seconds; // temporary clockwise rotation value
$('.bar', percircle).css({
'-webkit-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-moz-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-ms-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-o-transform' : 'rotate(' + rotationDegrees + 'deg)',
'transform' : 'rotate(' + rotationDegrees + 'deg)'
});
}, 1000);
} else if(perdown) {
getCountdown(percircle, options, progressBarColor);
}
});
};
// move to another file - functions
var getCountdown = function(percircle, options, progressBarColor) {
var secs = percircle.attr('data-secs') || options.secs;
var timeUpText = percircle.attr('data-timeUpText') || options.timeUpText;
var reset = percircle[0].hasAttribute('data-reset') || options.reset;
if (timeUpText.length > 8) timeUpText='the end';
var counter;
if (reset) {
percircle.on("click", timerReset);
}
function timer() {
secs-=1;
if (secs > 30) percircle.addClass('gt50');
if (secs < 30) percircle.removeClass('gt50');
timerUpdate();
if (secs <= 0) {
timerStop();
percircle.html('<span>'+timeUpText+'</span>');
return;
}
}
function timerStart() {
counter = setInterval(timer, 1000);
}
function timerStop() {
clearInterval(counter);
}
function timerReset() {
timerStop();
secs = options.secs;
timerUpdate();
timerStart();
}
function timerUpdate() {
percircle.html('<span>'+secs+'</span>');
// add divs for structure
$('<div class="slice"><div class="bar" '+progressBarColor+'></div><div class="fill" '+progressBarColor+'></div></div>').appendTo(percircle);
var rotationDegrees = 6 * secs; // temporary clockwise rotation value
$('.bar', percircle).css({
'-webkit-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-moz-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-ms-transform' : 'rotate(' + rotationDegrees + 'deg)',
'-o-transform' : 'rotate(' + rotationDegrees + 'deg)',
'transform' : 'rotate(' + rotationDegrees + 'deg)'
});
}
// Initialize timer
timerStart();
};
// display a presentable format of current time
var getPadded = function(val){
return val < 10 ? ('0' + val) : val;
};
});