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