| 1 |
|
| 2 |
"use strict"; |
| 3 |
var w3 = {}; |
| 4 |
w3.hide = function (sel) { |
| 5 |
w3.hideElements(w3.getElements(sel)); |
| 6 |
}; |
| 7 |
w3.hideElements = function (elements) { |
| 8 |
var i, l = elements.length; |
| 9 |
for (i = 0; i < l; i++) { |
| 10 |
w3.hideElement(elements[i]); |
| 11 |
} |
| 12 |
}; |
| 13 |
w3.hideElement = function (element) { |
| 14 |
w3.styleElement(element, "display", "none"); |
| 15 |
}; |
| 16 |
w3.show = function (sel, a) { |
| 17 |
var elements = w3.getElements(sel); |
| 18 |
if (a) {w3.hideElements(elements);} |
| 19 |
w3.showElements(elements); |
| 20 |
}; |
| 21 |
w3.showElements = function (elements) { |
| 22 |
var i, l = elements.length; |
| 23 |
for (i = 0; i < l; i++) { |
| 24 |
w3.showElement(elements[i]); |
| 25 |
} |
| 26 |
}; |
| 27 |
w3.showElement = function (element) { |
| 28 |
w3.styleElement(element, "display", "block"); |
| 29 |
}; |
| 30 |
w3.addStyle = function (sel, prop, val) { |
| 31 |
w3.styleElements(w3.getElements(sel), prop, val); |
| 32 |
}; |
| 33 |
w3.styleElements = function (elements, prop, val) { |
| 34 |
var i, l = elements.length; |
| 35 |
for (i = 0; i < l; i++) { |
| 36 |
w3.styleElement(elements[i], prop, val); |
| 37 |
} |
| 38 |
}; |
| 39 |
w3.styleElement = function (element, prop, val) { |
| 40 |
element.style.setProperty(prop, val); |
| 41 |
}; |
| 42 |
w3.toggleShow = function (sel) { |
| 43 |
var i, x = w3.getElements(sel), l = x.length; |
| 44 |
for (i = 0; i < l; i++) { |
| 45 |
if (x[i].style.display == "none") { |
| 46 |
w3.styleElement(x[i], "display", "block"); |
| 47 |
} else { |
| 48 |
w3.styleElement(x[i], "display", "none"); |
| 49 |
} |
| 50 |
} |
| 51 |
}; |
| 52 |
w3.addClass = function (sel, name) { |
| 53 |
w3.addClassElements(w3.getElements(sel), name); |
| 54 |
}; |
| 55 |
w3.addClassElements = function (elements, name) { |
| 56 |
var i, l = elements.length; |
| 57 |
for (i = 0; i < l; i++) { |
| 58 |
w3.addClassElement(elements[i], name); |
| 59 |
} |
| 60 |
}; |
| 61 |
w3.addClassElement = function (element, name) { |
| 62 |
var i, arr1, arr2; |
| 63 |
arr1 = element.className.split(" "); |
| 64 |
arr2 = name.split(" "); |
| 65 |
for (i = 0; i < arr2.length; i++) { |
| 66 |
if (arr1.indexOf(arr2[i]) == -1) {element.className += " " + arr2[i];} |
| 67 |
} |
| 68 |
}; |
| 69 |
w3.removeClass = function (sel, name) { |
| 70 |
w3.removeClassElements(w3.getElements(sel), name); |
| 71 |
}; |
| 72 |
w3.removeClassElements = function (elements, name) { |
| 73 |
var i, l = elements.length, arr1, arr2, j; |
| 74 |
for (i = 0; i < l; i++) { |
| 75 |
w3.removeClassElement(elements[i], name); |
| 76 |
} |
| 77 |
}; |
| 78 |
w3.removeClassElement = function (element, name) { |
| 79 |
var i, arr1, arr2; |
| 80 |
arr1 = element.className.split(" "); |
| 81 |
arr2 = name.split(" "); |
| 82 |
for (i = 0; i < arr2.length; i++) { |
| 83 |
while (arr1.indexOf(arr2[i]) > -1) { |
| 84 |
arr1.splice(arr1.indexOf(arr2[i]), 1); |
| 85 |
} |
| 86 |
} |
| 87 |
element.className = arr1.join(" "); |
| 88 |
}; |
| 89 |
w3.toggleClass = function (sel, c1, c2) { |
| 90 |
w3.toggleClassElements(w3.getElements(sel), c1, c2); |
| 91 |
}; |
| 92 |
w3.toggleClassElements = function (elements, c1, c2) { |
| 93 |
var i, l = elements.length; |
| 94 |
for (i = 0; i < l; i++) { |
| 95 |
w3.toggleClassElement(elements[i], c1, c2); |
| 96 |
} |
| 97 |
}; |
| 98 |
w3.toggleClassElement = function (element, c1, c2) { |
| 99 |
var t1, t2, t1Arr, t2Arr, j, arr, allPresent; |
| 100 |
t1 = (c1 || ""); |
| 101 |
t2 = (c2 || ""); |
| 102 |
t1Arr = t1.split(" "); |
| 103 |
t2Arr = t2.split(" "); |
| 104 |
arr = element.className.split(" "); |
| 105 |
if (t2Arr.length == 0) { |
| 106 |
allPresent = true; |
| 107 |
for (j = 0; j < t1Arr.length; j++) { |
| 108 |
if (arr.indexOf(t1Arr[j]) == -1) {allPresent = false;} |
| 109 |
} |
| 110 |
if (allPresent) { |
| 111 |
w3.removeClassElement(element, t1); |
| 112 |
} else { |
| 113 |
w3.addClassElement(element, t1); |
| 114 |
} |
| 115 |
} else { |
| 116 |
allPresent = true; |
| 117 |
for (j = 0; j < t1Arr.length; j++) { |
| 118 |
if (arr.indexOf(t1Arr[j]) == -1) {allPresent = false;} |
| 119 |
} |
| 120 |
if (allPresent) { |
| 121 |
w3.removeClassElement(element, t1); |
| 122 |
w3.addClassElement(element, t2); |
| 123 |
} else { |
| 124 |
w3.removeClassElement(element, t2); |
| 125 |
w3.addClassElement(element, t1); |
| 126 |
} |
| 127 |
} |
| 128 |
}; |
| 129 |
w3.getElements = function (id) { |
| 130 |
if (typeof id == "object") { |
| 131 |
return [id]; |
| 132 |
} else { |
| 133 |
return document.querySelectorAll(id); |
| 134 |
} |
| 135 |
}; |
| 136 |
w3.filterHTML = function(id, sel, filter) { |
| 137 |
var a, b, c, i, ii, iii, hit; |
| 138 |
a = w3.getElements(id); |
| 139 |
for (i = 0; i < a.length; i++) { |
| 140 |
b = w3.getElements(sel); |
| 141 |
for (ii = 0; ii < b.length; ii++) { |
| 142 |
hit = 0; |
| 143 |
if (b[ii].innerHTML.toUpperCase().indexOf(filter.toUpperCase()) > -1) { |
| 144 |
hit = 1; |
| 145 |
} |
| 146 |
c = b[ii].getElementsByTagName("*"); |
| 147 |
for (iii = 0; iii < c.length; iii++) { |
| 148 |
if (c[iii].innerHTML.toUpperCase().indexOf(filter.toUpperCase()) > -1) { |
| 149 |
hit = 1; |
| 150 |
} |
| 151 |
} |
| 152 |
if (hit == 1) { |
| 153 |
b[ii].style.display = ""; |
| 154 |
} else { |
| 155 |
b[ii].style.display = "none"; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
}; |
| 160 |
w3.sortHTML = function(id, sel, sortvalue) { |
| 161 |
var a, b, i, ii, y, bytt, v1, v2, cc, j; |
| 162 |
a = w3.getElements(id); |
| 163 |
for (i = 0; i < a.length; i++) { |
| 164 |
for (j = 0; j < 2; j++) { |
| 165 |
cc = 0; |
| 166 |
y = 1; |
| 167 |
while (y == 1) { |
| 168 |
y = 0; |
| 169 |
b = a[i].querySelectorAll(sel); |
| 170 |
for (ii = 0; ii < (b.length - 1); ii++) { |
| 171 |
bytt = 0; |
| 172 |
if (sortvalue) { |
| 173 |
v1 = b[ii].querySelector(sortvalue).innerHTML.toLowerCase(); |
| 174 |
v2 = b[ii + 1].querySelector(sortvalue).innerHTML.toLowerCase(); |
| 175 |
} else { |
| 176 |
v1 = b[ii].innerHTML.toLowerCase(); |
| 177 |
v2 = b[ii + 1].innerHTML.toLowerCase(); |
| 178 |
} |
| 179 |
if ((j == 0 && (v1 > v2)) || (j == 1 && (v1 < v2))) { |
| 180 |
bytt = 1; |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
if (bytt == 1) { |
| 185 |
b[ii].parentNode.insertBefore(b[ii + 1], b[ii]); |
| 186 |
y = 1; |
| 187 |
cc++; |
| 188 |
} |
| 189 |
} |
| 190 |
if (cc > 0) {break;} |
| 191 |
} |
| 192 |
} |
| 193 |
}; |
| 194 |
w3.slideshow = function (sel, ms, func) { |
| 195 |
var i, ss, x = w3.getElements(sel), l = x.length; |
| 196 |
ss = {}; |
| 197 |
ss.current = 1; |
| 198 |
ss.x = x; |
| 199 |
ss.ondisplaychange = func; |
| 200 |
if (!isNaN(ms) || ms == 0) { |
| 201 |
ss.milliseconds = ms; |
| 202 |
} else { |
| 203 |
ss.milliseconds = 1000; |
| 204 |
} |
| 205 |
ss.start = function() { |
| 206 |
ss.display(ss.current) |
| 207 |
if (ss.ondisplaychange) {ss.ondisplaychange();} |
| 208 |
if (ss.milliseconds > 0) { |
| 209 |
window.clearTimeout(ss.timeout); |
| 210 |
ss.timeout = window.setTimeout(ss.next, ss.milliseconds); |
| 211 |
} |
| 212 |
}; |
| 213 |
ss.next = function() { |
| 214 |
ss.current += 1; |
| 215 |
if (ss.current > ss.x.length) {ss.current = 1;} |
| 216 |
ss.start(); |
| 217 |
}; |
| 218 |
ss.previous = function() { |
| 219 |
ss.current -= 1; |
| 220 |
if (ss.current < 1) {ss.current = ss.x.length;} |
| 221 |
ss.start(); |
| 222 |
}; |
| 223 |
ss.display = function (n) { |
| 224 |
w3.styleElements(ss.x, "display", "none"); |
| 225 |
w3.styleElement(ss.x[n - 1], "display", "block"); |
| 226 |
} |
| 227 |
ss.start(); |
| 228 |
return ss; |
| 229 |
}; |
| 230 |
w3.includeHTML = function(cb) { |
| 231 |
var z, i, elmnt, file, xhttp; |
| 232 |
z = document.getElementsByTagName("*"); |
| 233 |
for (i = 0; i < z.length; i++) { |
| 234 |
elmnt = z[i]; |
| 235 |
file = elmnt.getAttribute("w3-include-html"); |
| 236 |
if (file) { |
| 237 |
xhttp = new XMLHttpRequest(); |
| 238 |
xhttp.onreadystatechange = function() { |
| 239 |
if (this.readyState == 4) { |
| 240 |
if (this.status == 200) {elmnt.innerHTML = this.responseText;} |
| 241 |
if (this.status == 404) {elmnt.innerHTML = "Page not found.";} |
| 242 |
elmnt.removeAttribute("w3-include-html"); |
| 243 |
w3.includeHTML(cb); |
| 244 |
} |
| 245 |
} |
| 246 |
xhttp.open("GET", file, true); |
| 247 |
xhttp.send(); |
| 248 |
return; |
| 249 |
} |
| 250 |
} |
| 251 |
if (cb) cb(); |
| 252 |
}; |
| 253 |
w3.getHttpData = function (file, func) { |
| 254 |
w3.http(file, function () { |
| 255 |
if (this.readyState == 4 && this.status == 200) { |
| 256 |
func(this.responseText); |
| 257 |
} |
| 258 |
}); |
| 259 |
}; |
| 260 |
w3.getHttpObject = function (file, func) { |
| 261 |
w3.http(file, function () { |
| 262 |
if (this.readyState == 4 && this.status == 200) { |
| 263 |
func(JSON.parse(this.responseText)); |
| 264 |
} |
| 265 |
}); |
| 266 |
}; |
| 267 |
w3.displayHttp = function (id, file) { |
| 268 |
w3.http(file, function () { |
| 269 |
if (this.readyState == 4 && this.status == 200) { |
| 270 |
w3.displayObject(id, JSON.parse(this.responseText)); |
| 271 |
} |
| 272 |
}); |
| 273 |
}; |
| 274 |
w3.http = function (target, readyfunc, xml, method) { |
| 275 |
var httpObj; |
| 276 |
if (!method) {method = "GET"; } |
| 277 |
if (window.XMLHttpRequest) { |
| 278 |
httpObj = new XMLHttpRequest(); |
| 279 |
} else if (window.ActiveXObject) { |
| 280 |
httpObj = new ActiveXObject("Microsoft.XMLHTTP"); |
| 281 |
} |
| 282 |
if (httpObj) { |
| 283 |
if (readyfunc) {httpObj.onreadystatechange = readyfunc;} |
| 284 |
httpObj.open(method, target, true); |
| 285 |
httpObj.send(xml); |
| 286 |
} |
| 287 |
}; |
| 288 |
w3.getElementsByAttribute = function (x, att) { |
| 289 |
var arr = [], arrCount = -1, i, l, y = x.getElementsByTagName("*"), z = att.toUpperCase(); |
| 290 |
l = y.length; |
| 291 |
for (i = -1; i < l; i += 1) { |
| 292 |
if (i == -1) {y[i] = x;} |
| 293 |
if (y[i].getAttribute(z) !== null) {arrCount += 1; arr[arrCount] = y[i];} |
| 294 |
} |
| 295 |
return arr; |
| 296 |
}; |
| 297 |
w3.dataObject = {}, |
| 298 |
w3.displayObject = function (id, data) { |
| 299 |
var htmlObj, htmlTemplate, html, arr = [], a, l, rowClone, x, j, i, ii, cc, repeat, repeatObj, repeatX = ""; |
| 300 |
htmlObj = document.getElementById(id); |
| 301 |
htmlTemplate = init_template(id, htmlObj); |
| 302 |
html = htmlTemplate.cloneNode(true); |
| 303 |
arr = w3.getElementsByAttribute(html, "w3-repeat"); |
| 304 |
l = arr.length; |
| 305 |
for (j = (l - 1); j >= 0; j -= 1) { |
| 306 |
cc = arr[j].getAttribute("w3-repeat").split(" "); |
| 307 |
if (cc.length == 1) { |
| 308 |
repeat = cc[0]; |
| 309 |
} else { |
| 310 |
repeatX = cc[0]; |
| 311 |
repeat = cc[2]; |
| 312 |
} |
| 313 |
arr[j].removeAttribute("w3-repeat"); |
| 314 |
repeatObj = data[repeat]; |
| 315 |
if (repeatObj && typeof repeatObj == "object" && repeatObj.length != "undefined") { |
| 316 |
i = 0; |
| 317 |
for (x in repeatObj) { |
| 318 |
i += 1; |
| 319 |
rowClone = arr[j]; |
| 320 |
rowClone = w3_replace_curly(rowClone, "element", repeatX, repeatObj[x]); |
| 321 |
a = rowClone.attributes; |
| 322 |
for (ii = 0; ii < a.length; ii += 1) { |
| 323 |
a[ii].value = w3_replace_curly(a[ii], "attribute", repeatX, repeatObj[x]).value; |
| 324 |
} |
| 325 |
(i === repeatObj.length) ? arr[j].parentNode.replaceChild(rowClone, arr[j]) : arr[j].parentNode.insertBefore(rowClone, arr[j]); |
| 326 |
} |
| 327 |
} else { |
| 328 |
console.log("w3-repeat must be an array. " + repeat + " is not an array."); |
| 329 |
continue; |
| 330 |
} |
| 331 |
} |
| 332 |
html = w3_replace_curly(html, "element"); |
| 333 |
htmlObj.parentNode.replaceChild(html, htmlObj); |
| 334 |
function init_template(id, obj) { |
| 335 |
var template; |
| 336 |
template = obj.cloneNode(true); |
| 337 |
if (w3.dataObject.hasOwnProperty(id)) {return w3.dataObject[id];} |
| 338 |
w3.dataObject[id] = template; |
| 339 |
return template; |
| 340 |
} |
| 341 |
function w3_replace_curly(elmnt, typ, repeatX, x) { |
| 342 |
var value, rowClone, pos1, pos2, originalHTML, lookFor, lookForARR = [], i, cc, r; |
| 343 |
rowClone = elmnt.cloneNode(true); |
| 344 |
pos1 = 0; |
| 345 |
while (pos1 > -1) { |
| 346 |
originalHTML = (typ == "attribute") ? rowClone.value : rowClone.innerHTML; |
| 347 |
pos1 = originalHTML.indexOf("{{", pos1); |
| 348 |
if (pos1 === -1) {break;} |
| 349 |
pos2 = originalHTML.indexOf("}}", pos1 + 1); |
| 350 |
lookFor = originalHTML.substring(pos1 + 2, pos2); |
| 351 |
lookForARR = lookFor.split("||"); |
| 352 |
value = undefined; |
| 353 |
for (i = 0; i < lookForARR.length; i += 1) { |
| 354 |
lookForARR[i] = lookForARR[i].replace(/^\s+|\s+$/gm, ''); |
| 355 |
if (x) {value = x[lookForARR[i]];} |
| 356 |
if (value == undefined && data) {value = data[lookForARR[i]];} |
| 357 |
if (value == undefined) { |
| 358 |
cc = lookForARR[i].split("."); |
| 359 |
if (cc[0] == repeatX) {value = x[cc[1]]; } |
| 360 |
} |
| 361 |
if (value == undefined) { |
| 362 |
if (lookForARR[i] == repeatX) {value = x;} |
| 363 |
} |
| 364 |
if (value == undefined) { |
| 365 |
if (lookForARR[i].substr(0, 1) == '"') { |
| 366 |
value = lookForARR[i].replace(/"/g, ""); |
| 367 |
} else if (lookForARR[i].substr(0,1) == "'") { |
| 368 |
value = lookForARR[i].replace(/'/g, ""); |
| 369 |
} |
| 370 |
} |
| 371 |
if (value != undefined) {break;} |
| 372 |
} |
| 373 |
if (value != undefined) { |
| 374 |
r = "{{" + lookFor + "}}"; |
| 375 |
if (typ == "attribute") { |
| 376 |
rowClone.value = rowClone.value.replace(r, value); |
| 377 |
} else { |
| 378 |
w3_replace_html(rowClone, r, value); |
| 379 |
} |
| 380 |
} |
| 381 |
pos1 = pos1 + 1; |
| 382 |
} |
| 383 |
return rowClone; |
| 384 |
} |
| 385 |
function w3_replace_html(a, r, result) { |
| 386 |
var b, l, i, a, x, j; |
| 387 |
if (a.hasAttributes()) { |
| 388 |
b = a.attributes; |
| 389 |
l = b.length; |
| 390 |
for (i = 0; i < l; i += 1) { |
| 391 |
if (b[i].value.indexOf(r) > -1) {b[i].value = b[i].value.replace(r, result);} |
| 392 |
} |
| 393 |
} |
| 394 |
x = a.getElementsByTagName("*"); |
| 395 |
l = x.length; |
| 396 |
a.innerHTML = a.innerHTML.replace(r, result); |
| 397 |
} |
| 398 |
}; |