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