Mercurial > hg > digilib-old
annotate client/digitallibrary/jquery/jquery.digilib.regions.js @ 808:ae8e98c479d5 jquery
stub for new plugins; overlay div for regions plugin
author | hertzhaft |
---|---|
date | Sun, 20 Feb 2011 13:24:49 +0100 |
parents | 12f790cb30de |
children | 1a7b14deae3a |
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($) { | |
14 // the data object passed by digilib | |
15 var data; | |
16 var buttons; | |
17 var fn; | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
18 // affine geometry plugin stub |
792 | 19 var geom; |
20 | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
21 var FULL_AREA; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
22 |
792 | 23 var buttons = { |
24 addregion : { | |
25 onclick : "setRegion", | |
26 tooltip : "set a region", | |
27 icon : "addregion.png" | |
28 }, | |
29 delregion : { | |
30 onclick : "removeRegion", | |
31 tooltip : "delete the last region", | |
32 icon : "delregion.png" | |
33 }, | |
34 regions : { | |
35 onclick : "toggleRegions", | |
36 tooltip : "show or hide regions", | |
37 icon : "regions.png" | |
38 }, | |
39 regioninfo : { | |
40 onclick : "infoRegions", | |
41 tooltip : "information about regions", | |
42 icon : "regioninfo.png" | |
43 } | |
44 }; | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
45 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
46 var defaults = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
47 // are regions shown? |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
48 'isRegionVisible' : true, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
49 // buttonset of this plugin |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
50 'regionSet' : ['addregion', 'delregion', 'regions', 'regioninfo', 'lessoptions'], |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
51 // array of defined regions |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
52 'regions' : [] |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
53 }; |
792 | 54 |
55 var actions = { | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
56 |
792 | 57 // define a region interactively with two clicked points |
58 "setRegion" : function(data) { | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
59 var $elem = data.$elem; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
60 var $body = $('body'); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
61 var bodyRect = geom.rectangle($body); |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
62 var $scaler = data.$scaler; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
63 var scalerRect = geom.rectangle($scaler); |
792 | 64 var pt1, pt2; |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
65 // overlay prevents other elements from reacting to mouse events |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
66 var $overlay = $('<div class="digilib-overlay"/>'); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
67 $body.append($overlay); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
68 bodyRect.adjustDiv($overlay); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
69 // the region to be defined |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
70 var $regionDiv = $('<div class="region" style="display:none"/>'); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
71 $elem.append($regionDiv); |
792 | 72 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
73 // mousedown handler: start sizing |
792 | 74 var regionStart = function (evt) { |
75 pt1 = geom.position(evt); | |
76 // setup and show zoom div | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
77 pt1.adjustDiv($regionDiv); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
78 $regionDiv.width(0).height(0); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
79 $regionDiv.show(); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
80 // register mouse events |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
81 $overlay.bind("mousemove.dlRegion", regionMove); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
82 $overlay.bind("mouseup.dlRegion", regionEnd); |
792 | 83 return false; |
84 }; | |
85 | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
86 // mousemove handler: size region |
792 | 87 var regionMove = function (evt) { |
88 pt2 = geom.position(evt); | |
89 var rect = geom.rectangle(pt1, pt2); | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
90 rect.clipTo(scalerRect); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
91 // update region |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
92 rect.adjustDiv($regionDiv); |
792 | 93 return false; |
94 }; | |
95 | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
96 // mouseup handler: end sizing |
792 | 97 var regionEnd = function (evt) { |
98 pt2 = geom.position(evt); | |
99 // assume a click and continue if the area is too small | |
100 var clickRect = geom.rectangle(pt1, pt2); | |
101 if (clickRect.getArea() <= 5) return false; | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
102 // unregister mouse events and get rid of overlay |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
103 $overlay.unbind("mousemove.dlRegion", regionMove); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
104 $overlay.unbind("mouseup.dlRegion", regionEnd); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
105 $overlay.remove(); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
106 // clip region |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
107 clickRect.clipTo(scalerRect); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
108 clickRect.adjustDiv($regionDiv); |
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
109 data.settings.regions.push(clickRect); // TODO: trafo, params |
792 | 110 // fn.redisplay(data); |
111 return false; | |
112 }; | |
113 | |
114 // bind start zoom handler | |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
115 $overlay.one('mousedown.dlRegion', regionStart); |
792 | 116 }, |
117 | |
118 // remove the last added region | |
119 "removeRegion" : function (data) { | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
120 var $regionDiv = data.settings.regions.pop(); |
792 | 121 $regionDiv.remove(); |
122 // fn.redisplay(data); | |
123 }, | |
124 | |
125 // add a region programmatically | |
126 "addRegion" : function(data, pos, url) { | |
127 // TODO: backlink mechanism | |
128 if (pos.length === 4) { | |
129 // TODO: trafo | |
130 var $regionDiv = $('<div class="region" style="display:none"/>'); | |
131 $regionDiv.attr("id", "region" + i); | |
132 var regionRect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); | |
133 regionRect.adjustDiv($regionDiv); | |
134 if (!data.regions) { | |
135 data.regions = []; | |
136 } | |
137 data.regions.push($regionDiv); | |
138 } | |
139 } | |
140 }; | |
141 | |
142 var addRegion = actions.addRegion; | |
143 | |
144 var realizeRegions = function (data) { | |
145 // create regions from parameters | |
146 var settings = data.settings; | |
147 var rg = settings.rg; | |
148 var regions = rg.split(","); | |
149 for (var i = 0; i < regions.length ; i++) { | |
150 var pos = regions.split("/", 4); | |
151 // TODO: backlink mechanism | |
152 var url = paramString.match(/http.*$/); | |
153 addRegion(data, pos, url); | |
154 } | |
155 }; | |
156 | |
157 // display current regions | |
158 var renderRegions = function (data) { | |
159 var regions = data.regions; | |
160 for (var i = 0; i < regions.length; i++) { | |
161 var region = regions[i]; | |
162 if (data.zoomArea.containsPosition(region)) { | |
163 var rpos = data.imgTrafo.transform(region); | |
164 console.debug("renderRegions: rpos=", rpos); | |
165 // create region | |
166 var $regionDiv = $('<div class="region" style="display:none"/>'); | |
167 $regionDiv.attr("id", "region" + data.regions.length); | |
168 $elem.append($regionDiv); | |
169 rpos.adjustDiv($regionDiv); | |
170 } | |
171 } | |
172 }; | |
173 | |
174 var serializeRegions = function (data) { | |
175 if (data.regions) { | |
176 settings.rg = ''; | |
177 for (var i = 0; i < data.regions.length; i++) { | |
178 if (i) { | |
179 settings.rg += ','; | |
180 } | |
181 settings.rg += | |
182 cropFloat(data.regions[i].x).toString() + '/' + | |
183 cropFloat(data.regions[i].y).toString() + '/' + | |
184 cropFloat(data.regions[i].width).toString() + '/' + | |
185 cropFloat(data.regions[i].height).toString(); | |
186 } | |
187 } | |
188 }; | |
189 | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
190 var handleSetup = function (evt) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
191 console.debug("regions: handleSetup"); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
192 data = this; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
193 // if (data.settings.isBirdDivVisible) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
194 // setupBirdDiv(data); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
195 // data.$birdDiv.show(); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
196 // } |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
197 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
198 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
199 var handleUpdate = function (evt) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
200 console.debug("regions: handleUpdate"); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
201 data = this; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
202 // if (data.settings.isBirdDivVisible) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
203 // renderBirdArea(data); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
204 // setupBirdDrag(data); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
205 // } |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
206 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
207 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
208 var handleRedisplay = function (evt) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
209 console.debug("regions: handleRedisplay"); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
210 data = this; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
211 // if (data.settings.isBirdDivVisible) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
212 // updateBirdDiv(data); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
213 // } |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
214 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
215 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
216 var handleDragZoom = function (evt, zoomArea) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
217 console.debug("regions: handleDragZoom, zoomArea:", zoomArea); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
218 data = this; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
219 // if (data.settings.isBirdDivVisible) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
220 // setBirdZoom(data, zoomArea); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
221 // } |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
222 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
223 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
224 // plugin installation called by digilib on plugin object. |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
225 var install = function(digilib) { |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
226 console.debug('installing regions plugin. digilib:', digilib); |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
227 // import geometry classes |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
228 geom = digilib.fn.geometry; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
229 FULL_AREA = geom.rectangle(0,0,1,1); |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
230 // add defaults, actions, buttons |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
231 $.extend(digilib.defaults, defaults); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
232 $.extend(digilib.actions, actions); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
233 $.extend(digilib.buttons, buttons); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
234 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
235 |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
236 // plugin initialization |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
237 var init = function (data) { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
238 console.debug('initialising regions plugin. data:', data); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
239 var $data = $(data); |
792 | 240 var buttonSettings = data.settings.buttonSettings.fullscreen; |
241 // configure buttons through digilib "regionSet" option | |
242 var buttonSet = data.settings.regionSet || regionSet; | |
243 // set regionSet to [] or '' for no buttons (when showing regions only) | |
244 if (buttonSet.length && buttonSet.length > 0) { | |
245 buttonSettings['regionSet'] = buttonSet; | |
246 buttonSettings.buttonSets.push('regionSet'); | |
247 } | |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
248 // install event handler |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
249 $data.bind('setup', handleSetup); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
250 $data.bind('update', handleUpdate); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
251 $data.bind('redisplay', handleRedisplay); |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
252 $data.bind('dragZoom', handleDragZoom); |
792 | 253 }; |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
254 |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
255 // plugin object with name and install/init methods |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
256 // shared objects filled by digilib on registration |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
257 var pluginProperties = { |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
258 name : 'region', |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
259 install : install, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
260 init : init, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
261 buttons : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
262 actions : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
263 fn : {}, |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
264 plugins : {} |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
265 }; |
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
266 |
792 | 267 if ($.fn.digilib == null) { |
808
ae8e98c479d5
stub for new plugins; overlay div for regions plugin
hertzhaft
parents:
799
diff
changeset
|
268 $.error("jquery.digilib.regions must be loaded after jquery.digilib!"); |
792 | 269 } else { |
799
12f790cb30de
started to adapt regions plugin to new plugin mechanism
hertzhaft
parents:
792
diff
changeset
|
270 $.fn.digilib('plugin', pluginProperties); |
792 | 271 } |
272 })(jQuery); |