Annotation of zogiLib/js/js_lib.js, revision 1.7
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.4 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.2 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:
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.4 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.4 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: }
1.2 casties 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: }
1.1 casties 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: }
1.2 casties 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: }
1.1 casties 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.2 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.2 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.2 casties 269: return null;
1.1 casties 270: }
271: }
272:
273: function setParameter(name, value) {
274: // sets parameter value
1.2 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
1.7 ! casties 285: if (! detail) {
! 286: detail = 10;
! 287: }
1.1 casties 288: var params = new Array();
289: for ( param in dlParams ) {
290: if ((dlParams[param].detail <= detail)&&(dlParams[param].hasValue)) {
291: var val = getParameter(param);
292: if (val != "") {
293: params.push(param + "=" + val);
294: }
295: }
296: }
297: return params.join("&");
298: }
299:
300: function parseParameters(query) {
301: // gets parameter values from query format string
302: var params = query.split("&");
303: for (var i = 0; i < params.length; i++) {
304: var keyval = params[i].split("=");
305: if (keyval.length == 2) {
306: setParameter(keyval[0], keyval[1]);
307: }
308: }
309: }
310:
311:
312: /* **********************************************
313: * HTML/DOM routines
314: * ******************************************** */
315:
1.2 casties 316: function getElement(tagid, quiet) {
1.1 casties 317: // returns the element object with the id tagid
1.2 casties 318: var e;
1.1 casties 319: if (document.getElementById) {
1.2 casties 320: e = document.getElementById(tagid);
1.1 casties 321: } else if (document.all) {
322: alert("document.all!");
1.2 casties 323: e = document.all[tagid];
324: } else if (document.layers) {
325: e = document.layers[tagid];
326: }
327: if (e) {
328: return e;
1.1 casties 329: } else {
1.2 casties 330: if (! quiet) {
331: alert("unable to find element: "+tagid);
1.1 casties 332: }
1.2 casties 333: return null;
1.1 casties 334: }
1.2 casties 335: }
1.1 casties 336:
1.2 casties 337: function getElementPosition(elem) {
338: // returns a Position with the position of the element
1.1 casties 339: var x = 0;
340: var y = 0;
1.2 casties 341: if (defined(elem.offsetLeft)) {
342: var e = elem;
343: while (e) {
344: if (defined(e.clientLeft)) {
345: // special for IE
346: if (browserType.isMac) {
347: if (e.offsetParent.tagName == "BODY") {
348: // IE for Mac extraspecial
349: x += e.clientLeft;
350: y += e.clientTop;
351: break;
352: }
353: } else {
354: if ((e.tagName != "TABLE") && (e.tagName != "BODY")) {
355: x += e.clientLeft;
356: y += e.clientTop;
357: }
358: }
359: }
360: x += e.offsetLeft;
361: y += e.offsetTop;
362: e = e.offsetParent;
363: }
364: } else if (defined(elem.x)) {
365: x = elem.x;
366: y = elem.y;
1.4 casties 367: } else if (defined(elem.pageX)) {
1.2 casties 368: x = elem.pageX;
369: y = elem.pageY;
370: } else {
371: alert("unable to get position of "+elem+" (id:"+elem.id+")");
372: }
373: return new Position(getInt(x), getInt(y));
374: }
375:
376: function getElementSize(elem) {
377: // returns a Rectangle with the size of the element
1.1 casties 378: var width = 0;
379: var height = 0;
1.2 casties 380: if (defined(elem.offsetWidth)) {
1.1 casties 381: width = elem.offsetWidth;
382: height = elem.offsetHeight;
1.4 casties 383: } else if (defined(elem.width)) {
384: width = elem.width;
385: height = elem.height;
386: } else if (defined(elem.clip.width)) {
1.2 casties 387: width = elem.clip.width;
388: height = elem.clip.height;
389: } else {
390: alert("unable to get size of "+elem+" (id:"+elem.id+")");
1.1 casties 391: }
1.2 casties 392: return new Size(getInt(width), getInt(height));
1.1 casties 393: }
394:
1.2 casties 395: function getElementRect(elem) {
396: // returns a Rectangle with the size and position of the element
397: var pos = getElementPosition(elem);
398: var size = getElementSize(elem);
399: return new Rectangle(pos.x, pos.y, size.width, size.height);
400: }
401:
402:
403:
404: function moveElement(elem, rect) {
405: // moves and sizes the element
1.1 casties 406: if (elem.style) {
1.2 casties 407: if (defined(rect.x)) {
408: elem.style.left = Math.round(rect.x) + "px";
409: elem.style.top = Math.round(rect.y) + "px";
410: }
411: if (defined(rect.width)) {
412: elem.style.width = Math.round(rect.width) + "px";
413: elem.style.height = Math.round(rect.height) + "px";
414: }
415: } else if (document.layers) {
416: if (defined(rect.x)) {
417: elem.pageX = getInt(rect.x);
418: elem.pageY = getInt(rect.y);
419: }
420: if (defined(rect.width)) {
421: elem.clip.width = getInt(rect.width);
422: elem.clip.height = getInt(rect.height);
423: }
1.1 casties 424: } else {
1.2 casties 425: alert("moveelement: no style nor layer property!");
426: return false;
1.1 casties 427: }
428: return true;
429: }
430:
1.2 casties 431: function showElement(elem, show) {
432: // shows or hides the element
1.1 casties 433: if (elem.style) {
434: if (show) {
435: elem.style.visibility = "visible";
436: } else {
437: elem.style.visibility = "hidden";
438: }
1.4 casties 439: } else if (defined(elem.visibility)) {
440: if (show) {
441: elem.visibility = "show";
442: } else {
443: elem.visibility = "hide";
444: }
1.1 casties 445: } else {
1.4 casties 446: alert("showelement: no style nor layer property!");
1.1 casties 447: }
448: return true;
449: }
450:
451: function evtPosition(evt) {
452: // returns the on-screen Position of the Event
453: var x;
454: var y;
1.2 casties 455: evt = (evt) ? evt : window.event;
456: if (!evt) {
457: alert("no event found! "+evt);
458: return;
459: }
460: if (defined(evt.pageX)) {
1.1 casties 461: x = parseInt(evt.pageX);
462: y = parseInt(evt.pageY);
1.2 casties 463: } else if (defined(evt.clientX)) {
464: x = parseInt(document.body.scrollLeft+evt.clientX);
1.3 casties 465: y = parseInt(document.body.scrollTop+evt.clientY);
1.2 casties 466: } else {
467: alert("evtPosition: don't know how to deal with "+evt);
1.1 casties 468: }
469: return new Position(x, y);
470: }
471:
1.2 casties 472: function registerMouseDown(elem, handler) {
1.1 casties 473: // register a mouse down event handler on the indicated element
1.2 casties 474: if (elem.addEventListener) {
475: elem.addEventListener("mousedown", handler, false);
1.1 casties 476: } else {
1.4 casties 477: if (elem.captureEvents) {
478: elem.captureEvents(Event.MOUSEDOWN);
479: }
1.2 casties 480: elem.onmousedown = handler;
1.1 casties 481: }
482: return true;
483: }
484:
1.2 casties 485: function unregisterMouseDown(elem, handler) {
1.1 casties 486: // unregister the mouse down event handler
1.2 casties 487: if (elem.removeEventListener) {
488: elem.removeEventListener("mousedown", handler, false);
1.1 casties 489: } else {
1.4 casties 490: if (elem.releaseEvents) {
491: elem.releaseEvents(Event.MOUSEDOWN);
492: }
1.2 casties 493: elem.onmousedown = null;
1.1 casties 494: }
495: return true;
496: }
497:
1.5 casties 498: function registerMouseUp(elem, handler) {
499: // register a mouse up event handler on the indicated element
500: if (elem.addEventListener) {
501: elem.addEventListener("mouseup", handler, false);
502: } else {
503: if (elem.captureEvents) {
504: elem.captureEvents(Event.MOUSEUP);
505: }
506: elem.onmouseup = handler;
507: }
508: return true;
509: }
510:
511: function unregisterMouseUp(elem, handler) {
512: // unregister the mouse up event handler
513: if (elem.removeEventListener) {
514: elem.removeEventListener("mouseup", handler, false);
515: } else {
516: if (elem.releaseEvents) {
517: elem.releaseEvents(Event.MOUSEUP);
518: }
519: elem.onmouseup = null;
520: }
521: return true;
522: }
523:
1.2 casties 524: function registerMouseMove(elem, handler) {
1.1 casties 525: // register a mouse move event handler on the indicated element
1.2 casties 526: if (elem.addEventListener) {
527: elem.addEventListener("mousemove", handler, false);
1.1 casties 528: } else {
1.4 casties 529: if (elem.captureEvents) {
530: elem.captureEvents(Event.MOUSEMOVE);
531: }
1.2 casties 532: elem.onmousemove = handler;
1.1 casties 533: }
534: return true;
535: }
536:
1.2 casties 537: function unregisterMouseMove(elem, handler) {
1.1 casties 538: // unregister the mouse move event handler
1.2 casties 539: if (elem.removeEventListener) {
540: elem.removeEventListener("mousemove", handler, false);
1.1 casties 541: } else {
1.4 casties 542: if (elem.releaseEvents) {
543: elem.releaseEvents(Event.MOUSEMOVE);
544: }
1.2 casties 545: elem.onmousemove = null;
1.1 casties 546: }
547: return true;
548: }
549:
550: function registerKeyDown(handler) {
551: // register a key down handler
1.2 casties 552: if ( document.addEventListener ) {
553: this.document.addEventListener('keypress', handler, false);
554: } else {
1.4 casties 555: if (elem.captureEvents) {
556: elem.captureEvents(Event.MOUSEDOWN);
557: }
1.1 casties 558: this.document.onkeypress = handler
559: }
560: return true;
561: }
562:
563: function getWinSize() {
564: // returns a Size with the current window size (from www.quirksmode.org)
565: var wsize = new Size(100, 100);
1.2 casties 566: if (defined(self.innerHeight)) {
1.1 casties 567: // all except Explorer
568: wsize.width = self.innerWidth;
569: wsize.height = self.innerHeight;
570: } else if (document.documentElement && document.documentElement.clientHeight) {
571: // Explorer 6 Strict Mode
572: wsize.width = document.documentElement.clientWidth;
573: wsize.height = document.documentElement.clientHeight;
574: } else if (document.body) {
575: // other Explorers
576: wsize.width = document.body.clientWidth;
577: wsize.height = document.body.clientHeight;
578: }
579: return wsize;
580: }
581:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>