annotate client/digitallibrary/jquery/jquery.digilib.regions.js @ 792:d742bd92b05a jquery

first step to a regions plugin
author hertzhaft
date Fri, 18 Feb 2011 11:11:49 +0100
parents
children 12f790cb30de
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($) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
14 // the data object passed by digilib
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
15 var data;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
16 var buttons;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
17 var fn;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
18 var geom;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
19
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
20 var buttons = {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
21 addregion : {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
22 onclick : "setRegion",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
23 tooltip : "set a region",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
24 icon : "addregion.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
25 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
26 delregion : {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
27 onclick : "removeRegion",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
28 tooltip : "delete the last region",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
29 icon : "delregion.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
30 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
31 regions : {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
32 onclick : "toggleRegions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
33 tooltip : "show or hide regions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
34 icon : "regions.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
35 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
36 regioninfo : {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
37 onclick : "infoRegions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
38 tooltip : "information about regions",
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
39 icon : "regioninfo.png"
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
40 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
41 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
42 var regionSet = ['addregion', 'delregion', 'regions', 'regioninfo', 'lessoptions'];
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
43
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
44 var actions = {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
45 // define a region interactively with two clicked points
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
46 "setRegion" : function(data) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
47 $elem = data.$elem;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
48 $scaler = data.$scaler;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
49 var pt1, pt2;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
50 var $regionDiv = $('<div class="region" style="display:none"/>');
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
51 $regionDiv.attr("id", "region" + data.regions.length);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
52 $elem.append($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
53 var picRect = geom.rectangle($scaler);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
54
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
55 var regionStart = function (evt) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
56 pt1 = geom.position(evt);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
57 // setup and show zoom div
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
58 pt1.adjustDiv($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
59 $regionDiv.width(0).height(0);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
60 $regionDiv.show();
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
61 // register events
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
62 $elem.bind("mousemove.dlRegion", regionMove);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
63 $elem.bind("mouseup.dlRegion", regionEnd);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
64 return false;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
65 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
66
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
67 // mouse move handler
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
68 var regionMove = function (evt) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
69 pt2 = geom.position(evt);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
70 var rect = geom.rectangle(pt1, pt2);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
71 rect.clipTo(picRect);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
72 // update zoom div
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
73 rect.adjustDiv($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
74 return false;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
75 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
76
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
77 // mouseup handler: end moving
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
78 var regionEnd = function (evt) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
79 pt2 = geom.position(evt);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
80 // assume a click and continue if the area is too small
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
81 var clickRect = geom.rectangle(pt1, pt2);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
82 if (clickRect.getArea() <= 5) return false;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
83 // unregister events
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
84 $elem.unbind("mousemove.dlRegion", regionMove);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
85 $elem.unbind("mouseup.dlRegion", regionEnd);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
86 // clip and transform
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
87 clickRect.clipTo(picRect);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
88 clickRect.adjustDiv($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
89 data.regions.push($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
90 // fn.redisplay(data);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
91 return false;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
92 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
93
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
94 // clear old handler (also ZoomDrag)
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
95 $scaler.unbind('.dlRegion');
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
96 $elem.unbind('.dlRegion');
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
97 // bind start zoom handler
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
98 $scaler.one('mousedown.dlRegion', regionStart);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
99 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
100
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
101 // remove the last added region
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
102 "removeRegion" : function (data) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
103 var $regionDiv = data.regions.pop();
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
104 $regionDiv.remove();
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
105 // fn.redisplay(data);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
106 },
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
107
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
108 // add a region programmatically
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
109 "addRegion" : function(data, pos, url) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
110 // TODO: backlink mechanism
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
111 if (pos.length === 4) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
112 // TODO: trafo
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
113 var $regionDiv = $('<div class="region" style="display:none"/>');
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
114 $regionDiv.attr("id", "region" + i);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
115 var regionRect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
116 regionRect.adjustDiv($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
117 if (!data.regions) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
118 data.regions = [];
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
119 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
120 data.regions.push($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
121 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
122 }
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 var addRegion = actions.addRegion;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
126
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
127 var realizeRegions = function (data) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
128 // create regions from parameters
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
129 var settings = data.settings;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
130 var rg = settings.rg;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
131 var regions = rg.split(",");
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
132 for (var i = 0; i < regions.length ; i++) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
133 var pos = regions.split("/", 4);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
134 // TODO: backlink mechanism
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
135 var url = paramString.match(/http.*$/);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
136 addRegion(data, pos, url);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
137 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
138 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
139
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
140 // display current regions
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
141 var renderRegions = function (data) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
142 var regions = data.regions;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
143 for (var i = 0; i < regions.length; i++) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
144 var region = regions[i];
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
145 if (data.zoomArea.containsPosition(region)) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
146 var rpos = data.imgTrafo.transform(region);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
147 console.debug("renderRegions: rpos=", rpos);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
148 // create region
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
149 var $regionDiv = $('<div class="region" style="display:none"/>');
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
150 $regionDiv.attr("id", "region" + data.regions.length);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
151 $elem.append($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
152 rpos.adjustDiv($regionDiv);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
153 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
154 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
155 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
156
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
157 var serializeRegions = function (data) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
158 if (data.regions) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
159 settings.rg = '';
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
160 for (var i = 0; i < data.regions.length; i++) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
161 if (i) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
162 settings.rg += ',';
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
163 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
164 settings.rg +=
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
165 cropFloat(data.regions[i].x).toString() + '/' +
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
166 cropFloat(data.regions[i].y).toString() + '/' +
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
167 cropFloat(data.regions[i].width).toString() + '/' +
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
168 cropFloat(data.regions[i].height).toString();
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
169 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
170 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
171 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
172
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
173 // export constructor functions to digilib plugin
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
174 var init = function (digilibdata) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
175 console.log('initialising regions plugin. data:', digilibdata);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
176 data = digilibdata;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
177 data.regions = [];
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
178 // setup geometry object
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
179 geom = data.plugins.geometry.init();
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
180 // add buttons and actions from this plugin
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
181 $.extend(this.buttons, buttons);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
182 $.extend(this.actions, actions);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
183 var buttonSettings = data.settings.buttonSettings.fullscreen;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
184 // configure buttons through digilib "regionSet" option
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
185 var buttonSet = data.settings.regionSet || regionSet;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
186 // set regionSet to [] or '' for no buttons (when showing regions only)
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
187 if (buttonSet.length && buttonSet.length > 0) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
188 buttonSettings['regionSet'] = buttonSet;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
189 buttonSettings.buttonSets.push('regionSet');
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
190 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
191 fn = this.fn;
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
192 // console.log(data.settings.buttonSettings.fullscreen.buttonSets);
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
193 return {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
194 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
195 };
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
196 if ($.fn.digilib == null) {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
197 $.error("jquery.digilib.regions must be loaded after jquery.digilib!");
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
198 } else {
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
199 $.fn.digilib('plugin', {name : 'regions', init : init});
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
200 }
d742bd92b05a first step to a regions plugin
hertzhaft
parents:
diff changeset
201 })(jQuery);