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         
90         this.play.defer(this.wait, this);
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             
109                         this.iImageId = 0;                                      
110                 } else {
111                         this.swapImage(imageShow,imageHide);                    
112                         this.iImageId++;
113                 }
114                 
115                 this.textIn = this.iImageId+1 + ' of ' + this.numOfImages;
116                 this.updatecounter();
117         this.play.defer(this.wait, this);
118         },
119         
120         stop: function  () {
121                 clearInterval(this.play);                               
122                 this.playButton && this.playButton.appear({ duration: 0});
123                 this.pauseButton && this.pauseButton.hide();
124         },
125         
126         goNext: function () {
127                 clearInterval(this.play);
128                 $(this.playButton).appear({ duration: 0});
129                 $(this.pauseButton).hide();
130                 
131                 var imageShow, imageHide;
132         
133                 imageShow = this.iImageId+1;
134                 imageHide = this.iImageId;
135                 
136                 if (imageShow == this.numOfImages) {
137                         this.swapImage(0,imageHide);    
138                         this.iImageId = 0;                                      
139                 } else {
140                         this.swapImage(imageShow,imageHide);                    
141                         this.iImageId++;
142                 }
143         
144                 this.updatecounter();
145         },
146         
147         goPrevious: function () {
148                 clearInterval(this.play);
149                 $(this.playButton).appear({ duration: 0});
150                 $(this.pauseButton).hide();
151         
152                 var imageShow, imageHide;
153                                         
154                 imageShow = this.iImageId-1;
155                 imageHide = this.iImageId;
156                 
157                 if (this.iImageId == 0) {
158                         this.swapImage(this.numOfImages-1,imageHide);   
159                         this.iImageId = this.numOfImages-1;             
160                         
161                         //alert(NumOfImages-1 + ' and ' + imageHide + ' i=' + i)
162                                                 
163                 } else {
164                         this.swapImage(imageShow,imageHide);                    
165                         this.iImageId--;
166                         
167                         //alert(imageShow + ' and ' + imageHide)
168                 }
169                 
170                 this.updatecounter();
171         },
172         
173         updatecounter: function () {
174         if (!this.counter) {
175             return;
176         }
177                 var textIn = this.iImageId+1 + ' of ' + this.numOfImages;
178                 this.counter.update( textIn );
179         var oNewCaption = this.slides[this.iImageId]).select('.image-caption', true)
180                 if ( this.caption &&  oNewCaption.length ) {
181                         this.caption.update( oNewCaption[0].innerHTML;
182                 }
183         }
184 }
185   
186