Mercurial > hg > digilib-old
annotate webapp/src/main/webapp/jquery/jquery.digilib.regions.js @ 1025:c75858ee4452
more fiddling with regions.
use map and area tags (can be changed by regionContentSelector).
new autoZoomRegionLinks and fullRegionLinks.
author | robcast |
---|---|
date | Tue, 06 Mar 2012 23:53:22 +0100 |
parents | 4ee43243a6a0 |
children | ff19b9e936d3 |
rev | line source |
---|---|
792 | 1 /** optional digilib regions plugin |
2 | |
1023 | 3 Mark up a digilib image with rectangular regions. |
4 | |
5 If hasRegionInfo=true reads regions from page HTML. | |
6 Element with regions has to be in digilib element, e.g. | |
792 | 7 |
1025 | 8 <map class="dl-keep dl-regioncontent"> |
9 <area href="http://www.mpiwg-berlin.mpg.de" coords="0.1,0.1,0.4,0.1" alt="MPI fuer Wissenschaftsgeschichte"/> | |
10 <area href="http://www.biblhertz.it" coords="0.5,0.8,0.4,0.1" alt="Bibliotheca Hertziana"/> | |
11 <area coords="0.3,0.5,0.15,0.1" /> | |
12 </map> | |
1023 | 13 |
792 | 14 */ |
15 | |
16 (function($) { | |
813 | 17 // the digilib object |
996 | 18 var digilib = null; |
813 | 19 // the functions made available by digilib |
996 | 20 var fn = null; |
813 | 21 // affine geometry plugin |
996 | 22 var geom = null; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
23 |
792 | 24 var buttons = { |
25 addregion : { | |
813 | 26 onclick : "defineRegion", |
27 tooltip : "define a region", | |
792 | 28 icon : "addregion.png" |
29 }, | |
30 delregion : { | |
31 onclick : "removeRegion", | |
32 tooltip : "delete the last region", | |
33 icon : "delregion.png" | |
34 }, | |
35 regions : { | |
36 onclick : "toggleRegions", | |
37 tooltip : "show or hide regions", | |
38 icon : "regions.png" | |
39 }, | |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
40 regionhtml : { |
855 | 41 onclick : "showRegionInfo", |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
42 tooltip : "show information about regions", |
792 | 43 icon : "regioninfo.png" |
44 } | |
45 }; | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
46 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
47 var defaults = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
48 // are regions shown? |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
49 'isRegionVisible' : true, |
813 | 50 // are region numbers shown? |
1023 | 51 'showRegionNumbers' : true, |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
52 // is window with region HTML shown? |
855 | 53 'showRegionInfo' : false, |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
54 // is there region content in the page? |
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
55 'hasRegionContent' : false, |
823 | 56 // turn any region into a clickable link to its detail view |
1025 | 57 'autoZoomRegionLinks' : false, |
58 // use full region as klickable link (instead of only number and text) | |
59 'fullRegionLinks' : false, | |
60 // css selector for area elements (should additionally be marked with class "keep") | |
61 'regionContentSelector' : 'map.dl-regioncontent area', | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
62 // buttonset of this plugin |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
63 'regionSet' : ['regions', 'addregion', 'delregion', 'regionhtml', 'lessoptions'], |
813 | 64 // url param for regions |
851 | 65 'rg' : null |
813 | 66 }; |
792 | 67 |
68 var actions = { | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
69 |
792 | 70 // define a region interactively with two clicked points |
813 | 71 "defineRegion" : function(data) { |
72 if (!data.settings.isRegionVisible) { | |
73 alert("Please turn on regions visibility!"); | |
74 return; | |
75 } | |
996 | 76 var cssPrefix = data.settings.cssPrefix; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
77 var $elem = data.$elem; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
78 var $body = $('body'); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
79 var bodyRect = geom.rectangle($body); |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
80 var $scaler = data.$scaler; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
81 var scalerRect = geom.rectangle($scaler); |
792 | 82 var pt1, pt2; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
83 // overlay prevents other elements from reacting to mouse events |
996 | 84 var $overlay = $('<div class="'+cssPrefix+'overlay" style="position:absolute"/>'); |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
85 $body.append($overlay); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
86 bodyRect.adjustDiv($overlay); |
823 | 87 var $regionDiv = addRegionDiv(data, data.regions.length); |
792 | 88 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
89 // mousedown handler: start sizing |
792 | 90 var regionStart = function (evt) { |
91 pt1 = geom.position(evt); | |
92 // setup and show zoom div | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
93 pt1.adjustDiv($regionDiv); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
94 $regionDiv.width(0).height(0); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
95 $regionDiv.show(); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
96 // register mouse events |
920 | 97 $overlay.on("mousemove.dlRegion", regionMove); |
98 $overlay.on("mouseup.dlRegion", regionEnd); | |
792 | 99 return false; |
100 }; | |
101 | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
102 // mousemove handler: size region |
792 | 103 var regionMove = function (evt) { |
104 pt2 = geom.position(evt); | |
105 var rect = geom.rectangle(pt1, pt2); | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
106 rect.clipTo(scalerRect); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
107 // update region |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
108 rect.adjustDiv($regionDiv); |
792 | 109 return false; |
110 }; | |
111 | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
112 // mouseup handler: end sizing |
792 | 113 var regionEnd = function (evt) { |
114 pt2 = geom.position(evt); | |
115 // assume a click and continue if the area is too small | |
116 var clickRect = geom.rectangle(pt1, pt2); | |
117 if (clickRect.getArea() <= 5) return false; | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
118 // unregister mouse events and get rid of overlay |
920 | 119 $overlay.off("mousemove.dlRegion", regionMove); |
120 $overlay.off("mouseup.dlRegion", regionEnd); | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
121 $overlay.remove(); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
122 // clip region |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
123 clickRect.clipTo(scalerRect); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
124 clickRect.adjustDiv($regionDiv); |
813 | 125 storeRegion(data, $regionDiv); |
792 | 126 // fn.redisplay(data); |
813 | 127 fn.highlightButtons(data, 'addregion', 0); |
128 redisplay(data); | |
792 | 129 return false; |
130 }; | |
131 | |
132 // bind start zoom handler | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
133 $overlay.one('mousedown.dlRegion', regionStart); |
813 | 134 fn.highlightButtons(data, 'addregion', 1); |
792 | 135 }, |
136 | |
137 // remove the last added region | |
138 "removeRegion" : function (data) { | |
813 | 139 if (!data.settings.isRegionVisible) { |
140 alert("Please turn on regions visibility!"); | |
141 return; | |
142 } | |
143 var region = data.regions.pop(); | |
144 if (region == null) return; | |
145 var $regionDiv = region.$div; | |
792 | 146 $regionDiv.remove(); |
813 | 147 redisplay(data); |
792 | 148 }, |
149 | |
813 | 150 // show/hide regions |
151 "toggleRegions" : function (data) { | |
152 var show = !data.settings.isRegionVisible; | |
153 data.settings.isRegionVisible = show; | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
154 fn.highlightButtons(data, 'regions', show); |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
155 renderRegions(data, 1); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
156 }, |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
157 |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
158 // show/hide region HTML code |
855 | 159 "showRegionInfo" : function (data) { |
160 var show = !data.settings.showRegionInfo; | |
161 data.settings.showRegionInfo = show; | |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
162 fn.highlightButtons(data, 'regionhtml', show); |
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
163 var $html = data.$htmlDiv; |
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
164 if (!show) { |
855 | 165 // empty the div |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
166 $html.fadeOut(function () { |
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
167 $html.contents().remove(); |
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
168 }); |
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
169 return; |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
170 } |
855 | 171 regionInfo(data); |
991
497ab03868ab
moved marks to separate plugin. (pack and unpack is still in digilib.js)
robcast
parents:
969
diff
changeset
|
172 } |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
173 |
792 | 174 }; |
175 | |
813 | 176 // store a region div |
177 var storeRegion = function (data, $regionDiv) { | |
178 var regions = data.regions; | |
179 var rect = geom.rectangle($regionDiv); | |
180 var regionRect = data.imgTrafo.invtransform(rect); | |
181 regionRect.$div = $regionDiv; | |
182 regions.push(regionRect); | |
183 console.debug("regions", data.regions, "regionRect", regionRect); | |
184 }; | |
185 | |
855 | 186 // clickable header |
187 var regionInfoHeader = function (data) { | |
996 | 188 var cssPrefix = data.settings.cssPrefix; |
189 var $infoDiv = $('<div class="'+cssPrefix+'infoheader"/>'); | |
190 var $h01 = $('<div class="'+cssPrefix+'infobutton">HTML</div>'); | |
191 var $h02 = $('<div class="'+cssPrefix+'infobutton">SVG</div>'); | |
192 var $h03 = $('<div class="'+cssPrefix+'infobutton">Digilib</div>'); | |
193 var $h04 = $('<div class="'+cssPrefix+'infobutton">X</div>'); | |
855 | 194 var bind = function($div, name) { |
920 | 195 $div.on('click.regioninfo', function () { |
855 | 196 var $top = $(this).parent().parent(); |
996 | 197 $top.find('.'+cssPrefix+'info').hide(); |
198 $top.find('.'+cssPrefix+ name).show(); | |
855 | 199 }); |
200 }; | |
201 bind($h01, 'html'); | |
202 bind($h02, 'svgattr'); | |
203 bind($h03, 'digilib'); | |
204 var $html = data.$htmlDiv; | |
920 | 205 $h04.on('click.regioninfo', function () { |
855 | 206 data.settings.showRegionInfo = false; |
207 fn.highlightButtons(data, 'regionhtml', false); | |
208 $html.fadeOut(function () { | |
209 $html.contents().remove(); | |
210 }); | |
211 }); | |
212 $infoDiv.append($h01, $h02, $h03, $h04); | |
213 return $infoDiv; | |
214 }; | |
215 | |
216 // html for later insertion | |
217 var regionInfoHTML = function (data) { | |
996 | 218 var cssPrefix = data.settings.cssPrefix; |
219 var $infoDiv = $('<div class="'+cssPrefix+'info '+cssPrefix+'html"/>'); | |
220 $infoDiv.append($('<div/>').text('<div class="'+cssPrefix+'keep '+cssPrefix+'regioncontent">')); | |
855 | 221 $.each(data.regions, function(index, r) { |
872 | 222 var area = [ |
223 fn.cropFloatStr(r.x), | |
224 fn.cropFloatStr(r.y), | |
225 fn.cropFloatStr(r.width), | |
226 fn.cropFloatStr(r.height)].join(','); | |
855 | 227 $infoDiv.append($('<div/>').text('<a coords="' + area + '" >')); |
228 }); | |
229 $infoDiv.append($('<div/>').text('</div>')); | |
230 return $infoDiv; | |
231 }; | |
232 | |
233 // SVG-style | |
234 var regionInfoSVG = function (data) { | |
996 | 235 var cssPrefix = data.settings.cssPrefix; |
236 var $infoDiv = $('<div class="'+cssPrefix+'info '+cssPrefix+'svgattr"/>'); | |
855 | 237 $.each(data.regions, function(index, r) { |
872 | 238 var area = [ |
239 fn.cropFloatStr(r.x), | |
240 fn.cropFloatStr(r.y), | |
241 fn.cropFloatStr(r.width), | |
242 fn.cropFloatStr(r.height)].join(' '); | |
855 | 243 $infoDiv.append($('<div/>').text('"' + area + '"')); |
244 }); | |
245 return $infoDiv; | |
246 }; | |
247 | |
248 // digilib-style | |
249 var regionInfoDigilib = function (data) { | |
996 | 250 var cssPrefix = data.settings.cssPrefix; |
251 var $infoDiv = $('<div class="'+cssPrefix+'info '+cssPrefix+'digilib"/>'); | |
855 | 252 $.each(data.regions, function(index, r) { |
862 | 253 var area = r.toString(); |
855 | 254 $infoDiv.append($('<div/>').text(area)); |
255 }); | |
256 return $infoDiv; | |
257 }; | |
258 | |
259 // show region info in a window | |
260 var regionInfo = function (data) { | |
261 var $html = data.$htmlDiv; | |
874 | 262 $html.append(regionInfoHeader(data)); |
855 | 263 $html.append(regionInfoHTML(data)); |
264 $html.append(regionInfoSVG(data)); | |
265 $html.append(regionInfoDigilib(data)); | |
266 $html.fadeIn(); | |
267 }; | |
268 | |
813 | 269 // add a region to data.$elem |
873 | 270 var addRegionDiv = function (data, index, attributes) { |
1025 | 271 var settings = data.settings; |
272 var cssPrefix = settings.cssPrefix; | |
823 | 273 var nr = index + 1; // we count regions from 1 |
996 | 274 var $regionDiv = $('<div class="'+cssPrefix+'region '+cssPrefix+'overlay" style="display:none"/>'); |
813 | 275 data.$elem.append($regionDiv); |
1025 | 276 if (settings.showRegionNumbers) { |
1024 | 277 var $regionLink = $('<a class="'+cssPrefix+'regionnumber">'+nr+'</a>'); |
920 | 278 if (attributes) $regionLink.attr(attributes); |
851 | 279 $regionDiv.append($regionLink); |
813 | 280 } |
1025 | 281 var url = null; |
282 if (attributes) { | |
283 $regionDiv.attr(attributes); | |
284 // UGLY: if we added href, remove it | |
285 if (attributes.href) { | |
286 url = attributes.href; | |
287 $regionDiv.removeAttr('href'); | |
288 } | |
289 } | |
290 if (settings.autoZoomRegionLinks || settings.fullRegionLinks) { | |
291 var region = data.regions[index]; | |
292 $regionDiv.on('click.dlRegion', function(evt) { | |
293 if (settings.fullRegionLinks && url) { | |
294 //TODO: how about target? | |
295 window.location = url; | |
296 } | |
297 if (evt.target !== $regionDiv.get(0)) { | |
298 // this was not our event | |
299 return; | |
1024 | 300 } |
1025 | 301 if (settings.autoZoomRegionLinks) { |
302 // zoom to region | |
303 digilib.actions.zoomArea(data, region); | |
1024 | 304 } |
851 | 305 }); |
823 | 306 } |
813 | 307 return $regionDiv; |
308 }; | |
309 | |
851 | 310 // create a region div from the data.regions array |
947 | 311 var createRegionDiv = function (data, regions, index, attributes) { |
873 | 312 var $regionDiv = addRegionDiv(data, index, attributes); |
813 | 313 var region = regions[index]; |
314 region.$div = $regionDiv; | |
315 return $regionDiv; | |
316 }; | |
317 | |
851 | 318 // create regions from URL parameters |
319 var createRegionsFromURL = function (data) { | |
320 unpackRegions(data); | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
321 var regions = data.regions; |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
322 $.each(regions, function(i) { |
947 | 323 createRegionDiv(data, regions, i); |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
324 }); |
792 | 325 }; |
326 | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
327 // create regions from HTML |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
328 var createRegionsFromHTML = function (data) { |
823 | 329 var regions = data.regions; |
1025 | 330 // regions are defined in "area" tags |
331 var $content = data.$elem.find(data.settings.regionContentSelector); | |
851 | 332 console.debug("createRegionsFromHTML. elems: ", $content); |
835 | 333 $content.each(function(index, a) { |
334 var $a = $(a); | |
852 | 335 // the "coords" attribute contains the region coords (0..1) |
336 var coords = $a.attr('coords'); | |
337 var pos = coords.split(",", 4); | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
338 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
339 regions.push(rect); |
1024 | 340 // copy some attributes |
873 | 341 var attributes = {}; |
1024 | 342 for (var n in {'id':1, 'href':1, 'title':1, 'target':1, 'style':1}) { |
343 attributes[n] = $a.attr(n); | |
344 } | |
851 | 345 // create the div |
947 | 346 var $regionDiv = createRegionDiv(data, regions, index, attributes); |
851 | 347 var $contents = $a.contents().clone(); |
1025 | 348 if (attributes.href != null) { |
349 // wrap contents in a-tag | |
350 var $ca = $('<a href="'+attributes.href+'"/>'); | |
351 $ca.append($contents); | |
352 $ca.append($a.attr('alt')); | |
353 $regionDiv.append($ca); | |
354 } else { | |
355 $regionDiv.append($contents); | |
356 // alt attribute is also content (BTW: area-tag has no content()) | |
357 $regionDiv.append($a.attr('alt')); | |
358 } | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
359 }); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
360 }; |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
361 |
813 | 362 // show a region on top of the scaler image |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
363 var renderRegion = function (data, index, anim) { |
813 | 364 if (!data.imgTrafo) return; |
365 var $elem = data.$elem; | |
792 | 366 var regions = data.regions; |
950
164d69e2c481
much nicer implementation of preview for scrolling and zooming.
robcast
parents:
947
diff
changeset
|
367 var zoomArea = data.zoomArea; |
813 | 368 if (index > regions.length) return; |
851 | 369 var region = regions[index]; |
813 | 370 var $regionDiv = region.$div; |
371 if (!$regionDiv) { | |
950
164d69e2c481
much nicer implementation of preview for scrolling and zooming.
robcast
parents:
947
diff
changeset
|
372 console.error("renderRegion: region has no $div", region); |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
373 // alert("renderRegion: region has no $div to show"); |
813 | 374 return; |
375 } | |
376 var regionRect = region.copy(); | |
377 var show = data.settings.isRegionVisible; | |
950
164d69e2c481
much nicer implementation of preview for scrolling and zooming.
robcast
parents:
947
diff
changeset
|
378 if (show && zoomArea.overlapsRect(regionRect)) { |
164d69e2c481
much nicer implementation of preview for scrolling and zooming.
robcast
parents:
947
diff
changeset
|
379 regionRect.clipTo(zoomArea); |
813 | 380 var screenRect = data.imgTrafo.transform(regionRect); |
851 | 381 console.debug("renderRegion: pos=",geom.position(screenRect)); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
382 if (anim) { |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
383 $regionDiv.fadeIn(); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
384 } else{ |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
385 $regionDiv.show(); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
386 } |
1024 | 387 // adjustDiv sets wrong coords when called BEFORE show() |
851 | 388 screenRect.adjustDiv($regionDiv); |
813 | 389 } else { |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
390 if (anim) { |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
391 $regionDiv.fadeOut(); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
392 } else{ |
824 | 393 $regionDiv.hide(); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
394 } |
813 | 395 } |
396 }; | |
397 | |
398 // show regions | |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
399 var renderRegions = function (data, anim) { |
813 | 400 for (var i = 0; i < data.regions.length ; i++) { |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
401 renderRegion(data, i, anim); |
813 | 402 } |
403 }; | |
404 | |
405 var unpackRegions = function (data) { | |
406 // create regions from parameters | |
407 var rg = data.settings.rg; | |
408 if (rg == null) return; | |
409 var regions = data.regions; | |
958
2a2e8c039c29
changed regions parameter separator to the same as for marks
hertzhaft
parents:
950
diff
changeset
|
410 var rs = rg.split(","); |
813 | 411 for (var i = 0; i < rs.length; i++) { |
412 var r = rs[i]; | |
958
2a2e8c039c29
changed regions parameter separator to the same as for marks
hertzhaft
parents:
950
diff
changeset
|
413 var pos = r.split("/", 4); |
813 | 414 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); |
415 regions.push(rect); | |
792 | 416 } |
417 }; | |
418 | |
813 | 419 // pack regions array into a parameter string |
420 var packRegions = function (data) { | |
421 var regions = data.regions; | |
815 | 422 if (!regions.length) { |
423 data.settings.rg = null; | |
424 return; | |
425 } | |
813 | 426 var rg = ''; |
427 for (var i = 0; i < regions.length; i++) { | |
1024 | 428 var region = regions[i]; |
813 | 429 if (i) { |
958
2a2e8c039c29
changed regions parameter separator to the same as for marks
hertzhaft
parents:
950
diff
changeset
|
430 rg += ','; |
792 | 431 } |
813 | 432 rg += [ |
433 fn.cropFloatStr(region.x), | |
434 fn.cropFloatStr(region.y), | |
435 fn.cropFloatStr(region.width), | |
436 fn.cropFloatStr(region.height) | |
958
2a2e8c039c29
changed regions parameter separator to the same as for marks
hertzhaft
parents:
950
diff
changeset
|
437 ].join('/'); |
813 | 438 } |
439 data.settings.rg = rg; | |
792 | 440 }; |
441 | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
442 // reload display after a region has been added or removed |
813 | 443 var redisplay = function (data) { |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
444 if (!data.settings.hasRegionContent) { |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
445 packRegions(data); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
446 } |
813 | 447 fn.redisplay(data); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
448 }; |
813 | 449 |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
450 // event handler, reads region parameter and creates region divs |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
451 var handleSetup = function (evt) { |
941 | 452 var data = this; |
813 | 453 console.debug("regions: handleSetup", data.settings.rg); |
823 | 454 // regions with content are given in HTML divs |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
455 if (data.settings.hasRegionContent) { |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
456 createRegionsFromHTML(data); |
823 | 457 // regions are defined in the URL |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
458 } else { |
851 | 459 createRegionsFromURL(data); |
855 | 460 fn.highlightButtons(data, 'regionhtml', data.settings.showRegionInfo); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
461 } |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
462 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
463 |
851 | 464 // event handler, sets buttons and shows regions when scaler img is reloaded |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
465 var handleUpdate = function (evt) { |
941 | 466 var data = this; |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
467 console.debug("regions: handleUpdate"); |
848 | 468 var settings = data.settings; |
469 fn.highlightButtons(data, 'regions' , settings.isRegionVisible); | |
855 | 470 fn.highlightButtons(data, 'regionhtml' , settings.showRegionInfo); |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
471 renderRegions(data); |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
472 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
473 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
474 // plugin installation called by digilib on plugin object. |
813 | 475 var install = function(plugin) { |
476 digilib = plugin; | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
477 console.debug('installing regions plugin. digilib:', digilib); |
813 | 478 fn = digilib.fn; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
479 // import geometry classes |
813 | 480 geom = fn.geometry; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
481 // add defaults, actions, buttons |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
482 $.extend(digilib.defaults, defaults); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
483 $.extend(digilib.actions, actions); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
484 $.extend(digilib.buttons, buttons); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
485 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
486 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
487 // plugin initialization |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
488 var init = function (data) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
489 console.debug('initialising regions plugin. data:', data); |
835 | 490 var $elem = data.$elem; |
996 | 491 var cssPrefix = data.settings.cssPrefix; |
823 | 492 // regions array |
493 data.regions = []; | |
835 | 494 // regions div |
996 | 495 var $html = $('<div class="'+cssPrefix+'keep '+cssPrefix+'regionHTML"/>'); |
835 | 496 $elem.append($html); |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
497 data.$htmlDiv = $html; |
848 | 498 // install event handler |
499 var $data = $(data); | |
920 | 500 $data.on('setup', handleSetup); |
501 $data.on('update', handleUpdate); | |
848 | 502 var settings = data.settings; |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
503 // neither URL-defined regions nor buttons when regions are predefined in HTML |
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
504 if (!settings.hasRegionContent) { |
848 | 505 var mode = settings.interactionMode; |
823 | 506 // add "rg" to digilibParamNames |
848 | 507 settings.digilibParamNames.push('rg'); |
823 | 508 // additional buttons |
848 | 509 var buttonSettings = settings.buttonSettings[mode]; |
823 | 510 // configure buttons through digilib "regionSet" option |
848 | 511 var buttonSet = settings.regionSet || regionSet; |
823 | 512 // set regionSet to [] or '' for no buttons (when showing regions only) |
513 if (buttonSet.length && buttonSet.length > 0) { | |
851 | 514 buttonSettings.regionSet = buttonSet; |
823 | 515 buttonSettings.buttonSets.push('regionSet'); |
516 } | |
813 | 517 } |
792 | 518 }; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
519 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
520 // plugin object with name and install/init methods |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
521 // shared objects filled by digilib on registration |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
522 var pluginProperties = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
523 name : 'region', |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
524 install : install, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
525 init : init, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
526 buttons : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
527 actions : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
528 fn : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
529 plugins : {} |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
530 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
531 |
792 | 532 if ($.fn.digilib == null) { |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
533 $.error("jquery.digilib.regions must be loaded after jquery.digilib!"); |
792 | 534 } else { |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
535 $.fn.digilib('plugin', pluginProperties); |
792 | 536 } |
537 })(jQuery); |