-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle-bar.js
More file actions
585 lines (450 loc) · 15.6 KB
/
Copy pathcircle-bar.js
File metadata and controls
585 lines (450 loc) · 15.6 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
function CircleBar() {
var circleBarDOM = undefined;
var circleDOM = $('<div class="bgBar"></div><svg class="circle"><circle class="bgBar"/><circle class="bar"/></svg>');
var barDOM = circleDOM.find(".bar");
var bgBarDOM = circleDOM.find(".bgBar");
var infosCtner = $('<div class="infos-ctner"></div>');
var pourcDOM = $('<div class="pourcentage"></div>');
var textDOM = $('<div class="text"></div>');
var value = 0;
var transition = {bar: [], bgBar: []};
var opts = {
lineColor : "#307bbb",
lineWidth: "10",
diameter : "100",
lineTransitionHover: "500ms",
viewPourcentage: true
};
/**
* Used to create the circle bar
*
* @public
* @param {string} _elem Class or id of the slider
* @param {Array} _opts List of options
* @param {number} _value Value of the circle bar
*/
function init(_elem, _opts, _value) {
// Defind variable
circleBarDOM = $(_elem);
circleBarDOM.empty(); // Clear element DOM
value = _value;
$.extend(opts, _opts);
circleBarDOM.addClass("circle-bar");
// Create the circle
createCircle();
// Create the infos container
createInfosCtner();
}
/**
* Used to create the infos content
*
* @private
*/
function createInfosCtner() {
// View pourcentage text if has option
if (opts.viewPourcentage) viewPourcentage();
// Set text if has a text
if (opts.text) setText();
// Set text CSS if has option
if (opts.textCSS) setTextCSS();
// Set pourcentage CSS if has option
if (opts.pourcentageCSS) setPourcentageCSS();
// Add the infos container
circleBarDOM.append(infosCtner);
}
/**
* Used to create the circle content
*
* @private
*/
function createCircle() {
// Set line width
setLineWidth();
// Set width the background line if has option
if (opts.bgLineWidth) setBgLineWidth();
// Set line width hover if has option
if (opts.lineWidthHover) setLineWidthHover();
// Set diameter of the circle
setDiameter();
// Set line color
setLineColor();
// Set line background color if has option
if (opts.lineBgColor) setLineBgColor();
// Set background color if has option
if (opts.backgroundColor) setBgColor();
// Set line duration if has option
if (opts.lineDuration) setLineDuration();
// Set line delay if has option
if (opts.lineDelay) setLineDelay();
// Set line transition hover
setLineTransitionHover();
// Set the circle DOM
circleBarDOM.append(circleDOM)
// Set a value
setValue();
}
/**
* Used to set text
*
* @public
* @param {string} _text Text to insert
*/
function setText(_text) {
if (_text) opts.text = _text;
textDOM.text(opts.text);
if(infosCtner.find(textDOM).length == 0) infosCtner.append(textDOM);
}
/**
* Used to view the pourcentage of the circle bar
*
* @public
* @param {boolean} _bool True for view pourcentage if not false
*/
function viewPourcentage(_bool) {
if (_bool) opts.viewPourcentage = _bool;
pourcDOM.text(value+'%');
if(infosCtner.find(pourcDOM).length == 0) infosCtner.append(pourcDOM);
}
/**
* Used to set a value
*
* @public
* @param {number} _val Value of the circle bar
*/
function setValue(_val) {
if (_val) value = _val;
$(window).on('resize scroll load', function() {
if (circleBarDOM.isInViewport()) {
animate();
} else {}
});
if (_val) animate();
}
/**
* Used animate the circle bar
*
* @private
*/
function animate() {
// Prepare the value for the dashoffset
var _dasharray = 2*Math.PI*((opts.diameter/2)-opts.lineWidth);
// Set the value
barDOM.css("stroke-dashoffset", _dasharray-((_dasharray/100)*value));
// DOM update
if (opts.viewPourcentage) viewPourcentage();
}
/**
* Used to set the color of the line of the circle bar
*
* @public
* @param {string} _lineColor <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
*/
function setLineColor(_lineColor) {
if (_lineColor) opts.lineColor = _lineColor;
// Set the color
barDOM.css("stroke", opts.lineColor);
}
/**
* Used to set the background color of the line of the circle bar
*
* @public
* @param {string} _lineBgColor <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
*/
function setLineBgColor(_lineBgColor) {
if (_lineBgColor) opts.lineBgColor = _lineBgColor;
// Set line background color
bgBarDOM.css("stroke", opts.lineBgColor);
}
/**
* Used to set the background color of the circle bar
*
* @public
* @param {string} _bgColor <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
*/
function setBgColor(_bgColor) {
if (_bgColor) opts.backgroundColor = _bgColor;
// Set background color
bgBarDOM.css("fill", opts.backgroundColor);
}
/**
* Used to set the CSS of the text
*
* @public
* @param {Array} _css Array of CSS
*/
function setTextCSS(_css) {
if (_css) opts.textCSS = _css;
// Set text CSS
textDOM.css(opts.textCSS);
}
/**
* Used to set the CSS of the pourcentage
*
* @public
* @param {Array} _css Array of CSS
*/
function setPourcentageCSS(_css) {
if (_css) opts.pourcentageCSS = _css;
// Set text CSS
pourcDOM.css(opts.pourcentageCSS);
}
/**
* Used to set the hover CSS of the text
*
* @public
* @param {Array} _css Array of CSS
*/
function setTextHoverCSS(_css) {
if (_css) opts.textHoverCSS = _css;
}
/**
* Used to set the hover CSS of the pourcentage
*
* @public
* @param {Array} _css Array of CSS
*/
function setPourcentageHoverCSS(_css) {
if (_css) opts.pourcentageHoverCSS = _css;
}
/**
* Used to initialize the transition of the circle bar
*
* @private
*/
function initTransition() {
// Init transition for barDOM
var _transitionStr = "";
for (var i = 0; i < transition.bar.length; i++) {
_transitionStr += (_transitionStr=="" ? "" : ",") + transition.bar[i].name + " " + transition.bar[i].value;
}
barDOM.css("transition", _transitionStr);
// Init transition for bgBarDOM
var _transitionStr = "";
for (var i = 0; i < transition.bgBar.length; i++) {
_transitionStr += (_transitionStr=="" ? "" : ",") + transition.bgBar[i].name + " " + transition.bgBar[i].value;
}
bgBarDOM.css("transition", _transitionStr);
}
/**
* Used to set the line width
*
* @public
* @param {number} _lineWidth Width of the line
*/
function setLineWidth(_lineWidth) {
if (_lineWidth) opts.lineWidth = _lineWidth;
// Set the line width
barDOM.attr("stroke-width", opts.lineWidth);
}
/**
* Used to set the background line width
*
* @public
* @param {number} _bgLineWidth Width of the background line
*/
function setBgLineWidth(_bgLineWidth) {
if (_bgLineWidth) opts.bgLineWidth = _bgLineWidth;
// Set the line width
bgBarDOM.attr("stroke-width", opts.bgLineWidth);
}
/**
* Used to set the line hover width
*
* @public
* @param {number} _lineWidthHover Width of the line hover
*/
function setLineWidthHover(_lineWidthHover) {
if (_lineWidthHover) opts.lineWidthHover = _lineWidthHover;
initHover();
}
/**
* Used to set the background line hover width
*
* @public
* @param {number} _bgLineWidthHover Width of the background line hover
*/
function setbgLineWidthHover(_bgLineWidthHover) {
if (_bgLineWidthHover) opts.bgLineWidthHover = _bgLineWidthHover;
initHover();
}
/**
* Used to initialize the transition of hover
*
* @private
*/
function initHover() {
// Set line width hover
circleBarDOM.hover(
function(){
// Set the new stroke width
if (opts.lineWidthHover) barDOM.css("stroke-width", opts.lineWidthHover);
if (opts.bgLineWidthHover) bgBarDOM.css("stroke-width", opts.bgLineWidthHover);
// Set the hover CSS of the infos content
if (opts.textHoverCSS) textDOM.css(opts.textHoverCSS);
if (opts.pourcentageHoverCSS) pourcDOM.css(opts.pourcentageHoverCSS);
},
function(){
// Set the old stroke width
if (opts.lineWidthHover) barDOM.css("stroke-width", opts.lineWidth);
if (opts.bgLineWidthHover) bgBarDOM.css("stroke-width", opts.bgLineWidth);
// Set the CSS of the infos content
if (opts.textHoverCSS) textDOM.css(opts.textCSS);
if (opts.pourcentageHoverCSS) pourcDOM.css(opts.pourcentageCSS);
}
);
}
/**
* Used to set the line duration of the circle bar transition
*
* @public
* @param {string} _lineDuration Duration of the transition
*/
function setLineDuration(_lineDuration) {
if (_lineDuration) opts.lineDuration = _lineDuration;
setTransitionDashoffset();
}
/**
* Used to set the line delay of the circle bar transition
*
* @public
* @param {string} _lineDelay Delay of the transition
*/
function setLineDelay(_lineDelay) {
if (_lineDelay) opts.lineDelay = _lineDelay;
// If line duration id undefined
if (!opts.lineDuration) opts.lineDuration = "1s";
setTransitionDashoffset();
}
/**
* Used to set the values of the circle bar transition
*
* @private
*/
function setTransitionDashoffset() {
transition.bar.push ( { name: "stroke-dashoffset", value: opts.lineDuration + " " + (opts.lineDelay ? opts.lineDelay : "") } );
initTransition();
}
/**
* Used to set the line transition hover of the circle bar transition
*
* @public
* @param {string} _lineTransitionHover Duration of the transition
*/
function setLineTransitionHover(_lineTransitionHover) {
if (_lineTransitionHover) opts.lineTransitionHover = _lineTransitionHover;
// Set line transition hover
transition.bar.push ( { name: "stroke-width", value: opts.lineTransitionHover } );
transition.bgBar.push( { name: "stroke-width", value: opts.lineTransitionHover } );
// Set text transition
pourcDOM.css('transition', 'all ' + opts.lineTransitionHover);
textDOM.css('transition', 'all ' + opts.lineTransitionHover);
initTransition();
}
/**
* Used to set the diameter of the circle bar
*
* @public
* @param {number} _diameter Diameter of the circle bar
*/
function setDiameter(_diameter) {
if (_diameter) opts.diameter = _diameter;
// Defind circle bar size
circleBarDOM.css("width", opts.diameter+"px");
circleBarDOM.css("height", opts.diameter+"px");
// Defind circle size
circleDOM.css("width", opts.diameter+"px");
circleDOM.css("height", opts.diameter+"px");
// Set values for the bar
barDOM.attr("r", ((opts.diameter/2)-opts.lineWidth));
barDOM.attr("cx", (opts.diameter/2));
barDOM.attr("cy", (opts.diameter/2));
barDOM.css("stroke-dasharray", 2*Math.PI*((opts.diameter/2)-opts.lineWidth));
barDOM.css("stroke-dashoffset", 2*Math.PI*((opts.diameter/2)-opts.lineWidth));
// Set values for the background bar
bgBarDOM.attr("r", ((opts.diameter/2)-opts.lineWidth));
bgBarDOM.attr("cx", (opts.diameter/2));
bgBarDOM.attr("cy", (opts.diameter/2));
bgBarDOM.css("stroke-dasharray", 2*Math.PI*((opts.diameter/2)-opts.lineWidth));
}
/**
* Used to get the value of the circle bar
*
* @public
*/
function getValue() { return value; }
/**
* Used to get the options of the circle bar
*
* @public
*/
function getOpts() { return opts; }
return {
"init": function(_elem, _opts, _value) {
init(_elem, _opts, _value);
},
"getValue": function() {
return getValue();
},
"getOpts": function() {
return getOpts();
},
"setText": function(_text) {
setText(_text);
},
"setValue": function(_val) {
setValue(_val);
},
"setLineColor": function(_lineColor) {
setLineColor(_lineColor);
},
"setLineBgColor": function(_lineBgColor) {
setLineBgColor(_lineBgColor);
},
"setBgColor": function(_bgColor) {
setBgColor(_bgColor);
},
"setLineWidth": function(_lineWidth) {
setLineWidth(_lineWidth);
},
"setBgLineWidth": function(_bgLineWidth) {
setBgLineWidth(_bgLineWidth);
},
"setLineDuration": function(_lineDuration) {
setLineDuration(_lineDuration);
},
"setLineDelay": function(_lineDelay) {
setLineDelay(_lineDelay);
},
"setLineWidthHover": function(_lineWidthHover) {
setLineWidthHover(_lineWidthHover);
},
"setbgLineWidthHover": function(_bgLineWidthHover) {
setbgLineWidthHover(_bgLineWidthHover);
},
"setLineTransitionHover": function(_lineTransitionHover) {
setLineTransitionHover(_lineTransitionHover);
},
"viewPourcentage": function(_bool) {
viewPourcentage(_bool);
},
"setTextCSS": function(_css) {
setTextCSS(_css);
},
"setTextHoverCSS": function(_css) {
setTextHoverCSS(_css);
},
"setPourcentageCSS": function(_css) {
setPourcentageCSS(_css);
},
"setPourcentageHoverCSS": function(_css) {
setPourcentageHoverCSS(_css);
}
};
}