-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
213 lines (172 loc) · 5.35 KB
/
util.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function hasClass(elem, name) {
var index = elem.className.indexOf(name);
return elem.className.indexOf(' ' + name + ' ') > -1 ||
elem.className.indexOf(name + ' ') === 0 ||
(elem.className.indexOf(' ' + name) > -1 && index + name.length === elem.className.length) ||
elem.className === name;
}
function addClass(elem, name) {
if (hasClass(elem, name)) return;
if (elem.className.length === 0) {
elem.className = name;
} else {
elem.className += ' ' + name;
}
}
function removeClass(elem, name) {
if (elem.className.length === 0 || !hasClass(elem, name)) return;
let allClasses = elem.className;
if (allClasses.indexOf(' ' + name) > -1 &&
allClasses.indexOf(name) + name.length === allClasses.length) {
elem.className = allClasses.replace(' ' + name, '');
} else if (allClasses === name) {
elem.className = "";
} else if (allClasses.indexOf(name + ' ') === 0) {
elem.className = allClasses.replace(name + ' ', '');
} else if (allClasses.indexOf(' ' + name + ' ') > -1) {
elem.className = allClasses.replace(' ' + name + ' ', ' ');
}
}
function toggleClass(elem, name) {
if(hasClass(elem, name)) {
removeClass(elem, name);
} else {
addClass(elem, name);
}
}
function show(elem) {
elem.style.display = "block";
}
function hide(elem) {
elem.style.display = "none";
}
function element(query) {
let identifier = query.substring(0, 1);
if(identifier === '#') {
return document.getElementById(query.replace('#', ''));
} else {
return document.querySelectorAll(query);
}
}
function getFontColorForBackgroundColor(hexCode) {
var bareHex = hexCode.replace('#', '');
var red = parseInt(bareHex.substring(0, 2), 16);
var green = parseInt(bareHex.substring(2, 4), 16);
var blue = parseInt(bareHex.substring(4, 6), 16);
//magic numbers
var luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
if(luminance > 186)
return "#aeaeae";
else
return "#ffffff";
}
function createRow(options) {
var row = document.createElement('tr');
if(options.rowClass !== undefined) row.classList.add(options.rowClass);
options.cells.forEach((cell) => {
var td = document.createElement('td');
cell.classes.forEach((className) => {
td.classList.add(className);
});
for(var key in cell.styles) {
if(cell.styles.hasOwnProperty(key)) {
td.style[key] = cell.styles[key];
}
}
td.colSpan = cell.span === undefined ? 1 : cell.span;
if(cell.value instanceof Array) {
cell.value.forEach((item) => {
if(isElement(item)) {
td.appendChild(item);
} else {
td.innerText = item;
}
});
} else {
if(isElement(cell.value)) {
td.appendChild(cell.value);
} else {
td.innerText = cell.value;
}
}
row.appendChild(td);
});
return row;
}
function createTimeEntryButton(clientId, projectId) {
var a = document.createElement('a');
a.setAttribute("href", "#");
a.classList.add("button", "time-entry");
a.setAttribute("data-client", clientId);
a.setAttribute("data-project", projectId);
a.innerHTML = "Add Time<i class='fa fa-check'></i>";
return a;
}
function createTimeEntryCheckbox(entryId) {
var checkbox = document.createElement("input");
checkbox.setAttribute("id", `time-entry-${entryId}`);
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("data-id", entryId);
checkbox.setAttribute("checked", "checked");
checkbox.classList.add("check");
return checkbox;
}
function getTimestamp() {
return new Date().toString();
}
function createTimestampMarkup(timestamp) {
var p = document.createElement('p');
p.innerText = timestamp.toLocaleString();
return p;
}
function getTotalTime(entries) {
return getIntranetFriendlyTimeFromSeconds(getTotalTimeInSeconds(entries));
}
function getTotalTimeInSeconds(entries) {
var totalTime = 0;
entries.forEach((entry) => {
totalTime += parseInt(entry.duration);
});
return totalTime;
}
function getIntranetFriendlyTimeFromSeconds(seconds) {
var hours = seconds / 3600;
seconds -= hours * 3600;
var minutes = seconds / 60;
seconds -= minutes * 60;
minutes += seconds > 30 ? 1 : 0;
return hours + (minutes / 60);
}
function roundProjectTime(hours, threshold) {
var hourPortion = Math.floor(hours);
var decimalPortion = hours - hourPortion;
if(decimalPortion >= 1) {
return hours;
}
if (decimalPortion > 0.0 && decimalPortion <= .25) {
decimalPortion = decimalPortion <= threshold ? 0.0 : .25;
} else if (decimalPortion > .25 && decimalPortion <= .5) {
decimalPortion = decimalPortion <= (threshold + .25) ? .25 : .5;
} else if (decimalPortion > .5 && decimalPortion <= .75) {
decimalPortion = decimalPortion <= (threshold + .5) ? .5 : .75;
} else if (decimalPortion > .75) {
decimalPortion = decimalPortion <= (threshold + .75) ? .75 : 1;
}
return hourPortion + decimalPortion;
}
function isEmpty(item) {
return item === '' || item === undefined || item === null || item === NaN;
}
function isValidDate(text) {
let regex = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{4}$/);
return !isEmpty(text) && regex.test(text);
}
function isElement(obj) {
return obj instanceof HTMLElement || obj[0] instanceof HTMLElement;
}
function removeActiveButtons(){
var button = element('a.button.active');
for(var i=0; i<button.length; i++) {
removeClass(button[i], 'active');
}
}