basically it works
[mkgallery.git] / include / show.js
1 /*
2         $Id$
3
4         This is a part of mkgallery.pl suite
5         http://www.average.org/mkgallery/
6
7         Uses mootools (1.2) http://www.mootools.net/
8         Inspired by slideshow http://www.phatfusion.net/slideshow/
9 */
10
11 /*
12         Slideshow
13
14   - On show image: find this and next urls; put in place
15     those that are already here; free unneeded; initiate download of
16     the rest; if needed image is ready then initiate "transitioning", else
17     initiate "loading".
18   - On load complete: if this is the target image, initiate "transitioning".
19   - On "loading": show "loading" image
20   - On "transitioning": hide "loading" image; initiate FX animation to the
21     needed image.
22   - On animation complete: blank previous image; if "playing" then schedule
23     autoswitch to next image in the future.
24   - On autoswitch to next image: if "playing" then switch to next image.
25   - On switch to next image: if next exists, show next image, else show
26     "last image" message.
27   - On switch to prev image: if prev exists, show prev image, else show
28     "first image" message.
29   - On "play": make "playing"; switch to next image.
30   - On "stop": if "playing" cancel autoswitch; break "playing".
31   - On "start show": set up things; set "playing" state; show needed image.
32   - On "stop show": cancel any schedules, hide things.
33   - On resize: recalculate existing image size and position; determine
34     what image is needed; if it is not the one on display then request
35     "show image" for the new image.
36 */
37
38 var Show = new Class({
39
40         getOptions: function(){
41                 return {
42                         cbStart: function(){ alert('show start undefined'); },
43                         cbExit: function(){ alert('show exit undefined'); },
44                         percentage: 98,
45                         delay: 5000,
46                         fxduration: 200,
47                 }
48         },
49
50         initialize: function(vimgs, container, controls, options){
51                 this.setOptions(this.getOptions(), options);
52                 this.vimgs = vimgs;
53                 this.container = container;
54                 this.controls = controls;
55                 this.controls.registershow(this);
56                 this.timer = 0;
57                 this.delay = this.options.delay;
58                 this.cache = {
59                         prev: {},
60                         curr: {},
61                         next: {},
62                 };
63                 this.updatecoords();
64                 this.prevdisplay = new Element('img').
65                         injectInside(this.container.container);
66                 this.ondisplay = new Element('img').
67                         injectInside(this.container.container);
68                 this.loadingdiv = new Element('div').
69                 addClass('loading').setStyles({
70                         position: 'absolute',
71                         top: 0,
72                         left: 0,
73                         zIndex: 4,
74                         display: 'none',
75                         width: this.coords.width,
76                         height: this.coords.height,
77                 }).injectInside(this.container.container);
78
79                 window.addEvent('resize', this.resizer.bind(this))
80         },
81
82         /* event handler for window resize */
83
84         resizer: function(){
85                 this.updatecoords();
86                 var newstyle = this.calcsize(this.cache.curr);
87                 this.ondisplay.setStyles(newstyle);
88                 /* check if we need reload */
89         },
90
91         /* prev, play, stop, next, exit, comm are methods for button presses */
92
93         prev: function(){
94                 if (this.currentid > 0) {
95                         this.show(this.currentid-1);
96                 } else {
97                         alert('show.prev called beyond first element');
98                 }
99         },
100
101         stop: function(){
102                 if (this.isplaying) { $clear(this.timer); }
103                 this.isplaying = false;
104                 $clear(this.timer);
105                 this.controls.running(0);
106         },
107
108         play: function(){
109                 this.isplaying = true;
110                 this.timer = this.autonext.delay(this.delay,this);
111                 this.controls.running(1);
112         },
113
114         next: function(){
115                 if (this.currentid < this.vimgs.length-1) {
116                         this.show(this.currentid+1);
117                 } else {
118                         alert('show.next called beyond last element');
119                 }
120         },
121
122         exit: function(){
123                 if (this.isplaying) { $clear(this.timer); }
124                 this.prevdisplay.setStyle('display', 'none');
125                 this.ondisplay.setStyle('display', 'none');
126                 this.stopfx();
127                 this.options.cbExit();
128         },
129
130         comm: function(){
131                 /* alert('show.comm called, do nothing'); */
132         },
133
134         /* Entry point: called to start doing things */
135
136         start: function(id, play){
137                 this.options.cbStart();
138                 this.isplaying = play;
139                 this.controls.running(this.isplaying);
140                 this.show(id);
141                 return false; /* to make it usable from href links */
142         },
143
144         /* "Private" methods to do the real job */
145
146         show: function(id){
147                 /* alert('called show.show('+id+')'); */
148                 this.currentid = id;
149                 var newcache = {
150                         prev: (id > 0)?this.prepare(id-1):{},
151                         curr: this.prepare(id),
152                         next: (id < (this.vimgs.length-1))?this.prepare(id+1):{},
153                 };
154                 delete this.cache;
155                 this.cache = newcache;
156                 if (this.cache.curr.ready) {
157                         this.display(this.cache.curr);
158                 } else {
159                         this.pendingload = true;
160                         this.showloading();
161                 }
162                 this.controls.info(id,this.vimgs.length,
163                                 this.vimgs[id][0],
164                                 this.vimgs[id][1]);
165         },
166
167         prepare: function(id){
168                 var vi;
169                 for (vi=0;vi<this.vimgs[id][2].length-1;vi++) {
170                         if ((this.vimgs[id][2][vi][0] >= this.target.width) ||
171                             (this.vimgs[id][2][vi][1] >= this.target.height)) {
172                                 break;
173                         }
174                 }
175                 /* alert('prepare id='+id+', selected '+vi+' at '+
176                         this.vimgs[id][2][vi][0]+'x'+
177                         this.vimgs[id][2][vi][1]); */
178                 var cachel;
179                 ['prev', 'curr', 'next'].each(function(el){
180                         if (this.cache[el] &&
181                             this.cache[el].id == id &&
182                             this.cache[el].vi == vi) {
183                                 cachel = this.cache[el];
184                         }
185                 }.bind(this));
186                 if (! cachel) {
187                         cachel = {
188                                 id: id,
189                                 vi: vi,
190                                 ready: false,
191                                 url: this.vimgs[id][2][vi][2],
192                         };
193                         cachel.img = this.bgload(cachel);
194                 }
195                 return cachel;
196         },
197
198         bgload: function(cachel){
199                 /* alert('bgload: id='+cachel.id+' vi='+cachel.vi+
200                         ' url='+cachel.url); */
201                 return new Asset.image(this.vimgs[cachel.id][2][cachel.vi][2],{
202                         id: this.vimgs[cachel.id][0],
203                         title: this.vimgs[cachel.id][1],
204                         onload: this.loadcomplete.bind(this,[cachel]),
205                 });
206         },
207
208         loadcomplete: function(cachel){
209                 /* alert('loadcomplete '+cachel.url+' id='+cachel.id+
210                         ' vi='+cachel.vi); */
211                 cachel.ready = true;
212                 if (cachel.id == this.currentid &&
213                     this.pendingload) {
214                         this.pendingload = false;
215                         this.hideloading();
216                         this.display(cachel);
217                 }
218         },
219
220         display: function(cachel){
221                 var newstyle = this.calcsize(cachel);
222                 var newimg = cachel.img.clone();
223                 newimg.setStyles(newstyle);
224                 newimg.setStyles({
225                         zIndex: 3,
226                         opacity: 0,
227                 });
228                 this.prevdisplay.dispose();
229                 this.prevdisplay = this.ondisplay.clone().
230                 setStyle('zIndex', 2).injectInside(this.container.container);
231                 newimg.replaces(this.ondisplay);
232                 this.ondisplay = newimg;
233                 this.effect();
234         },
235
236         effect: function(){
237                 this.fx = new Fx.Tween(this.ondisplay, {
238                         duration: this.options.fxduration,
239                 });
240                 this.fx.addEvent('complete',this.displaycomplete.bind(this));
241                 this.fx.start('opacity', 0, 1);
242         },
243
244         displaycomplete: function(){
245                 this.prevdisplay.setStyle('display', 'none');
246                 if (this.isplaying) {
247                         this.timer = this.autonext.delay(this.delay,this);
248                 }
249         },
250
251         autonext: function(){
252                 if (this.isplaying) {
253                         if (this.currentid < this.vimgs.length-1) {
254                                 this.show(this.currentid+1);
255                         } else {
256                                 this.exit();
257                         }
258                 }
259         },
260
261         calcsize: function(cachel){
262                 var factor = 1;
263                 var candidate;
264                 candidate = this.target.width /
265                                 this.vimgs[cachel.id][2][cachel.vi][0];
266                 if (factor > candidate) { factor = candidate; }
267                 candidate = this.target.height /
268                                 this.vimgs[cachel.id][2][cachel.vi][1];
269                 if (factor > candidate) { factor = candidate; }
270                 var w = Math.round(this.vimgs[cachel.id][2][cachel.vi][0] *
271                         factor);
272                 var h = Math.round(this.vimgs[cachel.id][2][cachel.vi][1] *
273                         factor);
274                 var t = Math.round((this.coords.height-h)/2);
275                 var l = Math.round((this.coords.width-w)/2);
276                 /* alert('new size: '+w+'x'+h+'+'+l+'+'+t); */
277                 return {
278                         position: 'absolute',
279                         top: t+'px',
280                         left: l+'px',
281                         width: w,
282                         height: h,
283                 };
284         },
285
286         showloading: function(){
287                 this.loadingdiv.setStyle('display', 'block');
288         },
289
290         hideloading: function(){
291                 this.loadingdiv.setStyle('display', 'none');
292         },
293
294         stopfx: function(){
295                 if (this.fx) this.fx.cancel();
296         },
297
298         updatecoords: function(){
299                 this.coords = this.container.getCoordinates();
300                 this.target = {
301                         width: Math.round(this.coords.width *
302                                                 this.options.percentage / 100),
303                         height: Math.round(this.coords.height *
304                                                 this.options.percentage / 100),
305                 };
306                 /* alert('coords: '+this.coords.width+'x'+this.coords.height+
307                      ', target: '+this.target.width+'x'+this.target.height); */
308         },
309
310 });
311 Show.implement(new Options);
312 Show.implement(new Events);
313