Annotation of zogiLib/js/dl_lib.js, revision 1.4

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: 
                     22: 
                     23: /*
                     24:  * parameter handling
                     25:  */
                     26: 
                     27: var dlArea = new Rectangle(0.0, 0.0, 1.0, 1.0);
                     28: var dlMaxArea = new Rectangle(0.0, 0.0, 1.0, 1.0);
                     29: 
                     30: function parseArea() {
                     31:     // returns area Rectangle from current parameters
                     32:     return new Rectangle(getParameter("wx"), getParameter("wy"), getParameter("ww"), getParameter("wh"));
                     33: }
                     34: 
                     35: function setParamFromArea(rect) {
                     36:     // sets digilib wx etc. from rect
                     37:     setParameter("wx", cropFloat(rect.x));
                     38:     setParameter("wy", cropFloat(rect.y));
                     39:     setParameter("ww", cropFloat(rect.width));
                     40:     setParameter("wh", cropFloat(rect.height));
                     41:     return true;
                     42: }
                     43: 
                     44: var dlTrafo = new Transform();
                     45: 
1.2       casties    46: function parseTrafo(elem) {
1.1       casties    47:     // returns Transform from current dlArea and picsize
1.2       casties    48:     var picsize = getElementRect(elem);
1.1       casties    49:     var trafo = new Transform();
                     50:     // subtract area offset and size
                     51:     trafo.concat(getTranslation(new Position(-dlArea.x, -dlArea.y)));
                     52:     trafo.concat(getScale(new Size(1/dlArea.width, 1/dlArea.height)));
                     53:     // scale to screen size
                     54:     trafo.concat(getScale(picsize));
                     55:     trafo.concat(getTranslation(picsize));
                     56:     // rotate
                     57:     //trafo.concat(getRotation(- getParameter("rot"), new Position(0.5*picsize.width, 0.5*picsize.height)));
                     58:     // mirror
                     59:     //if (hasFlag("hmir")) {
                     60:     //trafo.m00 = - trafo.m00;
                     61:     //}
                     62:     //if (hasFlag("vmir")) {
                     63:     //trafo.m11 = - trafo.m11;
                     64:     //}
                     65:     return trafo;
                     66: }
                     67: 
                     68: 
                     69: var dlMarks = new Array();
                     70: 
                     71: function parseMarks() {
                     72:     // returns marks array from current parameters
                     73:     var marks = new Array();
1.2       casties    74:     var ma;
                     75:     var mk = getParameter("mk");
                     76:     if (mk.indexOf(";") >= 0) {
                     77:    // old format with ";"
                     78:    ma = mk.split(";");
                     79:     } else {
                     80:    ma = mk.split(",");
                     81:     }
1.1       casties    82:     for (var i = 0; i < ma.length ; i++) {
                     83:    var pos = ma[i].split("/");
                     84:    if (pos.length > 1) {
                     85:        marks.push(new Position(pos[0], pos[1]));
                     86:    }
                     87:     }
                     88:     return marks;
                     89: }
                     90: 
                     91: function getAllMarks() {
                     92:     // returns a string with all marks in query format
                     93:     var marks = new Array();
                     94:     for (var i = 0; i < dlMarks.length; i++) {
                     95:    marks.push(cropFloat(dlMarks[i].x) + "/" + cropFloat(dlMarks[i].y));
                     96:     }
1.2       casties    97:     return marks.join(",");
1.1       casties    98: }
                     99: 
                    100: function addMark(pos) {
                    101:     // add a mark
                    102:     dlMarks.push(pos);
                    103:     setParameter("mk", getAllMarks());
                    104:     return true;
                    105: }
                    106: 
                    107: function deleteMark() {
                    108:     // delete the last mark
                    109:     dlMarks.pop();
                    110:     setParameter("mk", getAllMarks());
                    111:     return true;
                    112: }
                    113: 
                    114: var dlFlags = new Object();
                    115: 
                    116: function hasFlag(mode) {
                    117:     // returns if mode flag is set
                    118:     return (dlFlags[mode]);
                    119: }
                    120: 
                    121: function addFlag(mode) {
                    122:     // add a mode flag
                    123:     dlFlags[mode] = mode;
                    124:     return true;
                    125: }
                    126: 
                    127: function removeFlag(mode) {
                    128:     // remove a mode flag
                    129:     if (dlFlags[mode]) {
                    130:    delete dlFlags[mode];
                    131:     }
                    132:     return true;
                    133: }
                    134: 
                    135: function toggleFlag(mode) {
                    136:     // change a mode flag
                    137:     if (dlFlags[mode]) {
                    138:    delete dlFlags[mode];
                    139:     } else {
                    140:    dlFlags[mode] = mode;
                    141:     }
                    142:     return true;
                    143: }
                    144: 
                    145: function getAllFlags() {
                    146:     // returns a string with all flags in query format
                    147:     var fa = new Array();
                    148:     for (var f in dlFlags) {
                    149:    if ((f != "")&&(dlFlags[f] != null)) {
                    150:        fa.push(f);
                    151:    }
                    152:     }
                    153:     return fa.join(",");
                    154: }
                    155: 
                    156: function parseFlags() {
                    157:     // sets dlFlags from the current parameters
                    158:     var flags = new Object();
                    159:     var fa = getParameter("mo").split(",");
                    160:     for (var i = 0; i < fa.length ; i++) {
                    161:    var f = fa[i];
                    162:    if (f != "") {
                    163:        flags[f] = f;
                    164:    }
                    165:     }
                    166:     return flags;
                    167: }    
                    168: 
                    169: 
