Mercurial > hg > digilib-old
annotate client/digitallibrary/jquery/jquery.digilib.regions.js @ 826:980661e00372 stream
new default fullscreen page (uses automagic host selection)
author | robcast |
---|---|
date | Wed, 23 Feb 2011 16:00:07 +0100 |
parents | 60e8cca7ac81 |
children | eff74cfaaf97 |
rev | line source |
---|---|
792 | 1 /** optional digilib regions plugin |
2 | |
3 markup a digilib image with rectangular regions | |
4 | |
5 TODO: | |
6 - store region in params/cookie, regarding zoom, mirror, rotation (like marks) | |
7 - set regions programmatically | |
8 - read regions from params/cookie and display | |
9 - backlink mechanism | |
10 - don't write to data.settings? | |
11 */ | |
12 | |
13 (function($) { | |
813 | 14 // the digilib object |
15 var digilib; | |
792 | 16 // the data object passed by digilib |
17 var data; | |
813 | 18 // the functions made available by digilib |
792 | 19 var fn; |
813 | 20 // affine geometry plugin |
792 | 21 var geom; |
22 | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
23 var FULL_AREA; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
24 |
813 | 25 var ID_PREFIX = "digilib-region-"; |
26 | |
792 | 27 var buttons = { |
28 addregion : { | |
813 | 29 onclick : "defineRegion", |
30 tooltip : "define a region", | |
792 | 31 icon : "addregion.png" |
32 }, | |
33 delregion : { | |
34 onclick : "removeRegion", | |
35 tooltip : "delete the last region", | |
36 icon : "delregion.png" | |
37 }, | |
38 regions : { | |
39 onclick : "toggleRegions", | |
40 tooltip : "show or hide regions", | |
41 icon : "regions.png" | |
42 }, | |
43 regioninfo : { | |
44 onclick : "infoRegions", | |
45 tooltip : "information about regions", | |
46 icon : "regioninfo.png" | |
47 } | |
48 }; | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
49 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
50 var defaults = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
51 // are regions shown? |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
52 'isRegionVisible' : true, |
813 | 53 // are region numbers shown? |
54 'showRegionNumbers' : false, | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
55 // buttonset of this plugin |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
56 'regionSet' : ['addregion', 'delregion', 'regions', 'regioninfo', 'lessoptions'], |
813 | 57 // url param for regions |
58 'rg' : null, | |
59 }; | |
792 | 60 |
61 var actions = { | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
62 |
792 | 63 // define a region interactively with two clicked points |
813 | 64 "defineRegion" : function(data) { |
65 if (!data.settings.isRegionVisible) { | |
66 alert("Please turn on regions visibility!"); | |
67 return; | |
68 } | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
69 var $elem = data.$elem; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
70 var $body = $('body'); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
71 var bodyRect = geom.rectangle($body); |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
72 var $scaler = data.$scaler; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
73 var scalerRect = geom.rectangle($scaler); |
792 | 74 var pt1, pt2; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
75 // overlay prevents other elements from reacting to mouse events |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
76 var $overlay = $('<div class="digilib-overlay"/>'); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
77 $body.append($overlay); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
78 bodyRect.adjustDiv($overlay); |
813 | 79 // we count regions from 1 |
80 var $regionDiv = addRegionDiv(data, data.regions.length + 1); | |
792 | 81 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
82 // mousedown handler: start sizing |
792 | 83 var regionStart = function (evt) { |
84 pt1 = geom.position(evt); | |
85 // setup and show zoom div | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
86 pt1.adjustDiv($regionDiv); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
87 $regionDiv.width(0).height(0); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
88 $regionDiv.show(); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
89 // register mouse events |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
90 $overlay.bind("mousemove.dlRegion", regionMove); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
91 $overlay.bind("mouseup.dlRegion", regionEnd); |
792 | 92 return false; |
93 }; | |
94 | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
95 // mousemove handler: size region |
792 | 96 var regionMove = function (evt) { |
97 pt2 = geom.position(evt); | |
98 var rect = geom.rectangle(pt1, pt2); | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
99 rect.clipTo(scalerRect); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
100 // update region |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
101 rect.adjustDiv($regionDiv); |
792 | 102 return false; |
103 }; | |
104 | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
105 // mouseup handler: end sizing |
792 | 106 var regionEnd = function (evt) { |
107 pt2 = geom.position(evt); | |
108 // assume a click and continue if the area is too small | |
109 var clickRect = geom.rectangle(pt1, pt2); | |
110 if (clickRect.getArea() <= 5) return false; | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
111 // unregister mouse events and get rid of overlay |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
112 $overlay.unbind("mousemove.dlRegion", regionMove); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
113 $overlay.unbind("mouseup.dlRegion", regionEnd); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
114 $overlay.remove(); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
115 // clip region |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
116 clickRect.clipTo(scalerRect); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
117 clickRect.adjustDiv($regionDiv); |
813 | 118 storeRegion(data, $regionDiv); |
792 | 119 // fn.redisplay(data); |
813 | 120 fn.highlightButtons(data, 'addregion', 0); |
121 redisplay(data); | |
792 | 122 return false; |
123 }; | |
124 | |
125 // bind start zoom handler | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
126 $overlay.one('mousedown.dlRegion', regionStart); |
813 | 127 fn.highlightButtons(data, 'addregion', 1); |
792 | 128 }, |
129 | |
130 // remove the last added region | |
131 "removeRegion" : function (data) { | |
813 | 132 if (!data.settings.isRegionVisible) { |
133 alert("Please turn on regions visibility!"); | |
134 return; | |
135 } | |
136 var region = data.regions.pop(); | |
137 if (region == null) return; | |
138 var $regionDiv = region.$div; | |
792 | 139 $regionDiv.remove(); |
813 | 140 redisplay(data); |
792 | 141 }, |
142 | |
813 | 143 // show/hide regions |
144 "toggleRegions" : function (data) { | |
145 var show = !data.settings.isRegionVisible; | |
146 data.settings.isRegionVisible = show; | |
147 fn.highlightButtons(data, 'regions' , show); | |
148 showRegionDivs(data); | |
792 | 149 } |
150 }; | |
151 | |
152 var addRegion = actions.addRegion; | |
153 | |
813 | 154 // store a region div |
155 var storeRegion = function (data, $regionDiv) { | |
156 var regions = data.regions; | |
157 var rect = geom.rectangle($regionDiv); | |
158 var regionRect = data.imgTrafo.invtransform(rect); | |
159 regionRect.$div = $regionDiv; | |
160 regions.push(regionRect); | |
161 console.debug("regions", data.regions, "regionRect", regionRect); | |
162 }; | |
163 | |
164 // add a region to data.$elem | |
165 var addRegionDiv = function (data, nr) { | |
166 var $regionDiv = $('<div class="region overlay" style="display:none"/>'); | |
167 $regionDiv.attr("id", ID_PREFIX + nr); | |
168 data.$elem.append($regionDiv); | |
169 if (data.settings.showRegionNumbers) { | |
170 var $regionNr = $('<div class="regionnumber" />'); | |
171 $regionNr.text(nr); | |
172 $regionDiv.append($regionNr); | |
173 } | |
174 return $regionDiv; | |
175 }; | |
176 | |
177 // create a region div from the data.regions collection | |
178 var createRegionDiv = function (data, index) { | |
179 var regions = data.regions; | |
180 if (index > regions.length) return null; | |
181 var region = regions[index]; | |
182 var $regionDiv = addRegionDiv(data, index + 1); // we count regions from 1 | |
183 region.$div = $regionDiv; | |
184 // TODO store original coords in $regionDiv.data for embedded mode? | |
185 return $regionDiv; | |
186 }; | |
187 | |
188 // create regions | |
189 var createRegionDivs = function (data) { | |
190 for (var i = 0; i < data.regions.length ; i++) { | |
191 createRegionDiv(data, i); | |
192 } | |
792 | 193 }; |
194 | |
813 | 195 // show a region on top of the scaler image |
196 var showRegionDiv = function (data, index) { | |
197 if (!data.imgTrafo) return; | |
198 var $elem = data.$elem; | |
792 | 199 var regions = data.regions; |
813 | 200 if (index > regions.length) return; |
201 var region = regions[index] | |
202 var $regionDiv = region.$div; | |
203 if (!$regionDiv) { | |
204 console.debug("showRegionDiv: region has no $div", region); | |
205 // alert("showRegionDiv: region has no $div to show"); | |
206 return; | |
207 } | |
208 var regionRect = region.copy(); | |
209 var show = data.settings.isRegionVisible; | |
210 if (show && data.zoomArea.overlapsRect(regionRect)) { | |
211 regionRect.clipTo(data.zoomArea); | |
212 var screenRect = data.imgTrafo.transform(regionRect); | |
213 screenRect.adjustDiv($regionDiv); | |
214 $regionDiv.show(); | |
215 } else { | |
216 $regionDiv.hide(); | |
217 } | |
218 }; | |
219 | |
220 // show regions | |
221 var showRegionDivs = function (data) { | |
222 for (var i = 0; i < data.regions.length ; i++) { | |
223 showRegionDiv(data, i); | |
224 } | |
225 }; | |
226 | |
227 var unpackRegions = function (data) { | |
228 // create regions from parameters | |
229 var rg = data.settings.rg; | |
230 if (rg == null) return; | |
231 var regions = data.regions; | |
232 var rs = rg.split(","); | |
233 for (var i = 0; i < rs.length; i++) { | |
234 var r = rs[i]; | |
235 var pos = r.split("/", 4); | |
236 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); | |
237 regions.push(rect); | |
238 // TODO: backlink mechanism | |
239 // var url = paramString.match(/http.*$/); | |
792 | 240 } |
241 }; | |
242 | |
813 | 243 // pack regions array into a parameter string |
244 var packRegions = function (data) { | |
245 var regions = data.regions; | |
815 | 246 if (!regions.length) { |
247 data.settings.rg = null; | |
248 return; | |
249 } | |
813 | 250 var rg = ''; |
251 for (var i = 0; i < regions.length; i++) { | |
252 region = regions[i]; | |
253 if (i) { | |
254 rg += ','; | |
792 | 255 } |
813 | 256 rg += [ |
257 fn.cropFloatStr(region.x), | |
258 fn.cropFloatStr(region.y), | |
259 fn.cropFloatStr(region.width), | |
260 fn.cropFloatStr(region.height) | |
261 ].join('/'); | |
262 } | |
263 data.settings.rg = rg; | |
792 | 264 }; |
265 | |
813 | 266 var redisplay = function (data) { |
267 packRegions(data); | |
268 fn.redisplay(data); | |
269 } | |
270 | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
271 var handleSetup = function (evt) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
272 data = this; |
813 | 273 console.debug("regions: handleSetup", data.settings.rg); |
274 unpackRegions(data); | |
275 createRegionDivs(data); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
276 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
277 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
278 var handleUpdate = function (evt) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
279 data = this; |
813 | 280 fn.highlightButtons(data, 'regions' , data.settings.isRegionVisible); |
281 showRegionDivs(data); | |
282 console.debug("regions: handleUpdate", data.settings.rg); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
283 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
284 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
285 var handleRedisplay = function (evt) { |
813 | 286 data = this; |
287 showRegionDivs(data); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
288 console.debug("regions: handleRedisplay"); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
289 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
290 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
291 var handleDragZoom = function (evt, zoomArea) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
292 console.debug("regions: handleDragZoom, zoomArea:", zoomArea); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
293 data = this; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
294 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
295 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
296 // plugin installation called by digilib on plugin object. |
813 | 297 var install = function(plugin) { |
298 digilib = plugin; | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
299 console.debug('installing regions plugin. digilib:', digilib); |
813 | 300 fn = digilib.fn; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
301 // import geometry classes |
813 | 302 geom = fn.geometry; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
303 FULL_AREA = geom.rectangle(0,0,1,1); |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
304 // add defaults, actions, buttons |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
305 $.extend(digilib.defaults, defaults); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
306 $.extend(digilib.actions, actions); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
307 $.extend(digilib.buttons, buttons); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
308 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
309 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
310 // plugin initialization |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
311 var init = function (data) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
312 console.debug('initialising regions plugin. data:', data); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
313 var $data = $(data); |
792 | 314 var buttonSettings = data.settings.buttonSettings.fullscreen; |
315 // configure buttons through digilib "regionSet" option | |
316 var buttonSet = data.settings.regionSet || regionSet; | |
317 // set regionSet to [] or '' for no buttons (when showing regions only) | |
318 if (buttonSet.length && buttonSet.length > 0) { | |
319 buttonSettings['regionSet'] = buttonSet; | |
320 buttonSettings.buttonSets.push('regionSet'); | |
813 | 321 } |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
322 // install event handler |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
323 $data.bind('setup', handleSetup); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
324 $data.bind('update', handleUpdate); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
325 $data.bind('redisplay', handleRedisplay); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
326 $data.bind('dragZoom', handleDragZoom); |
813 | 327 // regions array |
328 data.regions = []; | |
329 // add "rg" to digilibParamNames | |
330 data.settings.digilibParamNames.push('rg'); | |
792 | 331 }; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
332 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
333 // plugin object with name and install/init methods |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
334 // shared objects filled by digilib on registration |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
335 var pluginProperties = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
336 name : 'region', |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
337 install : install, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
338 init : init, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
339 buttons : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
340 actions : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
341 fn : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
342 plugins : {} |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
343 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
344 |
792 | 345 if ($.fn.digilib == null) { |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
346 $.error("jquery.digilib.regions must be loaded after jquery.digilib!"); |
792 | 347 } else { |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
348 $.fn.digilib('plugin', pluginProperties); |
792 | 349 } |
350 })(jQuery); |