Annotation of ECHO_content/js/js_lib.js, revision 1.3

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

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