1.2       casties   170: function bestPicSize(elem, inset) {
                    171:     // returns a Size with the best image size for the given element
                    172:     if (! defined(inset)) {
                    173:    inset = 25;
                    174:     }
1.1       casties   175:     var ws = getWinSize();
1.2       casties   176:     var es = getElementPosition(elem);
1.1       casties   177:     if (es) {
                    178:    ws.width = ws.width - es.x - inset;
                    179:    ws.height = ws.height - es.y - inset;
                    180:     }
                    181:     return ws;
                    182: }
                    183: 
                    184: 
                    185: /* **********************************************
                    186:  *     digilib specific routines
                    187:  * ******************************************** */
                    188: 
1.2       casties   189: var elemScaler = null;
                    190: var picElem = null;
                    191: 
                    192: function dl_init() {
                    193:     elemScaler = getElement("scaler");
                    194:     if (! elemScaler) {
1.1       casties   195:    return false;
                    196:     }
1.2       casties   197:     picElem = getElement("pic", true);
                    198:     if (picElem == null) {
1.4     ! casties   199:    // in N4 pic is in the scaler layer
1.2       casties   200:    picElem = elemScaler.document.images[0];
                    201:     }
                    202:     // give a name to the window containing digilib
1.1       casties   203:     top.window.name = "digilib";
1.2       casties   204:     // put the query parameters (sans "?") in the parameters array
1.1       casties   205:     parseParameters(location.search.slice(1));
                    206:     // treat special parameters
                    207:     dlMarks = parseMarks();
                    208:     dlArea = parseArea();
                    209:     dlFlags = parseFlags();
1.2       casties   210:     // wait for image to load and display marks
1.1       casties   211:     renderMarks();
1.2       casties   212:     // done
1.1       casties   213:     focus();
1.4     ! casties   214:     return;
1.1       casties   215: }
                    216: 
                    217: function display(detail) {
                    218:     // redisplay the page
1.2       casties   219:     if (! detail) {
                    220:    detail = 9;
                    221:     }
1.1       casties   222:     var queryString = getAllParameters(detail);
                    223:     location.href = location.protocol + "//" + location.host + location.pathname + "?" + queryString;
                    224: }
                    225: 
1.2       casties   226: function openWin(url, title, params) {
                    227:     // open browser window
                    228:     var ow = window.open(url, title, params);
                    229:     ow.focus();
                    230: }
1.1       casties   231: 
                    232: /* **********************************************
                    233:  *     interactive digilib functions
                    234:  * ******************************************** */
                    235: 
1.2       casties   236: function getRef(select) {
1.1       casties   237:     // open a dialog with a reference to the current digilib set
1.3       casties   238:     if (! baseUrl) {
                    239:    var baseUrl = location.protocol + "//" + location.host + location.pathname;
                    240:     }
                    241:     var hyperlinkRef = baseUrl;
                    242:     var par = getAllParameters(9);
                    243:     if (par.length > 0) {
                    244:    hyperlinkRef += "?" + par;
                    245:     }
1.1       casties   246:     if ( select == 0 ) {
                    247:    prompt("Link for LaTeX-documents", "\\href{" + hyperlinkRef + "}{TEXT}");
                    248:     } else if ( select == 1 ) {
                    249:    prompt("Link for HTML-documents", hyperlinkRef);
                    250:     }
                    251: }
                    252: 
                    253: function renderMarks() {
                    254:     // put the visible marks on the image
                    255:     var mark_count = dlMarks.length;
                    256:     // make shure the image is loaded so we know its size
1.2       casties   257:     if (defined(picElem.complete) && picElem.complete == false && ! browserType.isN4 ) {
                    258:    setTimeout("renderMarks()", 100);
1.1       casties   259:     } else {
1.2       casties   260:    dlTrafo = parseTrafo(picElem);
1.1       casties   261:    for (var i = 0; i < 8; i++) {
1.2       casties   262:        var me = getElement("dot"+i);
1.1       casties   263:        if (i < mark_count) {
                    264:        if (dlArea.containsPosition(dlMarks[i])) {
                    265:            var mpos = dlTrafo.transform(dlMarks[i]);
                    266:            // suboptimal to place -5 pixels and not half size of mark-image
                    267:            mpos.x = mpos.x -5;
                    268:            mpos.y = mpos.y -5;
1.2       casties   269:            moveElement(me, mpos);
                    270:            showElement(me, true);
1.1       casties   271:        }
                    272:        } else {
                    273:        // hide the other marks
1.2       casties   274:        showElement(me, false);
1.1       casties   275:        }
                    276:    }
                    277:     }
                    278: }
                    279: 
