This repository was archived by the owner on Jun 4, 2024. It is now read-only.
forked from guserIln/validate_emails
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtinytools.progressbar.js
More file actions
147 lines (117 loc) · 4.12 KB
/
Copy pathtinytools.progressbar.js
File metadata and controls
147 lines (117 loc) · 4.12 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
/*!
ProgressBar 1.0.3 - 2015-02-04
jQuery Tiny Progress Bar
(c) 2015, http://tinytools.codesells.com
license: http://www.opensource.org/licenses/mit-license.php
*/
; (function ($, document, window) {
var progressBar = 'progressBar';
var progressBarGeneralSettings;
if ($.progressBar) {
return;
}
publicMethod = $.fn[progressBar] = $[progressBar] = function (options) {
var settings = options;
return this.each(function (i, obj) {
initializeProgressBar(obj, settings);
});
};
function setSettings(options) {
var settings = $.extend({
initializing: true,
percent: 0,
width: false,
height: false,
split: 1,
backSplitLineColor: "#999",
foreSplitLineColor: "#ddd",
showPercent: true,
//Events:
onPercentChanged: false
}, options);
return settings;
}
function getSettings(internalElement) {
return internalElement.closest('.ProgressBar').data('settings');
}
function initializeProgressBar(obj, settings) {
var setting = setSettings({});
setting = $.extend(setting, progressBarGeneralSettings);
settings = $.extend(setting, settings);
$(obj).addClass("ProgressBar");
var content = '<p class="BackProgressBarPercent">50%</p>';
content += '<canvas class="BackCanvas"></canvas>';
content += '<div class="Bar">';
content += '<p class="ForeProgressBarPercent">50%</p>';
content += '<canvas class="ForeCanvas"></canvas>';
content += '</div>';
$(obj).append(content);
$(obj).data("settings", settings);
if (settings.width != false)
$(obj).css('width', settings.width);
else if ($(obj).width() == 0)
$(obj).css('width', '200px');
if (settings.height != false)
$(obj).css('height', settings.height);
else if ($(obj).height() == 0)
$(obj).css('height', '20px');
$(obj).children(".Bar").height($(obj).height());
$(obj).find("p").css('line-height', $(obj).height().toString() + 'px');
$(obj).find("canvas").prop('height', $(obj).height().toString());
changePercentValue($(obj), settings.percent);
settings.initializing = undefined;
$(obj).data("settings", settings);
$(window).resize(function () {
$(obj).find("p").css('width', $(obj).width().toString() + 'px');
$(obj).find("canvas").prop('width', $(obj).width().toString());
drawSplitLines($(obj).find(".BackCanvas"), settings.backSplitLineColor, settings.split);
drawSplitLines($(obj).find(".ForeCanvas"), settings.foreSplitLineColor, settings.split);
}).resize();
}
function drawSplitLines(canvas, color, split) {
if (split > 1) {
var c = canvas.get(0);
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.strokeStyle = color;
ctx.lineWidth = 1;
for (var i = 1; i < split; i++) {
ctx.beginPath();
ctx.moveTo(c.width / split * i, 0);
ctx.lineTo(c.width / split * i, c.height);
ctx.stroke();
ctx.closePath();
}
}
}
$.fn.changePercent = function (newPercent) {
changePercentValue(this, newPercent);
}
$.fn.getPercent = function () {
return ('.ProgressBar').first().data('settings').percent;
}
function changePercentValue(element, newPercent) {
var currentSetting = getSettings($(element));
if ($(element).hasClass('ProgressBar')) {
var percentText = Math.min(Math.max(newPercent, 0), 100).toString() + "%";
$(element).children(".Bar").css('width', percentText);
$(element).find("p").text(currentSetting.showPercent == true ? percentText : "");
if (currentSetting.initializing == undefined) {
currentSetting.percent = newPercent;
$(element).data("settings", currentSetting);
trigger(currentSetting.onPercentChanged, newPercent, element);
}
}
}
publicMethod.getSettings = function (internalElement) {
return internalElement.closest('.ProgressBar').data('settings');
}
publicMethod.percentChanged = function (value, caller) {
trigger(getSettings(caller).onPercentChanged, value, caller);
}
function trigger(callback, value, caller) {
if ($.isFunction(callback)) {
callback.call(undefined, value, caller);
}
}
}(jQuery, document, window));