annotate client/digitallibrary/jquery/jquery.digilib.regions.js @ 815:60e8cca7ac81 jquery

could not remove last region
author hertzhaft
date Mon, 21 Feb 2011 01:20:22 +0100
parents 1a7b14deae3a
children eff74cfaaf97
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
1 /** optional digilib regions plugin
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
2
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
3 markup a digilib image with rectangular regions
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
4
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
5 TODO:
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
6 - store region in params/cookie, regarding zoom, mirror, rotation (like marks)
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
7 - set regions programmatically
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
8 - read regions from params/cookie and display
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
9 - backlink mechanism
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
10 - don't write to data.settings?
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
11 */
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
12
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
13 (function($) {
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
14 // the digilib object
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
15 var digilib;
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
16 // the data object passed by digilib
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
17 var data;
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
18 // the functions made available by digilib
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
19 var fn;
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
20 // affine geometry plugin
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
21 var geom;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
25 var ID_PREFIX = "digilib-region-";
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
26
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
27 var buttons = {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
28 addregion : {
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
29 onclick : "defineRegion",
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
30 tooltip : "define a region",
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
31 icon : "addregion.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
32 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
33 delregion : {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
34 onclick : "removeRegion",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
35 tooltip : "delete the last region",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
36 icon : "delregion.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
37 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
38 regions : {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
39 onclick : "toggleRegions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
40 tooltip : "show or hide regions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
41 icon : "regions.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
42 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
43 regioninfo : {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
44 onclick : "infoRegions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
45 tooltip : "information about regions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
46 icon : "regioninfo.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
47 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
53 // are region numbers shown?
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
57 // url param for regions
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
58 'rg' : null,
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
59 };
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
60
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
61 var actions = {
808
ae8e98c479d5 stub for new plugins; overlay div for regions plugin
hertzhaft
parents: 799
diff changeset
62
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
63 // define a region interactively with two clicked points
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
64 "defineRegion" : function(data) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
65 if (!data.settings.isRegionVisible) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
66 alert("Please turn on regions visibility!");
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
67 return;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
79 // we count regions from 1
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
80 var $regionDiv = addRegionDiv(data, data.regions.length + 1);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
81
808
ae8e98c479d5 stub for new plugins; overlay div for regions plugin
hertzhaft
parents: 799
diff changeset
82 // mousedown handler: start sizing
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
83 var regionStart = function (evt) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
84 pt1 = geom.position(evt);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
92 return false;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
93 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
94
808
ae8e98c479d5 stub for new plugins; overlay div for regions plugin
hertzhaft
parents: 799
diff changeset
95 // mousemove handler: size region
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
96 var regionMove = function (evt) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
97 pt2 = geom.position(evt);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
102 return false;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
103 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
104
808
ae8e98c479d5 stub for new plugins; overlay div for regions plugin
hertzhaft
parents: 799
diff changeset
105 // mouseup handler: end sizing
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
106 var regionEnd = function (evt) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
107 pt2 = geom.position(evt);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
108 // assume a click and continue if the area is too small
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
109 var clickRect = geom.rectangle(pt1, pt2);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
118 storeRegion(data, $regionDiv);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
119 // fn.redisplay(data);
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
120 fn.highlightButtons(data, 'addregion', 0);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
121 redisplay(data);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
122 return false;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
123 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
124
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
127 fn.highlightButtons(data, 'addregion', 1);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
128 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
129
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
130 // remove the last added region
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
131 "removeRegion" : function (data) {
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
132 if (!data.settings.isRegionVisible) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
133 alert("Please turn on regions visibility!");
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
134 return;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
135 }
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
136 var region = data.regions.pop();
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
137 if (region == null) return;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
138 var $regionDiv = region.$div;
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
139 $regionDiv.remove();
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
140 redisplay(data);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
141 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
142
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
143 // show/hide regions
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
144 "toggleRegions" : function (data) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
145 var show = !data.settings.isRegionVisible;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
146 data.settings.isRegionVisible = show;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
147 fn.highlightButtons(data, 'regions' , show);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
148 showRegionDivs(data);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
149 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
150 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
151
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
152 var addRegion = actions.addRegion;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
153
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
154 // store a region div
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
155 var storeRegion = function (data, $regionDiv) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
156 var regions = data.regions;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
157 var rect = geom.rectangle($regionDiv);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
158 var regionRect = data.imgTrafo.invtransform(rect);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
159 regionRect.$div = $regionDiv;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
160 regions.push(regionRect);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
161 console.debug("regions", data.regions, "regionRect", regionRect);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
162 };
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
163
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
164 // add a region to data.$elem
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
165 var addRegionDiv = function (data, nr) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
166 var $regionDiv = $('<div class="region overlay" style="display:none"/>');
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
167 $regionDiv.attr("id", ID_PREFIX + nr);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
168 data.$elem.append($regionDiv);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
169 if (data.settings.showRegionNumbers) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
170 var $regionNr = $('<div class="regionnumber" />');
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
171 $regionNr.text(nr);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
172 $regionDiv.append($regionNr);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
173 }
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
174 return $regionDiv;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
175 };
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
176
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
177 // create a region div from the data.regions collection
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
178 var createRegionDiv = function (data, index) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
179 var regions = data.regions;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
180 if (index > regions.length) return null;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
181 var region = regions[index];
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
182 var $regionDiv = addRegionDiv(data, index + 1); // we count regions from 1
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
183 region.$div = $regionDiv;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
184 // TODO store original coords in $regionDiv.data for embedded mode?
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
185 return $regionDiv;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
186 };
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
187
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
188 // create regions
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
189 var createRegionDivs = function (data) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
190 for (var i = 0; i < data.regions.length ; i++) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
191 createRegionDiv(data, i);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
192 }
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
193 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
194
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
195 // show a region on top of the scaler image
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
196 var showRegionDiv = function (data, index) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
197 if (!data.imgTrafo) return;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
198 var $elem = data.$elem;
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
199 var regions = data.regions;
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
200 if (index > regions.length) return;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
201 var region = regions[index]
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
202 var $regionDiv = region.$div;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
203 if (!$regionDiv) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
204 console.debug("showRegionDiv: region has no $div", region);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
205 // alert("showRegionDiv: region has no $div to show");
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
206 return;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
207 }
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
208 var regionRect = region.copy();
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
209 var show = data.settings.isRegionVisible;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
210 if (show && data.zoomArea.overlapsRect(regionRect)) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
211 regionRect.clipTo(data.zoomArea);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
212 var screenRect = data.imgTrafo.transform(regionRect);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
213 screenRect.adjustDiv($regionDiv);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
214 $regionDiv.show();
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
215 } else {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
216 $regionDiv.hide();
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
217 }
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
218 };
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
219
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
220 // show regions
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
221 var showRegionDivs = function (data) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
222 for (var i = 0; i < data.regions.length ; i++) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
223 showRegionDiv(data, i);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
224 }
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
225 };
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
226
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
227 var unpackRegions = function (data) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
228 // create regions from parameters
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
229 var rg = data.settings.rg;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
230 if (rg == null) return;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
231 var regions = data.regions;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
232 var rs = rg.split(",");
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
233 for (var i = 0; i < rs.length; i++) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
234 var r = rs[i];
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
235 var pos = r.split("/", 4);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
236 var rect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
237 regions.push(rect);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
238 // TODO: backlink mechanism
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
239 // var url = paramString.match(/http.*$/);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
240 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
241 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
242
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
243 // pack regions array into a parameter string
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
244 var packRegions = function (data) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
245 var regions = data.regions;
815
60e8cca7ac81 could not remove last region
hertzhaft
parents: 813
diff changeset
246 if (!regions.length) {
60e8cca7ac81 could not remove last region
hertzhaft
parents: 813
diff changeset
247 data.settings.rg = null;
60e8cca7ac81 could not remove last region
hertzhaft
parents: 813
diff changeset
248 return;
60e8cca7ac81 could not remove last region
hertzhaft
parents: 813
diff changeset
249 }
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
250 var rg = '';
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
251 for (var i = 0; i < regions.length; i++) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
252 region = regions[i];
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
253 if (i) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
254 rg += ',';
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
255 }
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
256 rg += [
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
257 fn.cropFloatStr(region.x),
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
258 fn.cropFloatStr(region.y),
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
259 fn.cropFloatStr(region.width),
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
260 fn.cropFloatStr(region.height)
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
261 ].join('/');
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
262 }
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
263 data.settings.rg = rg;
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
264 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
265
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
266 var redisplay = function (data) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
267 packRegions(data);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
268 fn.redisplay(data);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
269 }
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
273 console.debug("regions: handleSetup", data.settings.rg);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
274 unpackRegions(data);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
280 fn.highlightButtons(data, 'regions' , data.settings.isRegionVisible);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
281 showRegionDivs(data);
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
286 data = this;
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
297 var install = function(plugin) {
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
314 var buttonSettings = data.settings.buttonSettings.fullscreen;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
315 // configure buttons through digilib "regionSet" option
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
316 var buttonSet = data.settings.regionSet || regionSet;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
317 // set regionSet to [] or '' for no buttons (when showing regions only)
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
318 if (buttonSet.length && buttonSet.length > 0) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
319 buttonSettings['regionSet'] = buttonSet;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
320 buttonSettings.buttonSets.push('regionSet');
813
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
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
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
327 // regions array
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
328 data.regions = [];
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
329 // add "rg" to digilibParamNames
1a7b14deae3a regions plugin works, inclunding event handlers
hertzhaft
parents: 808
diff changeset
330 data.settings.digilibParamNames.push('rg');
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
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
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
347 } else {
799
12f790cb30de started to adapt regions plugin to new plugin mechanism
hertzhaft
parents: 792
diff changeset
348 $.fn.digilib('plugin', pluginProperties);
792
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
349 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
350 })(jQuery);