1.2       casties   280: function setMark() {
1.1       casties   281:     // add a mark where clicked
                    282:     if ( dlMarks.length > 7 ) {
                    283:    alert("Only 8 marks are possible at the moment!");
                    284:    return;
                    285:     }
                    286: 
                    287:     function markEvent(evt) {
1.4     ! casties   288:    // event handler adding a new mark
        !           289:    unregisterMouseDown(elemScaler, markEvent);
1.1       casties   290:    var p = dlTrafo.invtransform(evtPosition(evt));
                    291:    addMark(p);
1.2       casties   292:    display();
1.1       casties   293:     }
                    294: 
                    295:     // starting event capture
1.2       casties   296:     registerMouseDown(elemScaler, markEvent);
1.1       casties   297: }
                    298: 
                    299: var ZOOMFACTOR = Math.sqrt(2);
                    300: 
                    301: function zoomPoint(inout) {
                    302:     // zoom image in or out around the clicked point
                    303:     var zoom = ZOOMFACTOR;
                    304:     if (inout < 0) {
                    305:    zoom = 1/ZOOMFACTOR;
                    306:     }
                    307:     window.focus();
                    308: 
                    309:     function zoomPointEvent(evt) {
                    310:    // take new center and set zoom parameters
1.4     ! casties   311:    unregisterMouseDown(elemScaler, zoomPointEvent);
1.1       casties   312:    var p = dlTrafo.invtransform(evtPosition(evt));
                    313:    var neww = Math.min(dlArea.width * (1/zoom), 1.0);
                    314:    var newh = Math.min(dlArea.height * (1/zoom), 1.0);
                    315:    var newx = p.x - 0.5 * neww;
                    316:    var newy = p.y - 0.5 * newh;
                    317:    var zoomarea = new Rectangle(newx, newy, neww, newh);
                    318:    // check bounds
                    319:    zoomarea = dlMaxArea.fit(zoomarea);
                    320:    // set parameters
                    321:    setParamFromArea(zoomarea);
                    322:    parseArea();
1.2       casties   323:    // zoomed is always fit
                    324:    setParameter("ws", 1);
                    325:    display();
1.1       casties   326:     }
                    327: 
                    328:     // starting event capture
1.2       casties   329:     registerMouseDown(elemScaler, zoomPointEvent);
1.1       casties   330: }
                    331: 
                    332: 
                    333: function zoomArea() {
                    334:     var click = 1;
                    335:     var pt1, pt2;
                    336:     var eck1pos, eck2pos, eck3pos, eck4pos;
                    337:     window.focus();
1.2       casties   338:     var eck1 = getElement("eck1");
                    339:     var eck2 = getElement("eck2");
                    340:     var eck3 = getElement("eck3");
                    341:     var eck4 = getElement("eck4");
1.1       casties   342: 
                    343:     function zoomClick(evt) {
                    344:    // mouse click handler
                    345:    if (click == 1) {
                    346:        // first click -- start moving
                    347:        click = 2;
                    348:        pt1 = evtPosition(evt);
                    349:        pt2 = pt1;
                    350:        eck1pos = pt1;
                    351:        eck2pos = new Position(pt1.x - 12, pt1.y);
                    352:        eck3pos = new Position(pt1.x, pt1.y - 12);
                    353:        eck4pos = new Position(pt1.y - 12, pt1.y - 12);
1.2       casties   354:        moveElement(eck1, eck1pos);
                    355:        moveElement(eck2, eck2pos);
                    356:        moveElement(eck3, eck3pos);
                    357:        moveElement(eck4, eck4pos);
                    358:        showElement(eck1, true);
                    359:        showElement(eck2, true);
                    360:        showElement(eck3, true);
                    361:        showElement(eck4, true);
                    362:        registerMouseMove(elemScaler, zoomMove);
                    363:        registerMouseMove(eck4, zoomMove);
1.1       casties   364:    } else {
                    365:        // second click -- end moving
                    366:        pt2 = evtPosition(evt);
1.2       casties   367:        showElement(eck1, false);
                    368:        showElement(eck2, false);
                    369:        showElement(eck3, false);
                    370:        showElement(eck4, false);
                    371:        unregisterMouseMove(elemScaler, zoomMove);
                    372:        unregisterMouseMove(eck4, zoomMove);
                    373:        unregisterMouseDown(elemScaler, zoomClick);
                    374:        unregisterMouseDown(eck4, zoomClick);
1.1       casties   375:        var p1 = dlTrafo.invtransform(pt1);
                    376:        var p2 = dlTrafo.invtransform(pt2);
                    377:        var ww = p2.x-p1.x;
                    378:        var wh = p2.y-p1.y;
                    379:        if ((ww > 0)&&(wh > 0)) {
                    380:        setParameter("wx", cropFloat(p1.x));
                    381:        setParameter("wy", cropFloat(p1.y));
                    382:        setParameter("ww", cropFloat(ww));
                    383:        setParameter("wh", cropFloat(wh));
                    384:        parseArea();
1.2       casties   385:        // zoomed is always fit
                    386:        setParameter("ws", 1);
                    387:        display();
1.1       casties   388:        }
                    389:    }
                    390:     }
                    391: 
                    392:     function zoomMove(evt) {
                    393:    // mouse move handler
                    394:    pt2 = evtPosition(evt);
                    395:    // restrict marks to move right and down
                    396:    eck1pos = pt1;
                    397:    eck2pos = new Position(Math.max(pt1.x, pt2.x)-12, pt1.y);
                    398:    eck3pos = new Position(pt1.x, Math.max(pt1.y, pt2.y)-12);
                    399:    eck4pos = new Position(Math.max(pt1.x, pt2.x)-12, Math.max(pt1.y, pt2.y)-12);
1.2       casties   400:    moveElement(eck1, eck1pos);
                    401:    moveElement(eck2, eck2pos);
                    402:    moveElement(eck3, eck3pos);
                    403:    moveElement(eck4, eck4pos);
1.1       casties   404:     }
                    405: 
                    406:     // starting event capture
1.2       casties   407:     registerMouseDown(elemScaler, zoomClick);
                    408:     registerMouseDown(eck4, zoomClick);
1.1       casties   409: }
                    410: 
                    411: 
