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         if (this.slides[x] ) {
66             this.slides[x].animate( 
67                 { opacity : { from : 0.0, to : 1.0 }},
68                 this.duration
69             ); 
70             this.slides[x].show();
71         }
72         if (this.slides[y] ) {
73             this.slides[y].animate( 
74                 { opacity : { from : 1.0, to : 0.0 }},
75                 this.duration
76             ); 
77             this.slides[y].show();
78         } 
79                 
80         },
81         
82         // the onload event handler that starts the fading.
83         startSlideShow: function () {
84         var _t  = this;
85                 window.setInterval(function () {
86             _t.play() }, this.wait);
87             
88                 this.playButton && this.playButton.hide();
89                 this.pauseButton && this.pauseButton.show();
90      
91                 this.updatecounter();
92                                                                         
93         },
94         
95         play: function () {
96                 
97                 var imageShow, imageHide;
98         
99                 imageShow = this.iImageId+1;
100                 imageHide = this.iImageId;
101                 
102                 if (imageShow == this.numOfImages) {
103                         this.swapImage(0,imageHide);    
104                         this.iImageId = 0;                                      
105                 } else {
106                         this.swapImage(imageShow,imageHide);                    
107                         this.iImageId++;
108                 }
109                 
110                 this.textIn = this.iImageId+1 + ' of ' + this.numOfImages;
111                 this.updatecounter();
112         },
113         
114         stop: function  () {
115                 clearInterval(this.play);                               
116                 this.playButton && this.playButton.appear({ duration: 0});
117                 this.pauseButton && this.pauseButton.hide();
118         },
119         
120         goNext: function () {
121                 clearInterval(this.play);
122                 $(this.playButton).appear({ duration: 0});
123                 $(this.pauseButton).hide();
124                 
125                 var imageShow, imageHide;
126         
127                 imageShow = this.iImageId+1;
128                 imageHide = this.iImageId;
129                 
130                 if (imageShow == this.numOfImages) {
131                         this.swapImage(0,imageHide);    
132                         this.iImageId = 0;                                      
133                 } else {
134                         this.swapImage(imageShow,imageHide);                    
135                         this.iImageId++;
136                 }
137         
138                 this.updatecounter();
139         },
140         
141         goPrevious: function () {
142                 clearInterval(this.play);
143                 $(this.playButton).appear({ duration: 0});
144                 $(this.pauseButton).hide();
145         
146                 var imageShow, imageHide;
147                                         
148                 imageShow = this.iImageId-1;
149                 imageHide = this.iImageId;
150                 
151                 if (this.iImageId == 0) {
152                         this.swapImage(this.numOfImages-1,imageHide);   
153                         this.iImageId = this.numOfImages-1;             
154                         
155                         //alert(NumOfImages-1 + ' and ' + imageHide + ' i=' + i)
156                                                 
157                 } else {
158                         this.swapImage(imageShow,imageHide);                    
159                         this.iImageId--;
160                         
161                         //alert(imageShow + ' and ' + imageHide)
162                 }
163                 
164                 this.updatecounter();
165         },
166         
167         updatecounter: function () {
168         
169                 var textIn = this.iImageId+1 + ' of ' + this.numOfImages;
170                 $(this.counter) && ( $(this.counter).innerHTML = textIn );
171                 if ( $(this.caption) && ( oNewCaption = $(this.slides[this.iImageId]).down('.image-caption') ) ) {
172                         $(this.caption).innerHTML = oNewCaption.innerHTML;
173                 }
174         }
175 }
176   
177