1 | /* |
||
2 | * JQuery ProgressBarTimer - v 1.0 |
||
3 | * A simple countdown timer using bootstrap 4 progress bar component. |
||
4 | * |
||
5 | * |
||
6 | * Requires: bootstrap >= 4, jquery >=3 |
||
7 | * Made by Jacob malliaros |
||
8 | * Under MIT License |
||
9 | */ |
||
10 | |||
11 | ; |
||
12 | (function($, window, document, undefined) { |
||
13 | |||
14 | var pluginName = 'progressBarTimer', |
||
15 | dataPlugin = "plugin_" + pluginName, |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
16 | defaults = { |
||
17 | timeLimit: 60, //total number of seconds |
||
18 | warningThreshold: 5, //seconds remaining triggering switch to warning color |
||
19 | autoStart: false, // start the countdown automatically |
||
20 | onFinish: function() {}, //invoked once the timer expires |
||
21 | baseStyle: '', //bootstrap progress bar style at the beginning of the timer |
||
22 | warningStyle: 'bg-warning', //bootstrap progress bar style in the warning phase |
||
23 | smooth: false, // should the timer be smooth or stepping |
||
24 | completeStyle: 'bg-danger', //bootstrap progress bar style at completion of timer |
||
25 | striped: false, //allow bootstrap striped progress bar |
||
26 | animated: false, //allow bootstrap animated progress bar (striped should also be on) |
||
27 | height: 0, //height of progress bar in pixels (0 is the default height) |
||
28 | label: { |
||
29 | show: false, //show label inside progress bar |
||
30 | type: 'percent' //type of label. Allowable types: 'percent' => 30% , 'seconds' => 23/60 |
||
31 | } |
||
32 | }; |
||
33 | |||
34 | function Plugin(element, options) { |
||
35 | this.element = element; |
||
36 | this._name = pluginName; |
||
37 | this.settings = $.extend({}, defaults, options); |
||
38 | this._defaults = defaults; |
||
39 | this._name = pluginName; |
||
40 | this.init(); |
||
41 | } |
||
42 | |||
43 | $.extend(Plugin.prototype, { |
||
44 | init: function() { |
||
45 | $(this).empty(); |
||
46 | var barContainer = $("<div>").addClass("progress active progress-striped"); |
||
47 | var bar = $("<div>").addClass("progress-bar").addClass(this.settings.baseStyle) |
||
48 | .attr("role", "progressbar") |
||
49 | .attr("aria-valuenow", "0") |
||
50 | .attr("aria-valuemin", "0") |
||
51 | .attr("aria-valuemax", this.settings.timeLimit); |
||
52 | if (this.settings.animated) { |
||
53 | bar.addClass('progress-bar-striped'); |
||
54 | bar.addClass('progress-bar-animated'); |
||
55 | } else if (this.settings.striped) bar.addClass('progress-bar-striped'); |
||
56 | if (this.settings.height) barContainer.height(this.settings.height); |
||
57 | bar.appendTo(barContainer); |
||
58 | barContainer.appendTo(this.element); |
||
59 | |||
60 | |||
61 | if (this.settings.smooth) { |
||
62 | this.timerInterval = 20; |
||
63 | this.ticks = this.settings.timeLimit * 50; |
||
64 | } else { |
||
65 | this.timerInterval = 1000; |
||
66 | this.ticks = this.settings.timeLimit; |
||
67 | } |
||
68 | this.remainingTicks = this.ticks; |
||
69 | |||
70 | if (this.settings.autoStart === true) this.start(); |
||
71 | |||
72 | this.bindEvents(); |
||
73 | }, |
||
74 | destroy: function() { |
||
75 | this.unbindEvents(); |
||
76 | this.element.removeData(); |
||
77 | }, |
||
78 | bindEvents: function() { |
||
79 | var plugin = this; |
||
0 ignored issues
–
show
|
|||
80 | }, |
||
81 | unbindEvents: function() { |
||
82 | this.element.off('.' + this._name); |
||
83 | }, |
||
84 | start: function() { |
||
85 | this.interval = setInterval(jQuery.proxy(this._draw, this), this.timerInterval); |
||
86 | }, |
||
87 | reset: function() { |
||
88 | clearInterval(this.interval); |
||
89 | this.remainingTicks = this.ticks; |
||
90 | this._draw(); |
||
91 | }, |
||
92 | stop: function(callBack) { |
||
93 | clearInterval(this.interval); |
||
94 | if (callBack) { |
||
95 | callBack(); |
||
96 | } |
||
97 | }, |
||
98 | getElapsedTime: function() { |
||
99 | return this._getSecondsFromTicks(this.ticks - this.remainingTicks); |
||
100 | }, |
||
101 | getRemainingTime: function() { |
||
102 | return this._getSecondsFromTicks(this.remainingTicks); |
||
103 | }, |
||
104 | addSeconds: function(seconds) { |
||
105 | var added = _getTicksFromSeconds(seconds); |
||
106 | this.ticks += added; |
||
107 | this.remainingTicks += added; |
||
108 | }, |
||
109 | _getTicksFromSeconds: function(secs) { |
||
110 | return this.settings.smooth ? secs * 50 : secs; |
||
111 | }, |
||
112 | _getSecondsFromTicks: function(ticks) { |
||
113 | return this.settings.smooth ? ticks / 50 : ticks; |
||
114 | }, |
||
115 | _draw: function() { |
||
116 | var bar = $(this.element).find('.progress-bar'); |
||
117 | var elapsedTicks = this.ticks - this.remainingTicks; |
||
118 | var percent = ((elapsedTicks / this.ticks) * 100); |
||
119 | bar.width(percent + "%"); |
||
120 | if (this.settings.label && this.settings.label.show === true) { |
||
121 | if (this.settings.label.type == 'percent') { |
||
122 | bar.html(Math.round(percent) + "%"); |
||
123 | } else { |
||
124 | if (this.settings.smooth === true) { |
||
125 | bar.html(Math.round(elapsedTicks / 50) + "/" + Math.round(this.ticks / 50)); |
||
126 | } else { |
||
127 | bar.html(elapsedTicks + "/" + this.ticks); |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | --this.remainingTicks; |
||
132 | if (this._getSecondsFromTicks(this.remainingTicks) <= this.settings.warningThreshold && !bar.hasClass(this.settings.warningStyle)) { |
||
133 | bar.removeClass(this.settings.baseStyle).addClass(this.settings.warningStyle); |
||
134 | } |
||
135 | if (this.remainingTicks === 0) { |
||
136 | this.stop(); |
||
137 | |||
138 | bar.removeClass(this.settings.baseStyle) |
||
139 | .removeClass(this.settings.warningStyle) |
||
140 | .addClass(this.settings.completeStyle); |
||
141 | bar.width("100%"); |
||
142 | this.settings.onFinish(); |
||
143 | } |
||
144 | } |
||
145 | }); |
||
146 | |||
147 | $.fn.progressBarTimer = function(options) { |
||
148 | var plugin; |
||
149 | this.each(function() { |
||
150 | plugin = $.data(this, "plugin_" + pluginName); |
||
151 | if (!plugin) { |
||
152 | plugin = new Plugin(this, options); |
||
153 | |||
154 | $.data(this, "plugin_" + pluginName, plugin); |
||
155 | } |
||
156 | }); |
||
157 | return plugin; |
||
158 | }; |
||
159 | |||
160 | |||
161 | })(jQuery, window, document); |