Annotation of zogiLib/js/dl_lib.js, revision 1.7
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() {
1.7 ! casties 193: elemScaler = getElement("scaler", true);
1.2 casties 194: picElem = getElement("pic", true);
1.7 ! casties 195: if (picElem == null && elemScaler) {
1.4 casties 196: // in N4 pic is in the scaler layer
1.2 casties 197: picElem = elemScaler.document.images[0];
1.6 casties 198: }
199: if ((!elemScaler)||(!picElem)) {
200: alert("Sorry, zogilib doesn't work here!");
201: return false;
1.2 casties 202: }
203: // give a name to the window containing digilib
1.1 casties 204: top.window.name = "digilib";
1.2 casties 205: // put the query parameters (sans "?") in the parameters array
1.1 casties 206: parseParameters(location.search.slice(1));
207: // treat special parameters
208: dlMarks = parseMarks();
209: dlArea = parseArea();
210: dlFlags = parseFlags();
1.2 casties 211: // wait for image to load and display marks
1.1 casties 212: renderMarks();
1.2 casties 213: // done
1.1 casties 214: focus();
1.4 casties 215: return;
1.1 casties 216: }
217:
218: function display(detail) {
219: // redisplay the page
1.2 casties 220: if (! detail) {
221: detail = 9;
222: }
1.1 casties 223: var queryString = getAllParameters(detail);
224: location.href = location.protocol + "//" + location.host + location.pathname + "?" + queryString;
225: }
226:
1.2 casties 227: function openWin(url, title, params) {
228: // open browser window
229: var ow = window.open(url, title, params);
230: ow.focus();
231: }
1.1 casties 232:
233: /* **********************************************
234: * interactive digilib functions
235: * ******************************************** */
236:
1.2 casties 237: function getRef(select) {
1.1 casties 238: // open a dialog with a reference to the current digilib set
1.3 casties 239: if (! baseUrl) {
240: var baseUrl = location.protocol + "//" + location.host + location.pathname;
241: }
242: var hyperlinkRef = baseUrl;
243: var par = getAllParameters(9);
244: if (par.length > 0) {
245: hyperlinkRef += "?" + par;
246: }
1.1 casties 247: if ( select == 0 ) {
248: prompt("Link for LaTeX-documents", "\\href{" + hyperlinkRef + "}{TEXT}");
249: } else if ( select == 1 ) {
250: prompt("Link for HTML-documents", hyperlinkRef);
251: }
252: }
253:
254: function renderMarks() {
255: // put the visible marks on the image
256: var mark_count = dlMarks.length;
257: // make shure the image is loaded so we know its size
1.2 casties 258: if (defined(picElem.complete) && picElem.complete == false && ! browserType.isN4 ) {
259: setTimeout("renderMarks()", 100);
1.1 casties 260: } else {
1.2 casties 261: dlTrafo = parseTrafo(picElem);
1.1 casties 262: for (var i = 0; i < 8; i++) {
1.2 casties 263: var me = getElement("dot"+i);
1.1 casties 264: if (i < mark_count) {
265: if (dlArea.containsPosition(dlMarks[i])) {
266: var mpos = dlTrafo.transform(dlMarks[i]);
267: // suboptimal to place -5 pixels and not half size of mark-image
268: mpos.x = mpos.x -5;
269: mpos.y = mpos.y -5;
1.2 casties 270: moveElement(me, mpos);
271: showElement(me, true);
1.1 casties 272: }
273: } else {
274: // hide the other marks
1.2 casties 275: showElement(me, false);
1.1 casties 276: }
277: }
278: }
279: }
280:
1.2 casties 281: function setMark() {
1.1 casties 282: // add a mark where clicked
283: if ( dlMarks.length > 7 ) {
284: alert("Only 8 marks are possible at the moment!");
285: return;
286: }
287:
288: function markEvent(evt) {
1.4 casties 289: // event handler adding a new mark
290: unregisterMouseDown(elemScaler, markEvent);
1.1 casties 291: var p = dlTrafo.invtransform(evtPosition(evt));
292: addMark(p);
1.2 casties 293: display();
1.1 casties 294: }
295:
296: // starting event capture
1.2 casties 297: registerMouseDown(elemScaler, markEvent);
1.1 casties 298: }
299:
300: var ZOOMFACTOR = Math.sqrt(2);
301:
302: function zoomPoint(inout) {
303: // zoom image in or out around the clicked point
304: var zoom = ZOOMFACTOR;
305: if (inout < 0) {
306: zoom = 1/ZOOMFACTOR;
307: }
308: window.focus();
309:
310: function zoomPointEvent(evt) {
311: // take new center and set zoom parameters
1.4 casties 312: unregisterMouseDown(elemScaler, zoomPointEvent);
1.1 casties 313: var p = dlTrafo.invtransform(evtPosition(evt));
314: var neww = Math.min(dlArea.width * (1/zoom), 1.0);
315: var newh = Math.min(dlArea.height * (1/zoom), 1.0);
316: var newx = p.x - 0.5 * neww;
317: var newy = p.y - 0.5 * newh;
318: var zoomarea = new Rectangle(newx, newy, neww, newh);
319: // check bounds
320: zoomarea = dlMaxArea.fit(zoomarea);
321: // set parameters
322: setParamFromArea(zoomarea);
323: parseArea();
1.2 casties 324: // zoomed is always fit
325: setParameter("ws", 1);
326: display();
1.1 casties 327: }
328:
329: // starting event capture
1.2 casties 330: registerMouseDown(elemScaler, zoomPointEvent);
1.1 casties 331: }
332:
333:
334: function zoomArea() {
335: var click = 1;
336: var pt1, pt2;
337: var eck1pos, eck2pos, eck3pos, eck4pos;
338: window.focus();
1.2 casties 339: var eck1 = getElement("eck1");
340: var eck2 = getElement("eck2");
341: var eck3 = getElement("eck3");
342: var eck4 = getElement("eck4");
1.1 casties 343:
344: function zoomClick(evt) {
345: // mouse click handler
346: if (click == 1) {
347: // first click -- start moving
348: click = 2;
349: pt1 = evtPosition(evt);
350: pt2 = pt1;
351: eck1pos = pt1;
352: eck2pos = new Position(pt1.x - 12, pt1.y);
353: eck3pos = new Position(pt1.x, pt1.y - 12);
354: eck4pos = new Position(pt1.y - 12, pt1.y - 12);
1.2 casties 355: moveElement(eck1, eck1pos);
356: moveElement(eck2, eck2pos);
357: moveElement(eck3, eck3pos);
358: moveElement(eck4, eck4pos);
359: showElement(eck1, true);
360: showElement(eck2, true);
361: showElement(eck3, true);
362: showElement(eck4, true);
363: registerMouseMove(elemScaler, zoomMove);
364: registerMouseMove(eck4, zoomMove);
1.1 casties 365: } else {
366: // second click -- end moving
367: pt2 = evtPosition(evt);
1.2 casties 368: showElement(eck1, false);
369: showElement(eck2, false);
370: showElement(eck3, false);
371: showElement(eck4, false);
372: unregisterMouseMove(elemScaler, zoomMove);
373: unregisterMouseMove(eck4, zoomMove);
374: unregisterMouseDown(elemScaler, zoomClick);
375: unregisterMouseDown(eck4, zoomClick);
1.1 casties 376: var p1 = dlTrafo.invtransform(pt1);
377: var p2 = dlTrafo.invtransform(pt2);
378: var ww = p2.x-p1.x;
379: var wh = p2.y-p1.y;
380: if ((ww > 0)&&(wh > 0)) {
381: setParameter("wx", cropFloat(p1.x));
382: setParameter("wy", cropFloat(p1.y));
383: setParameter("ww", cropFloat(ww));
384: setParameter("wh", cropFloat(wh));
385: parseArea();
1.2 casties 386: // zoomed is always fit
387: setParameter("ws", 1);
388: display();
1.1 casties 389: }
390: }
391: }
392:
393: function zoomMove(evt) {
394: // mouse move handler
395: pt2 = evtPosition(evt);
396: // restrict marks to move right and down
397: eck1pos = pt1;
398: eck2pos = new Position(Math.max(pt1.x, pt2.x)-12, pt1.y);
399: eck3pos = new Position(pt1.x, Math.max(pt1.y, pt2.y)-12);
400: eck4pos = new Position(Math.max(pt1.x, pt2.x)-12, Math.max(pt1.y, pt2.y)-12);
1.2 casties 401: moveElement(eck1, eck1pos);
402: moveElement(eck2, eck2pos);
403: moveElement(eck3, eck3pos);
404: moveElement(eck4, eck4pos);
1.1 casties 405: }
406:
407: // starting event capture
1.2 casties 408: registerMouseDown(elemScaler, zoomClick);
409: registerMouseDown(eck4, zoomClick);
1.1 casties 410: }
411:
412:
1.4 casties 413: function moveCenter() {
414: // move visible area so that it's centered around the clicked point
1.1 casties 415: if ( (dlArea.width == 1.0) && (dlArea.height == 1.0) ) {
1.4 casties 416: // noting to do
1.1 casties 417: return;
418: }
1.4 casties 419: window.focus();
1.1 casties 420:
1.4 casties 421: function moveCenterEvent(evt) {
1.1 casties 422: // move to handler
1.4 casties 423: unregisterMouseDown(elemScaler, moveCenterEvent);
424: var pt = dlTrafo.invtransform(evtPosition(evt));
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>