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