]> www.average.org Git - mkgallery.git/blob - include/show.js
248dee6829f6d755a851ea387c5268e47dddc56e
[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                         setStyle('opacity', 0).
66                         injectInside(this.container.container);
67                 this.ondisplay = new Element('img').
68                         setStyle('opacity', 0).
69                         injectInside(this.container.container);
70                 this.loadingdiv = new Element('div').
71                 addClass('loading').setStyles({
72                         position: 'absolute',
73                         top: 0,
74                         left: 0,
75                         zIndex: 4,
76                         display: 'none',
77                         width: this.coords.width,
78                         height: this.coords.height,
79                 }).injectInside(this.container.container);
80
81                 window.addEvent('resize', this.resizer.bind(this))
82         },
83
84         /* event handler for window resize */
85
86         resizer: function(){
87                 this.updatecoords();
88                 var newstyle = this.calcsize(this.cache.curr);
89                 this.ondisplay.setStyles(newstyle);
90                 /* check if we need reload */
91         },
92
93         /* prev, play, stop, next, exit, comm are methods for button presses */
94
95         prev: function(){
96                 if (this.currentid > 0) {
97                         this.show(this.currentid-1);
98                 } else {
99                         alert('show.prev called beyond first element');
100                 }
101         },
102
103         stop: function(){
104                 if (this.isplaying) { $clear(this.timer); }
105                 this.isplaying = false;
106                 $clear(this.timer);
107                 this.controls.running(0);
108         },
109
110         play: function(){
111                 this.isplaying = true;
112                 this.timer = this.autonext.delay(this.delay,this);
113                 this.controls.running(1);
114         },
115
116         next: function(){
117                 if (this.currentid < this.vimgs.length-1) {
118                         this.show(this.currentid+1);
119                 } else {
120                         alert('show.next called beyond last element');
121                 }
122         },
123
124         exit: function(){
125                 if (this.isplaying) { $clear(this.timer); }
126                 this.prevdisplay.setStyle('display', 'none');
127                 this.ondisplay.setStyle('display', 'none');
128                 this.stopfx();
129                 this.options.cbExit();
130         },
131
132         comm: function(){
133                 /* alert('show.comm called, do nothing'); */
134         },
135
136         /* Entry point: called to start doing things */
137
138         start: function(id, play){
139                 this.options.cbStart();
140                 this.isplaying = play;
141                 this.controls.running(this.isplaying);
142                 this.show(id);
143                 return false; /* to make it usable from href links */
144         },
145
146         /* "Private" methods to do the real job */
147
148         show: function(id){
149                 /* alert('called show.show('+id+')'); */
150                 this.currentid = id;
151                 var newcache = {
152                         prev: (id > 0)?this.prepare(id-1):{},
153                         curr: this.prepare(id),
154                         next: (id < (this.vimgs.length-1))?this.prepare(id+1):{},
155                 };
156                 delete this.cache;
157                 this.cache = newcache;
158                 if (this.cache.curr.ready) {
159                         this.display(this.cache.curr);
160                 } else {
161                         this.pendingload = true;
162                         this.showloading();
163                 }
164                 this.controls.info(id,this.vimgs.length,
165                                 this.vimgs[id][0],
166                                 this.vimgs[id][1]);
167         },
168
169         prepare: function(id){
170                 var vi;
171                 for (vi=0;vi<this.vimgs[id][2].length-1;vi++) {
172                         if ((this.vimgs[id][2][vi][0] >= this.target.width) ||
173                             (this.vimgs[id][2][vi][1] >= this.target.height)) {
174                                 break;
175                         }
176                 }
177                 /* alert('prepare id='+id+', selected '+vi+' at '+
178                         this.vimgs[id][2][vi][0]+'x'+
179                         this.vimgs[id][2][vi][1]); */
180                 var cachel;
181                 ['prev', 'curr', 'next'].each(function(el){
182                         if (this.cache[el] &&
183                             this.cache[el].id == id &&
184                             this.cache[el].vi == vi) {
185                                 cachel = this.cache[el];
186                         }
187                 }.bind(this));
188                 if (! cachel) {
189                         cachel = {
190                                 id: id,
191                                 vi: vi,
192                                 ready: false,
193                                 url: this.vimgs[id][2][vi][2],
194                         };
195                         cachel.img = this.bgload(cachel);
196                 }
197                 return cachel;
198         },
199
200         bgload: function(cachel){
201                 /* alert('bgload: id='+cachel.id+' vi='+cachel.vi+
202                         ' url='+cachel.url); */
203                 return new Asset.image(this.vimgs[cachel.id][2][cachel.vi][2],{
204                         id: this.vimgs[cachel.id][0],
205                         title: this.vimgs[cachel.id][1],
206                         onload: this.loadcomplete.bind(this,[cachel]),
207                 });
208         },
209
210         loadcomplete: function(cachel){
211                 /* alert('loadcomplete '+cachel.url+' id='+cachel.id+
212                         ' vi='+cachel.vi); */
213                 cachel.ready = true;
214                 if (cachel.id == this.currentid &&
215                     this.pendingload) {
216                         this.pendingload = false;
217                         this.hideloading();
218                         this.display(cachel);
219                 }
220         },
221
222         display: function(cachel){
223                 var newstyle = this.calcsize(cachel);
224                 var newimg = cachel.img.clone();
225                 newimg.setStyles(newstyle);
226                 newimg.setStyles({
227                         zIndex: 3,
228                         opacity: 0,
229                 });
230                 this.prevdisplay.dispose();
231                 this.prevdisplay = this.ondisplay.clone().
232                 setStyle('zIndex', 2).injectInside(this.container.container);
233                 newimg.replaces(this.ondisplay);
234                 this.ondisplay = newimg;
235                 this.effect();
236         },
237
238         effect: function(){
239                 this.fx = new Fx.Tween(this.ondisplay, {
240                         duration: this.options.fxduration,
241                 });
242                 this.fx.addEvent('complete',this.displaycomplete.bind(this));
243                 this.fx.start('opacity', 0, 1);
244         },
245
246         displaycomplete: function(){
247                 this.prevdisplay.setStyle('display', 'none');
248                 if (this.isplaying) {
249                         this.timer = this.autonext.delay(this.delay,this);
250                 }
251         },
252
253         autonext: function(){
254                 if (this.isplaying) {
255                         if (this.currentid < this.vimgs.length-1) {
256                                 this.show(this.currentid+1);
257                         } else {
258                                 this.exit();
259                         }
260                 }
261         },
262
263         calcsize: function(cachel){
264                 var factor = 1;
265                 var candidate;
266                 candidate = this.target.width /
267                                 this.vimgs[cachel.id][2][cachel.vi][0];
268                 if (factor > candidate) { factor = candidate; }
269                 candidate = this.target.height /
270                                 this.vimgs[cachel.id][2][cachel.vi][1];
271                 if (factor > candidate) { factor = candidate; }
272                 var w = Math.round(this.vimgs[cachel.id][2][cachel.vi][0] *
273                         factor);
274                 var h = Math.round(this.vimgs[cachel.id][2][cachel.vi][1] *
275                         factor);
276                 var t = Math.round((this.coords.height-h)/2);
277                 var l = Math.round((this.coords.width-w)/2);
278                 /* alert('new size: '+w+'x'+h+'+'+l+'+'+t); */
279                 return {
280                         position: 'absolute',
281                         top: t+'px',
282                         left: l+'px',
283                         width: w,
284                         height: h,
285                 };
286         },
287
288         showloading: function(){
289                 this.loadingdiv.setStyle('display', 'block');
290         },
291
292         hideloading: function(){
293                 this.loadingdiv.setStyle('display', 'none');
294         },
295
296         stopfx: function(){
297                 if (this.fx) this.fx.cancel();
298         },
299
300         updatecoords: function(){
301                 this.coords = this.container.getCoordinates();
302                 this.target = {
303                         width: Math.round(this.coords.width *
304                                                 this.options.percentage / 100),
305                         height: Math.round(this.coords.height *
306                                                 this.options.percentage / 100),
307                 };
308                 /* alert('coords: '+this.coords.width+'x'+this.coords.height+
309                      ', target: '+this.target.width+'x'+this.target.height); */
310         },
311
312 });
313 Show.implement(new Options);
314 Show.implement(new Events);
315