init
[garradin.git] / www / admin / static / global.js
1 (function () {
2 window.$ = function(selector) {
3 if (!selector.match(/^[.#]?[a-z0-9_-]+$/i))
4 {
5 return document.querySelectorAll(selector);
6 }
7 else if (selector.substr(0, 1) == '.')
8 {
9 return document.getElementsByClassName(selector.substr(1));
10 }
11 else if (selector.substr(0, 1) == '#')
12 {
13 return document.getElementById(selector.substr(1));
14 }
15 else
16 {
17 return document.getElementsByTagName(selector);
18 }
19 };
20
21 window.toggleElementVisibility = function(selector, visibility)
22 {
23 if (!('classList' in document.documentElement))
24 return false;
25
26 if (selector instanceof Array)
27 {
28 for (var i = 0; i < selector.length; i++)
29 {
30 toggleElementVisibility(selector[i], visibility);
31 }
32
33 return true;
34 }
35
36 var elements = $(selector);
37
38 for (var i = 0; i < elements.length; i++)
39 {
40 if (!visibility)
41 elements[i].classList.add('hidden');
42 else
43 elements[i].classList.remove('hidden');
44 }
45
46 return true;
47 };
48
49 function dateInputFallback()
50 {
51 var input = document.createElement('input');
52 input.setAttribute('type', 'date');
53 input.value = ':-)';
54 input.style.position = 'absolute';
55 input.style.visibility = 'hidden';
56 document.body.appendChild(input);
57
58 // If input type changed or value hasn't been sanitized then
59 // the input type date element is not supported
60 if (input.type === 'text' || input.value === ':-)')
61 {
62 var www_url = document.body.getAttribute('data-url') + 'static/';
63
64 var script = document.createElement('script');
65 script.type = "text/javascript";
66 script.src = www_url + 'datepickr.js';
67 document.head.appendChild(script);
68
69 var link = document.createElement('link');
70 link.type = 'text/css';
71 link.rel = 'stylesheet';
72 link.href = www_url + 'datepickr.css';
73 document.head.appendChild(link);
74 }
75
76 document.body.removeChild(input);
77 }
78
79 if (document.addEventListener)
80 {
81 document.addEventListener("DOMContentLoaded", dateInputFallback, false);
82 }
83 else
84 {
85 document.attachEvent("onDOMContentLoaded", dateInputFallback);
86 }
87 })();