Première version en Python-Django.
[auf_paf.git] / 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 if (obj.addEventListener) {
10 obj.addEventListener(evType, fn, false);
11 return true;
12 } else if (obj.attachEvent) {
13 var r = obj.attachEvent("on" + evType, fn);
14 return r;
15 } else {
16 return false;
17 }
18 }
19
20 function removeEvent(obj, evType, fn) {
21 if (obj.removeEventListener) {
22 obj.removeEventListener(evType, fn, false);
23 return true;
24 } else if (obj.detachEvent) {
25 obj.detachEvent("on" + evType, fn);
26 return true;
27 } else {
28 return false;
29 }
30 }
31
32 // quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]);
33 function quickElement() {
34 var obj = document.createElement(arguments[0]);
35 if (arguments[2] != '' && arguments[2] != null) {
36 var textNode = document.createTextNode(arguments[2]);
37 obj.appendChild(textNode);
38 }
39 var len = arguments.length;
40 for (var i = 3; i < len; i += 2) {
41 obj.setAttribute(arguments[i], arguments[i+1]);
42 }
43 arguments[1].appendChild(obj);
44 return obj;
45 }
46
47 // ----------------------------------------------------------------------------
48 // Cross-browser xmlhttp object
49 // from http://jibbering.com/2002/4/httprequest.html
50 // ----------------------------------------------------------------------------
51 var xmlhttp;
52 /*@cc_on @*/
53 /*@if (@_jscript_version >= 5)
54 try {
55 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
56 } catch (e) {
57 try {
58 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
59 } catch (E) {
60 xmlhttp = false;
61 }
62 }
63 @else
64 xmlhttp = false;
65 @end @*/
66 if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
67 xmlhttp = new XMLHttpRequest();
68 }
69
70 // ----------------------------------------------------------------------------
71 // Find-position functions by PPK
72 // See http://www.quirksmode.org/js/findpos.html
73 // ----------------------------------------------------------------------------
74 function findPosX(obj) {
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 var curtop = 0;
93 if (obj.offsetParent) {
94 while (obj.offsetParent) {
95 curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
96 obj = obj.offsetParent;
97 }
98 // IE offsetParent does not include the top-level
99 if (isIE && obj.parentElement){
100 curtop += obj.offsetTop - obj.scrollTop;
101 }
102 } else if (obj.y) {
103 curtop += obj.y;
104 }
105 return curtop;
106 }
107
108 //-----------------------------------------------------------------------------
109 // Date object extensions
110 // ----------------------------------------------------------------------------
111
112 Date.prototype.getTwelveHours = function() {
113 hours = this.getHours();
114 if (hours == 0) {
115 return 12;
116 }
117 else {
118 return hours <= 12 ? hours : hours-12
119 }
120 }
121
122 Date.prototype.getTwoDigitMonth = function() {
123 return (this.getMonth() < 9) ? '0' + (this.getMonth()+1) : (this.getMonth()+1);
124 }
125
126 Date.prototype.getTwoDigitDate = function() {
127 return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
128 }
129
130 Date.prototype.getTwoDigitTwelveHour = function() {
131 return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
132 }
133
134 Date.prototype.getTwoDigitHour = function() {
135 return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
136 }
137
138 Date.prototype.getTwoDigitMinute = function() {
139 return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
140 }
141
142 Date.prototype.getTwoDigitSecond = function() {
143 return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
144 }
145
146 Date.prototype.getHourMinute = function() {
147 return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
148 }
149
150 Date.prototype.getHourMinuteSecond = function() {
151 return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
152 }
153
154 Date.prototype.strftime = function(format) {
155 var fields = {
156 c: this.toString(),
157 d: this.getTwoDigitDate(),
158 H: this.getTwoDigitHour(),
159 I: this.getTwoDigitTwelveHour(),
160 m: this.getTwoDigitMonth(),
161 M: this.getTwoDigitMinute(),
162 p: (this.getHours() >= 12) ? 'PM' : 'AM',
163 S: this.getTwoDigitSecond(),
164 w: '0' + this.getDay(),
165 x: this.toLocaleDateString(),
166 X: this.toLocaleTimeString(),
167 y: ('' + this.getFullYear()).substr(2, 4),
168 Y: '' + this.getFullYear(),
169 '%' : '%'
170 };
171 var result = '', i = 0;
172 while (i < format.length) {
173 if (format.charAt(i) === '%') {
174 result = result + fields[format.charAt(i + 1)];
175 ++i;
176 }
177 else {
178 result = result + format.charAt(i);
179 }
180 ++i;
181 }
182 return result;
183 }
184
185 // ----------------------------------------------------------------------------
186 // String object extensions
187 // ----------------------------------------------------------------------------
188 String.prototype.pad_left = function(pad_length, pad_string) {
189 var new_string = this;
190 for (var i = 0; new_string.length < pad_length; i++) {
191 new_string = pad_string + new_string;
192 }
193 return new_string;
194 }
195
196 // ----------------------------------------------------------------------------
197 // Get the computed style for and element
198 // ----------------------------------------------------------------------------
199 function getStyle(oElm, strCssRule){
200 var strValue = "";
201 if(document.defaultView && document.defaultView.getComputedStyle){
202 strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
203 }
204 else if(oElm.currentStyle){
205 strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
206 return p1.toUpperCase();
207 });
208 strValue = oElm.currentStyle[strCssRule];
209 }
210 return strValue;
211 }