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