Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
212 lines (186 loc) · 6.18 KB
/
Copy pathhelpers.js
File metadata and controls
212 lines (186 loc) · 6.18 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
/* global WIDTH, HEIGHT, parseInt */
var HELPER = new HELPER_CLASS();
/**
* various helpers
*
* @author ViliusL
*/
function HELPER_CLASS() {
var time;
this.timer_init = function () {
time = Date.now();
};
this.timer = function (s, echo) {
var str = "time(" + s + ") = " + (Math.round(Date.now() - time) / 1000) + " s";
if (echo === true)
return str;
else
console.log(str);
};
this.strpos = function (haystack, needle, offset) {
var i = (haystack + '').indexOf(needle, (offset || 0));
return i === -1 ? false : i;
};
this.getCookie = function (NameOfCookie) {
if (document.cookie.length > 0){
begin = document.cookie.indexOf(NameOfCookie + "=");
if (begin != -1){
begin += NameOfCookie.length + 1;
end = document.cookie.indexOf(";", begin);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(begin, end));
}
}
return '';
};
this.setCookie = function (NameOfCookie, value, expiredays) {
var ExpireDate = new Date();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
};
this.delCookie = function (NameOfCookie) {
if (HELPER.getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
};
this.getRandomInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
this.font_pixel_to_height = function (px) {
return Math.round(px * 0.75);
};
this.rgbToHex = function (r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
};
this.rgb2hex_all = function (rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" + HELPER.hex(rgb[1]) + HELPER.hex(rgb[2]) + HELPER.hex(rgb[3]);
};
this.hex = function (x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
};
this.hex2rgb = function (hex) {
if (hex[0] == "#")
hex = hex.substr(1);
if (hex.length == 3) {
var temp = hex;
hex = '';
temp = /^([a-f0-9])([a-f0-9])([a-f0-9])$/i.exec(temp).slice(1);
for (var i = 0; i < 3; i++)
hex += temp[i] + temp[i];
}
var triplets = /^([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i.exec(hex).slice(1);
return {
r: parseInt(triplets[0], 16),
g: parseInt(triplets[1], 16),
b: parseInt(triplets[2], 16),
a: 255
};
};
this.remove_selection = function () {
if (window.getSelection) {
if (window.getSelection().empty) // Chrome
window.getSelection().empty();
else if (window.getSelection().removeAllRanges) // Firefox
window.getSelection().removeAllRanges();
}
else if (document.selection) // IE?
document.selection.empty();
};
this.get_dimensions = function () {
var theWidth, theHeight;
if (window.innerWidth) {
theWidth = window.innerWidth;
}
else if (document.documentElement && document.documentElement.clientWidth) {
theWidth = document.documentElement.clientWidth;
}
else if (document.body) {
theWidth = document.body.clientWidth;
}
if (window.innerHeight) {
theHeight = window.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight) {
theHeight = document.documentElement.clientHeight;
}
else if (document.body) {
theHeight = document.body.clientHeight;
}
return [theWidth, theHeight];
};
//credits: richard maloney 2006
this.darkenColor = function (color, v) {
if (color.length > 6) {
color = color.substring(1, color.length);
}
var rgb = parseInt(color, 16);
var r = Math.abs(((rgb >> 16) & 0xFF) + v);
if (r > 255)
r = r - (r - 255);
var g = Math.abs(((rgb >> 8) & 0xFF) + v);
if (g > 255)
g = g - (g - 255);
var b = Math.abs((rgb & 0xFF) + v);
if (b > 255)
b = b - (b - 255);
r = Number(r < 0 || isNaN(r)) ? 0 : ((r > 255) ? 255 : r).toString(16);
if (r.length == 1)
r = '0' + r;
g = Number(g < 0 || isNaN(g)) ? 0 : ((g > 255) ? 255 : g).toString(16);
if (g.length == 1)
g = '0' + g;
b = Number(b < 0 || isNaN(b)) ? 0 : ((b > 255) ? 255 : b).toString(16);
if (b.length == 1)
b = '0' + b;
return "#" + r + g + b;
};
//IntegraXor Web SCADA - JavaScript Number Formatter, author: KPL, KHL
this.number_format = function (n, decPlaces, thouSeparator, decSeparator) {
var decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces;
var decSeparator = decSeparator == undefined ? "." : decSeparator;
var thouSeparator = thouSeparator == undefined ? "," : thouSeparator;
var sign = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "";
var j = (j = i.length) > 3 ? j % 3 : 0;
return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
};
this.chech_input_color_support = function (id) {
if (document.getElementById(id).value != undefined && document.getElementById(id).value[0] == '#')
return true;
return false;
};
this.b64toBlob = function(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
};
this.escapeHtml = function(text){
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
this.isNumeric = function(n){
return !isNaN(parseFloat(n)) && isFinite(n);
};
}