ux/Slideshow.js
[roojs1] / ux / Slideshow.js
1 //<script type="text/javascript">
2 /**
3  * Slideshow  - based on a prototype one..
4  *
5  *
6  * usage:
7  * 
8 Roo.onReady( function() 
9 {
10    // use a dom selector to define slides..
11     var slides = Roo.DomQuery.select('.category-slide');
12    
13     oMySlides = new Roo.ux.Slideshow({
14         slides          : slides,
15     });
16              
17 });
18  * 
19  * 
20  * @class Roo.ux.Slideshow
21  */
22
23 Roo.ux.Slideshow = function(cfg ) 
24 {
25     Roo.apply(this, cfg);
26     if ( this.slides ) {
27         this.numOfImages        = this.slides.length;
28         if ( !this.numOfImages ) {
29             alert('No slides?');
30         }
31     }
32     this.playButton = Roo.get(this.playButton);
33     this.pauseButton = Roo.get(this.pauseButton);
34     this.counter = Roo.get(this.counter);
35     if ( this.autostart ) {
36         this.startSlideShow();
37     }
38 }
39
40 Roo.apply(Roo.ux.Slideshow.prototype, {
41     
42     wait                        : 4000
43         start                   : 0
44         duration                : 0.5,
45         autostart               : true ,
46         slides              : false,
47     /**
48      * @config id/dom element counter to show the 
49      */
50         counter             : false,
51         caption             : '',
52      /**
53      * @config id/dom element for play button
54      */
55         playButton              : false, 
56     /**
57      * @config id/dom element of the pause button
58      */
59         pauseButton         : false, 
60         iImageId                : 0, 
61     
62     
63         // The Fade Function
64         swapImage: function (x,y) { 
65                 $(this.slides[x]) && $(this.slides[x]).appear({ duration: this.duration });
66                 $(this.slides[y]) && $(this.slides[y]).fade({duration: this.duration });
67         },
68         
69         // the onload event handler that starts the fading.
70         startSlideShow: function () {
71         var _t  = this;
72                 window.setInterval(function () {
73             _t.play() },this.wait);
74                 this.playButton && this.playButton.hide();
75                 this.pauseButton && this.pauseButton.appear({ duration: 0});
76      
77                 this.updatecounter();
78                                                                         
79         },
80         
81         play: function () {
82                 
83                 var imageShow, imageHide;
84         
85                 imageShow = this.iImageId+1;
86                 imageHide = this.iImageId;
87                 
88                 if (imageShow == this.numOfImages) {
89                         this.swapImage(0,imageHide);    
90                         this.iImageId = 0;                                      
91                 } else {
92                         this.swapImage(imageShow,imageHide);                    
93                         this.iImageId++;
94                 }
95                 
96                 this.textIn = this.iImageId+1 + ' of ' + this.numOfImages;
97                 this.updatecounter();
98         },
99         
100         stop: function  () {
101                 clearInterval(this.play);                               
102                 this.playButton && this.playButton.appear({ duration: 0});
103                 this.pauseButton && this.pauseButton.hide();
104         },
105         
106         goNext: function () {
107                 clearInterval(this.play);
108                 $(this.playButton).appear({ duration: 0});
109                 $(this.pauseButton).hide();
110                 
111                 var imageShow, imageHide;
112         
113                 imageShow = this.iImageId+1;
114                 imageHide = this.iImageId;
115                 
116                 if (imageShow == this.numOfImages) {
117                         this.swapImage(0,imageHide);    
118                         this.iImageId = 0;                                      
119                 } else {
120                         this.swapImage(imageShow,imageHide);                    
121                         this.iImageId++;
122                 }
123         
124                 this.updatecounter();
125         },
126         
127         goPrevious: function () {
128                 clearInterval(this.play);
129                 $(this.playButton).appear({ duration: 0});
130                 $(this.pauseButton).hide();
131         
132                 var imageShow, imageHide;
133                                         
134                 imageShow = this.iImageId-1;
135                 imageHide = this.iImageId;
136                 
137                 if (this.iImageId == 0) {
138                         this.swapImage(this.numOfImages-1,imageHide);   
139                         this.iImageId = this.numOfImages-1;             
140                         
141                         //alert(NumOfImages-1 + ' and ' + imageHide + ' i=' + i)
142                                                 
143                 } else {
144                         this.swapImage(imageShow,imageHide);                    
145                         this.iImageId--;
146                         
147                         //alert(imageShow + ' and ' + imageHide)
148                 }
149                 
150                 this.updatecounter();
151         },
152         
153         updatecounter: function () {
154         
155                 var textIn = this.iImageId+1 + ' of ' + this.numOfImages;
156                 $(this.counter) && ( $(this.counter).innerHTML = textIn );
157                 if ( $(this.caption) && ( oNewCaption = $(this.slides[this.iImageId]).down('.image-caption') ) ) {
158                         $(this.caption).innerHTML = oNewCaption.innerHTML;
159                 }
160         }
161 }
162   
163