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