Annotation of zogiLib/js/baselib.js, revision 1.3

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)
        !            20:   Robert Casties, 03.08.2004
        !            21: 
1.1       casties    22: */
                     23: 
                     24: function getInt(n) {
                     25:     // returns always an integer
                     26:     n = parseInt(n);
                     27:     if (isNaN(n)) return 0;
                     28:     return n;
                     29: }
                     30: 
                     31: function defined(x) {
                     32:     // returns if x is defined
                     33:     return (typeof arguments[0] != "undefined");
                     34: }
                     35: 
                     36: // auxiliary function to crop senseless precision
                     37: function cropFloat(x) {
                     38:     return parseInt(10000*x)/10000;
                     39: }
                     40: 
                     41: // browser sniffer
                     42: var browserType = Object();
                     43: browserType.doDHTML = false;
                     44: browserType.versIE = 0;
                     45: 
                     46: if ((! document.cssonly && document.layers) || document.all || document.getElementById) {
                     47:     var vers = navigator.appVersion.split('MSIE ');
                     48:     vers = vers[vers.length - 1];
                     49:     browserType.versIE = getInt(vers);
                     50:     browserType.isIE = navigator.userAgent.indexOf('MSIE') >= 0;
                     51:     browserType.isMac = navigator.platform.indexOf('Mac') >= 0;
                     52:     browserType.isWin = navigator.platform.indexOf('Win') >= 0;
                     53:     browserType.isN4 = (navigator.userAgent.indexOf('Mozilla/4.') >= 0) && ! browserType.isIE;
                     54:     browserType.isIEWin = browserType.versIE > 0 && browserType.isWin;
                     55:     if (navigator.appVersion.indexOf('MSIE') < 0 || ! browserType.isMac || browserType.versIE >= 5) {
                     56:    browserType.doDHTML = true;
                     57:    browserType.isOpera = navigator.userAgent.indexOf(' Opera ') >= 0;
                     58:    browserType.isKonq = navigator.userAgent.indexOf(' Konqueror') >= 0;
                     59:     }
                     60: }
                     61: 
                     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
                    247:     if (defined(dlParams[name])) {
                    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
                    262:     if (defined(dlParams[name])) {
                    263:    if (dlParams[name].hasValue) {
                    264:        return dlParams[name].value;
                    265:    } else {
                    266:        return dlParams[name].defaultValue;
                    267:    }
                    268:     } else {
                    269:    return null;
                    270:     }
                    271: }
                    272: 
                    273: function setParameter(name, value) {
                    274:     // sets parameter value
                    275:     if (defined(dlParams[name])) {
                    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:     if (! detail) {
                    286:    detail = 10;
                    287:     }
                    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: 
                    316: function getElement(tagid, quiet) {
                    317:     // returns the element object with the id tagid
                    318:     var e;
                    319:     if (document.getElementById) {
                    320:    e = document.getElementById(tagid);
                    321:     } else if (document.all) {
                    322:    alert("document.all!");
                    323:    e = document.all[tagid];
                    324:     } else if (document.layers) {
                    325:         e = document.layers[tagid];
                    326:     } 
                    327:     if (e) {
                    328:    return e;
                    329:     } else {
                    330:    if (! quiet) {
                    331:        alert("unable to find element: "+tagid);
                    332:    }
                    333:    return null;
                    334:     }
                    335: }
                    336: 
                    337: function getElementPosition(elem) {
                    338:     // returns a Position with the position of the element
                    339:     var x = 0;
                    340:     var y = 0;
                    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;
                    367:     } else if (defined(elem.pageX)) {
                    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
                    378:     var width = 0;
                    379:     var height = 0;
                    380:     if (defined(elem.offsetWidth)) {
                    381:    width = elem.offsetWidth;
                    382:    height = elem.offsetHeight;
                    383:     } else if (defined(elem.width)) {
                    384:    width = elem.width;
                    385:    height = elem.height;
                    386:     } else if (defined(elem.clip.width)) {
                    387:    width = elem.clip.width;
                    388:    height = elem.clip.height;
                    389:     } else {
                    390:    alert("unable to get size of "+elem+" (id:"+elem.id+")");
                    391:     }
                    392:     return new Size(getInt(width), getInt(height));
                    393: }
                    394: 
                    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
                    406:     if (elem.style) {
                    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:    }
                    424:     } else {
                    425:        alert("moveelement: no style nor layer property!");
                    426:    return false;
                    427:     }
                    428:     return true;
                    429: }
                    430: 
                    431: function showElement(elem, show) {
                    432:     // shows or hides the element
                    433:     if (elem.style) {
                    434:    if (show) {
                    435:        elem.style.visibility = "visible";
                    436:    } else {
                    437:        elem.style.visibility = "hidden";
                    438:    }
                    439:     } else if (defined(elem.visibility)) {
                    440:    if (show) {
                    441:        elem.visibility = "show";
                    442:    } else {
                    443:        elem.visibility = "hide";
                    444:    }
                    445:     } else {
                    446:    alert("showelement: no style nor layer property!");
                    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;
                    455:     evt = (evt) ? evt : window.event;
                    456:     if (!evt) {
                    457:    alert("no event found! "+evt);
                    458:    return;
                    459:     }
                    460:     if (defined(evt.pageX)) {
                    461:    x = parseInt(evt.pageX);
                    462:    y = parseInt(evt.pageY);
                    463:     } else if (defined(evt.clientX)) {
                    464:    x = parseInt(document.body.scrollLeft+evt.clientX);
                    465:    y = parseInt(document.body.scrollTop+evt.clientY);
                    466:     } else {
                    467:    alert("evtPosition: don't know how to deal with "+evt);
                    468:     }
                    469:     return new Position(x, y);
                    470: }
                    471: 
1.3     ! casties   472: function registerEvent(type, elem, handler) {
        !           473:     // register the given event handler on the indicated element
        !           474:      if (elem.addEventListener) {
        !           475:     elem.addEventListener(type, handler, false);
        !           476:      } else {
        !           477:     if (type = "mousedown") {
        !           478:         if (elem.captureEvents) {
        !           479:         elem.captureEvents(Event.MOUSEDOWN);
        !           480:         }
        !           481:         elem.onmousedown = handler;
        !           482:     } else if (type = "mouseup") {
        !           483:         if (elem.captureEvents) {
        !           484:         elem.captureEvents(Event.MOUSEUP);
        !           485:         }
        !           486:         elem.onmouseup = handler;
        !           487:     } else if (type = "mousemove") {
        !           488:         if (elem.captureEvents) {
        !           489:         elem.captureEvents(Event.MOUSEMOVE);
        !           490:         }
        !           491:         elem.onmousemove = handler;
        !           492:     } else if (type = "keypress") {
        !           493:         if (elem.captureEvents) {
        !           494:         elem.captureEvents(Event.KEYPRESS);
        !           495:         }
        !           496:         elem.onkeypress = handler;
        !           497:     } else {
        !           498:         alert("registerEvent: unknown event type "+type);
        !           499:         return false;
        !           500:     }
1.1       casties   501:     }
                    502:     return true;
                    503: }
                    504: 
1.3     ! casties   505: function unregisterEvent(type, elem, handler) {
        !           506:     // unregister the given event handler from the indicated element
        !           507:      if (elem.removeEventListener) {
        !           508:     elem.removeEventListener(type, handler, false);
        !           509:      } else {
        !           510:     if (type = "mousedown") {
        !           511:         if (elem.releaseEvents) {
        !           512:         elem.releaseEvents(Event.MOUSEDOWN);
        !           513:         }
        !           514:         elem.onmousedown = null;
        !           515:     } else if (type = "mouseup") {
        !           516:         if (elem.releaseEvents) {
        !           517:         elem.releaseEvents(Event.MOUSEUP);
        !           518:         }
        !           519:         elem.onmouseup = null;
        !           520:     } else if (type = "mousemove") {
        !           521:         if (elem.releaseEvents) {
        !           522:         elem.releaseEvents(Event.MOUSEMOVE);
        !           523:         }
        !           524:         elem.onmousemove = null;
        !           525:     } else if (type = "keypress") {
        !           526:         if (elem.releaseEvents) {
        !           527:         elem.releaseEvents(Event.KEYPRESS);
        !           528:         }
        !           529:         elem.onkeypress = null;
        !           530:     } else {
        !           531:         alert("unregisterEvent: unknown event type "+type);
        !           532:         return false;
        !           533:     }
1.1       casties   534:     }
                    535:     return true;
                    536: }
                    537: 
1.3     ! casties   538: 
        !           539: // old registerXXYY API for compatibility
        !           540: function registerMouseDown(elem, handler) {
        !           541:     return registerEvent("mousedown", elem, handler);
        !           542: }
        !           543: function unregisterMouseDown(elem, handler) {
        !           544:     return unregisterEvent("mousedown", elem, handler);
        !           545: }
1.1       casties   546: function registerMouseMove(elem, handler) {
1.3     ! casties   547:     return registerEvent("mousemove", elem, handler);
1.1       casties   548: }
                    549: function unregisterMouseMove(elem, handler) {
1.3     ! casties   550:     return unregisterEvent("mousemove", elem, handler);
1.1       casties   551: }
                    552: function registerKeyDown(handler) {
1.3     ! casties   553:     return registerEvent("keypress", elem, handler);
1.1       casties   554: }
1.3     ! casties   555: 
1.1       casties   556: 
                    557: function getWinSize() {
                    558:     // returns a Size with the current window size (mostly from www.quirksmode.org)
                    559:     var wsize = new Size(100, 100);
                    560:     if (defined(self.innerHeight))  {
                    561:    // all except Explorer
                    562:    wsize.width = self.innerWidth;
                    563:    wsize.height = self.innerHeight;
                    564:     } else if (document.documentElement && document.documentElement.clientHeight) {
                    565:    // Explorer 6 Strict Mode
                    566:    wsize.width = document.documentElement.clientWidth;
                    567:    wsize.height = document.documentElement.clientHeight;
                    568:     } else if (document.body) {
                    569:    // other Explorers
                    570:    wsize.width = document.body.clientWidth;
                    571:    wsize.height = document.body.clientHeight;
                    572:     }
                    573:     return wsize;
                    574: }
                    575: 
1.2       casties   576: function openWin(url, name, params) {
1.1       casties   577:     // open browser window
1.2       casties   578:     var ow = window.open(url, name, params);
1.1       casties   579:     ow.focus();
                    580: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>