source: trunk/plog-includes/js/plog-slideshow.js @ 561

Revision 561, 18.5 KB checked in by sidtheduck, 2 years ago (diff)

+ Slideshow fixes and javascript redirect to album view at end of slideshow (instead of repeat)
+ Miscellaneous small syntax edits

Line 
1/*==================================================*
2 $Id: slideshow.js,v 1.16 2003/10/14 12:39:00 pat Exp $
3 Copyright 2000-2003 Patrick Fitzgerald
4 http://slideshow.barelyfitz.com/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *==================================================*/
20
21// There are two objects defined in this file:
22// "slide" - contains all the information for a single slide
23// "slideshow" - consists of multiple slide objects and runs the slideshow
24
25//==================================================
26// slide object
27//==================================================
28function slide(src,link,text,target,attr,exif,image_name) {
29        // This is the constructor function for the slide object.
30        // It is called automatically when you create a new slide object.
31        // For example:
32        // s = new slide();
33
34        // Image URL
35        this.src = src;
36
37        // Link URL
38        this.link = link;
39
40        // Text to display
41        this.text = text;
42
43        // Name of the target window ("_blank")
44        this.target = target;
45
46        //EXIF information table
47        this.exif = exif;
48
49        // image name
50        this.image_name = image_name;
51
52        // Custom duration for the slide, in milliseconds.
53        // This is an optional parameter.
54        // this.timeout = 3000
55
56        // Attributes for the target window:
57        // width=n,height=n,resizable=yes or no,scrollbars=yes or no,
58        // toolbar=yes or no,location=yes or no,directories=yes or no,
59        // status=yes or no,menubar=yes or no,copyhistory=yes or no
60        // Example: "width=200,height=300"
61        this.attr = attr;
62
63        // Create an image object for the slide
64        if (document.images) {
65                this.image = new Image();
66        }
67
68        // Flag to tell when load() has already been called
69        this.loaded = false;
70
71        //--------------------------------------------------
72        this.load = function() {
73                // This method loads the image for the slide
74
75                if (!document.images) { return; }
76
77                if (!this.loaded) {
78                        this.image.src = this.src;
79                        this.loaded = true;
80                }
81        }
82
83        //--------------------------------------------------
84        this.hotlink = function() {
85                // This method jumps to the slide's link.
86                // If a window was specified for the slide, then it opens a new window.
87
88                var mywindow;
89
90                // If this slide does not have a link, do nothing
91                if (!this.link) return;
92
93                // Open the link in a separate window?
94                if (this.target) {
95
96                        // If window attributes are specified,
97                        // use them to open the new window
98                        if (this.attr) {
99                                mywindow = window.open(this.link, this.target, this.attr);
100
101                        } else {
102                                // If window attributes are not specified, do not use them
103                                // (this will copy the attributes from the originating window)
104                                mywindow = window.open(this.link, this.target);
105                        }
106
107                        // Pop the window to the front
108                        if (mywindow && mywindow.focus) mywindow.focus();
109
110                } else {
111                        // Open the link in the current window
112                        location.href = this.link;
113                }
114        }
115}
116
117//==================================================
118// slideshow object
119//==================================================
120function slideshow( slideshowname ) {
121        // This is the constructor function for the slideshow object.
122        // It is called automatically when you create a new object.
123        // For example:
124        // ss = new slideshow("ss");
125
126        // Name of this object
127        // (required if you want your slideshow to auto-play)
128        // For example, "SLIDES1"
129        this.name = slideshowname;
130
131        // When we reach the last slide, should we loop around to start the
132        // slideshow again?
133        this.repeat = false;
134
135        // Number of images to pre-fetch.
136        // -1 = preload all images.
137        //      0 = load each image is it is used.
138        //      n = pre-fetch n images ahead of the current image.
139        // I recommend preloading all images unless you have large
140        // images, or a large amount of images.
141        this.prefetch = -1;
142
143        // IMAGE element on your HTML page.
144        // For example, document.images.SLIDES1IMG
145        this.image;
146
147        // ID of a DIV element on your HTML page that will contain the text.
148        // For example, "slides2text"
149        // Note: after you set this variable, you should call
150        // the update() method to update the slideshow display.
151        this.textid;
152
153        // ID of a DIV containing HTML for your EXIF info table
154        this.exifid;
155
156        // ID of a DIV containing HTML for your image name
157        this.imagenameid;
158
159        // TEXTAREA element on your HTML page.
160        // For example, document.SLIDES1FORM.SLIDES1TEXT
161        // This is a deprecated method for displaying the text,
162        // but you might want to supply it for older browsers.
163        this.textarea;
164
165        // Milliseconds to pause between slides.
166        // Individual slides can override this.
167        this.timeout = 3000;
168
169        // Redirect URL called at the end of the slideshow
170        this.redirect = false;
171
172        // Hook functions to be called before and after updating the slide
173        // this.pre_update_hook = function() { }
174        // this.post_update_hook = function() { }
175
176        // These are private variables
177        this.slides = new Array();
178        this.current = 0;
179        this.timeoutid = 0;
180
181        //--------------------------------------------------
182        // Public methods
183        //--------------------------------------------------
184        this.add_slide = function(slide) {
185                // Add a slide to the slideshow.
186                // For example:
187                // SLIDES1.add_slide(new slide("s1.jpg", "link.html"))
188
189                var i = this.slides.length;
190
191                // Prefetch the slide image if necessary
192                if (this.prefetch == -1) {
193                        slide.load();
194                }
195
196                this.slides[i] = slide;
197        }
198
199        //--------------------------------------------------
200        this.play = function(timeout) {
201                // This method implements the automatically running slideshow.
202                // If you specify the "timeout" argument, then a new default
203                // timeout will be set for the slideshow.
204
205                // Make sure we're not already playing
206                this.pause();
207
208                // If the timeout argument was specified (optional)
209                // then make it the new default
210                if (timeout) {
211                        this.timeout = timeout;
212                }
213
214                // If the current slide has a custom timeout, use it;
215                // otherwise use the default timeout
216                if (typeof this.slides[ this.current ].timeout != 'undefined') {
217                        timeout = this.slides[ this.current ].timeout;
218                } else {
219                        timeout = this.timeout;
220                }
221
222                // After the timeout, call this.loop()
223                this.timeoutid = setTimeout( this.name + ".loop()", timeout);
224        }
225
226        //--------------------------------------------------
227        this.pause = function() {
228                // This method stops the slideshow if it is automatically running.
229
230                if (this.timeoutid != 0) {
231
232                        clearTimeout(this.timeoutid);
233                        this.timeoutid = 0;
234
235                }
236        }
237
238        //--------------------------------------------------
239        this.update = function() {
240                // This method updates the slideshow image on the page
241
242                // Make sure the slideshow has been initialized correctly
243                if (! this.valid_image()) { return; }
244
245                // Call the pre-update hook function if one was specified
246                if (typeof this.pre_update_hook == 'function') {
247                        this.pre_update_hook();
248                }
249
250                // Convenience variable for the current slide
251                var slide = this.slides[ this.current ];
252
253                // Determine if the browser supports filters
254                var dofilter = false;
255                if (this.image &&
256                                typeof this.image.filters != 'undefined' &&
257                                typeof this.image.filters[0] != 'undefined') {
258
259                        dofilter = true;
260
261                }
262
263                // Load the slide image if necessary
264                slide.load();
265
266                // Apply the filters for the image transition
267                if (dofilter) {
268
269                        // If the user has specified a custom filter for this slide,
270                        // then set it now
271                        if (slide.filter &&
272                                        this.image.style &&
273                                        this.image.style.filter) {
274
275                                this.image.style.filter = slide.filter;
276
277                        }
278                        this.image.filters[0].Apply();
279                }
280
281                // Update the image.
282                this.image.src = slide.image.src;
283
284                // Play the image transition filters
285                if (dofilter) {
286                        this.image.filters[0].Play();
287                }
288
289                // Update the text
290                this.display_text();
291
292                // Call the post-update hook function if one was specified
293                if (typeof this.post_update_hook == 'function') {
294                        this.post_update_hook();
295                }
296
297                // Do we need to pre-fetch images?
298                if (this.prefetch > 0) {
299
300                        var next, prev, count;
301
302                        // Pre-fetch the next slide image(s)
303                        next = this.current;
304                        prev = this.current;
305                        count = 0;
306                        do {
307
308                                // Get the next and previous slide number
309                                // Loop past the ends of the slideshow if necessary
310                                if (++next >= this.slides.length) next = 0;
311                                if (--prev < 0) prev = this.slides.length - 1;
312
313                                // Preload the slide image
314                                this.slides[next].load();
315                                this.slides[prev].load();
316
317                                // Keep going until we have fetched
318                                // the designated number of slides
319
320                        } while (++count < this.prefetch);
321                }
322        }
323
324        //--------------------------------------------------
325        this.goto_slide = function(n) {
326                // This method jumpts to the slide number you specify.
327                // If you use slide number -1, then it jumps to the last slide.
328                // You can use this to make links that go to a specific slide,
329                // or to go to the beginning or end of the slideshow.
330                // Examples:
331                // onClick="myslides.goto_slide(0)"
332                // onClick="myslides.goto_slide(-1)"
333                // onClick="myslides.goto_slide(5)"
334
335                if (n == -1) {
336                        n = this.slides.length - 1;
337                }
338
339                if (n < this.slides.length && n >= 0) {
340                        this.current = n;
341                }
342
343                this.update();
344        }
345
346        //--------------------------------------------------
347        this.goto_random_slide = function(include_current) {
348                // Picks a random slide (other than the current slide) and
349                // displays it.
350                // If the include_current parameter is true,
351                // then
352                // See also: shuffle()
353
354                var i;
355
356                // Make sure there is more than one slide
357                if (this.slides.length > 1) {
358
359                        // Generate a random slide number,
360                        // but make sure it is not the current slide
361                        do {
362                                i = Math.floor(Math.random()*this.slides.length);
363                        } while (i == this.current);
364
365                        // Display the slide
366                        this.goto_slide(i);
367                }
368        }
369
370        //--------------------------------------------------
371        this.next = function() {
372                // This method advances to the next slide.
373
374                // Increment the image number
375                if (this.current < this.slides.length - 1) {
376                        this.current++;
377                } else if (this.repeat) {
378                        this.current = 0;
379                } else if (this.redirect) {
380                        window.location = this.redirect;
381                }
382
383                this.update();
384        }
385
386        //--------------------------------------------------
387        this.previous = function() {
388                // This method goes to the previous slide.
389
390                // Decrement the image number
391                if (this.current > 0) {
392                        this.current--;
393                } else if (this.repeat) {
394                        this.current = this.slides.length - 1;
395                }
396
397                this.update();
398        }
399
400        //--------------------------------------------------
401        this.shuffle = function() {
402                // This method randomly shuffles the order of the slides.
403
404                var i, i2, slides_copy, slides_randomized;
405
406                // Create a copy of the array containing the slides
407                // in sequential order
408                slides_copy = new Array();
409                for (i = 0; i < this.slides.length; i++) {
410                        slides_copy[i] = this.slides[i];
411                }
412
413                // Create a new array to contain the slides in random order
414                slides_randomized = new Array();
415
416                // To populate the new array of slides in random order,
417                // loop through the existing slides, picking a random
418                // slide, removing it from the ordered list and adding it to
419                // the random list.
420
421                do {
422
423                        // Pick a random slide from those that remain
424                        i = Math.floor(Math.random()*slides_copy.length);
425
426                        // Add the slide to the end of the randomized array
427                        slides_randomized[ slides_randomized.length ] =
428                                slides_copy[i];
429
430                        // Remove the slide from the sequential array,
431                        // so it cannot be chosen again
432                        for (i2 = i + 1; i2 < slides_copy.length; i2++) {
433                                slides_copy[i2 - 1] = slides_copy[i2];
434                        }
435                        slides_copy.length--;
436
437                        // Keep going until we have removed all the slides
438
439                } while (slides_copy.length);
440
441                // Now set the slides to the randomized array
442                this.slides = slides_randomized;
443        }
444
445        //--------------------------------------------------
446        this.get_text = function() {
447                // This method returns the text of the current slide
448
449                return(this.slides[ this.current ].text);
450        }
451
452        //--------------------------------------------------
453        this.get_all_text = function(before_slide, after_slide) {
454                // Return the text for all of the slides.
455                // For the text of each slide, add "before_slide" in front of the
456                // text, and "after_slide" after the text.
457                // For example:
458                // document.write("<ul>");
459                // document.write(s.get_all_text("<li>","\n"));
460                // document.write("<\/ul>");
461
462                all_text = "";
463
464                // Loop through all the slides in the slideshow
465                for (i=0; i < this.slides.length; i++) {
466
467                        slide = this.slides[i];
468
469                        if (slide.text) {
470                                all_text += before_slide + slide.text + after_slide;
471                        }
472
473                }
474
475                return(all_text);
476        }
477
478        //--------------------------------------------------
479        this.display_text = function(text) {
480                // Display the text for the current slide
481
482                // If the "text" arg was not supplied (usually it isn't),
483                // get the text from the slideshow
484                if (!text) {
485                        text = this.slides[ this.current ].image_name + ": " + this.slides[ this.current ].text;
486                }
487
488                // If a textarea has been specified,
489                // then change the text displayed in it
490                if (this.textarea && typeof this.textarea.value != 'undefined') {
491                        this.textarea.value = text;
492                }
493
494                // If a text id has been specified,
495                // then change the contents of the HTML element
496                if (this.textid) {
497
498                        r = this.getElementById(this.textid);
499                        if (!r) { return false; }
500                        if (typeof r.innerHTML == 'undefined') { return false; }
501
502                        // Update the text
503                        r.innerHTML = text;
504                }
505        }
506
507        //--------------------------------------------------
508        this.hotlink = function() {
509                // This method calls the hotlink() method for the current slide.
510       
511                this.slides[ this.current ].hotlink();
512        }
513
514        //--------------------------------------------------
515        this.save_position = function(cookiename) {
516                // Saves the position of the slideshow in a cookie,
517                // so when you return to this page, the position in the slideshow
518                // won't be lost.
519
520                if (!cookiename) {
521                        cookiename = this.name + '_slideshow';
522                }
523
524                document.cookie = cookiename + '=' + this.current;
525        }
526
527        //--------------------------------------------------
528        this.restore_position = function(cookiename) {
529        // If you previously called slideshow_save_position(),
530        // returns the slideshow to the previous state.
531
532                //Get cookie code by Shelley Powers
533
534                if (!cookiename) {
535                        cookiename = this.name + '_slideshow';
536                }
537
538                var search = cookiename + "=";
539
540                if (document.cookie.length > 0) {
541                        offset = document.cookie.indexOf(search);
542                        // if cookie exists
543                        if (offset != -1) {
544                                offset += search.length;
545                                // set index of beginning of value
546                                end = document.cookie.indexOf(";", offset);
547                                // set index of end of cookie value
548                                if (end == -1) end = document.cookie.length;
549                                this.current = parseInt(unescape(document.cookie.substring(offset, end)));
550                                }
551                 }
552        }
553
554        //--------------------------------------------------
555        this.noscript = function() {
556                // This method is not for use as part of your slideshow,
557                // but you can call it to get a plain HTML version of the slideshow
558                // images and text.
559                // You should copy the HTML and put it within a NOSCRIPT element, to
560                // give non-javascript browsers access to your slideshow information.
561                // This also ensures that your slideshow text and images are indexed
562                // by search engines.
563
564                $html = "\n";
565
566                // Loop through all the slides in the slideshow
567                for (i=0; i < this.slides.length; i++) {
568
569                        slide = this.slides[i];
570
571                        $html += '<p>';
572
573                        if (slide.link) {
574                                $html += '<a href="' + slide.link + '">';
575                        }
576
577                        $html += '<img src="' + slide.src + '" alt="slideshow image" \/>';
578
579                        if (slide.link) {
580                                $html += "<\/a>";
581                        }
582
583                        if (slide.text) {
584                                $html += "<br \/>\n" + slide.text;
585                        }
586
587                        $html += "<\/p>" + "\n\n";
588                }
589
590                // Make the HTML browser-safe
591                $html = $html.replace(/\&/g, "&amp;" );
592                $html = $html.replace(/</g, "&lt;" );
593                $html = $html.replace(/>/g, "&gt;" );
594       
595                return('<pre>' + $html + '</pre>');
596        }
597
598        //==================================================
599        // Private methods
600        //==================================================
601
602        //--------------------------------------------------
603        this.loop = function() {
604                // This method is for internal use only.
605                // This method gets called automatically by a JavaScript timeout.
606                // It advances to the next slide, then sets the next timeout.
607                // If the next slide image has not completed loading yet,
608                // then do not advance to the next slide yet.
609
610                // Make sure the next slide image has finished loading
611                if (this.current < this.slides.length - 1) {
612                        next_slide = this.slides[this.current + 1];
613                        if (next_slide.image.complete == null || next_slide.image.complete) {
614                                this.next();
615                        }
616                } else { // we're at the last slide
617                        this.next();
618                }
619               
620                // Keep playing the slideshow
621                this.play( );
622        }
623
624
625        //--------------------------------------------------
626        this.valid_image = function() {
627                // Returns 1 if a valid image has been set for the slideshow
628       
629                if (!this.image)
630                {
631                        return false;
632                }
633                else {
634                        return true;
635                }
636        }
637
638        //--------------------------------------------------
639        this.getElementById = function(element_id) {
640                // This method returns the element corresponding to the id
641
642                if (document.getElementById) {
643                        return document.getElementById(element_id);
644                }
645                else if (document.all) {
646                        return document.all[element_id];
647                }
648                else if (document.layers) {
649                        return document.layers[element_id];
650                } else {
651                        return undefined;
652                }
653        }
654       
655
656        //==================================================
657        // Deprecated methods
658        // I don't recommend the use of the following methods,
659        // but they are included for backward compatibility.
660        // You can delete them if you don't need them.
661        //==================================================
662
663        //--------------------------------------------------
664        this.set_image = function(imageobject) {
665                // This method is deprecated; you should use
666                // the following code instead:
667                // s.image = document.images.myimagename;
668                // s.update();
669
670                if (!document.images)
671                        return;
672                this.image = imageobject;
673        }
674
675        //--------------------------------------------------
676        this.set_textarea = function(textareaobject) {
677                // This method is deprecated; you should use
678                // the following code instead:
679                // s.textarea = document.form.textareaname;
680                // s.update();
681
682                this.textarea = textareaobject;
683                this.display_text();
684        }
685
686        //--------------------------------------------------
687        this.set_textid = function(textidstr) {
688                // This method is deprecated; you should use
689                // the following code instead:
690                // s.textid = "mytextid";
691                // s.update();
692
693                this.textid = textidstr;
694                this.display_text();
695        }
696}
Note: See TracBrowser for help on using the repository browser.