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