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