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