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