Mercurial > hg > digilib-old
annotate client/digitallibrary/jquery/jquery.digilib.regions.js @ 813:1a7b14deae3a jquery
regions plugin works, inclunding event handlers
author | hertzhaft |
---|---|
date | Mon, 21 Feb 2011 01:03:55 +0100 |
parents | ae8e98c479d5 |
children | 60e8cca7ac81 |
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; | |
246 if (!regions.length) return; | |
247 var rg = ''; | |
248 for (var i = 0; i < regions.length; i++) { | |
249 region = regions[i]; | |
250 if (i) { | |
251 rg += ','; | |
792 | 252 } |
813 | 253 rg += [ |
254 fn.cropFloatStr(region.x), | |
255 fn.cropFloatStr(region.y), | |
256 fn.cropFloatStr(region.width), | |
257 fn.cropFloatStr(region.height) | |
258 ].join('/'); | |
259 } | |
260 data.settings.rg = rg; | |
792 | 261 }; |
262 | |
813 | 263 var redisplay = function (data) { |
264 packRegions(data); | |
265 fn.redisplay(data); | |
266 } | |
267 | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
268 var handleSetup = function (evt) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
269 data = this; |
813 | 270 console.debug("regions: handleSetup", data.settings.rg); |
271 unpackRegions(data); | |
272 createRegionDivs(data); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
273 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
274 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
275 var handleUpdate = function (evt) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
276 data = this; |
813 | 277 fn.highlightButtons(data, 'regions' , data.settings.isRegionVisible); |
278 showRegionDivs(data); | |
279 console.debug("regions: handleUpdate", data.settings.rg); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
280 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
281 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
282 var handleRedisplay = function (evt) { |
813 | 283 data = this; |
284 showRegionDivs(data); | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
285 console.debug("regions: handleRedisplay"); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
286 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
287 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
288 var handleDragZoom = function (evt, zoomArea) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
289 console.debug("regions: handleDragZoom, zoomArea:", zoomArea); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
290 data = this; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
291 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
292 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
293 // plugin installation called by digilib on plugin object. |
813 | 294 var install = function(plugin) { |
295 digilib = plugin; | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
296 console.debug('installing regions plugin. digilib:', digilib); |
813 | 297 fn = digilib.fn; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
298 // import geometry classes |
813 | 299 geom = fn.geometry; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
300 FULL_AREA = geom.rectangle(0,0,1,1); |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
301 // add defaults, actions, buttons |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
302 $.extend(digilib.defaults, defaults); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
303 $.extend(digilib.actions, actions); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
304 $.extend(digilib.buttons, buttons); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
305 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
306 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
307 // plugin initialization |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
308 var init = function (data) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
309 console.debug('initialising regions plugin. data:', data); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
310 var $data = $(data); |
792 | 311 var buttonSettings = data.settings.buttonSettings.fullscreen; |
312 // configure buttons through digilib "regionSet" option | |
313 var buttonSet = data.settings.regionSet || regionSet; | |
314 // set regionSet to [] or '' for no buttons (when showing regions only) | |
315 if (buttonSet.length && buttonSet.length > 0) { | |
316 buttonSettings['regionSet'] = buttonSet; | |
317 buttonSettings.buttonSets.push('regionSet'); | |
813 | 318 } |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
319 // install event handler |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
320 $data.bind('setup', handleSetup); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
321 $data.bind('update', handleUpdate); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
322 $data.bind('redisplay', handleRedisplay); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
323 $data.bind('dragZoom', handleDragZoom); |
813 | 324 // regions array |
325 data.regions = []; | |
326 // add "rg" to digilibParamNames | |
327 data.settings.digilibParamNames.push('rg'); | |
792 | 328 }; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
329 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
330 // plugin object with name and install/init methods |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
331 // shared objects filled by digilib on registration |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
332 var pluginProperties = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
333 name : 'region', |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
334 install : install, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
335 init : init, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
336 buttons : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
337 actions : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
338 fn : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
339 plugins : {} |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
340 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
341 |
792 | 342 if ($.fn.digilib == null) { |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
343 $.error("jquery.digilib.regions must be loaded after jquery.digilib!"); |
792 | 344 } else { |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
345 $.fn.digilib('plugin', pluginProperties); |
792 | 346 } |
347 })(jQuery); |