forked from ndg63276/smartlife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral_functions.js
191 lines (175 loc) · 5.05 KB
/
general_functions.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
function testFirstCookie(){
var visit = getCookie("cookieCompliancyAccepted");
if (visit == ""){
$("#myCookieConsent").fadeIn(400); // Show warning
}
}
function acceptCookies(){
setCookie("cookieCompliancyAccepted", "true", 365*24)
$("#myCookieConsent").hide(400);
}
function last_element(arr) {
return arr[arr.length-1]
}
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(";");
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function setCookie(cname, cvalue, exhours) {
var d = new Date();
d.setTime(d.getTime() + (exhours * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function parseDateParam(param) {
var to_return = new Date();
if (param == 'now') {
// do nothing
} else if (param == 'future_prices') {
if (to_return.getHours() >= 16) {
to_return.setDate(to_return.getDate()+1);
to_return.setHours(23, 0, 0, 0);
} else {
to_return.setHours(23, 0, 0, 0);
}
} else if (param.startsWith("20")) {
var pattern = /([0-9]{4})-?([0-9]{2})-?([0-9]{2})T?([0-9]{2})?:?([0-9]{2})?:?([0-9]{2})?.*/;
var match = param.match(pattern);
year = match[1];
month = match[2];
day = match[3];
if (match[4] == null) { hour = 0; } else { hour = match[4]; }
if (match[5] == null) { min = 0; } else { min = match[5]; }
if (match[6] == null) { sec = 0; } else { sec = match[6]; }
to_return = new Date(year, month-1, day, hour, min, sec);
} else {
var pattern = /([+ -])([0-9]+)(year|month|week|day|hour|min|sec).*/i;
var match = param.match(pattern);
if (match != null) {
if (match[1] == "-") {
plusminus = -1
} else {
plusminus = 1
}
if (match[3].includes("year")) {
to_return.setFullYear(to_return.getFullYear() + plusminus * match[2]);
}
if (match[3].includes("month")) {
to_return.setMonth(to_return.getMonth() + plusminus * match[2]);
}
if (match[3].includes("week")) {
to_return.setDate(to_return.getDate() + plusminus * match[2] * 7);
}
if (match[3].includes("day")) {
to_return.setDate(to_return.getDate() + plusminus * match[2]);
}
if (match[3].includes("hour")) {
to_return.setHours(to_return.getHours() + plusminus * match[2]);
}
if (match[3].includes("min")) {
to_return.setMinutes(to_return.getMinutes() + plusminus * match[2]);
}
if (match[3].includes("sec")) {
to_return.setSeconds(to_return.getSeconds() + plusminus * match[2]);
}
}
}
return to_return;
}
function capitalise(str) {
if (typeof str !== 'undefined' && str !== null) {
return str.charAt(0).toUpperCase() + str.slice(1)
} else {
return str
}
}
function hardCapitalise(str) {
if (typeof str !== 'undefined' && str !== null) {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
} else {
return str
}
}
function get_json(jsonfile) {
var to_return = {};
$.ajax({
url: jsonfile,
async: false,
dataType: "json",
success: function (json) {
to_return = json;
}
});
return to_return;
}
function get_text(file) {
var to_return = {};
$.ajax({
url: file,
async: false,
success: function (text) {
to_return = text.trim();
}
});
return to_return;
}
String.prototype.obfuscate = function () {
var bytes = [];
for (var i = 0; i < this.length; i++) {
bytes.push(this.charCodeAt(i).toString(16));
}
return bytes.join('$');
}
String.prototype.deobfuscate = function () {
var arr = this.split('$');
return arr.map(function(c) {
return String.fromCharCode(parseInt(c, 16))
}).reduce(function(a, b) {return a + b})
}
$.mobile.changeGlobalTheme = function(theme) {
// These themes will be cleared, add more
// swatch letters as needed.
var themes = " a b c d e";
// Updates the theme for all elements that match the
// CSS selector with the specified theme class.
function setTheme(cssSelector, themeClass, theme) {
$(cssSelector)
.removeClass(themes.split(" ").join(" " + themeClass + "-"))
.addClass(themeClass + "-" + theme)
.attr("data-theme", theme);
}
// Add more selectors/theme classes as needed.
setTheme(".ui-mobile-viewport", "ui-overlay", theme);
setTheme("[data-role='page']", "ui-body", theme);
setTheme("[data-role='header']", "ui-bar", theme);
setTheme("[data-role='listview'] > li", "ui-bar", theme);
setTheme(".ui-btn", "ui-btn-up", theme);
setTheme(".ui-btn", "ui-btn-hover", theme);
};
function switchTheme() {
if (document.getElementById("page").classList.contains('ui-body-b')) {
$.mobile.changeGlobalTheme("a");
document.getElementById("logo").classList.remove("hidden")
document.getElementById("logo_dark").classList.add("hidden")
} else {
$.mobile.changeGlobalTheme("b");
document.getElementById("logo").classList.add("hidden")
document.getElementById("logo_dark").classList.remove("hidden")
}
}