initial commit
[emels.git] / emels / static / admin / js / core.js
1 // Core javascript helper functions
2
3 // basic browser identification & version
4 var isOpera = (navigator.userAgent.indexOf("Opera") >= 0) && parseFloat(navigator.appVersion);
5 var isIE = ((document.all) && (!isOpera)) && parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]);
6
7 // Cross-browser event handlers.
8 function addEvent(obj, evType, fn) {
9     'use strict';
10     if (obj.addEventListener) {
11         obj.addEventListener(evType, fn, false);
12         return true;
13     } else if (obj.attachEvent) {
14         var r = obj.attachEvent("on" + evType, fn);
15         return r;
16     } else {
17         return false;
18     }
19 }
20
21 function removeEvent(obj, evType, fn) {
22     'use strict';
23     if (obj.removeEventListener) {
24         obj.removeEventListener(evType, fn, false);
25         return true;
26     } else if (obj.detachEvent) {
27         obj.detachEvent("on" + evType, fn);
28         return true;
29     } else {
30         return false;
31     }
32 }
33
34 function cancelEventPropagation(e) {
35     'use strict';
36     if (!e) {
37         e = window.event;
38     }
39     e.cancelBubble = true;
40     if (e.stopPropagation) {
41         e.stopPropagation();
42     }
43 }
44
45 // quickElement(tagType, parentReference [, textInChildNode, attribute, attributeValue ...]);
46 function quickElement() {
47     'use strict';
48     var obj = document.createElement(arguments[0]);
49     if (arguments[2]) {
50         var textNode = document.createTextNode(arguments[2]);
51         obj.appendChild(textNode);
52     }
53     var len = arguments.length;
54     for (var i = 3; i < len; i += 2) {
55         obj.setAttribute(arguments[i], arguments[i + 1]);
56     }
57     arguments[1].appendChild(obj);
58     return obj;
59 }
60
61 // "a" is reference to an object
62 function removeChildren(a) {
63     'use strict';
64     while (a.hasChildNodes()) {
65         a.removeChild(a.lastChild);
66     }
67 }
68
69 // ----------------------------------------------------------------------------
70 // Find-position functions by PPK
71 // See http://www.quirksmode.org/js/findpos.html
72 // ----------------------------------------------------------------------------
73 function findPosX(obj) {
74     'use strict';
75     var curleft = 0;
76     if (obj.offsetParent) {
77         while (obj.offsetParent) {
78             curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft);
79             obj = obj.offsetParent;
80         }
81         // IE offsetParent does not include the top-level
82         if (isIE && obj.parentElement) {
83             curleft += obj.offsetLeft - obj.scrollLeft;
84         }
85     } else if (obj.x) {
86         curleft += obj.x;
87     }
88     return curleft;
89 }
90
91 function findPosY(obj) {
92     'use strict';
93     var curtop = 0;
94     if (obj.offsetParent) {
95         while (obj.offsetParent) {
96             curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
97             obj = obj.offsetParent;
98         }
99         // IE offsetParent does not include the top-level
100         if (isIE && obj.parentElement) {
101             curtop += obj.offsetTop - obj.scrollTop;
102         }
103     } else if (obj.y) {
104         curtop += obj.y;
105     }
106     return curtop;
107 }
108
109 //-----------------------------------------------------------------------------
110 // Date object extensions
111 // ----------------------------------------------------------------------------
112 (function() {
113     'use strict';
114     Date.prototype.getTwelveHours = function() {
115         var hours = this.getHours();
116         if (hours === 0) {
117             return 12;
118         }
119         else {
120             return hours <= 12 ? hours : hours - 12;
121         }
122     };
123
124     Date.prototype.getTwoDigitMonth = function() {
125         return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1);
126     };
127
128     Date.prototype.getTwoDigitDate = function() {
129         return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
130     };
131
132     Date.prototype.getTwoDigitTwelveHour = function() {
133         return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
134     };
135
136     Date.prototype.getTwoDigitHour = function() {
137         return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
138     };
139
140     Date.prototype.getTwoDigitMinute = function() {
141         return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
142     };
143
144     Date.prototype.getTwoDigitSecond = function() {
145         return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
146     };
147
148     Date.prototype.getHourMinute = function() {
149         return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
150     };
151
152     Date.prototype.getHourMinuteSecond = function() {
153         return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
154     };
155
156     Date.prototype.getFullMonthName = function() {
157         return typeof window.CalendarNamespace === "undefined"
158             ? this.getTwoDigitMonth()
159             : window.CalendarNamespace.monthsOfYear[this.getMonth()];
160     };
161
162     Date.prototype.strftime = function(format) {
163         var fields = {
164             B: this.getFullMonthName(),
165             c: this.toString(),
166             d: this.getTwoDigitDate(),
167             H: this.getTwoDigitHour(),
168             I: this.getTwoDigitTwelveHour(),
169             m: this.getTwoDigitMonth(),
170             M: this.getTwoDigitMinute(),
171             p: (this.getHours() >= 12) ? 'PM' : 'AM',
172             S: this.getTwoDigitSecond(),
173             w: '0' + this.getDay(),
174             x: this.toLocaleDateString(),
175             X: this.toLocaleTimeString(),
176             y: ('' + this.getFullYear()).substr(2, 4),
177             Y: '' + this.getFullYear(),
178             '%': '%'
179         };
180         var result = '', i = 0;
181         while (i < format.length) {
182             if (format.charAt(i) === '%') {
183                 result = result + fields[format.charAt(i + 1)];
184                 ++i;
185             }
186             else {
187                 result = result + format.charAt(i);
188             }
189             ++i;
190         }
191         return result;
192     };
193
194 // ----------------------------------------------------------------------------
195 // String object extensions
196 // ----------------------------------------------------------------------------
197     String.prototype.pad_left = function(pad_length, pad_string) {
198         var new_string = this;
199         for (var i = 0; new_string.length < pad_length; i++) {
200             new_string = pad_string + new_string;
201         }
202         return new_string;
203     };
204
205     String.prototype.strptime = function(format) {
206         var split_format = format.split(/[.\-/]/);
207         var date = this.split(/[.\-/]/);
208         var i = 0;
209         var day, month, year;
210         while (i < split_format.length) {
211             switch (split_format[i]) {
212                 case "%d":
213                     day = date[i];
214                     break;
215                 case "%m":
216                     month = date[i] - 1;
217                     break;
218                 case "%Y":
219                     year = date[i];
220                     break;
221                 case "%y":
222                     year = date[i];
223                     break;
224             }
225             ++i;
226         }
227         // Create Date object from UTC since the parsed value is supposed to be
228         // in UTC, not local time. Also, the calendar uses UTC functions for
229         // date extraction.
230         return new Date(Date.UTC(year, month, day));
231     };
232
233 })();
234 // ----------------------------------------------------------------------------
235 // Get the computed style for and element
236 // ----------------------------------------------------------------------------
237 function getStyle(oElm, strCssRule) {
238     'use strict';
239     var strValue = "";
240     if(document.defaultView && document.defaultView.getComputedStyle) {
241         strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
242     }
243     else if(oElm.currentStyle) {
244         strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
245             return p1.toUpperCase();
246         });
247         strValue = oElm.currentStyle[strCssRule];
248     }
249     return strValue;
250 }