Mercurial > hg > digilib
annotate client/digitallibrary/jquery/jquery.digilib.regions.js @ 865:e96f4ff42450 jquery
crop region info
| author | hertzhaft |
|---|---|
| date | Sun, 13 Mar 2011 18:50:38 +0100 |
| parents | 34b20eec036b |
| children | 3dd878bf07e0 |
| rev | line source |
|---|---|
| 785 | 1 /** optional digilib regions plugin |
| 2 | |
| 3 markup a digilib image with rectangular regions | |
| 4 | |
| 5 TODO: | |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
6 how to display regions correctly in embedded mode? |
| 785 | 7 */ |
| 8 | |
| 9 (function($) { | |
| 806 | 10 // the digilib object |
| 11 var digilib; | |
| 785 | 12 // the data object passed by digilib |
| 13 var data; | |
| 806 | 14 // the functions made available by digilib |
| 785 | 15 var fn; |
| 806 | 16 // affine geometry plugin |
| 785 | 17 var geom; |
| 18 | |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
19 var FULL_AREA; |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
20 |
| 806 | 21 var ID_PREFIX = "digilib-region-"; |
| 22 | |
| 785 | 23 var buttons = { |
| 24 addregion : { | |
| 806 | 25 onclick : "defineRegion", |
| 26 tooltip : "define a region", | |
| 785 | 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 }, | |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
39 regionhtml : { |
| 848 | 40 onclick : "showRegionInfo", |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
41 tooltip : "show information about regions", |
| 785 | 42 icon : "regioninfo.png" |
| 43 } | |
| 44 }; | |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
45 |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
46 var defaults = { |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
47 // are regions shown? |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
48 'isRegionVisible' : true, |
| 806 | 49 // are region numbers shown? |
| 50 'showRegionNumbers' : false, | |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
51 // is window with region HTML shown? |
| 848 | 52 'showRegionInfo' : false, |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
53 // is there region content in the page? |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
54 'hasRegionContent' : false, |
| 816 | 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") | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
58 'regionContentSelector' : 'div.regioncontent', |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
59 // buttonset of this plugin |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
60 'regionSet' : ['regions', 'addregion', 'delregion', 'regionhtml', 'lessoptions'], |
| 806 | 61 // url param for regions |
| 844 | 62 'rg' : null |
| 806 | 63 }; |
| 785 | 64 |
| 65 var actions = { | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
66 |
| 785 | 67 // define a region interactively with two clicked points |
| 806 | 68 "defineRegion" : function(data) { |
| 69 if (!data.settings.isRegionVisible) { | |
| 70 alert("Please turn on regions visibility!"); | |
| 71 return; | |
| 72 } | |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
73 var $elem = data.$elem; |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
74 var $body = $('body'); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
75 var bodyRect = geom.rectangle($body); |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
76 var $scaler = data.$scaler; |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
77 var scalerRect = geom.rectangle($scaler); |
| 785 | 78 var pt1, pt2; |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
79 // overlay prevents other elements from reacting to mouse events |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
80 var $overlay = $('<div class="digilib-overlay"/>'); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
81 $body.append($overlay); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
82 bodyRect.adjustDiv($overlay); |
| 816 | 83 var $regionDiv = addRegionDiv(data, data.regions.length); |
| 785 | 84 |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
85 // mousedown handler: start sizing |
| 785 | 86 var regionStart = function (evt) { |
| 87 pt1 = geom.position(evt); | |
| 88 // setup and show zoom div | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
89 pt1.adjustDiv($regionDiv); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
90 $regionDiv.width(0).height(0); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
91 $regionDiv.show(); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
92 // register mouse events |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
93 $overlay.bind("mousemove.dlRegion", regionMove); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
94 $overlay.bind("mouseup.dlRegion", regionEnd); |
| 785 | 95 return false; |
| 96 }; | |
| 97 | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
98 // mousemove handler: size region |
| 785 | 99 var regionMove = function (evt) { |
| 100 pt2 = geom.position(evt); | |
| 101 var rect = geom.rectangle(pt1, pt2); | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
102 rect.clipTo(scalerRect); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
103 // update region |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
104 rect.adjustDiv($regionDiv); |
| 785 | 105 return false; |
| 106 }; | |
| 107 | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
108 // mouseup handler: end sizing |
| 785 | 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; | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
114 // unregister mouse events and get rid of overlay |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
115 $overlay.unbind("mousemove.dlRegion", regionMove); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
116 $overlay.unbind("mouseup.dlRegion", regionEnd); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
117 $overlay.remove(); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
118 // clip region |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
119 clickRect.clipTo(scalerRect); |
|
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
120 clickRect.adjustDiv($regionDiv); |
| 806 | 121 storeRegion(data, $regionDiv); |
| 785 | 122 // fn.redisplay(data); |
| 806 | 123 fn.highlightButtons(data, 'addregion', 0); |
| 124 redisplay(data); | |
| 785 | 125 return false; |
| 126 }; | |
| 127 | |
| 128 // bind start zoom handler | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
129 $overlay.one('mousedown.dlRegion', regionStart); |
| 806 | 130 fn.highlightButtons(data, 'addregion', 1); |
| 785 | 131 }, |
| 132 | |
| 133 // remove the last added region | |
| 134 "removeRegion" : function (data) { | |
| 806 | 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; | |
| 785 | 142 $regionDiv.remove(); |
| 806 | 143 redisplay(data); |
| 785 | 144 }, |
| 145 | |
| 806 | 146 // show/hide regions |
| 147 "toggleRegions" : function (data) { | |
| 148 var show = !data.settings.isRegionVisible; | |
| 149 data.settings.isRegionVisible = show; | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
150 fn.highlightButtons(data, 'regions', show); |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
151 renderRegions(data, 1); |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
152 }, |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
153 |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
154 // show/hide region HTML code |
| 848 | 155 "showRegionInfo" : function (data) { |
| 156 var show = !data.settings.showRegionInfo; | |
| 157 data.settings.showRegionInfo = show; | |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
158 fn.highlightButtons(data, 'regionhtml', show); |
|
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
159 var $html = data.$htmlDiv; |
|
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
160 if (!show) { |
| 848 | 161 // empty the div |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
162 $html.fadeOut(function () { |
|
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
163 $html.contents().remove(); |
|
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
164 }); |
|
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
165 return; |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
166 } |
| 848 | 167 regionInfo(data); |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
168 }, |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
169 |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
170 "redraw" : function (data) { |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
171 renderRegions(data); |
| 785 | 172 } |
| 173 }; | |
| 174 | |
| 806 | 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 | |
| 848 | 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) { | |
| 865 | 219 var area = [ |
| 220 fn.cropFloatStr(r.x), | |
| 221 fn.cropFloatStr(r.y), | |
| 222 fn.cropFloatStr(r.width), | |
| 223 fn.cropFloatStr(r.height)].join(','); | |
| 848 | 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) { | |
| 865 | 234 var area = [ |
| 235 fn.cropFloatStr(r.x), | |
| 236 fn.cropFloatStr(r.y), | |
| 237 fn.cropFloatStr(r.width), | |
| 238 fn.cropFloatStr(r.height)].join(' '); | |
| 848 | 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) { | |
| 855 | 248 var area = r.toString(); |
| 848 | 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; | |
| 257 $html.append(regionInfoHeader(data)) | |
| 258 $html.append(regionInfoHTML(data)); | |
| 259 $html.append(regionInfoSVG(data)); | |
| 260 $html.append(regionInfoDigilib(data)); | |
| 261 $html.fadeIn(); | |
| 262 }; | |
| 263 | |
| 806 | 264 // add a region to data.$elem |
| 844 | 265 var addRegionDiv = function (data, index, url) { |
| 816 | 266 var nr = index + 1; // we count regions from 1 |
| 267 // create a digilib URL for this detail | |
| 844 | 268 url = url || getRegionUrl(data, index); |
| 806 | 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) { | |
| 844 | 273 var $regionLink = $('<a class="regionnumber"/>'); |
| 274 $regionLink.attr('href', url); | |
| 816 | 275 $regionLink.text(nr); |
| 844 | 276 $regionDiv.append($regionLink); |
| 806 | 277 } |
| 816 | 278 if (data.settings.autoRegionLinks) { |
| 279 $regionDiv.bind('click.dlRegion', function() { | |
| 844 | 280 window.location = url; |
| 281 }); | |
| 816 | 282 } |
| 806 | 283 return $regionDiv; |
| 284 }; | |
| 285 | |
| 844 | 286 // create a region div from the data.regions array |
| 287 var createRegionDiv = function (regions, index, url) { | |
| 288 var $regionDiv = addRegionDiv(data, index, url); | |
| 806 | 289 var region = regions[index]; |
| 290 region.$div = $regionDiv; | |
| 291 return $regionDiv; | |
| 292 }; | |
| 293 | |
| 844 | 294 // create regions from URL parameters |
| 295 var createRegionsFromURL = function (data) { | |
| 296 unpackRegions(data); | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
297 var regions = data.regions; |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
298 $.each(regions, function(i) { |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
299 createRegionDiv(regions, i); |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
300 }); |
| 785 | 301 }; |
| 302 | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
303 // create regions from HTML |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
304 var createRegionsFromHTML = function (data) { |
| 816 | 305 var regions = data.regions; |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
306 var selector = data.settings.regionContentSelector; |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
307 // regions are defined in "a" tags |
| 828 | 308 var $content = data.$elem.contents(selector).contents('a'); |
| 844 | 309 console.debug("createRegionsFromHTML. elems: ", $content); |
| 828 | 310 $content.each(function(index, a) { |
| 311 var $a = $(a); | |
| 845 | 312 // the "coords" attribute contains the region coords (0..1) |
| 313 var coords = $a.attr('coords'); | |
| 314 var pos = coords.split(",", 4); | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
315 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
316 regions.push(rect); |
| 844 | 317 // create the div |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
318 var href = $a.attr('href'); |
| 844 | 319 var $regionDiv = createRegionDiv(regions, index, href); |
| 320 var $contents = $a.contents().clone(); | |
| 321 $regionDiv.append($contents); | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
322 }); |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
323 }; |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
324 |
| 806 | 325 // show a region on top of the scaler image |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
326 var renderRegion = function (data, index, anim) { |
| 806 | 327 if (!data.imgTrafo) return; |
| 328 var $elem = data.$elem; | |
| 785 | 329 var regions = data.regions; |
| 806 | 330 if (index > regions.length) return; |
| 844 | 331 var region = regions[index]; |
| 806 | 332 var $regionDiv = region.$div; |
| 333 if (!$regionDiv) { | |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
334 console.debug("renderRegion: region has no $div", region); |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
335 // alert("renderRegion: region has no $div to show"); |
| 806 | 336 return; |
| 337 } | |
| 338 var regionRect = region.copy(); | |
| 339 var show = data.settings.isRegionVisible; | |
| 340 if (show && data.zoomArea.overlapsRect(regionRect)) { | |
| 341 regionRect.clipTo(data.zoomArea); | |
| 342 var screenRect = data.imgTrafo.transform(regionRect); | |
| 844 | 343 console.debug("renderRegion: pos=",geom.position(screenRect)); |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
344 if (anim) { |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
345 $regionDiv.fadeIn(); |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
346 } else{ |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
347 $regionDiv.show(); |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
348 } |
| 844 | 349 // for some reason adjustDiv sets wrong coords when called BEFORE show()? |
| 350 screenRect.adjustDiv($regionDiv); | |
| 806 | 351 } else { |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
352 if (anim) { |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
353 $regionDiv.fadeOut(); |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
354 } else{ |
| 817 | 355 $regionDiv.hide(); |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
356 } |
| 806 | 357 } |
| 358 }; | |
| 359 | |
| 360 // show regions | |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
361 var renderRegions = function (data, anim) { |
| 806 | 362 for (var i = 0; i < data.regions.length ; i++) { |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
363 renderRegion(data, i, anim); |
| 806 | 364 } |
| 365 }; | |
| 366 | |
| 367 var unpackRegions = function (data) { | |
| 368 // create regions from parameters | |
| 369 var rg = data.settings.rg; | |
| 370 if (rg == null) return; | |
| 371 var regions = data.regions; | |
| 845 | 372 var rs = rg.split(";"); |
| 806 | 373 for (var i = 0; i < rs.length; i++) { |
| 374 var r = rs[i]; | |
| 845 | 375 var pos = r.split(",", 4); |
| 806 | 376 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); |
| 377 regions.push(rect); | |
| 785 | 378 } |
| 379 }; | |
| 380 | |
| 806 | 381 // pack regions array into a parameter string |
| 382 var packRegions = function (data) { | |
| 383 var regions = data.regions; | |
| 808 | 384 if (!regions.length) { |
| 385 data.settings.rg = null; | |
| 386 return; | |
| 387 } | |
| 806 | 388 var rg = ''; |
| 389 for (var i = 0; i < regions.length; i++) { | |
| 390 region = regions[i]; | |
| 391 if (i) { | |
| 845 | 392 rg += ';'; |
| 785 | 393 } |
| 806 | 394 rg += [ |
| 395 fn.cropFloatStr(region.x), | |
| 396 fn.cropFloatStr(region.y), | |
| 397 fn.cropFloatStr(region.width), | |
| 398 fn.cropFloatStr(region.height) | |
| 845 | 399 ].join(','); |
| 806 | 400 } |
| 401 data.settings.rg = rg; | |
| 785 | 402 }; |
| 403 | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
404 // reload display after a region has been added or removed |
| 806 | 405 var redisplay = function (data) { |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
406 if (!data.settings.hasRegionContent) { |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
407 packRegions(data); |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
408 } |
| 806 | 409 fn.redisplay(data); |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
410 }; |
| 806 | 411 |
| 816 | 412 // for turning region numbers/region divs into links to zoomed details |
| 413 var getRegionUrl = function (data, index) { | |
| 414 var region = data.regions[index]; | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
415 var settings = data.settings; |
| 816 | 416 var params = { |
| 417 "fn" : settings.fn, | |
| 418 "pn" : settings.pn | |
| 419 }; | |
| 420 fn.packArea(params, region); | |
| 421 fn.packMarks(params, data.marks); | |
| 422 fn.packScalerFlags(params, data.scalerFlags); | |
| 423 var paramNames = digilib.defaults.digilibParamNames; | |
| 424 // build our own digilib URL without storing anything | |
| 425 var queryString = fn.getParamString(params, paramNames, digilib.defaults); | |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
426 return settings.digilibBaseUrl + '?' + queryString; |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
427 }; |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
428 |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
429 // event handler, reads region parameter and creates region divs |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
430 var handleSetup = function (evt) { |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
431 data = this; |
| 806 | 432 console.debug("regions: handleSetup", data.settings.rg); |
| 816 | 433 // regions with content are given in HTML divs |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
434 if (data.settings.hasRegionContent) { |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
435 createRegionsFromHTML(data); |
| 816 | 436 // regions are defined in the URL |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
437 } else { |
| 844 | 438 createRegionsFromURL(data); |
| 848 | 439 fn.highlightButtons(data, 'regionhtml', data.settings.showRegionInfo); |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
440 } |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
441 }; |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
442 |
| 844 | 443 // event handler, sets buttons and shows regions when scaler img is reloaded |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
444 var handleUpdate = function (evt) { |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
445 data = this; |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
446 console.debug("regions: handleUpdate"); |
| 841 | 447 var settings = data.settings; |
| 448 fn.highlightButtons(data, 'regions' , settings.isRegionVisible); | |
| 848 | 449 fn.highlightButtons(data, 'regionhtml' , settings.showRegionInfo); |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
450 renderRegions(data); |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
451 }; |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
452 |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
453 // event handler, redisplays regions (e.g. in a new position) |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
454 var handleRedisplay = function (evt) { |
| 806 | 455 data = this; |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
456 console.debug("regions: handleRedisplay"); |
| 844 | 457 // renderRegions(data); |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
458 }; |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
459 |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
460 // event handler |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
461 var handleDragZoom = function (evt, zoomArea) { |
|
811
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
462 // console.debug("regions: handleDragZoom, zoomArea:", zoomArea); |
|
29de310ed4f3
read regions from HTML (not working yet), show info
hertzhaft
parents:
808
diff
changeset
|
463 // data = this; |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
464 }; |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
465 |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
466 // plugin installation called by digilib on plugin object. |
| 806 | 467 var install = function(plugin) { |
| 468 digilib = plugin; | |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
469 console.debug('installing regions plugin. digilib:', digilib); |
| 806 | 470 fn = digilib.fn; |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
471 // import geometry classes |
| 806 | 472 geom = fn.geometry; |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
473 FULL_AREA = geom.rectangle(0,0,1,1); |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
474 // add defaults, actions, buttons |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
475 $.extend(digilib.defaults, defaults); |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
476 $.extend(digilib.actions, actions); |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
477 $.extend(digilib.buttons, buttons); |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
478 }; |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
479 |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
480 // plugin initialization |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
481 var init = function (data) { |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
482 console.debug('initialising regions plugin. data:', data); |
| 828 | 483 var $elem = data.$elem; |
| 816 | 484 // regions array |
| 485 data.regions = []; | |
| 828 | 486 // regions div |
| 487 var $html = $('<div class="keep regionHTML"/>'); | |
| 488 $elem.append($html); | |
|
825
f07b8ed13194
display regions as HTML for use in digilib element
hertzhaft
parents:
817
diff
changeset
|
489 data.$htmlDiv = $html; |
| 841 | 490 // install event handler |
| 491 var $data = $(data); | |
| 492 $data.bind('setup', handleSetup); | |
| 493 $data.bind('update', handleUpdate); | |
| 494 $data.bind('redisplay', handleRedisplay); | |
| 495 $data.bind('dragZoom', handleDragZoom); | |
| 496 var settings = data.settings; | |
|
843
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
497 var selector = data.settings.regionContentSelector; |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
498 settings.hasRegionContent = $elem.has(selector).length > 0; |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
499 // neither URL-defined regions nor buttons when regions are predefined in HTML |
|
76c23870ac0b
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
841
diff
changeset
|
500 if (!settings.hasRegionContent) { |
| 841 | 501 var mode = settings.interactionMode; |
| 816 | 502 // add "rg" to digilibParamNames |
| 841 | 503 settings.digilibParamNames.push('rg'); |
| 816 | 504 // additional buttons |
| 841 | 505 var buttonSettings = settings.buttonSettings[mode]; |
| 816 | 506 // configure buttons through digilib "regionSet" option |
| 841 | 507 var buttonSet = settings.regionSet || regionSet; |
| 816 | 508 // set regionSet to [] or '' for no buttons (when showing regions only) |
| 509 if (buttonSet.length && buttonSet.length > 0) { | |
| 844 | 510 buttonSettings.regionSet = buttonSet; |
| 816 | 511 buttonSettings.buttonSets.push('regionSet'); |
| 512 } | |
| 806 | 513 } |
| 785 | 514 }; |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
515 |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
516 // plugin object with name and install/init methods |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
517 // shared objects filled by digilib on registration |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
518 var pluginProperties = { |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
519 name : 'region', |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
520 install : install, |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
521 init : init, |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
522 buttons : {}, |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
523 actions : {}, |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
524 fn : {}, |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
525 plugins : {} |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
526 }; |
|
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
527 |
| 785 | 528 if ($.fn.digilib == null) { |
|
801
2fb1f576375d
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
792
diff
changeset
|
529 $.error("jquery.digilib.regions must be loaded after jquery.digilib!"); |
| 785 | 530 } else { |
|
792
52c5595af03e
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
785
diff
changeset
|
531 $.fn.digilib('plugin', pluginProperties); |
| 785 | 532 } |
| 533 })(jQuery); |
