Mercurial > hg > digilib-old
annotate client/digitallibrary/dllib.js @ 299:0ddfc57a79af
Servlet version 1.5.0b -- the beginning of the next generation :-)
- code restructuring to improve scaleability
- new Initialiser servlet that must be run first
- image transformation work moved to DigilibImageWorker class
- Maximum number of concurrent threads limited by Semaphore
- old JIMI toolkit implementation removed
author | robcast |
---|---|
date | Sun, 24 Oct 2004 20:23:50 +0200 |
parents | 4dbff786ff50 |
children | 610c7ee770cb |
rev | line source |
---|---|
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
1 /* Copyright (C) 2003,2004 IT-Group MPIWG, WTWG Uni Bern and others |
237 | 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 | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
17 Authors: |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
18 Christian Luginbuehl, 01.05.2003 (first version) |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
19 DW 24.03.2004 (Changed for digiLib in Zope) |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
20 Robert Casties, 04.08.2004 |
237 | 21 |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
22 ! Requires baselib.js ! |
237 | 23 |
24 */ | |
25 | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
26 var dlScriptVersion = "1.0b2"; |
237 | 27 |
28 /* | |
29 * more parameter handling | |
30 */ | |
31 | |
32 var dlArea = new Rectangle(0.0, 0.0, 1.0, 1.0); | |
33 var dlMaxArea = new Rectangle(0.0, 0.0, 1.0, 1.0); | |
34 | |
35 function parseArea() { | |
36 // returns area Rectangle from current parameters | |
37 return new Rectangle(getParameter("wx"), getParameter("wy"), getParameter("ww"), getParameter("wh")); | |
38 } | |
39 | |
40 function setParamFromArea(rect) { | |
41 // sets digilib wx etc. from rect | |
42 setParameter("wx", cropFloat(rect.x)); | |
43 setParameter("wy", cropFloat(rect.y)); | |
44 setParameter("ww", cropFloat(rect.width)); | |
45 setParameter("wh", cropFloat(rect.height)); | |
46 return true; | |
47 } | |
48 | |
49 var dlTrafo = new Transform(); | |
50 | |
51 function parseTrafo(elem) { | |
52 // returns Transform from current dlArea and picsize | |
53 var picsize = getElementRect(elem); | |
54 var trafo = new Transform(); | |
55 // subtract area offset and size | |
56 trafo.concat(getTranslation(new Position(-dlArea.x, -dlArea.y))); | |
57 trafo.concat(getScale(new Size(1/dlArea.width, 1/dlArea.height))); | |
58 // scale to screen size | |
59 trafo.concat(getScale(picsize)); | |
60 trafo.concat(getTranslation(picsize)); | |
61 // rotate | |
62 //trafo.concat(getRotation(- getParameter("rot"), new Position(0.5*picsize.width, 0.5*picsize.height))); | |
63 // mirror | |
64 //if (hasFlag("hmir")) { | |
65 //trafo.m00 = - trafo.m00; | |
66 //} | |
67 //if (hasFlag("vmir")) { | |
68 //trafo.m11 = - trafo.m11; | |
69 //} | |
70 return trafo; | |
71 } | |
72 | |
73 | |
74 var dlMarks = new Array(); | |
75 | |
76 function parseMarks() { | |
77 // returns marks array from current parameters | |
78 var marks = new Array(); | |
79 var ma; | |
80 var mk = getParameter("mk"); | |
81 if (mk.indexOf(";") >= 0) { | |
82 // old format with ";" | |
83 ma = mk.split(";"); | |
84 } else { | |
85 ma = mk.split(","); | |
86 } | |
87 for (var i = 0; i < ma.length ; i++) { | |
88 var pos = ma[i].split("/"); | |
89 if (pos.length > 1) { | |
90 marks.push(new Position(pos[0], pos[1])); | |
91 } | |
92 } | |
93 return marks; | |
94 } | |
95 | |
96 function getAllMarks() { | |
97 // returns a string with all marks in query format | |
98 var marks = new Array(); | |
99 for (var i = 0; i < dlMarks.length; i++) { | |
100 marks.push(cropFloat(dlMarks[i].x) + "/" + cropFloat(dlMarks[i].y)); | |
101 } | |
102 return marks.join(","); | |
103 } | |
104 | |
105 function addMark(pos) { | |
106 // add a mark | |
107 dlMarks.push(pos); | |
108 setParameter("mk", getAllMarks()); | |
109 return true; | |
110 } | |
111 | |
112 function deleteMark() { | |
113 // delete the last mark | |
114 dlMarks.pop(); | |
115 setParameter("mk", getAllMarks()); | |
116 return true; | |
117 } | |
118 | |
119 var dlFlags = new Object(); | |
120 | |
121 function hasFlag(mode) { | |
122 // returns if mode flag is set | |
123 return (dlFlags[mode]); | |
124 } | |
125 | |
126 function addFlag(mode) { | |
127 // add a mode flag | |
128 dlFlags[mode] = mode; | |
239 | 129 setParameter("mo", getAllFlags()); |
237 | 130 return true; |
131 } | |
132 | |
133 function removeFlag(mode) { | |
134 // remove a mode flag | |
135 if (dlFlags[mode]) { | |
136 delete dlFlags[mode]; | |
137 } | |
239 | 138 setParameter("mo", getAllFlags()); |
237 | 139 return true; |
140 } | |
141 | |
142 function toggleFlag(mode) { | |
143 // change a mode flag | |
144 if (dlFlags[mode]) { | |
145 delete dlFlags[mode]; | |
146 } else { | |
147 dlFlags[mode] = mode; | |
148 } | |
239 | 149 setParameter("mo", getAllFlags()); |
237 | 150 return true; |
151 } | |
152 | |
153 function getAllFlags() { | |
154 // returns a string with all flags in query format | |
155 var fa = new Array(); | |
156 for (var f in dlFlags) { | |
157 if ((f != "")&&(dlFlags[f] != null)) { | |
158 fa.push(f); | |
159 } | |
160 } | |
161 return fa.join(","); | |
162 } | |
163 | |
164 function parseFlags() { | |
165 // sets dlFlags from the current parameters | |
166 var flags = new Object(); | |
167 var fa = getParameter("mo").split(","); | |
168 for (var i = 0; i < fa.length ; i++) { | |
169 var f = fa[i]; | |
170 if (f != "") { | |
171 flags[f] = f; | |
172 } | |
173 } | |
174 return flags; | |
175 } | |
176 | |
177 | |
178 function bestPicSize(elem, inset) { | |
179 // returns a Size with the best image size for the given element | |
180 if (! defined(inset)) { | |
181 inset = 25; | |
182 } | |
183 var ws = getWinSize(); | |
184 var es = getElementPosition(elem); | |
185 if (es) { | |
186 ws.width = ws.width - es.x - inset; | |
187 ws.height = ws.height - es.y - inset; | |
188 } | |
189 return ws; | |
190 } | |
191 | |
192 | |
193 /* ********************************************** | |
194 * digilib specific routines | |
195 * ******************************************** */ | |
196 | |
197 var elemScaler = null; | |
198 var picElem = null; | |
199 | |
238 | 200 |
201 function dl_param_init() { | |
202 // parameter initialisation before onload | |
203 | |
204 // put the query parameters (sans "?") in the parameters array | |
205 parseParameters(location.search.slice(1)); | |
206 // treat special parameters | |
207 dlMarks = parseMarks(); | |
208 dlArea = parseArea(); | |
209 dlFlags = parseFlags(); | |
210 } | |
211 | |
212 | |
237 | 213 function dl_init() { |
238 | 214 // initalisation on load |
237 | 215 elemScaler = getElement("scaler", true); |
216 picElem = getElement("pic", true); | |
217 if (picElem == null && elemScaler) { | |
218 // in N4 pic is in the scaler layer | |
219 picElem = elemScaler.document.images[0]; | |
220 } | |
221 if ((!elemScaler)||(!picElem)) { | |
222 alert("Sorry, zogilib doesn't work here!"); | |
223 return false; | |
224 } | |
225 // give a name to the window containing digilib | |
226 if (defined(dlTarget)&&(dlTarget)) { | |
227 window.name = dlTarget; | |
228 } else { | |
229 window.name = "digilib"; | |
230 } | |
231 // put the query parameters (sans "?") in the parameters array | |
232 parseParameters(location.search.slice(1)); | |
233 // treat special parameters | |
234 dlMarks = parseMarks(); | |
235 dlArea = parseArea(); | |
236 dlFlags = parseFlags(); | |
237 // wait for image to load and display marks | |
238 renderMarks(); | |
239 // done | |
240 focus(); | |
241 return; | |
242 } | |
243 | |
244 | |
245 function display(detail) { | |
246 // redisplay the page | |
247 if (! detail) { | |
248 detail = 9; | |
249 } | |
250 var queryString = getAllParameters(detail); | |
251 location.href = location.protocol + "//" + location.host + location.pathname + "?" + queryString; | |
252 } | |
253 | |
254 | |
255 /* ********************************************** | |
256 * interactive digilib functions | |
257 * ******************************************** */ | |
258 | |
259 | |
260 function renderMarks() { | |
261 // put the visible marks on the image | |
262 var mark_count = dlMarks.length; | |
263 // make shure the image is loaded so we know its size | |
264 if (defined(picElem.complete) && picElem.complete == false && ! browserType.isN4 ) { | |
265 setTimeout("renderMarks()", 100); | |
266 } else { | |
267 dlTrafo = parseTrafo(picElem); | |
268 for (var i = 0; i < 8; i++) { | |
269 var me = getElement("dot"+i); | |
270 if (i < mark_count) { | |
271 if (dlArea.containsPosition(dlMarks[i])) { | |
272 var mpos = dlTrafo.transform(dlMarks[i]); | |
273 // suboptimal to place -5 pixels and not half size of mark-image | |
274 mpos.x = mpos.x -5; | |
275 mpos.y = mpos.y -5; | |
276 moveElement(me, mpos); | |
277 showElement(me, true); | |
278 } | |
279 } else { | |
280 // hide the other marks | |
281 showElement(me, false); | |
282 } | |
283 } | |
284 } | |
285 } | |
286 | |
287 | |
239 | 288 function setMark(reload) { |
237 | 289 // add a mark where clicked |
290 if ( dlMarks.length > 7 ) { | |
291 alert("Only 8 marks are possible at the moment!"); | |
292 return; | |
293 } | |
294 | |
295 function markEvent(evt) { | |
296 // event handler adding a new mark | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
297 unregisterEvent("mousedown", elemScaler, markEvent); |
237 | 298 var p = dlTrafo.invtransform(evtPosition(evt)); |
299 addMark(p); | |
239 | 300 if (defined(reload)&&(!reload)) { |
301 // don't redisplay | |
302 renderMarks(); | |
303 return; | |
304 } | |
237 | 305 display(); |
306 } | |
307 | |
308 // starting event capture | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
309 registerEvent("mousedown", elemScaler, markEvent); |
237 | 310 } |
311 | |
312 | |
239 | 313 function removeMark(reload) { |
314 // remove the last mark | |
315 deleteMark(); | |
316 if (defined(reload)&&(!reload)) { | |
317 // don't redisplay | |
318 renderMarks(); | |
319 return; | |
320 } | |
321 display(); | |
322 } | |
323 | |
324 | |
237 | 325 function zoomArea() { |
326 var click = 1; | |
327 var pt1, pt2; | |
328 var eck1pos, eck2pos, eck3pos, eck4pos; | |
329 window.focus(); | |
330 var eck1 = getElement("eck1"); | |
331 var eck2 = getElement("eck2"); | |
332 var eck3 = getElement("eck3"); | |
333 var eck4 = getElement("eck4"); | |
334 | |
335 function zoomClick(evt) { | |
336 // mouse click handler | |
337 if (click == 1) { | |
338 // first click -- start moving | |
339 click = 2; | |
340 pt1 = evtPosition(evt); | |
341 pt2 = pt1; | |
342 eck1pos = pt1; | |
343 eck2pos = new Position(pt1.x - 12, pt1.y); | |
344 eck3pos = new Position(pt1.x, pt1.y - 12); | |
345 eck4pos = new Position(pt1.y - 12, pt1.y - 12); | |
346 moveElement(eck1, eck1pos); | |
347 moveElement(eck2, eck2pos); | |
348 moveElement(eck3, eck3pos); | |
349 moveElement(eck4, eck4pos); | |
350 showElement(eck1, true); | |
351 showElement(eck2, true); | |
352 showElement(eck3, true); | |
353 showElement(eck4, true); | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
354 // show moving |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
355 registerEvent("mousemove", elemScaler, zoomMove); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
356 registerEvent("mousemove", eck4, zoomMove); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
357 // enable drag-to-zoom |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
358 registerEvent("mouseup", elemScaler, zoomClick); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
359 registerEvent("mouseup", eck4, zoomClick); |
237 | 360 } else { |
361 // second click -- end moving | |
362 pt2 = evtPosition(evt); | |
363 showElement(eck1, false); | |
364 showElement(eck2, false); | |
365 showElement(eck3, false); | |
366 showElement(eck4, false); | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
367 unregisterEvent("mousemove", elemScaler, zoomMove); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
368 unregisterEvent("mousemove", eck4, zoomMove); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
369 unregisterEvent("mousedown", elemScaler, zoomClick); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
370 unregisterEvent("mousedown", eck4, zoomClick); |
237 | 371 var p1 = dlTrafo.invtransform(pt1); |
372 var p2 = dlTrafo.invtransform(pt2); | |
373 var ww = p2.x-p1.x; | |
374 var wh = p2.y-p1.y; | |
375 if ((ww > 0)&&(wh > 0)) { | |
376 setParameter("wx", cropFloat(p1.x)); | |
377 setParameter("wy", cropFloat(p1.y)); | |
378 setParameter("ww", cropFloat(ww)); | |
379 setParameter("wh", cropFloat(wh)); | |
380 parseArea(); | |
381 // zoomed is always fit | |
382 setParameter("ws", 1); | |
383 display(); | |
384 } | |
385 } | |
386 } | |
387 | |
388 function zoomMove(evt) { | |
389 // mouse move handler | |
390 pt2 = evtPosition(evt); | |
391 // restrict marks to move right and down | |
392 eck1pos = pt1; | |
393 eck2pos = new Position(Math.max(pt1.x, pt2.x)-12, pt1.y); | |
394 eck3pos = new Position(pt1.x, Math.max(pt1.y, pt2.y)-12); | |
395 eck4pos = new Position(Math.max(pt1.x, pt2.x)-12, Math.max(pt1.y, pt2.y)-12); | |
396 moveElement(eck1, eck1pos); | |
397 moveElement(eck2, eck2pos); | |
398 moveElement(eck3, eck3pos); | |
399 moveElement(eck4, eck4pos); | |
400 } | |
401 | |
402 // starting event capture | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
403 registerEvent("mousedown", elemScaler, zoomClick); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
404 registerEvent("mousedown", eck4, zoomClick); |
237 | 405 } |
406 | |
407 var ZOOMFACTOR = Math.sqrt(2); | |
408 | |
409 function zoomBy(factor) { | |
410 // zooms by the given factor | |
239 | 411 var newarea = dlArea.copy(); |
237 | 412 newarea.width /= factor; |
413 newarea.height /= factor; | |
414 newarea.x -= 0.5 * (newarea.width - dlArea.width); | |
415 newarea.y -= 0.5 * (newarea.height - dlArea.height); | |
416 newarea = dlMaxArea.fit(newarea); | |
417 setParamFromArea(newarea); | |
418 display(); | |
419 } | |
420 | |
421 | |
422 function zoomFullpage() { | |
423 // zooms out to show the whole image | |
424 setParameter("wx", 0.0); | |
425 setParameter("wy", 0.0); | |
426 setParameter("ww", 1.0); | |
427 setParameter("wh", 1.0); | |
428 display(); | |
429 } | |
430 | |
431 | |
432 function moveCenter() { | |
433 // move visible area so that it's centered around the clicked point | |
434 if ( (dlArea.width == 1.0) && (dlArea.height == 1.0) ) { | |
435 // noting to do | |
436 return; | |
437 } | |
438 window.focus(); | |
439 | |
440 function moveCenterEvent(evt) { | |
441 // move to handler | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
442 unregisterEvent("mousedown", elemScaler, moveCenterEvent); |
237 | 443 var pt = dlTrafo.invtransform(evtPosition(evt)); |
444 var newarea = new Rectangle(pt.x-0.5*dlArea.width, pt.y-0.5*dlArea.height, dlArea.width, dlArea.height); | |
445 newarea = dlMaxArea.fit(newarea); | |
446 // set parameters | |
447 setParamFromArea(newarea); | |
448 parseArea(); | |
449 display(); | |
450 } | |
451 | |
452 // starting event capture | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
453 registerEvent("mousedown", elemScaler, moveCenterEvent); |
237 | 454 } |
455 | |
243
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
456 function moveBy(movx, movy) { |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
457 // move visible area by movx and movy (in units of dw, dh) |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
458 if ((dlArea.width == 1.0)&&(dlArea.height == 1.0)) { |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
459 // nothing to do |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
460 return; |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
461 } |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
462 var newarea = dlArea.copy(); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
463 newarea.x += parseFloat(movx)*dlArea.width; |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
464 newarea.y += parseFloat(movy)*dlArea.height; |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
465 newarea = dlMaxArea.fit(newarea); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
466 // set parameters |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
467 setParamFromArea(newarea); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
468 parseArea(); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
469 display(); |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
470 } |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
471 |
4dbff786ff50
new digimage with red triangles for moving the zoomed area
robcast
parents:
239
diff
changeset
|
472 |
237 | 473 |
474 | |
238 | 475 function getRef() { |
237 | 476 // returns a reference to the current digilib set |
477 if (! baseUrl) { | |
478 var baseUrl = location.protocol + "//" + location.host + location.pathname; | |
479 } | |
480 var hyperlinkRef = baseUrl; | |
481 var par = getAllParameters(9); | |
482 if (par.length > 0) { | |
483 hyperlinkRef += "?" + par; | |
484 } | |
485 return hyperlinkRef; | |
486 } |