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