Annotation of zogiLib/js/baselib.js, revision 1.13
1.3 casties 1: /* Copyright (C) 2003,2004 IT-Group MPIWG, WTWG Uni Bern and others
2:
1.1 casties 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.3 casties 17: Authors:
18: Christian Luginbuehl, 01.05.2003 (first version)
19: DW 24.03.2004 (Changed for digiLib in Zope)
1.11 casties 20: Robert Casties, 22.1.2008
1.3 casties 21:
1.1 casties 22: */
23:
1.4 casties 24: function base_init() {
25: // init function
1.13 ! casties 26: baseScriptVersion = "1.2.5";
1.4 casties 27: dlParams = new Object();
28: browserType = getBrowserType();
29: }
30:
31:
1.1 casties 32: function getInt(n) {
33: // returns always an integer
1.13 ! casties 34: var n = parseInt(n);
1.1 casties 35: if (isNaN(n)) return 0;
36: return n;
37: }
38:
39: function defined(x) {
40: // returns if x is defined
41: return (typeof arguments[0] != "undefined");
42: }
43:
44: function cropFloat(x) {
1.4 casties 45: // auxiliary function to crop senseless precision
1.1 casties 46: return parseInt(10000*x)/10000;
47: }
48:
1.4 casties 49: function getBrowserType() {
50: // browser sniffer
1.13 ! casties 51: var bt = {
! 52: doDHTML: false,
! 53: versIE: 0
! 54: };
1.4 casties 55:
56: if ((! document.cssonly && document.layers) || document.all || document.getElementById) {
1.5 casties 57: var vers = navigator.appVersion.split('MSIE ');
58: vers = vers[vers.length - 1];
59: bt.versIE = getInt(vers);
60: bt.isIE = navigator.userAgent.indexOf('MSIE') >= 0;
61: bt.isMac = navigator.platform.indexOf('Mac') >= 0;
62: bt.isWin = navigator.platform.indexOf('Win') >= 0;
63: bt.isN4 = (navigator.userAgent.indexOf('Mozilla/4.') >= 0) && ! bt.isIE;
64: bt.isIEWin = bt.versIE > 0 && bt.isWin;
65: if (navigator.appVersion.indexOf('MSIE') < 0 || ! bt.isMac || bt.versIE >= 5) {
66: bt.doDHTML = true;
67: bt.isOpera = navigator.userAgent.indexOf(' Opera ') >= 0;
68: bt.isKonq = navigator.userAgent.indexOf(' Konqueror') >= 0;
69: }
1.1 casties 70: }
1.4 casties 71: return bt;
1.1 casties 72: }
73:
74:
75: /* **********************************************
76: * geometry classes
77: * ******************************************** */
78:
79: /*
80: * Size class
81: */
82: function Size(w, h) {
83: this.width = parseFloat(w);
84: this.height = parseFloat(h);
85: return this;
86: }
1.4 casties 87: Size.prototype.toString = function() {
88: return this.width + "x" + this.height;
89: }
90:
1.1 casties 91:
92: /*
93: * Position class
94: */
95: function Position(x, y) {
96: this.x = parseFloat(x);
97: this.y = parseFloat(y);
98: return this;
99: }
1.4 casties 100: Position.prototype.toString = function() {
101: return this.x + "," + this.y;
102: }
1.1 casties 103:
104: /*
105: * Rectangle class
106: */
107: function Rectangle(x, y, w, h) {
108: this.x = parseFloat(x);
109: this.y = parseFloat(y);
110: this.width = parseFloat(w);
111: this.height = parseFloat(h);
112: return this;
113: }
1.4 casties 114: Rectangle.prototype.toString = function() {
115: return this.width+"x"+this.height+"@"+this.x+","+this.y;
116: }
1.1 casties 117: Rectangle.prototype.copy = function() {
118: // returns a copy of this Rectangle
119: return new Rectangle(this.x, this.y, this.width, this.height);
120: }
121: Rectangle.prototype.getPosition = function() {
122: // returns the position of this Rectangle
123: return new Position(this.x, this.y);
124: }
125: Rectangle.prototype.getSize = function() {
126: // returns the size of this Rectangle
127: return new Size(this.width, this.height);
128: }
129: Rectangle.prototype.getArea = function() {
130: // returns the area of this Rectangle
131: return (this.width * this.height);
132: }
133: Rectangle.prototype.containsPosition = function(pos) {
134: // returns if the given Position lies in this Rectangle
135: return ((pos.x >= this.x)&&(pos.y >= this.y)&&(pos.x <= this.x+this.width)&&(pos.y <= this.y+this.width));
136: }
137: Rectangle.prototype.intersect = function(rect) {
138: // returns the intersection of the given Rectangle and this one
139: var sec = rect.copy();
140: if (sec.x < this.x) {
1.5 casties 141: sec.width = sec.width - (this.x - sec.x);
142: sec.x = this.x;
1.1 casties 143: }
144: if (sec.y < this.y) {
1.5 casties 145: sec.height = sec.height - (this.y - sec.y);
146: sec.y = this.y;
1.1 casties 147: }
148: if (sec.x + sec.width > this.x + this.width) {
1.5 casties 149: sec.width = (this.x + this.width) - sec.x;
1.1 casties 150: }
151: if (sec.y + sec.height > this.y + this.height) {
1.5 casties 152: sec.height = (this.y + this.height) - sec.y;
1.1 casties 153: }
154: return sec;
155: }
156: Rectangle.prototype.fit = function(rect) {
157: // returns a Rectangle that fits into this one (by moving first)
158: var sec = rect.copy();
159: sec.x = Math.max(sec.x, this.x);
160: sec.x = Math.max(sec.x, this.x);
161: if (sec.x + sec.width > this.x + this.width) {
1.5 casties 162: sec.x = this.x + this.width - sec.width;
1.1 casties 163: }
164: if (sec.y + sec.height > this.y + this.height) {
1.5 casties 165: sec.y = this.y + this.height - sec.height;
1.1 casties 166: }
167: return sec.intersect(this);
168: }
169:
170: /*
171: * Transform class
172: *
173: * defines a class of affine transformations
174: */
175: function Transform() {
176: this.m00 = 1.0;
177: this.m01 = 0.0;
178: this.m02 = 0.0;
179: this.m10 = 0.0;
180: this.m11 = 1.0;
181: this.m12 = 0.0;
182: this.m20 = 0.0;
183: this.m21 = 0.0;
184: this.m22 = 1.0;
185: return this;
186: }
187: Transform.prototype.concat = function(traf) {
188: // add Transform traf to this Transform
189: for (var i = 0; i < 3; i++) {
1.5 casties 190: for (var j = 0; j < 3; j++) {
191: var c = 0.0;
192: for (var k = 0; k < 3; k++) {
193: c += traf["m"+i+k] * this["m"+k+j];
194: }
195: this["m"+i+j] = c;
196: }
1.1 casties 197: }
198: return this;
199: }
200: Transform.prototype.transform = function(rect) {
201: // returns transformed Rectangle or Position with this Transform applied
202: var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
203: var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
204: if (rect.width) {
1.5 casties 205: var width = this.m00 * rect.width + this.m01 * rect.height;
206: var height = this.m10 * rect.width + this.m11 * rect.height;
207: return new Rectangle(x, y, width, height);
1.1 casties 208: }
209: return new Position(x, y);
210: }
211: Transform.prototype.invtransform = function(pos) {
212: // returns transformed Position pos with the inverse of this Transform applied
213: var det = this.m00 * this.m11 - this.m01 * this.m10;
214: var x = (this.m11 * pos.x - this.m01 * pos.y - this.m11 * this.m02 + this.m01 * this.m12) / det;
215: var y = (- this.m10 * pos.x + this.m00 * pos.y + this.m10 * this.m02 - this.m00 * this.m12) / det;
216: return new Position(x, y);
217: }
218: function getRotation(angle, pos) {
219: // returns a Transform that is a rotation by angle degrees around [pos.x, pos.y]
220: var traf = new Transform();
221: if (angle != 0) {
1.5 casties 222: var t = 2.0 * Math.PI * parseFloat(angle) / 360.0;
223: traf.m00 = Math.cos(t);
224: traf.m01 = - Math.sin(t);
225: traf.m10 = Math.sin(t);
226: traf.m11 = Math.cos(t);
227: traf.m02 = pos.x - pos.x * Math.cos(t) + pos.y * Math.sin(t);
228: traf.m12 = pos.y - pos.x * Math.sin(t) - pos.y * Math.cos(t);
1.1 casties 229: }
230: return traf;
231: }
232: function getTranslation(pos) {
233: // returns a Transform that is a translation by [pos.x, pos,y]
234: var traf = new Transform();
235: traf.m02 = pos.x;
236: traf.m12 = pos.y;
237: return traf;
238: }
239: function getScale(size) {
240: // returns a Transform that is a scale by [size.width, size.height]
241: var traf = new Transform();
242: traf.m00 = size.width;
243: traf.m11 = size.height;
244: return traf;
245: }
246:
247:
248: /* **********************************************
249: * parameter routines
250: * ******************************************** */
251:
252: function newParameter(name, defaultValue, detail) {
253: // create a new parameter with a name and a default value
254: if (defined(dlParams[name])) {
1.5 casties 255: alert("Fatal: An object with name '" + name + "' already exists - cannot recreate!");
256: return false;
1.1 casties 257: } else {
1.5 casties 258: dlParams[name] = new Object();
259: dlParams[name].defaultValue = defaultValue;
260: dlParams[name].hasValue = false;
261: dlParams[name].value = defaultValue;
262: dlParams[name].detail = detail;
263: return dlParams[name];
1.1 casties 264: }
265: }
266:
267: function getParameter(name) {
268: // returns the named parameter value or its default value
269: if (defined(dlParams[name])) {
1.5 casties 270: if (dlParams[name].hasValue) {
271: return dlParams[name].value;
272: } else {
273: return dlParams[name].defaultValue;
274: }
1.1 casties 275: } else {
1.5 casties 276: return null;
1.1 casties 277: }
278: }
279:
1.6 casties 280: function setParameter(name, value, relative) {
1.5 casties 281: // sets parameter value (relative values with +/- unless literal)
1.1 casties 282: if (defined(dlParams[name])) {
1.6 casties 283: if ((relative)&&(value.slice)) {
1.5 casties 284: var sign = value.slice(0,1);
285: if (sign == '+') {
286: dlParams[name].value = parseFloat(dlParams[name].value) + parseFloat(value.slice(1));
287: } else if (sign == '-') {
288: dlParams[name].value = parseFloat(dlParams[name].value) - parseFloat(value.slice(1));
289: } else {
290: dlParams[name].value = value;
291: }
292: } else {
293: dlParams[name].value = value;
294: }
295: dlParams[name].hasValue = true;
296: return true;
1.1 casties 297: }
298: return false;
299: }
300:
1.4 casties 301: function hasParameter(name) {
302: // returns if the parameter's value has been set
303: if (defined(dlParams[name])) {
1.5 casties 304: return dlParams[name].hasValue;
1.4 casties 305: }
306: return false;
307: }
308:
1.1 casties 309: function getAllParameters(detail) {
1.13 ! casties 310: // returns a string of all (set) parameters in query format
1.1 casties 311: if (! detail) {
1.6 casties 312: detail = 255;
1.1 casties 313: }
314: var params = new Array();
1.13 ! casties 315: for (var p in dlParams) {
! 316: var param = dlParams[p];
! 317: if (((param.detail & detail) > 0)&&(param.hasValue)) {
! 318: var val = param.value;
1.5 casties 319: if (val != "") {
1.13 ! casties 320: params.push(p + "=" + val);
1.5 casties 321: }
322: }
1.1 casties 323: }
324: return params.join("&");
325: }
326:
1.13 ! casties 327: function parseParameters(query,newParamDetail) {
1.1 casties 328: // gets parameter values from query format string
329: var params = query.split("&");
330: for (var i = 0; i < params.length; i++) {
1.5 casties 331: var keyval = params[i].split("=");
332: if (keyval.length == 2) {
1.13 ! casties 333: var ex = setParameter(keyval[0], keyval[1]);
! 334: if ((!ex) && newParamDetail) {
! 335: // setParameter returned false = parameter doesn't exist -- add it
! 336: newParameter(keyval[0], null, newParamDetail);
! 337: setParameter(keyval[0], keyval[1]);
! 338: }
1.5 casties 339: }
1.1 casties 340: }
341: }
342:
343:
344: /* **********************************************
345: * HTML/DOM routines
346: * ******************************************** */
347:
348: function getElement(tagid, quiet) {
349: // returns the element object with the id tagid
350: var e;
351: if (document.getElementById) {
1.5 casties 352: e = document.getElementById(tagid);
1.1 casties 353: } else if (document.all) {
1.5 casties 354: alert("document.all!");
355: e = document.all[tagid];
1.1 casties 356: } else if (document.layers) {
357: e = document.layers[tagid];
358: }
359: if (e) {
1.5 casties 360: return e;
1.1 casties 361: } else {
1.5 casties 362: if (! quiet) {
363: alert("unable to find element: "+tagid);
364: }
365: return null;
1.1 casties 366: }
367: }
368:
369: function getElementPosition(elem) {
370: // returns a Position with the position of the element
371: var x = 0;
372: var y = 0;
1.11 casties 373: if (defined(elem.offsetParent)) {
1.8 casties 374: // use .offsetLeft for most browsers
1.5 casties 375: var e = elem;
376: while (e) {
1.8 casties 377: if (browserType.isIE) {
1.5 casties 378: if (browserType.isMac) {
1.8 casties 379: // IE for Mac extraspecial
1.5 casties 380: if (e.offsetParent.tagName == "BODY") {
381: x += e.clientLeft;
382: y += e.clientTop;
383: break;
384: }
385: } else {
1.8 casties 386: // special for IE
1.5 casties 387: if ((e.tagName != "TABLE") && (e.tagName != "BODY")) {
388: x += e.clientLeft;
389: y += e.clientTop;
390: }
391: }
392: }
393: x += e.offsetLeft;
394: y += e.offsetTop;
395: e = e.offsetParent;
396: }
1.1 casties 397: } else if (defined(elem.x)) {
1.8 casties 398: // use .x for other (which?)
1.5 casties 399: x = elem.x;
400: y = elem.y;
1.1 casties 401: } else if (defined(elem.pageX)) {
1.8 casties 402: // use pageX for N4
1.5 casties 403: x = elem.pageX;
404: y = elem.pageY;
1.1 casties 405: } else {
1.5 casties 406: alert("unable to get position of "+elem+" (id:"+elem.id+")");
1.1 casties 407: }
408: return new Position(getInt(x), getInt(y));
409: }
410:
411: function getElementSize(elem) {
412: // returns a Rectangle with the size of the element
413: var width = 0;
414: var height = 0;
415: if (defined(elem.offsetWidth)) {
1.5 casties 416: width = elem.offsetWidth;
417: height = elem.offsetHeight;
1.1 casties 418: } else if (defined(elem.width)) {
1.5 casties 419: width = elem.width;
420: height = elem.height;
1.1 casties 421: } else if (defined(elem.clip.width)) {
1.5 casties 422: width = elem.clip.width;
423: height = elem.clip.height;
1.1 casties 424: } else {
1.5 casties 425: alert("unable to get size of "+elem+" (id:"+elem.id+")");
1.1 casties 426: }
427: return new Size(getInt(width), getInt(height));
428: }
429:
430: function getElementRect(elem) {
431: // returns a Rectangle with the size and position of the element
432: var pos = getElementPosition(elem);
433: var size = getElementSize(elem);
434: return new Rectangle(pos.x, pos.y, size.width, size.height);
435: }
436:
437: function moveElement(elem, rect) {
438: // moves and sizes the element
439: if (elem.style) {
1.5 casties 440: if (defined(rect.x)) {
441: elem.style.left = Math.round(rect.x) + "px";
442: elem.style.top = Math.round(rect.y) + "px";
443: }
444: if (defined(rect.width)) {
445: elem.style.width = Math.round(rect.width) + "px";
446: elem.style.height = Math.round(rect.height) + "px";
447: }
1.1 casties 448: } else if (document.layers) {
1.5 casties 449: if (defined(rect.x)) {
450: elem.pageX = getInt(rect.x);
451: elem.pageY = getInt(rect.y);
452: }
453: if (defined(rect.width)) {
454: elem.clip.width = getInt(rect.width);
455: elem.clip.height = getInt(rect.height);
456: }
1.1 casties 457: } else {
1.5 casties 458: alert("moveelement: no style nor layer property!");
459: return false;
1.1 casties 460: }
461: return true;
462: }
463:
464: function showElement(elem, show) {
465: // shows or hides the element
466: if (elem.style) {
1.5 casties 467: if (show) {
468: elem.style.visibility = "visible";
469: } else {
470: elem.style.visibility = "hidden";
471: }
1.1 casties 472: } else if (defined(elem.visibility)) {
1.5 casties 473: if (show) {
474: elem.visibility = "show";
475: } else {
476: elem.visibility = "hide";
477: }
1.1 casties 478: } else {
1.5 casties 479: alert("showelement: no style nor layer property!");
1.1 casties 480: }
481: return true;
482: }
483:
1.7 casties 484: function isElementVisible(elem) {
485: // returns of the is shown or hidden
486: if (elem.style) {
487: return (elem.style.visibility == "visible");
488: } else if (defined(elem.visibility)) {
489: return (elem.visibility == "show");
490: } else {
491: alert("iselementvisible: no style nor layer property!");
492: }
493: }
494:
1.1 casties 495: function evtPosition(evt) {
496: // returns the on-screen Position of the Event
497: var x;
498: var y;
499: evt = (evt) ? evt : window.event;
500: if (!evt) {
1.5 casties 501: alert("no event found! "+evt);
502: return;
1.1 casties 503: }
504: if (defined(evt.pageX)) {
1.5 casties 505: x = parseInt(evt.pageX);
506: y = parseInt(evt.pageY);
1.1 casties 507: } else if (defined(evt.clientX)) {
1.5 casties 508: x = parseInt(document.body.scrollLeft+evt.clientX);
509: y = parseInt(document.body.scrollTop+evt.clientY);
1.1 casties 510: } else {
1.5 casties 511: alert("evtPosition: don't know how to deal with "+evt);
1.1 casties 512: }
513: return new Position(x, y);
514: }
515:
1.3 casties 516: function registerEvent(type, elem, handler) {
517: // register the given event handler on the indicated element
1.4 casties 518: if (elem.addEventListener) {
1.5 casties 519: elem.addEventListener(type, handler, false);
1.4 casties 520: } else {
1.5 casties 521: if (type == "mousedown") {
522: if (elem.captureEvents) {
523: elem.captureEvents(Event.MOUSEDOWN);
524: }
525: elem.onmousedown = handler;
526: } else if (type == "mouseup") {
527: if (elem.captureEvents) {
528: elem.captureEvents(Event.MOUSEUP);
529: }
530: elem.onmouseup = handler;
531: } else if (type == "mousemove") {
532: if (elem.captureEvents) {
533: elem.captureEvents(Event.MOUSEMOVE);
534: }
535: elem.onmousemove = handler;
536: } else if (type == "keypress") {
537: if (elem.captureEvents) {
538: elem.captureEvents(Event.KEYPRESS);
539: }
540: elem.onkeypress = handler;
541: } else {
542: alert("registerEvent: unknown event type "+type);
543: return false;
544: }
1.1 casties 545: }
546: return true;
547: }
548:
1.3 casties 549: function unregisterEvent(type, elem, handler) {
550: // unregister the given event handler from the indicated element
1.4 casties 551: if (elem.removeEventListener) {
1.5 casties 552: elem.removeEventListener(type, handler, false);
1.4 casties 553: } else {
1.5 casties 554: if (type == "mousedown") {
555: if (elem.releaseEvents) {
556: elem.releaseEvents(Event.MOUSEDOWN);
557: }
558: elem.onmousedown = null;
559: } else if (type == "mouseup") {
560: if (elem.releaseEvents) {
561: elem.releaseEvents(Event.MOUSEUP);
562: }
563: elem.onmouseup = null;
564: } else if (type == "mousemove") {
565: if (elem.releaseEvents) {
566: elem.releaseEvents(Event.MOUSEMOVE);
567: }
568: elem.onmousemove = null;
569: } else if (type == "keypress") {
570: if (elem.releaseEvents) {
571: elem.releaseEvents(Event.KEYPRESS);
572: }
573: elem.onkeypress = null;
574: } else {
575: alert("unregisterEvent: unknown event type "+type);
576: return false;
577: }
1.1 casties 578: }
579: return true;
580: }
581:
1.3 casties 582:
583: // old registerXXYY API for compatibility
584: function registerMouseDown(elem, handler) {
585: return registerEvent("mousedown", elem, handler);
586: }
587: function unregisterMouseDown(elem, handler) {
588: return unregisterEvent("mousedown", elem, handler);
589: }
1.1 casties 590: function registerMouseMove(elem, handler) {
1.3 casties 591: return registerEvent("mousemove", elem, handler);
1.1 casties 592: }
593: function unregisterMouseMove(elem, handler) {
1.3 casties 594: return unregisterEvent("mousemove", elem, handler);
1.1 casties 595: }
596: function registerKeyDown(handler) {
1.3 casties 597: return registerEvent("keypress", elem, handler);
1.1 casties 598: }
1.3 casties 599:
1.1 casties 600:
601: function getWinSize() {
602: // returns a Size with the current window size (mostly from www.quirksmode.org)
603: var wsize = new Size(100, 100);
1.12 casties 604: if (!browserType.isIE) {
1.5 casties 605: // all except Explorer
606: if ((self.innerWidth == 0)||(self.innerHeight == 0)) {
1.9 casties 607: // Safari 1.2 (and other) bug
1.5 casties 608: if (parent) {
1.9 casties 609: wsize.height = parent.innerHeight;
610: wsize.width = parent.innerWidth;
1.5 casties 611: }
1.9 casties 612: } else {
613: wsize.width = self.innerWidth;
614: wsize.height = self.innerHeight;
1.5 casties 615: }
1.1 casties 616: } else if (document.documentElement && document.documentElement.clientHeight) {
1.5 casties 617: // Explorer 6 Strict Mode
618: wsize.width = document.documentElement.clientWidth;
619: wsize.height = document.documentElement.clientHeight;
1.1 casties 620: } else if (document.body) {
1.5 casties 621: // other Explorers
622: wsize.width = document.body.clientWidth;
623: wsize.height = document.body.clientHeight;
1.1 casties 624: }
625: return wsize;
626: }
627:
1.2 casties 628: function openWin(url, name, params) {
1.1 casties 629: // open browser window
1.2 casties 630: var ow = window.open(url, name, params);
1.1 casties 631: ow.focus();
632: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>