Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / simpletestlib / ui / js / x.js
blob78edb02528ea1f181e9de13d0494c0ea78781cb5
1 // x.js
2 // X v3.14.1, Cross-Browser DHTML Library from Cross-Browser.com
3 // Copyright (c) 2002,2003 Michael Foster (mike@cross-browser.com)
4 // This library is distributed under the terms of the LGPL (gnu.org)
6 // Variables:
7 var xVersion='3.14.1',xOp7=false,xOp5or6=false,xIE4Up=false,xNN4=false,xUA=navigator.userAgent.toLowerCase();
8 if(window.opera){
9 xOp7=(xUA.indexOf('opera 7')!=-1 || xUA.indexOf('opera/7')!=-1);
10 if (!xOp7) xOp5or6=(xUA.indexOf('opera 5')!=-1 || xUA.indexOf('opera/5')!=-1 || xUA.indexOf('opera 6')!=-1 || xUA.indexOf('opera/6')!=-1);
12 else if(document.layers) {xNN4=true;}
13 else {xIE4Up=document.all && xUA.indexOf('msie')!=-1 && parseInt(navigator.appVersion)>=4;}
14 // Object:
15 function xGetElementById(e) {
16 if(typeof(e)!='string') return e;
17 if(document.getElementById) e=document.getElementById(e);
18 else if(document.all) e=document.all[e];
19 else if(document.layers) e=xLayer(e);
20 else e=null;
21 return e;
23 function xParent(e,bNode){
24 if (!(e=xGetElementById(e))) return null;
25 var p=null;
26 if (!bNode && xDef(e.offsetParent)) p=e.offsetParent;
27 else if (xDef(e.parentNode)) p=e.parentNode;
28 else if (xDef(e.parentElement)) p=e.parentElement;
29 else if (xDef(e.parentLayer)){if (e.parentLayer!=window) p=e.parentLayer;}
30 return p;
32 function xDef() {
33 for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
34 return true;
36 function xStr(s) {
37 return typeof(s)=='string';
39 function xNum(n) {
40 return typeof(n)=='number';
42 // Appearance:
43 function xShow(e) {
44 if(!(e=xGetElementById(e))) return;
45 if(e.style && xDef(e.style.visibility)) e.style.visibility='visible';
46 else if(xDef(e.visibility)) e.visibility='show';
48 function xHide(e) {
49 if(!(e=xGetElementById(e))) return;
50 if(e.style && xDef(e.style.visibility)) e.style.visibility='hidden';
51 else if(xDef(e.visibility)) e.visibility='hide';
53 function xZIndex(e,uZ) {
54 if(!(e=xGetElementById(e))) return 0;
55 if(e.style && xDef(e.style.zIndex)) {
56 if(xNum(uZ)) e.style.zIndex=uZ;
57 uZ=parseInt(e.style.zIndex);
59 else if(xDef(e.zIndex)) {
60 if(xNum(uZ)) e.zIndex=uZ;
61 uZ=e.zIndex;
63 return uZ;
65 function xColor(e,sColor) {
66 if(!(e=xGetElementById(e))) return '';
67 var c='';
68 if(e.style && xDef(e.style.color)) {
69 if(xStr(sColor)) e.style.color=sColor;
70 c=e.style.color;
72 return c;
74 function xBackground(e,sColor,sImage) {
75 if(!(e=xGetElementById(e))) return '';
76 var bg='';
77 if(e.style) {
78 if(xStr(sColor)) {
79 if(!xOp5or6) e.style.backgroundColor=sColor;
80 else e.style.background=sColor;
82 if(xStr(sImage)) e.style.backgroundImage=(sImage!='')? 'url('+sImage+')' : null;
83 if(!xOp5or6) bg=e.style.backgroundColor;
84 else bg=e.style.background;
86 else if(xDef(e.bgColor)) {
87 if(xStr(sColor)) e.bgColor=sColor;
88 bg=e.bgColor;
89 if(xStr(sImage)) e.background.src=sImage;
91 return bg;
93 // Position:
94 function xMoveTo(e,iX,iY) {
95 xLeft(e,iX);
96 xTop(e,iY);
98 function xLeft(e,iX) {
99 if(!(e=xGetElementById(e))) return 0;
100 var css=xDef(e.style);
101 if (css && xStr(e.style.left)) {
102 if(xNum(iX)) e.style.left=iX+'px';
103 else {
104 iX=parseInt(e.style.left);
105 if(isNaN(iX)) iX=0;
108 else if(css && xDef(e.style.pixelLeft)) {
109 if(xNum(iX)) e.style.pixelLeft=iX;
110 else iX=e.style.pixelLeft;
112 else if(xDef(e.left)) {
113 if(xNum(iX)) e.left=iX;
114 else iX=e.left;
116 return iX;
118 function xTop(e,iY) {
119 if(!(e=xGetElementById(e))) return 0;
120 var css=xDef(e.style);
121 if(css && xStr(e.style.top)) {
122 if(xNum(iY)) e.style.top=iY+'px';
123 else {
124 iY=parseInt(e.style.top);
125 if(isNaN(iY)) iY=0;
128 else if(css && xDef(e.style.pixelTop)) {
129 if(xNum(iY)) e.style.pixelTop=iY;
130 else iY=e.style.pixelTop;
132 else if(xDef(e.top)) {
133 if(xNum(iY)) e.top=iY;
134 else iY=e.top;
136 return iY;
138 function xPageX(e) {
139 if (!(e=xGetElementById(e))) return 0;
140 if (xDef(e.pageX)) return e.pageX;
141 var x = 0;
142 while (e) {
143 if (xDef(e.offsetLeft)) x += e.offsetLeft;
144 e = xDef(e.offsetParent) ? e.offsetParent : null;
146 return x;
148 function xPageY(e) {
149 if (!(e=xGetElementById(e))) return 0;
150 if (xDef(e.pageY)) return e.pageY;
151 var y = 0;
152 while (e) {
153 if (xDef(e.offsetTop)) y += e.offsetTop;
154 e = xDef(e.offsetParent) ? e.offsetParent : null;
156 // if (xOp7) return y - document.body.offsetTop; // v3.14, temporary hack for opera bug 130324
157 return y;
159 function xOffsetLeft(e) { // v3.14, see x_nn4.js
160 if (!(e=xGetElementById(e))) return 0;
161 if (xDef(e.offsetLeft)) return e.offsetLeft;
162 else return 0;
164 function xOffsetTop(e) {
165 if (!(e=xGetElementById(e))) return 0;
166 if (xDef(e.offsetTop)) return e.offsetTop;
167 else return 0;
169 function xScrollLeft(e) {
170 var offset=0;
171 if (!(e=xGetElementById(e))) {
172 if(xDef(window.pageXOffset)) offset=window.pageXOffset;
173 else if(document.documentElement && document.documentElement.scrollLeft) offset=document.documentElement.scrollLeft;
174 else if(document.body && xDef(document.body.scrollLeft)) offset=document.body.scrollLeft;
176 else { if (xNum(e.scrollLeft)) offset = e.scrollLeft; }
177 return offset;
179 function xScrollTop(e) {
180 var offset=0;
181 if (!(e=xGetElementById(e))) {
182 if(xDef(window.pageYOffset)) offset=window.pageYOffset;
183 else if(document.documentElement && document.documentElement.scrollTop) offset=document.documentElement.scrollTop;
184 else if(document.body && xDef(document.body.scrollTop)) offset=document.body.scrollTop;
186 else { if (xNum(e.scrollTop)) offset = e.scrollTop; }
187 return offset;
189 // Size:
190 function xResizeTo(e,uW,uH) {
191 xWidth(e,uW);
192 xHeight(e,uH);
194 function xWidth(e,uW) {
195 if(!(e=xGetElementById(e))) return 0;
196 if (xNum(uW)) { // v3.13.1
197 if (uW<0) uW = 0;
198 else uW=Math.round(uW);
200 else uW=0;
201 var css=xDef(e.style);
202 if(css && xDef(e.offsetWidth) && xStr(e.style.width)) {
203 if(uW) xSetCW(e, uW);
204 uW=e.offsetWidth;
206 else if(css && xDef(e.style.pixelWidth)) {
207 if(uW) e.style.pixelWidth=uW;
208 uW=e.style.pixelWidth;
210 else if(xDef(e.clip) && xDef(e.clip.right)) {
211 if(uW) e.clip.right=uW;
212 uW=e.clip.right;
214 return uW;
216 function xHeight(e,uH) {
217 if(!(e=xGetElementById(e))) return 0;
218 if (xNum(uH)) { // v3.13.1
219 if (uH<0) uH = 0;
220 else uH=Math.round(uH);
222 else uH=0;
223 var css=xDef(e.style);
224 if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
225 if(uH) xSetCH(e, uH);
226 uH=e.offsetHeight;
228 else if(css && xDef(e.style.pixelHeight)) {
229 if(uH) e.style.pixelHeight=uH;
230 uH=e.style.pixelHeight;
232 else if(xDef(e.clip) && xDef(e.clip.bottom)) {
233 if(uH) e.clip.bottom=uH;
234 uH=e.clip.bottom;
236 return uH;
238 function xGetCS(ele,sP){return parseInt(document.defaultView.getComputedStyle(ele,'').getPropertyValue(sP));}
239 function xSetCW(ele,uW){
240 var pl=0,pr=0,bl=0,br=0;
241 if(xDef(document.defaultView) && xDef(document.defaultView.getComputedStyle)){
242 pl=xGetCS(ele,'padding-left');
243 pr=xGetCS(ele,'padding-right');
244 bl=xGetCS(ele,'border-left-width');
245 br=xGetCS(ele,'border-right-width');
247 else if(xDef(ele.currentStyle,document.compatMode)){
248 if(document.compatMode=='CSS1Compat'){
249 pl=parseInt(ele.currentStyle.paddingLeft);
250 pr=parseInt(ele.currentStyle.paddingRight);
251 bl=parseInt(ele.currentStyle.borderLeftWidth);
252 br=parseInt(ele.currentStyle.borderRightWidth);
255 else if(xDef(ele.offsetWidth,ele.style.width)){ // ?
256 ele.style.width=uW+'px';
257 pl=ele.offsetWidth-uW;
259 if(isNaN(pl)) pl=0; if(isNaN(pr)) pr=0; if(isNaN(bl)) bl=0; if(isNaN(br)) br=0;
260 var cssW=uW-(pl+pr+bl+br);
261 if(isNaN(cssW)||cssW<0) return;
262 else ele.style.width=cssW+'px';
264 function xSetCH(ele,uH){
265 var pt=0,pb=0,bt=0,bb=0;
266 if(xDef(document.defaultView) && xDef(document.defaultView.getComputedStyle)){
267 pt=xGetCS(ele,'padding-top');
268 pb=xGetCS(ele,'padding-bottom');
269 bt=xGetCS(ele,'border-top-width');
270 bb=xGetCS(ele,'border-bottom-width');
272 else if(xDef(ele.currentStyle,document.compatMode)){
273 if(document.compatMode=='CSS1Compat'){
274 pt=parseInt(ele.currentStyle.paddingTop);
275 pb=parseInt(ele.currentStyle.paddingBottom);
276 bt=parseInt(ele.currentStyle.borderTopWidth);
277 bb=parseInt(ele.currentStyle.borderBottomWidth);
280 else if(xDef(ele.offsetHeight,ele.style.height)){ // ?
281 ele.style.height=uH+'px';
282 pt=ele.offsetHeight-uH;
284 if(isNaN(pt)) pt=0; if(isNaN(pb)) pb=0; if(isNaN(bt)) bt=0; if(isNaN(bb)) bb=0;
285 var cssH=uH-(pt+pb+bt+bb);
286 if(isNaN(cssH)||cssH<0) return;
287 else ele.style.height=cssH+'px';
289 function xClip(e,iTop,iRight,iBottom,iLeft) {
290 if(!(e=xGetElementById(e))) return;
291 if(e.style) {
292 if (xNum(iLeft)) e.style.clip='rect('+iTop+'px '+iRight+'px '+iBottom+'px '+iLeft+'px)';
293 else e.style.clip='rect(0 '+parseInt(e.style.width)+'px '+parseInt(e.style.height)+'px 0)';
295 else if(e.clip) {
296 if (xNum(iLeft)) { e.clip.top=iTop; e.clip.right=iRight; e.clip.bottom=iBottom; e.clip.left=iLeft; }
297 else { e.clip.top=0; e.clip.right=xWidth(e); e.clip.bottom=xHeight(e); e.clip.left=0; }
300 // Window:
301 function xClientWidth() {
302 var w=0;
303 if(xOp5or6) w=window.innerWidth;
304 else if(!window.opera && document.documentElement && document.documentElement.clientWidth) // v3.12
305 w=document.documentElement.clientWidth;
306 else if(document.body && document.body.clientWidth)
307 w=document.body.clientWidth;
308 else if(xDef(window.innerWidth,window.innerHeight,document.height)) {
309 w=window.innerWidth;
310 if(document.height>window.innerHeight) w-=16;
312 return w;
314 function xClientHeight() {
315 var h=0;
316 if(xOp5or6) h=window.innerHeight;
317 else if(!window.opera && document.documentElement && document.documentElement.clientHeight) // v3.12
318 h=document.documentElement.clientHeight;
319 else if(document.body && document.body.clientHeight)
320 h=document.body.clientHeight;
321 else if(xDef(window.innerWidth,window.innerHeight,document.width)) {
322 h=window.innerHeight;
323 if(document.width>window.innerWidth) h-=16;
325 return h;
327 // Animation:
328 function xSlideTo(e,x,y,uTime) {
329 if (!(e=xGetElementById(e))) return;
330 if (!e.timeout) e.timeout = 25;
331 e.xTarget = x; e.yTarget = y; e.slideTime = uTime; e.stop = false;
332 e.yA = e.yTarget - xTop(e); e.xA = e.xTarget - xLeft(e); // A = distance
333 e.B = Math.PI / (2 * e.slideTime); // B = period
334 e.yD = xTop(e); e.xD = xLeft(e); // D = initial position
335 var d = new Date(); e.C = d.getTime();
336 if (!e.moving) xSlide(e);
338 function xSlide(e) {
339 if (!(e=xGetElementById(e))) return;
340 var now, s, t, newY, newX;
341 now = new Date();
342 t = now.getTime() - e.C;
343 if (e.stop) { e.moving = false; }
344 else if (t < e.slideTime) {
345 setTimeout("xSlide('"+e.id+"')", e.timeout);
346 s = Math.sin(e.B * t);
347 newX = Math.round(e.xA * s + e.xD);
348 newY = Math.round(e.yA * s + e.yD);
349 xMoveTo(e, newX, newY);
350 e.moving = true;
352 else {
353 xMoveTo(e, e.xTarget, e.yTarget);
354 e.moving = false;
357 // Event:
358 function xAddEventListener(e,eventType,eventListener,useCapture) {
359 if(!(e=xGetElementById(e))) return;
360 eventType=eventType.toLowerCase();
361 if((!xIE4Up && !xOp7) && e==window) {
362 if(eventType=='resize') { window.xPCW=xClientWidth(); window.xPCH=xClientHeight(); window.xREL=eventListener; xResizeEvent(); return; }
363 if(eventType=='scroll') { window.xPSL=xScrollLeft(); window.xPST=xScrollTop(); window.xSEL=eventListener; xScrollEvent(); return; }
365 var eh='e.on'+eventType+'=eventListener';
366 if(e.addEventListener) e.addEventListener(eventType,eventListener,useCapture);
367 else if(e.attachEvent) e.attachEvent('on'+eventType,eventListener);
368 else if(e.captureEvents) {
369 if(useCapture||(eventType.indexOf('mousemove')!=-1)) { e.captureEvents(eval('Event.'+eventType.toUpperCase())); }
370 eval(eh);
372 else eval(eh);
374 function xRemoveEventListener(e,eventType,eventListener,useCapture) {
375 if(!(e=xGetElementById(e))) return;
376 eventType=eventType.toLowerCase();
377 if((!xIE4Up && !xOp7) && e==window) {
378 if(eventType=='resize') { window.xREL=null; return; }
379 if(eventType=='scroll') { window.xSEL=null; return; }
381 var eh='e.on'+eventType+'=null';
382 if(e.removeEventListener) e.removeEventListener(eventType,eventListener,useCapture);
383 else if(e.detachEvent) e.detachEvent('on'+eventType,eventListener);
384 else if(e.releaseEvents) {
385 if(useCapture||(eventType.indexOf('mousemove')!=-1)) { e.releaseEvents(eval('Event.'+eventType.toUpperCase())); }
386 eval(eh);
388 else eval(eh);
390 function xEvent(evt) { // cross-browser event object prototype
391 this.type = '';
392 this.target = null;
393 this.pageX = 0;
394 this.pageY = 0;
395 this.offsetX = 0;
396 this.offsetY = 0;
397 this.keyCode = 0;
398 var e = evt ? evt : window.event;
399 if(!e) return;
400 if(e.type) this.type = e.type;
401 if(e.target) this.target = e.target;
402 else if(e.srcElement) this.target = e.srcElement;
403 else if(xNN4) this.target = xLayerFromPoint(e.pageX, e.pageY);
404 if(xOp5or6) { this.pageX = e.clientX; this.pageY = e.clientY; }
405 else if(xDef(e.pageX,e.pageY)) { this.pageX = e.pageX; this.pageY = e.pageY; } // v3.14
406 else if(xDef(e.clientX,e.clientY)) { this.pageX = e.clientX + xScrollLeft(); this.pageY = e.clientY + xScrollTop(); }
407 if(xDef(e.offsetX,e.offsetY)) { this.offsetX = e.offsetX; this.offsetY = e.offsetY; }
408 else if(xDef(e.layerX,e.layerY)) { this.offsetX = e.layerX; this.offsetY = e.layerY; }
409 else { this.offsetX = this.pageX - xPageX(this.target); this.offsetY = this.pageY - xPageY(this.target); }
410 if (e.keyCode) { this.keyCode = e.keyCode; } // for moz/fb, if keyCode==0 use which
411 else if (xDef(e.which)) { this.keyCode = e.which; }
413 function xResizeEvent() { // window resize event simulation
414 if (window.xREL) setTimeout('xResizeEvent()', 250);
415 var cw = xClientWidth(), ch = xClientHeight();
416 if (window.xPCW != cw || window.xPCH != ch) { window.xPCW = cw; window.xPCH = ch; if (window.xREL) window.xREL(); }
418 function xScrollEvent() { // window scroll event simulation
419 if (window.xSEL) setTimeout('xScrollEvent()', 250);
420 var sl = xScrollLeft(), st = xScrollTop();
421 if (window.xPSL != sl || window.xPST != st) { window.xPSL = sl; window.xPST = st; if (window.xSEL) window.xSEL(); }
423 // end x.js