-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-class.js
195 lines (167 loc) · 5.37 KB
/
js-class.js
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
var BasicFunctions = function() {
}
BasicFunctions.prototype.toggleVisibility = function(id) {
if(document.getElementById(id).style.display == "none") {
document.getElementById(id).style.display = "inline";
} else {
document.getElementById(id).style.display = "none";
}
}
BasicFunctions.prototype.validateEmail = function(email) {
var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
return re.test(String(email).toLowerCase());
}
BasicFunctions.prototype.getElement = function(id) {
return document.getElementById(id);
}
/*
* can be used to center an absolute or fixed div
*/
BasicFunctions.prototype.centerAbsoluteElement = function(div) {
var divWidth = div.offsetWidth;
var divHeight = div.offsetHeight;
var pageWidth = window.innerWidth;
var pageHeight = window.innerHeight;
div.style.left = ((pageWidth - divWidth) / 2) + "px";
div.style.top = ((pageHeight - divHeight) / 2) + "px";
}
BasicFunctions.prototype.addActionButtonForDiv = function(div, action) {
div.classList.add("centeredContainer");
var firstNode = div.firstChild;
var btn = document.createElement("button");
btn.innerHTML = "×";
btn.classList.add("closeButton");
btn.dataset.close = "1";
btn.addEventListener("click", function(event) {
var child = event.target.parentNode;
var parent = event.target.parentNode.parentNode;
switch (action) {
case 'hide':
child.style.display = "none";
break;
case 'remove':
parent.removeChild(child);
break;
default:
break;
}
}.bind(action), false);
if (firstNode.dataset == null || firstNode.dataset.close == null) {
div.insertBefore(btn, firstNode);
}
}
BasicFunctions.prototype.removeElement = function(element) {
if (typeof element === 'string' || element instanceof String) {
var child = document.getElementById(element);
var parent = child.parentNode;
parent.removeChild(child);
} else if (element instanceof HTMLElement) {
var parent = element.parentNode;
parent.removeChild(element);
}
}
/*
* data should be an array containing arrays the size of a row, the data[0] array should contain the heading
* of the table, so the size of data is rows + 1;
*/
BasicFunctions.prototype.createTable = function(rows, columns, data, emptyFields) {
var table = document.createElement("table");
var tbody = document.createElement("tobdy");
if (emptyFields == null || emptyFields == undefined) {
emptyFields = false;
}
var tr, td, th;
for (var i = 0; i <= rows; i++) {
tr = document.createElement("tr");
for (var n = 0; n < columns; n++) {
if (i == 0) {
th = document.createElement("th");
th.innerText = data[i][n] == undefined ? "" : data[i][n];
tr.appendChild(th);
} else {
td = document.createElement("td");
if (emptyFields == true && data[i][n] == undefined) {
td.contentEditable = "true";
}
td.innerText = data[i][n] == undefined ? "" : data[i][n];
tr.appendChild(td);
}
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
return table;
}
/* AjaxCall class */
var AjaxCall = function(paramString, ajaxType) {
this.type = (ajaxType != null) ? ajaxType : "POST";
this.paramString = (paramString != null) ? paramString : "";
this.url;
}
AjaxCall.prototype.setType = function(type) {
if(type != null) {
this.type = type;
} else {
console.error("Ajax Type not defined");
}
}
AjaxCall.prototype.setParamString = function(paramString) {
if(paramString != null) {
this.paramString = paramString;
} else {
console.warn("AjaxCall: no parameters given");
}
}
AjaxCall.prototype.setUrl = function(url) {
this.url = url;
}
AjaxCall.prototype.makeAjaxCall = function(dataCallback, ...args) {
if(this.paramString == null) {
console.warn("AjaxCall: no parameters given");
}
if(this.type == "POST") {
var ajaxCall = new XMLHttpRequest();
ajaxCall.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
dataCallback(this.responseText, args);
}
}
ajaxCall.open(this.type, this.url, true);
ajaxCall.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxCall.send(this.paramString);
} else if(this.type == "GET") {
var ajaxCall = new XMLHttpRequest();
ajaxCall.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
dataCallback(this.responseText, args);
}
}
ajaxCall.open("GET", this.url + this.paramString, true);
ajaxCall.send();
} else {
console.error("AjaxCall: Ajax Type not defined");
}
}
var CanvasCreation = function() {
}
/* https://stackoverflow.com/questions/15661339/how-do-i-fix-blurry-text-in-my-html5-canvas */
CanvasCreation.prototype.PIXEL_RATIO = (function () {
var ctx = document.createElement("canvas").getContext("2d"),
dpr = window.devicePixelRatio || 1,
bsr = ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1;
return dpr / bsr;
})();
CanvasCreation.prototype.createHiDPICanvas = function(w, h, ratio) {
if (!ratio) { ratio = this.PIXEL_RATIO; }
var can = document.createElement("canvas");
can.width = w * ratio;
can.height = h * ratio;
can.style.width = w + "px";
can.style.height = h + "px";
can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0);
return can;
}