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     this.caption = Roo.get(this.caption);
36     if ( this.autostart ) {
37         this.startSlideShow();
38     }
39 }
40
41 Roo.apply(Roo.ux.Slideshow.prototype, {
42     
43     wait                        : 4000
44         start                   : 0
45         duration                : 0.5,
46         autostart               : true ,
47         slides              : false,
48     /**
49      * @config id/dom element counter to show the 
50      */
51         counter             : false,
52      /**
53      * @config id/dom element of the caption.
54      */
55         caption             : false,
56      /**
57      * @config id/dom element for play button
58      */
59         playButton              : false, 
60     /**
61      * @config id/dom element of the pause button
62      */
63         pauseButton         : false, 
64         iImageId                : 0, 
65     
66     
67         // The Fade Function
68         swapImage: function (x,y) {
69         if (this.slides[x] ) {
70             this.slides[x].animate( 
71                 { opacity : { from : 0.0, to : 1.0 }},
72                 this.duration
73             ); 
74             this.slides[x].show();
75         }
76         if (this.slides[y] ) {
77             this.slides[y].animate( 
78                 { opacity : { from : 1.0, to : 0.0 }},
79                 this.duration
80             ); 
81             this.slides[y].show();
82         } 
83                 
84         },
85         
86         // the onload event handler that starts the fading.
87         startSlideShow: function () {
88         var _t  = this;
89                 window.setInterval(function () {
90             _t.play() }, this.wait);
91             
92                 this.playButton && this.playButton.hide();
93                 this.pauseButton && this.pauseButton.show();
94      
95                 this.updatecounter();
96                                                                         
97         },
98         
99         play: function () {
100                 
101                 var imageShow, imageHide;
102         
103                 imageShow = this.iImageId+1;
104                 imageHide = this.iImageId;
105                 
106                 if (imageShow == this.numOfImages) {
107                         this.swapImage(0,imageHide);    
108                         this.iImageId = 0;                                      
109                 } else {
110                         this.swapImage(imageShow,imageHide);                    
111                         this.iImageId++;
112                 }
113                 
114                 this.textIn = this.iImageId+1 + ' of ' + this.numOfImages;
115                 this.updatecounter();
116         },
117         
118         stop: function  () {
119                 clearInterval(this.play);                               
120                 this.playButton && this.playButton.appear({ duration: 0});
121                 this.pauseButton && this.pauseButton.hide();
122         },
123         
124         goNext: function () {
125                 clearInterval(this.play);
126                 $(this.playButton).appear({ duration: 0});
127                 $(this.pauseButton).hide();
128                 
129                 var imageShow, imageHide;
130         
131                 imageShow = this.iImageId+1;
132                 imageHide = this.iImageId;
133                 
134                 if (imageShow == this.numOfImages) {
135                         this.swapImage(0,imageHide);    
136                         this.iImageId = 0;                                      
137                 } else {
138                         this.swapImage(imageShow,imageHide);                    
139                         this.iImageId++;
140                 }
141         
142                 this.updatecounter();
143         },
144         
145         goPrevious: function () {
146                 clearInterval(this.play);
147                 $(this.playButton).appear({ duration: 0});
148                 $(this.pauseButton).hide();
149         
150                 var imageShow, imageHide;
151                                         
152                 imageShow = this.iImageId-1;
153                 imageHide = this.iImageId;
154                 
155                 if (this.iImageId == 0) {
156                         this.swapImage(this.numOfImages-1,imageHide);   
157                         this.iImageId = this.numOfImages-1;             
158                         
159                         //alert(NumOfImages-1 + ' and ' + imageHide + ' i=' + i)
160                                                 
161                 } else {
162                         this.swapImage(imageShow,imageHide);                    
163                         this.iImageId--;
164                         
165                         //alert(imageShow + ' and ' + imageHide)
166                 }
167                 
168                 this.updatecounter();
169         },
170         
171         updatecounter: function () {
172         if (!this.counter) {
173             return;
174         }
175                 var textIn = this.iImageId+1 + ' of ' + this.numOfImages;
176                 this.counter.update( textIn );
177         var oNewCaption = this.slides[this.iImageId]).select('.image-caption', true)
178                 if ( this.caption &&  oNewCaption.length ) {
179                         this.caption.update( oNewCaption[0].innerHTML;
180                 }
181         }
182 }
183   
184