1.4     ! casties   412: function moveCenter() {
        !           413:     // move visible area so that it's centered around the clicked point
1.1       casties   414:     if ( (dlArea.width == 1.0) && (dlArea.height == 1.0) ) {
1.4     ! casties   415:    // noting to do
1.1       casties   416:    return;
                    417:     }
1.4     ! casties   418:     window.focus();
1.1       casties   419: 
1.4     ! casties   420:     function moveCenterEvent(evt) {
1.1       casties   421:    // move to handler
1.4     ! casties   422:    unregisterMouseDown(elemScaler, moveCenterEvent);
        !           423:    var pt = dlTrafo.invtransform(evtPosition(evt));
        !           424:    alert("pt: "+pt.x+", "+pt.y);
1.1       casties   425:    var newarea = new Rectangle(pt.x-0.5*dlArea.width, pt.y-0.5*dlArea.height, dlArea.width, dlArea.height);
                    426:    newarea = dlMaxArea.fit(newarea);
                    427:    // set parameters
1.4     ! casties   428:    setParamFromArea(newarea);
        !           429:    parseArea();
1.2       casties   430:    display();
1.1       casties   431:     }
                    432: 
                    433:     // starting event capture
1.4     ! casties   434:     registerMouseDown(elemScaler, moveCenterEvent);
1.1       casties   435: }
                    436: 
                    437: 
1.2       casties   438: // debuggin'
                    439: function showcoordsN4() {
                    440:     var s = "";
                    441:     for (var l in document.layers) {
                    442:    if (l == "length") continue;
                    443:    e = document.layers[l];
                    444:    if (e) {
                    445:        s += " [" + e.name + "]: pageX:" + e.pageX + " pageY:" + e.pageY + " width:" + e.clip.width + " height:" + e.clip.height + " visibility:" + e.visibility + " zindex:" + e.zIndex + "<br>\n";
                    446:    } else {
                    447:        s += " {" + l + "}<br>\n";
1.1       casties   448:    }
                    449:     }
1.2       casties   450:     return s;
                    451: }

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