Mercurial > hg > digilib-old
annotate webapp/src/main/webapp/jquery/jquery.digilib.regions.js @ 1107:365f95a14057
factor out defineArea, using overlay div
author | hertzhaft |
---|---|
date | Sat, 27 Oct 2012 00:06:29 +0200 |
parents | 415da4e4b76b |
children | ae796bcac7f4 |
rev | line source |
---|---|
792 | 1 /** optional digilib regions plugin |
2 | |
1023 | 3 Mark up a digilib image with rectangular regions. |
4 | |
1102 | 5 If the parameter "processHtmlRegions" is set, the plugin reads region data from HTML. |
6 Region data should be declared inside in the digilib element, like so: | |
792 | 7 |
1025 | 8 <map class="dl-keep dl-regioncontent"> |
9 <area href="http://www.mpiwg-berlin.mpg.de" coords="0.1,0.1,0.4,0.1" alt="MPI fuer Wissenschaftsgeschichte"/> | |
10 <area href="http://www.biblhertz.it" coords="0.5,0.8,0.4,0.1" alt="Bibliotheca Hertziana"/> | |
11 <area coords="0.3,0.5,0.15,0.1" /> | |
12 </map> | |
1023 | 13 |
1067 | 14 According to the HTML specs, "area" and "a" elements are allowed inside of a "map". |
15 Both can have a "coords" attribute, but "area" elements can't contain child nodes. | |
1102 | 16 To have regions with content use "a" tags, like so:. |
1067 | 17 |
18 <map class="dl-keep dl-regioncontent"> | |
19 <a href="http://www.mpiwg-berlin.mpg.de" coords="0.4907,0.3521,0.1458,0.107"> | |
20 MPI fuer Wissenschaftsgeschichte | |
21 </a> | |
22 <a href="http://www.biblhertz.it" coords="0.3413,0.2912,0.4345,0.2945"> | |
23 Bibliotheca Hertziana | |
24 </a> | |
25 <area coords="0.3,0.5,0.15,0.1" /> | |
26 </map> | |
27 | |
1102 | 28 Regions can also be defined in Javascript: |
29 Set the parameter "regions" to an array with items. | |
30 Each item should be an object containing the following fields: | |
31 | |
32 rect | |
33 a retangle with relative coordinates (0..1); | |
34 index (optional) | |
35 a number to display in the region | |
36 attributes (optional) | |
37 HTML attributes for the region (id, class, title) | |
38 inner (optional) | |
39 inner HTML (has to be a jQuery object) | |
40 | |
792 | 41 */ |
42 | |
43 (function($) { | |
813 | 44 // the digilib object |
996 | 45 var digilib = null; |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
46 // the normal zoom area |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
47 var FULL_AREA = null; |
813 | 48 // the functions made available by digilib |
1048 | 49 var fn = { |
50 // dummy function to avoid errors, gets overwritten by buttons plugin | |
51 highlightButtons : function () { | |
52 console.debug('regions: dummy function - highlightButtons'); | |
53 } | |
54 }; | |
813 | 55 // affine geometry plugin |
996 | 56 var geom = null; |
1102 | 57 // convenience variable, set in init() |
58 var CSS = ''; | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
59 |
792 | 60 var buttons = { |
1091 | 61 defineregion : { |
62 onclick : "defineUserRegion", | |
813 | 63 tooltip : "define a region", |
792 | 64 icon : "addregion.png" |
65 }, | |
1091 | 66 removeregion : { |
67 onclick : "removeUserRegion", | |
68 tooltip : "delete the last user defined region", | |
792 | 69 icon : "delregion.png" |
70 }, | |
1091 | 71 removeallregions : { |
72 onclick : "removeAllUserRegions", | |
73 tooltip : "delete all user defined regions", | |
1067 | 74 icon : "delallregions.png" |
75 }, | |
792 | 76 regions : { |
77 onclick : "toggleRegions", | |
78 tooltip : "show or hide regions", | |
79 icon : "regions.png" | |
80 }, | |
1092 | 81 findcoords : { |
82 onclick : "findCoords", | |
1098 | 83 tooltip : "find a region by entering its relative coordinates", |
84 icon : "regions.png" | |
85 }, | |
86 finddata : { | |
87 onclick : "findData", | |
88 tooltip : "find a region by entering text", | |
1092 | 89 icon : "regions.png" |
90 }, | |
1048 | 91 regioninfo : { |
855 | 92 onclick : "showRegionInfo", |
1091 | 93 tooltip : "show information about user defined regions", |
792 | 94 icon : "regioninfo.png" |
95 } | |
96 }; | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
97 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
98 var defaults = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
99 // are regions shown? |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
100 'isRegionVisible' : true, |
813 | 101 // are region numbers shown? |
1023 | 102 'showRegionNumbers' : true, |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
103 // default width for region when only point is given |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
104 'regionWidth' : 0.005, |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
105 // is there region content in the page? |
1027
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
106 'processHtmlRegions' : false, |
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
107 // region defined by users and in the URL |
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
108 'processUserRegions' : true, |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
109 // callback for click on region |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
110 'onClickRegion' : null, |
1091 | 111 // callback when new user region is defined |
112 'onNewRegion' : null, | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
113 // turn any region into a clickable link to its detail view (DEPRECATED) |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
114 'autoZoomOnClick' : false, |
1091 | 115 // css selector for area/a elements (must also be marked with class "dl-keep") |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
116 'areaSelector' : 'map.dl-regioncontent area, map.dl-regioncontent a', |
1104 | 117 // general buttonset of this plugin |
118 'regionSet' : ['regions', 'findcoords', 'finddata', 'lessoptions'], | |
119 // buttonset for region editing by user | |
120 'userRegionSet' : ['defineregion', 'removeregion', 'removeallregions', 'regioninfo'], | |
813 | 121 // url param for regions |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
122 'rg' : null, |
1102 | 123 // array with region data |
124 'regions' : null, | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
125 // region attributes to copy from HTML |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
126 'regionAttributes' : { |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
127 'id' :1, |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
128 'href' :1, |
1102 | 129 'name' :1, |
130 'alt' :1, | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
131 'target':1, |
1102 | 132 'title' :1, |
133 'style' :1 | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
134 } |
813 | 135 }; |
792 | 136 |
137 var actions = { | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
138 |
792 | 139 // define a region interactively with two clicked points |
1091 | 140 defineUserRegion : function(data) { |
813 | 141 if (!data.settings.isRegionVisible) { |
142 alert("Please turn on regions visibility!"); | |
143 return; | |
144 } | |
1107 | 145 var onComplete = function(data, rect) { |
146 if (rect == null) return; | |
1103 | 147 var count = getRegions(data, 'regionURL').length; |
1107 | 148 var attr = {'class' : CSS+'regionURL '+CSS+'overlay'}; |
149 var item = {'rect' : rect, 'index' : count, 'attributes' : attr}; | |
150 var $regionDiv = addRegionDiv(data, item); | |
1091 | 151 fn.highlightButtons(data, 'defineregion', 0); |
813 | 152 redisplay(data); |
1095 | 153 $(data).trigger('newRegion', [$regionDiv]); |
1107 | 154 }; |
1091 | 155 fn.highlightButtons(data, 'defineregion', 1); |
1107 | 156 fn.defineArea(data, onComplete, CSS+'regionArea'); |
792 | 157 }, |
158 | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
159 // remove the last added URL region |
1091 | 160 removeUserRegion : function (data) { |
813 | 161 if (!data.settings.isRegionVisible) { |
162 alert("Please turn on regions visibility!"); | |
163 return; | |
164 } | |
1102 | 165 var selector = 'div.'+CSS+'regionURL'; |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
166 var $regionDiv = data.$elem.find(selector).last(); |
1107 | 167 if ($regionDiv.length == 0) return; |
792 | 168 $regionDiv.remove(); |
813 | 169 redisplay(data); |
792 | 170 }, |
171 | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
172 // remove all manually added regions (defined through URL "rg" parameter) |
1091 | 173 removeAllUserRegions : function (data) { |
1067 | 174 if (!data.settings.isRegionVisible) { |
175 alert("Please turn on regions visibility!"); | |
176 return; | |
177 } | |
1102 | 178 var selector = 'div.'+CSS+'regionURL'; |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
179 var $regionDivs = data.$elem.find(selector); |
1107 | 180 if ($regionDivs.length == 0) return; |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
181 $regionDivs.remove(); |
1067 | 182 redisplay(data); |
183 }, | |
184 | |
813 | 185 // show/hide regions |
1060 | 186 toggleRegions : function (data) { |
813 | 187 var show = !data.settings.isRegionVisible; |
188 data.settings.isRegionVisible = show; | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
189 fn.highlightButtons(data, 'regions', show); |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
190 renderRegions(data, 1); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
191 }, |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
192 |
1048 | 193 // show region info in a window |
1060 | 194 showRegionInfo : function (data) { |
1048 | 195 var $elem = data.$elem; |
1102 | 196 var infoSelector = '#'+CSS+'regionInfo'; |
1098 | 197 if (fn.isOnScreen(data, infoSelector)) return; // already onscreen |
1048 | 198 var html = '\ |
1102 | 199 <div id="'+CSS+'regionInfo" class="'+CSS+'keep '+CSS+'regionInfo">\ |
200 <table class="'+CSS+'infoheader">\ | |
1060 | 201 <tr>\ |
1102 | 202 <td class="'+CSS+'infobutton html">HTML</td>\ |
203 <td class="'+CSS+'infobutton svgattr">SVG</td>\ | |
204 <td class="'+CSS+'infobutton csv">CSV</td>\ | |
205 <td class="'+CSS+'infobutton digilib">Digilib</td>\ | |
206 <td class="'+CSS+'infobutton x">X</td>\ | |
1060 | 207 </tr>\ |
208 </table>\ | |
1048 | 209 </div>'; |
210 $info = $(html); | |
211 $info.appendTo($elem); | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
212 var $regions = getRegions(data, 'regionURL'); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
213 $info.append(regionInfoHTML(data, $regions)); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
214 $info.append(regionInfoSVG(data, $regions)); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
215 $info.append(regionInfoCSV(data, $regions)); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
216 $info.append(regionInfoDigilib(data, $regions)); |
1048 | 217 var bind = function(name) { |
1060 | 218 $info.find('.'+name).on('click.regioninfo', function () { |
1102 | 219 $info.find('div.'+CSS+'info').hide(); |
220 $info.find('div.'+CSS+name).show(); | |
1060 | 221 fn.centerOnScreen(data, $info); |
1107 | 222 return false; |
832
e4133946a9ad
display regions as HTML for use in digilib element
hertzhaft
parents:
824
diff
changeset
|
223 }); |
1048 | 224 }; |
225 bind('html'); | |
226 bind('svgattr'); | |
1060 | 227 bind('csv'); |
1048 | 228 bind('digilib'); |
1107 | 229 $info.on('click.regioninfo', function () { |
230 fn.withdraw($info); | |
231 }); | |
1048 | 232 $info.find('.x').on('click.regioninfo', function () { |
233 fn.withdraw($info); | |
234 }); | |
235 $info.fadeIn(); | |
1060 | 236 fn.centerOnScreen(data, $info); |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
237 }, |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
238 |
1091 | 239 // display region coordinates in an edit line |
1095 | 240 showRegionCoords : function (data, $regionDiv) { |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
241 var $elem = data.$elem; |
1095 | 242 var rect = $regionDiv.data('rect'); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
243 var text = $regionDiv.data('text'); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
244 var coordString = packCoords(rect, ','); |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
245 var html = '\ |
1102 | 246 <div id="'+CSS+'regionInfo" class="'+CSS+'keep '+CSS+'regionInfo">\ |
1098 | 247 <div>'+text+'</div>\ |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
248 <input name="coords" type="text" size="30" maxlength="40" value="'+coordString+'"/>\ |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
249 </div>'; |
1091 | 250 var $info = $(html); |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
251 $info.appendTo($elem); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
252 $div = $info.find('div'); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
253 $div.text(text); |
1091 | 254 $input = $info.find('input'); |
255 $input.on('focus.regioninfo', function (event) { | |
256 this.select(); | |
257 }); | |
258 $input.on('blur.regioninfo', function (event) { | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
259 fn.withdraw($info); |
1091 | 260 return false; |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
261 }); |
1091 | 262 $input.on('keypress.regioninfo', function (event) { |
1092 | 263 fn.withdraw($info); // OBS: "return false" disables copy! |
1091 | 264 }); |
265 $input.prop("readonly",true); | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
266 $info.fadeIn(); |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
267 fn.centerOnScreen(data, $info); |
1091 | 268 $input.focus(); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
269 console.debug('showRegionCoords', coordString); |
1092 | 270 }, |
1091 | 271 |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
272 // draw a find region from coords and move into view |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
273 regionFromCoords : function (data, coords) { |
1095 | 274 var rect = parseCoords(data, coords); |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
275 if (rect == null) { |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
276 alert('invalid coordinates: ' + coords); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
277 return; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
278 } |
1107 | 279 var attr = { 'class' : CSS+'findregion' }; |
1102 | 280 var item = { 'rect' : rect, 'attributes' : attr }; |
281 var $regionDiv = addRegionDiv(data, item); | |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
282 var za = data.zoomArea; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
283 if (!fn.isFullArea(za)) { |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
284 za.setCenter(rect.getCenter()).stayInside(FULL_AREA); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
285 if (!za.containsRect(rect)) { |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
286 za = FULL_AREA.copy(); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
287 } |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
288 fn.setZoomArea(data, za); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
289 } |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
290 console.debug('regionFromCoords', coords, rect, za); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
291 redisplay(data); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
292 }, |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
293 |
1092 | 294 // find coordinates and display as new region |
295 findCoords : function (data) { | |
296 var $elem = data.$elem; | |
1102 | 297 var findSelector = '#'+CSS+'regionFindCoords'; |
1098 | 298 if (fn.isOnScreen(data, findSelector)) return; // already onscreen |
1092 | 299 var html = '\ |
1102 | 300 <div id="'+CSS+'regionFindCoords" class="'+CSS+'keep '+CSS+'regionInfo">\ |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
301 <div>coordinates to find:</div>\ |
1102 | 302 <form class="'+CSS+'form">\ |
1092 | 303 <div>\ |
1102 | 304 <input class="'+CSS+'input" name="coords" type="text" size="30" maxlength="40"/> \ |
1092 | 305 </div>\ |
1102 | 306 <input class="'+CSS+'submit" type="submit" name="sub" value="Ok"/>\ |
307 <input class="'+CSS+'cancel" type="button" value="Cancel"/>\ | |
1092 | 308 </form>\ |
309 </div>'; | |
310 var $info = $(html); | |
311 $info.appendTo($elem); | |
312 var $form = $info.find('form'); | |
1102 | 313 var $input = $info.find('input.'+CSS+'input'); |
1092 | 314 // handle submit |
315 $form.on('submit', function () { | |
316 var coords = $input.val(); | |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
317 actions.regionFromCoords(data, coords); |
1092 | 318 fn.withdraw($info); |
319 return false; | |
1102 | 320 }); |
1107 | 321 // handle blur |
322 $input.on('blur', function () { | |
323 fn.withdraw($info); | |
324 }); | |
1092 | 325 // handle cancel |
1102 | 326 $form.find('.'+CSS+'cancel').on('click', function () { |
1092 | 327 fn.withdraw($info); |
1102 | 328 }); |
1092 | 329 $info.fadeIn(); |
330 fn.centerOnScreen(data, $info); | |
331 $input.focus(); | |
1098 | 332 }, |
333 | |
334 // find text data and display as new region | |
335 findData : function (data) { | |
336 var $elem = data.$elem; | |
1102 | 337 var findSelector = '#'+CSS+'regionFindData'; |
1098 | 338 if (fn.isOnScreen(data, findSelector)) return; // already onscreen |
339 var textOptions = getTextOptions(data, 'regionHTML'); | |
340 var html = '\ | |
1102 | 341 <div id="'+CSS+'regionFindData" class="'+CSS+'keep '+CSS+'regionInfo">\ |
1098 | 342 <div>text to find:</div>\ |
1102 | 343 <form class="'+CSS+'form">\ |
1098 | 344 <div>\ |
1102 | 345 <select class="'+CSS+'findData">\ |
1098 | 346 '+textOptions+'\ |
347 </select>\ | |
348 </div>\ | |
1102 | 349 <input class="'+CSS+'input" name="data" type="text" size="30" maxlength="40"/> \ |
350 <input class="'+CSS+'submit" type="submit" name="sub" value="Ok"/>\ | |
351 <input class="'+CSS+'cancel" type="button" value="Cancel"/>\ | |
1098 | 352 </form>\ |
353 </div>'; | |
354 var $info = $(html); | |
355 $info.appendTo($elem); | |
356 var $form = $info.find('form'); | |
1102 | 357 var $input = $info.find('input.'+CSS+'input'); |
1098 | 358 var $select = $info.find('select'); |
359 var findRegion = function () { | |
360 var coords = $select.val(); | |
361 actions.regionFromCoords(data, coords); | |
362 fn.withdraw($info); | |
363 return false; | |
364 }; | |
365 // handle submit | |
366 $form.on('submit', findRegion); | |
367 $select.on('change', findRegion); | |
368 // handle cancel | |
1102 | 369 $form.find('.'+CSS+'cancel').on('click', function () { |
1098 | 370 fn.withdraw($info); |
1102 | 371 }); |
1098 | 372 $info.fadeIn(); |
373 fn.centerOnScreen(data, $info); | |
374 $input.focus(); | |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
375 } |
792 | 376 }; |
377 | |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
378 // make a coords string |
1095 | 379 var packCoords = function (rect, sep) { |
1091 | 380 if (sep == null) sep = ','; // comma as default separator |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
381 return [ |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
382 fn.cropFloatStr(rect.x), |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
383 fn.cropFloatStr(rect.y), |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
384 fn.cropFloatStr(rect.width), |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
385 fn.cropFloatStr(rect.height) |
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
386 ].join(sep); |
813 | 387 }; |
388 | |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
389 // create a rectangle from a coords string |
1095 | 390 var parseCoords = function (data, coords) { |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
391 var pos = coords.match(/[0-9.]+/g); // TODO: check validity? |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
392 if (pos == null) { |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
393 return null; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
394 } |
1092 | 395 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
396 if (!fn.isNumber(rect.x) || !fn.isNumber(rect.y)) { |
1092 | 397 return null; |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
398 } |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
399 if (!rect.getArea()) { |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
400 var pt = rect.getPosition(); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
401 rect.width = data.settings.regionWidth; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
402 rect.height = rect.width; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
403 rect.setCenter(pt); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
404 } |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
405 return rect; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
406 }; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
407 |
1095 | 408 // create a new regionDiv and add it to data.$elem |
1102 | 409 var newRegionDiv = function (data, attr) { |
410 var cls = CSS+'region'; | |
1107 | 411 var $regionDiv = $('<div class="'+cls+'"/>'); |
1102 | 412 addRegionAttributes(data, $regionDiv, attr); |
1095 | 413 data.$elem.append($regionDiv); |
414 return $regionDiv; | |
415 }; | |
416 | |
417 // copy attributes to a region div | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
418 var addRegionAttributes = function (data, $regionDiv, attributes) { |
1095 | 419 if (attributes == null) return; |
420 if (attributes['class']) { | |
421 $regionDiv.addClass(attributes['class']); | |
422 delete attributes['class']; | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
423 } |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
424 if (attributes['href']) { |
1102 | 425 $regionDiv.data('href', attributes['href']); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
426 delete attributes['href']; |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
427 } |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
428 if (attributes['title']) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
429 $regionDiv.data('text', attributes['title']); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
430 } |
1095 | 431 $regionDiv.attr(attributes); |
432 }; | |
433 | |
434 // set region number | |
435 var addRegionNumber = function (data, $regionDiv, index) { | |
1103 | 436 if (!data.settings.showRegionNumbers) return; |
437 if (!fn.isNumber(index)) return; | |
1102 | 438 var $number = $('<a class="'+CSS+'regionnumber">'+index+'</a>'); |
1095 | 439 $regionDiv.append($number); |
440 return $regionDiv; | |
441 }; | |
442 | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
443 // construct a region from a rectangle |
1102 | 444 var addRegionDiv = function (data, item) { |
445 var $regionDiv = newRegionDiv(data, item.attributes); | |
1095 | 446 var settings = data.settings; |
1102 | 447 // add region number |
1103 | 448 addRegionNumber(data, $regionDiv, item.index); |
1102 | 449 // add inner HTML |
450 if (item.inner) { | |
451 $regionDiv.append(item.inner); | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
452 } |
1102 | 453 // store the coordinates in data |
454 $regionDiv.data('rect', item.rect); | |
455 // trigger a region event on click | |
1095 | 456 $regionDiv.on('click.dlRegion', function(evt) { |
1102 | 457 $(data).trigger('regionClick', [$regionDiv]); |
458 }); | |
1095 | 459 return $regionDiv; |
460 }; | |
461 | |
1102 | 462 // create regions from a Javascript array of items |
463 var createRegionsFromJS = function (data, items) { | |
464 $.each(items, function(index, item) { | |
465 addRegionDiv(data, item); | |
466 }); | |
467 }; | |
468 | |
851 | 469 // create regions from URL parameters |
470 var createRegionsFromURL = function (data) { | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
471 var userRegions = unpackRegions(data); |
1102 | 472 if (!userRegions) return; |
473 createRegionsFromJS(data, userRegions); | |
792 | 474 }; |
475 | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
476 // create regions from HTML |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
477 var createRegionsFromHTML = function (data) { |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
478 // regions are defined in "area" tags |
1102 | 479 var $areas = data.$elem.find(data.settings.areaSelector); |
480 console.debug("createRegionsFromHTML - elems found: ", $areas.length); | |
481 $areas.each(function(index, area) { | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
482 var $area = $(area); |
1102 | 483 // the "title" attribute contains the text for the tooltip |
484 var title = $area.attr('title'); | |
852 | 485 // the "coords" attribute contains the region coords (0..1) |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
486 var coords = $area.attr('coords'); |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
487 // create the rectangle |
1095 | 488 var rect = parseCoords(data, coords); |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
489 if (rect == null) { |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
490 return console.error('bad coords in HTML:', title, coords); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
491 } |
1102 | 492 // mark div class as regionHTML |
493 var cls = $area.attr('class') || ''; | |
1107 | 494 cls += ' '+CSS+'regionHTML '+CSS+'overlay'; |
1102 | 495 var attr = {'class' : cls}; |
1095 | 496 // copy attributes |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
497 for (var n in data.settings.regionAttributes) { |
1102 | 498 attr[n] = $area.attr(n); |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
499 } |
1102 | 500 // copy inner HTML |
501 var $inner = $area.contents().clone(); | |
502 if (attr.href != null) { | |
1025 | 503 // wrap contents in a-tag |
1102 | 504 var $a = $('<a href="'+attr.href+'"/>'); |
505 $inner = $a.append($inner); | |
1025 | 506 } |
1102 | 507 var item = {'rect' : rect, 'attributes' : attr, 'inner' : $inner}; |
508 var $regionDiv = addRegionDiv(data, item); | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
509 }); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
510 }; |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
511 |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
512 // select region divs (HTML or URL) |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
513 var getRegions = function (data, selector) { |
1102 | 514 var $regions = data.$elem.find('div.'+CSS+selector); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
515 return $regions; |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
516 }; |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
517 |
1098 | 518 // make text data options html |
519 var getTextOptions = function (data, selector) { | |
1102 | 520 var createOption = function(item, index) { |
1098 | 521 var $item = $(item); |
522 var rect = $item.data('rect'); | |
523 if (rect == null) | |
524 return null; | |
525 var coords = packCoords(rect, ','); | |
526 var text = $item.data('text'); | |
527 return '<option value="'+coords+'">'+text+'</option>'; | |
528 }; | |
529 var $regions = getRegions(data, selector); | |
1102 | 530 var options = $.map($regions, createOption); |
1098 | 531 return options.join(''); |
532 }; | |
533 | |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
534 // list of HTML regions matching text in its title attribute |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
535 var matchRegionText = function (data, text) { |
1098 | 536 var $regions = getRegions(data, 'regionHTML'); |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
537 var re = new RegExp(text); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
538 return $.grep($regions, function($item, index) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
539 return re.match($item.data('text')); |
1094
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
540 }); |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
541 }; |
8d6bc18f7145
some polishing and refactoring, preparation for region text search
hertzhaft
parents:
1093
diff
changeset
|
542 |
1095 | 543 // html for later insertion |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
544 var regionInfoHTML = function (data, $regions) { |
1102 | 545 var $infoDiv = $('<div class="'+CSS+'info '+CSS+'html"/>'); |
546 $infoDiv.append($('<div/>').text('<map class="'+CSS+'keep '+CSS+'regioncontent">')); | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
547 $regions.each(function(index, region) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
548 var rect = $(region).data('rect'); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
549 var coords = packCoords(rect, ','); |
1095 | 550 $infoDiv.append($('<div/>').text('<area coords="' + coords + '"/>')); |
551 }); | |
552 $infoDiv.append($('<div/>').text('</map>')); | |
553 return $infoDiv; | |
554 }; | |
555 | |
556 // SVG-style | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
557 var regionInfoSVG = function (data, $regions) { |
1102 | 558 var $infoDiv = $('<div class="'+CSS+'info '+CSS+'svgattr"/>'); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
559 $regions.each(function(index, region) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
560 var rect = $(region).data('rect'); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
561 var coords = packCoords(rect, ','); |
1095 | 562 $infoDiv.append($('<div/>').text('"' + coords + '"')); |
563 }); | |
564 return $infoDiv; | |
565 }; | |
566 | |
567 // CSV-style | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
568 var regionInfoCSV = function (data, $regions) { |
1102 | 569 var $infoDiv = $('<div class="'+CSS+'info '+CSS+'csv"/>'); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
570 $regions.each(function(index, region) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
571 var rect = $(region).data('rect'); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
572 var coords = packCoords(rect, ','); |
1095 | 573 $infoDiv.append($('<div/>').text(index+1 + ": " + coords)); |
574 }); | |
575 return $infoDiv; | |
576 }; | |
577 | |
578 // digilib-style (h,w@x,y) | |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
579 var regionInfoDigilib = function (data, $regions) { |
1102 | 580 var $infoDiv = $('<div class="'+CSS+'info '+CSS+'digilib"/>'); |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
581 $regions.each(function(index, region) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
582 var rect = $(region).data('rect'); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
583 var coords = packCoords(rect, ','); |
1095 | 584 $infoDiv.append($('<div/>').text(coords)); |
585 }); | |
586 return $infoDiv; | |
587 }; | |
588 | |
813 | 589 // show a region on top of the scaler image |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
590 var renderRegion = function (data, $regionDiv, anim) { |
813 | 591 if (!data.imgTrafo) return; |
950
164d69e2c481
much nicer implementation of preview for scrolling and zooming.
robcast
parents:
947
diff
changeset
|
592 var zoomArea = data.zoomArea; |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
593 var rect = $regionDiv.data('rect').copy(); |
813 | 594 var show = data.settings.isRegionVisible; |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
595 if (show && zoomArea.overlapsRect(rect) && !rect.containsRect(zoomArea)) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
596 rect.clipTo(zoomArea); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
597 var screenRect = data.imgTrafo.transform(rect); |
1067 | 598 // console.debug("renderRegion: pos=",geom.position(screenRect)); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
599 if (anim) { |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
600 $regionDiv.fadeIn(); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
601 } else{ |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
602 $regionDiv.show(); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
603 } |
1024 | 604 // adjustDiv sets wrong coords when called BEFORE show() |
851 | 605 screenRect.adjustDiv($regionDiv); |
813 | 606 } else { |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
607 if (anim) { |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
608 $regionDiv.fadeOut(); |
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
609 } else{ |
824 | 610 $regionDiv.hide(); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
611 } |
813 | 612 } |
613 }; | |
614 | |
615 // show regions | |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
616 var renderRegions = function (data, anim) { |
1096
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
617 var render = function(index, region) { |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
618 renderRegion(data, $(region), anim); |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
619 }; |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
620 var $regions = getRegions(data, 'region') |
3c75dda660bf
more refactoring: use jquery selections instead of arrays
hertzhaft
parents:
1095
diff
changeset
|
621 $regions.each(render); |
813 | 622 }; |
623 | |
1091 | 624 // read region data from URL parameters |
813 | 625 var unpackRegions = function (data) { |
626 var rg = data.settings.rg; | |
1095 | 627 if (rg == null) return []; |
628 var coords = rg.split(","); | |
1098 | 629 var regions = $.map(coords, function(coord, index) { |
1095 | 630 var pos = coord.split("/", 4); |
813 | 631 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); |
1107 | 632 var attr = {'class' : CSS+"regionURL "+CSS+"overlay"}; |
1102 | 633 var item = {'rect' : rect, 'index' : index+1, 'attributes' : attr}; |
634 return item; | |
1095 | 635 }); |
636 return regions; | |
792 | 637 }; |
638 | |
1091 | 639 // pack user regions array into a URL parameter string |
813 | 640 var packRegions = function (data) { |
1102 | 641 var $regions = getRegions(data, 'regionURL'); |
1103 | 642 if ($regions.length == 0 || !data.settings.processUserRegions) { |
815 | 643 data.settings.rg = null; |
644 return; | |
645 } | |
1102 | 646 var pack = function(region, index) { |
1099 | 647 var $region = $(region); |
648 var rect = $region.data('rect'); | |
1102 | 649 var packed = packCoords(rect, '/'); |
650 return packed; | |
1099 | 651 }; |
1102 | 652 var coords = $.map($regions, pack); |
1095 | 653 var rg = coords.join(','); |
813 | 654 data.settings.rg = rg; |
1088
16215fc22fbf
better separation of HTML and user defined regions
hertzhaft
parents:
1067
diff
changeset
|
655 console.debug('pack regions:', rg); |
792 | 656 }; |
657 | |
1095 | 658 // zoom to the region coordinates |
659 var zoomToRegion = function (data, region) { | |
660 digilib.actions.zoomArea(data, region); | |
661 }; | |
662 | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
663 // reload display after a region has been added or removed |
813 | 664 var redisplay = function (data) { |
1103 | 665 packRegions(data); |
813 | 666 fn.redisplay(data); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
667 }; |
813 | 668 |
1091 | 669 // event handler, gets called when a newRegion event is triggered |
1095 | 670 var handleNewRegion = function (evt, $regionDiv) { |
1091 | 671 var data = this; |
672 var settings = data.settings; | |
1095 | 673 console.debug("regions: handleNewRegion", $regionDiv); |
1091 | 674 if (typeof settings.onNewRegion === 'function') { |
675 // execute callback | |
1095 | 676 return settings.onNewRegion(data, $regionDiv); |
1091 | 677 } |
678 if (typeof settings.onNewRegion === 'string') { | |
679 // execute action | |
1095 | 680 return actions[settings.onNewRegion](data, $regionDiv); |
1091 | 681 } |
682 }; | |
683 | |
684 // event handler, gets called when a regionClick event is triggered | |
1095 | 685 var handleRegionClick = function (evt, $regionDiv) { |
1091 | 686 var data = this; |
687 var settings = data.settings; | |
1095 | 688 console.debug("regions: handleRegionClick", $regionDiv); |
689 if ($regionDiv.data('href')) { | |
1091 | 690 // follow the href attribute of the region area |
1095 | 691 window.location = $regionDiv.data('href'); //TODO: how about target? |
1091 | 692 } |
693 if (typeof settings.onClickRegion === 'function') { | |
694 // execute callback | |
1095 | 695 return settings.onClickRegion(data, $regionDiv); |
1091 | 696 } |
697 if (typeof settings.onClickRegion === 'string') { | |
698 // execute action | |
1095 | 699 return actions[settings.onClickRegion](data, $regionDiv); |
1091 | 700 } |
701 }; | |
702 | |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
703 // 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
|
704 var handleSetup = function (evt) { |
941 | 705 var data = this; |
1027
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
706 var settings = data.settings; |
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
707 console.debug("regions: handleSetup", settings.rg); |
1102 | 708 // regions with content are given in a Javascript array |
709 if (settings.regions) { | |
710 createRegionsFromJS(data, settings.regions); | |
711 } | |
1048 | 712 // regions with content are given in HTML divs |
1027
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
713 if (settings.processHtmlRegions) { |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
714 createRegionsFromHTML(data); |
1027
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
715 } |
1048 | 716 // regions are defined in the URL |
1027
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
717 if (settings.processUserRegions) { |
851 | 718 createRegionsFromURL(data); |
818
eff74cfaaf97
read regions from HTML (not working yet), show info
hertzhaft
parents:
815
diff
changeset
|
719 } |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
720 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
721 |
851 | 722 // 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
|
723 var handleUpdate = function (evt) { |
941 | 724 var data = this; |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
725 console.debug("regions: handleUpdate"); |
848 | 726 var settings = data.settings; |
727 fn.highlightButtons(data, 'regions' , settings.isRegionVisible); | |
850
47a6b93bde43
always show html-defined regions. wrong pos still not fixed
hertzhaft
parents:
848
diff
changeset
|
728 renderRegions(data); |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
729 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
730 |
1048 | 731 // additional buttons |
732 var installButtons = function (data) { | |
733 var settings = data.settings; | |
734 var mode = settings.interactionMode; | |
735 var buttonSettings = settings.buttonSettings[mode]; | |
1104 | 736 var buttonSet = settings.regionSet; |
737 if (settings.processUserRegions) { | |
738 var first = buttonSet.slice(0,1); | |
739 var rest = buttonSet.slice(1); | |
740 buttonSet = first.concat(settings.userRegionSet, rest); | |
741 } | |
1048 | 742 // set regionSet to [] or '' for no buttons (when showing regions only) |
743 if (buttonSet.length && buttonSet.length > 0) { | |
744 buttonSettings.regionSet = buttonSet; | |
745 buttonSettings.buttonSets.push('regionSet'); | |
746 } | |
747 }; | |
748 | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
749 // plugin installation called by digilib on plugin object. |
813 | 750 var install = function(plugin) { |
751 digilib = plugin; | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
752 console.debug('installing regions plugin. digilib:', digilib); |
1048 | 753 // import digilib functions |
754 $.extend(fn, digilib.fn); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
755 // import geometry classes |
813 | 756 geom = fn.geometry; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
757 // add defaults, actions, buttons |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
758 $.extend(digilib.defaults, defaults); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
759 $.extend(digilib.actions, actions); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
760 $.extend(digilib.buttons, buttons); |
1103 | 761 // add "rg" to digilibParamNames |
762 digilib.defaults.digilibParamNames.push('rg'); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
763 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
764 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
765 // plugin initialization |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
766 var init = function (data) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
767 console.debug('initialising regions plugin. data:', data); |
835 | 768 var $elem = data.$elem; |
1027
ff19b9e936d3
allow predefined and user regions at the same time.
robcast
parents:
1025
diff
changeset
|
769 var settings = data.settings; |
1102 | 770 CSS = settings.cssPrefix; |
1093
75a54db031dd
move found region into view, keep zoom factor if possible
hertzhaft
parents:
1092
diff
changeset
|
771 FULL_AREA = geom.rectangle(0, 0, 1, 1); |
1048 | 772 // install event handlers |
848 | 773 var $data = $(data); |
920 | 774 $data.on('setup', handleSetup); |
775 $data.on('update', handleUpdate); | |
1091 | 776 $data.on('newRegion', handleNewRegion); |
777 $data.on('regionClick', handleRegionClick); | |
1095 | 778 // default: autoZoom to region, when clicked - DEPRECATED |
779 if (settings.autoZoomOnClick && settings.onClickRegion == null) { | |
780 settings.onClickRegion = zoomToRegion; | |
781 } | |
782 // install region buttons if user defined regions are allowed | |
1104 | 783 if (digilib.plugins.buttons != null) { |
1103 | 784 installButtons(data); |
813 | 785 } |
792 | 786 }; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
787 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
788 // plugin object with name and install/init methods |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
789 // shared objects filled by digilib on registration |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
790 var pluginProperties = { |
1048 | 791 name : 'regions', |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
792 install : install, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
793 init : init, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
794 buttons : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
795 actions : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
796 fn : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
797 plugins : {} |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
798 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
799 |
792 | 800 if ($.fn.digilib == null) { |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
801 $.error("jquery.digilib.regions must be loaded after jquery.digilib!"); |
792 | 802 } else { |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
803 $.fn.digilib('plugin', pluginProperties); |
792 | 804 } |
805 })(jQuery); |