Annotation of ECHO_content/js/js_lib.js, revision 1.5
1.1 casties 1: /* Copyright (C) 2003,2004 WTWG, Uni Bern and others
2:
3: This program is free software; you can redistribute it and/or
4: modify it under the terms of the GNU General Public License
5: as published by the Free Software Foundation; either version 2
6: of the License, or (at your option) any later version.
7:
8: This program is distributed in the hope that it will be useful,
9: but WITHOUT ANY WARRANTY; without even the implied warranty of
10: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: GNU General Public License for more details.
12:
13: You should have received a copy of the GNU General Public License
14: along with this program; if not, write to the Free Software
15: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
16:
1.5 ! casties 17: Authors: ROC 03.06.2004
1.1 casties 18: first version by Christian Luginbuehl, 01.05.2003
19: Changed for digiLib in Zope by DW 24.03.2004
20: */
21:
1.5 ! casties 22: function getInt (n) {
! 23: // returns always an integer
! 24: n = parseInt (n);
! 25: if (isNaN (n))
! 26: return 0;
! 27: return n;
! 28: }
! 29:
! 30: function defined(x) {
! 31: // returns if x is defined
! 32: return (typeof arguments[0] != "undefined");
! 33: }
! 34:
! 35: // auxiliary function to crop senseless precision
! 36: function cropFloat(x) {
! 37: return parseInt(10000*x)/10000;
! 38: }
! 39:
1.2 casties 40: // browser sniffer
41: var browserType = Object();
42: browserType.doDHTML = false;
43: browserType.versIE = 0;
44:
45: if ((! document.cssonly && document.layers) || document.all || document.getElementById) {
46: var vers = navigator.appVersion.split('MSIE ');
47: vers = vers[vers.length - 1];
48: browserType.versIE = getInt(vers);
1.5 ! casties 49: browserType.isIE = navigator.userAgent.indexOf('MSIE') >= 0;
1.2 casties 50: browserType.isMac = navigator.platform.indexOf('Mac') >= 0;
51: browserType.isWin = navigator.platform.indexOf('Win') >= 0;
1.5 ! casties 52: browserType.isN4 = (navigator.userAgent.indexOf('Mozilla/4.') >= 0) && ! browserType.isIE;
1.2 casties 53: browserType.isIEWin = browserType.versIE > 0 && browserType.isWin;
54: if (navigator.appVersion.indexOf('MSIE') < 0 || ! browserType.isMac || browserType.versIE >= 5) {
55: browserType.doDHTML = true;
56: browserType.isOpera = navigator.userAgent.indexOf(' Opera ') >= 0;
57: browserType.isKonq = navigator.userAgent.indexOf(' Konqueror') >= 0;
58: }
59: }
60: //alert("browser: dhtml="+browserType.doDHTML+" ie="+browserType.versIE+" mac="+browserType.isMac);
61:
1.1 casties 62: // fixes for javascript < 1.2
63: if (! Array.prototype.push) {
64: Array.prototype.push = function(val) {
65: this[this.length] = val;
66: return this.length;
67: }
68: Array.prototype.pop = function() {
69: var val = this[this.length-1];
70: this.length -= 1;
71: return val;
72: }
73: }
74:
75:
76: /* **********************************************
77: * geometry classes
78: * ******************************************** */
79:
80: /*
81: * Size class
82: */
83: function Size(w, h) {
84: this.width = parseFloat(w);
85: this.height = parseFloat(h);
86: return this;
87: }
88:
89: /*
90: * Position class
91: */
92: function Position(x, y) {
93: this.x = parseFloat(x);
94: this.y = parseFloat(y);
95: return this;
96: }
97:
98: /*
99: * Rectangle class
100: */
101: function Rectangle(x, y, w, h) {
102: this.x = parseFloat(x);
103: this.y = parseFloat(y);
104: this.width = parseFloat(w);
105: this.height = parseFloat(h);
106: return this;
107: }
108: Rectangle.prototype.copy = function() {
109: // returns a copy of this Rectangle
110: return new Rectangle(this.x, this.y, this.width, this.height);
111: }
112: Rectangle.prototype.getPosition = function() {
113: // returns the position of this Rectangle
114: return new Position(this.x, this.y);
115: }
116: Rectangle.prototype.getSize = function() {
117: // returns the size of this Rectangle
118: return new Size(this.width, this.height);
119: }
120: Rectangle.prototype.getArea = function() {
121: // returns the area of this Rectangle
122: return (this.width * this.height);
123: }
124: Rectangle.prototype.containsPosition = function(pos) {
125: // returns if the given Position lies in this Rectangle
126: return ((pos.x >= this.x)&&(pos.y >= this.y)&&(pos.x <= this.x+this.width)&&(pos.y <= this.y+this.width));
127: }
128: Rectangle.prototype.intersect = function(rect) {
129: // returns the intersection of the given Rectangle and this one
130: var sec = rect.copy();
131: if (sec.x < this.x) {
132: sec.width = sec.width - (this.x - sec.x);
133: sec.x = this.x;
134: }
135: if (sec.y < this.y) {
136: sec.height = sec.height - (this.y - sec.y);
137: sec.y = this.y;
138: }
139: if (sec.x + sec.width > this.x + this.width) {
140: sec.width = (this.x + this.width) - sec.x;
141: }
142: if (sec.y + sec.height > this.y + this.height) {
143: sec.height = (this.y + this.height) - sec.y;
144: }
145: return sec;
146: }
147: Rectangle.prototype.fit = function(rect) {
148: // returns a Rectangle that fits into this one (by moving first)
149: var sec = rect.copy();
150: sec.x = Math.max(sec.x, this.x);
151: sec.x = Math.max(sec.x, this.x);
152: if (sec.x + sec.width > this.x + this.width) {
153: sec.x = this.x + this.width - sec.width;
154: }
155: if (sec.y + sec.height > this.y + this.height) {
156: sec.y = this.y + this.height - sec.height;
157: }
158: return sec.intersect(this);
159: }
160:
161: /*
162: * Transform class
163: *
164: * defines a class of affine transformations
165: */
166: function Transform() {
167: this.m00 = 1.0;
168: this.m01 = 0.0;
169: this.m02 = 0.0;
170: this.m10 = 0.0;
171: this.m11 = 1.0;
172: this.m12 = 0.0;
173: this.m20 = 0.0;
174: this.m21 = 0.0;
175: this.m22 = 1.0;
176: return this;
177: }
178: Transform.prototype.concat = function(traf) {
179: // add Transform traf to this Transform
180: for (var i = 0; i < 3; i++) {
181: for (var j = 0; j < 3; j++) {
182: var c = 0.0;
183: for (var k = 0; k < 3; k++) {
184: c += traf["m"+i+k] * this["m"+k+j];
185: }
186: this["m"+i+j] = c;
187: }
188: }
189: return this;
190: }
191: Transform.prototype.transform = function(rect) {
192: // returns transformed Rectangle or Position with this Transform applied
193: var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
194: var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
195: if (rect.width) {
196: var width = this.m00 * rect.width + this.m01 * rect.height;
197: var height = this.m10 * rect.width + this.m11 * rect.height;
198: return new Rectangle(x, y, width, height);
199: }
200: return new Position(x, y);
201: }
202: Transform.prototype.invtransform = function(pos) {
203: // returns transformed Position pos with the inverse of this Transform applied
204: var det = this.m00 * this.m11 - this.m01 * this.m10;
205: var x = (this.m11 * pos.x - this.m01 * pos.y - this.m11 * this.m02 + this.m01 * this.m12) / det;
206: var y = (- this.m10 * pos.x + this.m00 * pos.y + this.m10 * this.m02 - this.m00 * this.m12) / det;
207: return new Position(x, y);
208: }
209: function getRotation(angle, pos) {
210: // returns a Transform that is a rotation by angle degrees around [pos.x, pos.y]
211: var traf = new Transform();
212: if (angle != 0) {
213: var t = 2.0 * Math.PI * parseFloat(angle) / 360.0;
214: traf.m00 = Math.cos(t);
215: traf.m01 = - Math.sin(t);
216: traf.m10 = Math.sin(t);
217: traf.m11 = Math.cos(t);
218: traf.m02 = pos.x - pos.x * Math.cos(t) + pos.y * Math.sin(t);
219: traf.m12 = pos.y - pos.x * Math.sin(t) - pos.y * Math.cos(t);
220: }
221: return traf;
222: }
223: function getTranslation(pos) {
224: // returns a Transform that is a translation by [pos.x, pos,y]
225: var traf = new Transform();
226: traf.m02 = pos.x;
227: traf.m12 = pos.y;
228: return traf;
229: }
230: function getScale(size) {
231: // returns a Transform that is a scale by [size.width, size.height]
232: var traf = new Transform();
233: traf.m00 = size.width;
234: traf.m11 = size.height;
235: return traf;
236: }
237:
238:
239: /* **********************************************
240: * parameter routines
241: * ******************************************** */
242:
243: var dlParams = new Object();
244:
245: function newParameter(name, defaultValue, detail) {
246: // create a new parameter with a name and a default value
1.5 ! casties 247: if (defined(dlParams[name])) {
1.1 casties 248: alert("Fatal: An object with name '" + name + "' already exists - cannot recreate!");
249: return false;
250: } else {
251: dlParams[name] = new Object();
252: dlParams[name].defaultValue = defaultValue;
253: dlParams[name].hasValue = false;
254: dlParams[name].value = defaultValue;
255: dlParams[name].detail = detail;
256: return dlParams[name];
257: }
258: }
259:
260: function getParameter(name) {
261: // returns the named parameter value or its default value
1.5 ! casties 262: if (defined(dlParams[name])) {
1.1 casties 263: if (dlParams[name].hasValue) {
264: return dlParams[name].value;
265: } else {
266: return dlParams[name].defaultValue;
267: }
268: } else {
1.5 ! casties 269: return null;
1.1 casties 270: }
271: }
272:
273: function setParameter(name, value) {
274: // sets parameter value
1.5 ! casties 275: if (defined(dlParams[name])) {
1.1 casties 276: dlParams[name].value = value;
277: dlParams[name].hasValue = true;
278: return true;
279: }
280: return false;
281: }
282:
283: function getAllParameters(detail) {
284: // returns a string of all parameters in query format
285: var params = new Array();
286: for ( param in dlParams ) {
287: if ((dlParams[param].detail <= detail)&&(dlParams[param].hasValue)) {
288: var val = getParameter(param);
289: if (val != "") {
290: params.push(param + "=" + val);
291: }
292: }
293: }
294: return params.join("&");
295: }
296:
297: function parseParameters(query) {
298: // gets parameter values from query format string
299: var params = query.split("&");
300: for (var i = 0; i < params.length; i++) {
301: var keyval = params[i].split("=");
302: if (keyval.length == 2) {
303: setParameter(keyval[0], keyval[1]);
304: }
305: }
306: }
307:
308:
309: /* **********************************************
310: * HTML/DOM routines
311: * ******************************************** */
312:
1.5 ! casties 313: function getElement(tagid, quiet) {
1.1 casties 314: // returns the element object with the id tagid
1.2 casties 315: var e;
1.1 casties 316: if (document.getElementById) {
1.2 casties 317: e = document.getElementById(tagid);
1.1 casties 318: } else if (document.all) {
319: alert("document.all!");
1.2 casties 320: e = document.all[tagid];
321: } else if (document.layers) {
322: e = document.layers[tagid];
323: }
324: if (e) {
325: return e;
1.1 casties 326: } else {
1.5 ! casties 327: if (! quiet) {
! 328: alert("unable to find element: "+tagid);
! 329: }
1.2 casties 330: return null;
1.1 casties 331: }
1.2 casties 332: }
1.1 casties 333:
1.2 casties 334: function getElementPosition(elem) {
335: // returns a Position with the position of the element
1.1 casties 336: var x = 0;
337: var y = 0;
1.5 ! casties 338: if (defined(elem.offsetLeft)) {
1.2 casties 339: var e = elem;
340: while (e) {
1.5 ! casties 341: if (defined(e.clientLeft)) {
1.4 casties 342: // special for IE
1.2 casties 343: if (browserType.isMac) {
344: if (e.offsetParent.tagName == "BODY") {
345: // IE for Mac extraspecial
346: x += e.clientLeft;
347: y += e.clientTop;
348: break;
349: }
350: } else {
351: if ((e.tagName != "TABLE") && (e.tagName != "BODY")) {
352: x += e.clientLeft;
353: y += e.clientTop;
354: }
355: }
356: }
357: x += e.offsetLeft;
358: y += e.offsetTop;
359: e = e.offsetParent;
360: }
1.5 ! casties 361: } else if (defined(elem.x)) {
1.4 casties 362: x = elem.x;
363: y = elem.y;
1.5 ! casties 364: } else if (defined(elem.pageX)) {
1.2 casties 365: x = elem.pageX;
366: y = elem.pageY;
367: } else {
1.3 casties 368: alert("unable to get position of "+elem+" (id:"+elem.id+")");
1.2 casties 369: }
370: return new Position(getInt(x), getInt(y));
371: }
372:
373: function getElementSize(elem) {
374: // returns a Rectangle with the size of the element
1.1 casties 375: var width = 0;
376: var height = 0;
1.5 ! casties 377: if (defined(elem.offsetWidth)) {
1.1 casties 378: width = elem.offsetWidth;
379: height = elem.offsetHeight;
1.5 ! casties 380: } else if (defined(elem.width)) {
! 381: width = elem.width;
! 382: height = elem.height;
! 383: } else if (defined(elem.clip.width)) {
1.2 casties 384: width = elem.clip.width;
385: height = elem.clip.height;
386: } else {
1.3 casties 387: alert("unable to get size of "+elem+" (id:"+elem.id+")");
1.1 casties 388: }
1.2 casties 389: return new Size(getInt(width), getInt(height));
390: }
391:
392: function getElementRect(elem) {
393: // returns a Rectangle with the size and position of the element
394: var pos = getElementPosition(elem);
395: var size = getElementSize(elem);
396: return new Rectangle(pos.x, pos.y, size.width, size.height);
1.1 casties 397: }
398:
1.2 casties 399:
400:
401: function moveElement(elem, rect) {
402: // moves and sizes the element
1.1 casties 403: if (elem.style) {
1.5 ! casties 404: if (defined(rect.x)) {
1.1 casties 405: elem.style.left = Math.round(rect.x) + "px";
406: elem.style.top = Math.round(rect.y) + "px";
407: }
1.5 ! casties 408: if (defined(rect.width)) {
1.1 casties 409: elem.style.width = Math.round(rect.width) + "px";
410: elem.style.height = Math.round(rect.height) + "px";
411: }
1.2 casties 412: } else if (document.layers) {
1.5 ! casties 413: if (defined(rect.x)) {
1.2 casties 414: elem.pageX = getInt(rect.x);
415: elem.pageY = getInt(rect.y);
1.1 casties 416: }
1.5 ! casties 417: if (defined(rect.width)) {
1.2 casties 418: elem.clip.width = getInt(rect.width);
419: elem.clip.height = getInt(rect.height);
1.1 casties 420: }
1.2 casties 421: } else {
1.5 ! casties 422: alert("moveelement: no style nor layer property!");
1.2 casties 423: return false;
1.1 casties 424: }
425: return true;
426: }
427:
1.2 casties 428: function showElement(elem, show) {
429: // shows or hides the element
1.1 casties 430: if (elem.style) {
431: if (show) {
432: elem.style.visibility = "visible";
433: } else {
434: elem.style.visibility = "hidden";
435: }
1.5 ! casties 436: } else if (defined(elem.visibility)) {
! 437: if (show) {
! 438: elem.visibility = "show";
! 439: } else {
! 440: elem.visibility = "hide";
! 441: }
1.1 casties 442: } else {
1.5 ! casties 443: alert("showelement: no style nor layer property!");
1.1 casties 444: }
445: return true;
446: }
447:
448: function evtPosition(evt) {
449: // returns the on-screen Position of the Event
450: var x;
451: var y;
1.5 ! casties 452: evt = (evt) ? evt : window.event;
! 453: if (!evt) {
! 454: alert("no event found! "+evt);
! 455: return;
! 456: }
! 457: if (defined(evt.pageX)) {
1.1 casties 458: x = parseInt(evt.pageX);
459: y = parseInt(evt.pageY);
1.5 ! casties 460: } else if (defined(evt.clientX)) {
! 461: x = parseInt(document.body.scrollLeft+evt.clientX);
! 462: y = parseInt(document.body.scrollTop+evt.clientY);
! 463: } else {
! 464: alert("evtPosition: don't know how to deal with "+evt);
1.1 casties 465: }
466: return new Position(x, y);
467: }
468:
1.5 ! casties 469: function registerMouseDown(elem, handler) {
1.1 casties 470: // register a mouse down event handler on the indicated element
1.5 ! casties 471: if (elem.addEventListener) {
! 472: elem.addEventListener("mousedown", handler, false);
1.1 casties 473: } else {
1.5 ! casties 474: if (elem.captureEvents) {
! 475: elem.captureEvents(Event.MOUSEDOWN);
! 476: }
! 477: elem.onmousedown = handler;
1.1 casties 478: }
479: return true;
480: }
481:
1.5 ! casties 482: function unregisterMouseDown(elem, handler) {
1.1 casties 483: // unregister the mouse down event handler
1.5 ! casties 484: if (elem.removeEventListener) {
! 485: elem.removeEventListener("mousedown", handler, false);
1.1 casties 486: } else {
1.5 ! casties 487: if (elem.releaseEvents) {
! 488: elem.releaseEvents(Event.MOUSEDOWN);
! 489: }
! 490: elem.onmousedown = null;
1.1 casties 491: }
492: return true;
493: }
494:
1.5 ! casties 495: function registerMouseMove(elem, handler) {
1.1 casties 496: // register a mouse move event handler on the indicated element
1.5 ! casties 497: if (elem.addEventListener) {
! 498: elem.addEventListener("mousemove", handler, false);
1.1 casties 499: } else {
1.5 ! casties 500: if (elem.captureEvents) {
! 501: elem.captureEvents(Event.MOUSEMOVE);
! 502: }
! 503: elem.onmousemove = handler;
1.1 casties 504: }
505: return true;
506: }
507:
1.5 ! casties 508: function unregisterMouseMove(elem, handler) {
1.1 casties 509: // unregister the mouse move event handler
1.5 ! casties 510: if (elem.removeEventListener) {
! 511: elem.removeEventListener("mousemove", handler, false);
1.1 casties 512: } else {
1.5 ! casties 513: if (elem.releaseEvents) {
! 514: elem.releaseEvents(Event.MOUSEMOVE);
! 515: }
! 516: elem.onmousemove = null;
1.1 casties 517: }
518: return true;
519: }
520:
521: function registerKeyDown(handler) {
522: // register a key down handler
1.5 ! casties 523: if ( document.addEventListener ) {
! 524: this.document.addEventListener('keypress', handler, false);
! 525: } else {
! 526: if (elem.captureEvents) {
! 527: elem.captureEvents(Event.MOUSEDOWN);
! 528: }
1.1 casties 529: this.document.onkeypress = handler
530: }
531: return true;
532: }
533:
534: function getWinSize() {
535: // returns a Size with the current window size (from www.quirksmode.org)
536: var wsize = new Size(100, 100);
1.5 ! casties 537: if (defined(self.innerHeight)) {
1.1 casties 538: // all except Explorer
539: wsize.width = self.innerWidth;
540: wsize.height = self.innerHeight;
541: } else if (document.documentElement && document.documentElement.clientHeight) {
542: // Explorer 6 Strict Mode
543: wsize.width = document.documentElement.clientWidth;
544: wsize.height = document.documentElement.clientHeight;
545: } else if (document.body) {
546: // other Explorers
547: wsize.width = document.body.clientWidth;
548: wsize.height = document.body.clientHeight;
549: }
550: return wsize;
551: }
552:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>