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