-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.js
133 lines (124 loc) · 3.47 KB
/
helper.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
function log(msg) {
console.log(msg);
}
/**
* short form function to capture id, class or tag in document
* @param {string} selector
* @return {object} object or reference to the element
*/
function $(selector) {
if (/^#/.test(selector)) {
return document.getElementById(selector.slice(1));
}
if (/^\./.test(selector)) {
return document.getElementsByClassName(selector.slice(1));
}
return document.getElementsByTagName(selector);
}
/**
* short form for Document Query selector
* @param {string} selector
*/
function $$(selector, all = 0) {
if (all === 0)
return document.querySelector(selector);
else
return document.querySelectorAll(selector);
}
/**
* Insert new spanor div element after an existing element
* @param {string} newNode span|div
* @param {Node} el reference to existing element
* return reference to new element
*/
function insertAfter(newNode, el) {
if (newNode === 'span') {
var newNodeEl = document.createElement("span");
} else {
var newNodeEl = document.createElement("div");
}
return el.parentNode.insertBefore(newNodeEl, el.nextSibling);
}
function isset(el) {
return el === 'undefined' ? 0 : (el == null ? 0 : 1);
}
/**
* event listener function
*/
function on(event, e, func, propagation = false) {
return e.addEventListener(event, func, propagation);
}
function off(event, e, func) {
return e.removeEventListener(event, func);
}
function simulateClick(id) {
document.getElementById(id).click();
}
async function ajax(url, authToken, method = 'GET') {
try {
let response = await fetch(url, {
method: method,
headers: {
"Accept": "*/*",
"Authorization": "enctoken " + authToken
}
});
if(response.status == 200){
response = await response.text();
try {
return JSON.parse(response);
} catch (e) {
return response;
}
}
return false;
} catch (error) {
console.log(error.message);
}
}
function kformater($number) {
if (typeof ($number) !== 'number') {
return -1;
}
$formated_num = $number;
let $format = [{ v: 1, s: '' }, { v: 1E3, s: 'K' }, { v: 1E6, s: 'M' }, { v: 1E9, s: 'B' }];
let $j = 3;
while (1) {
if ($number >= $format[$j].v) {
if ($j == 0)
$formated_num = ($number / $format[$j].v) + $format[$j].s;
else
$formated_num = ($number / $format[$j].v).toFixed(2) + $format[$j].s;
break;
}
if ($j <= 0) { break; } $j--;
}
return $formated_num;
}
function addRow(data, tableId, rowAttributes, position = 0) {
let newRow = '<tr ' + rowAttributes + '>';
data.forEach(el => {
newRow += '<td>' + el + '</td>';
});
newRow += '</tr>';
if (position != 0) {
$$('#' + tableId + '>tbody>tr', 1)[(position - 2)].outerHTML += newRow;
} else {
$('#' + tableId).innerHTML += newRow;
}
}
function escapeHTML(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
function parseUrlQuery(url) {
var query = [];
url.substring(url.indexOf('?') + 1).split('&').
forEach(el => {
var split = el.split('=');
query[split[0]] = split[1];
});
return query;
}
function hide(el){
el.style.display = 'none';
}