source: Dev/trunk/src/client/dojox/widget/rotator/Controller.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.6 KB
Line 
1define([
2        "dojo/_base/declare",
3        "dojo/_base/lang",
4        "dojo/_base/html",
5        "dojo/_base/event",
6        "dojo/_base/array",
7        "dojo/_base/connect",
8        "dojo/query"
9], function(declare, lang, html, event, array, connect, query) {
10
11        var _dojoxRotator = "dojoxRotator",
12                _play = _dojoxRotator + "Play",
13                _pause = _dojoxRotator + "Pause",
14                _number = _dojoxRotator + "Number",
15                _tab = _dojoxRotator + "Tab",
16                _selected = _dojoxRotator + "Selected";
17
18        return declare("dojox.widget.rotator.Controller", null, {
19                // summary:
20                //              A controller that manipulates a Rotator or AutoRotator.
21                // description:
22                //              Displays a series of controls that send actions to a Rotator or
23                //              AutoRotator.  The Controller supports the following controls:
24                //
25                //              - Next pane
26                //              - Previous pane
27                //              - Play/Pause toggler
28                //              - Numbered tabs
29                //              - Titled tabs
30                //              - Information
31                //
32                //              You may specify any of these controls in any order.  You may also
33                //              have multiple Controllers tied to a single Rotator instance.
34                //
35                //              The Controller's DOM node may also be styled for positioning or
36                //              other styled preferences.
37                // example:
38                //      |       <div dojoType="dojox.widget.rotator.Controller"
39                //      |               rotator="myRotator"
40                //      |       ></div>
41                // example:
42                //      |       <div dojoType="dojox.widget.rotator.Controller"
43                //      |               rotator="myRotator"
44                //      |               controls="prev,#,next"
45                //      |               class="myCtrl"
46                //      |       ></div>
47                // example:
48                //      |       <div dojoType="dojox.widget.rotator.Controller"
49                //      |               rotator="myRotator"
50                //      |               controls="titles"
51                //      |               class="myCtrl"
52                //      |       ></div>s
53
54                // rotator: dojox.widget.Rotator
55                //              An instance of a Rotator widget.
56                rotator: null,
57
58                // commands: string
59                //              A comma-separated list of commands. Valid commands are:
60                //                prev                  An icon button to go to the previous pane.
61                //                next                  An icon button to go to the next pane.
62                //                play/pause    A play and pause toggle icon button.
63                //                info                  Displays the current and total panes. (ie "1 / 4")
64                //                #                             Displays a number button for each pane. (ie "1 2 3 4")
65                //                titles                Displays each pane's title as a tab. (ie "Home Services Contact Blog")
66                commands: "prev,play/pause,info,next",
67
68                constructor: function(/*Object*/params, /*DomNode|string*/node){
69                        // summary:
70                        //              Initializes the pager and connect to the rotator.
71
72                        lang.mixin(this, params);
73
74                        // check if we have a valid rotator
75                        var r = this.rotator;
76                        if(r){
77                                // remove all of the controller's child nodes just in case
78                                while(node.firstChild){
79                                        node.removeChild(node.firstChild);
80                                }
81
82                                var ul = this._domNode = html.create("ul", null, node),
83                                        icon = " " + _dojoxRotator + "Icon",
84
85                                        // helper function for creating a button
86                                        cb = function(/*string*/label, /*string*/css, /*array*/action){
87                                                html.create("li", {
88                                                        className: css,
89                                                        innerHTML: '<a href="#"><span>' + label + '</span></a>',
90                                                        onclick: function(/*event*/e){
91                                                                event.stop(e);
92                                                                if(r){
93                                                                        r.control.apply(r, action);
94                                                                }
95                                                        }
96                                                }, ul);
97                                        };
98
99                                // build out the commands
100                                array.forEach(this.commands.split(','), function(b, i){
101                                        switch(b){
102                                                case "prev":
103                                                        cb("Prev", _dojoxRotator + "Prev" + icon, ["prev"]);
104                                                        break;
105                                                case "play/pause":
106                                                        cb("Play", _play + icon, ["play"]);
107                                                        cb("Pause", _pause + icon, ["pause"]);
108                                                        break;
109                                                case "info":
110                                                        this._info = html.create("li", {
111                                                                className: _dojoxRotator + "Info",
112                                                                innerHTML: this._buildInfo(r)
113                                                        }, ul);
114                                                        break;
115                                                case "next":
116                                                        cb("Next", _dojoxRotator + "Next" + icon, ["next"]);
117                                                        break;
118                                                case "#":
119                                                case "titles":
120                                                        for(var j=0; j<r.panes.length; j++){
121                                                                cb(
122                                                                        /*label*/ b == '#' ? j+1 : r.panes[j].title || "Tab " + (j+1),
123                                                                        /*css*/ (b == '#' ? _number : _tab) + ' ' + (j == r.idx ? _selected : "") + ' ' + _dojoxRotator + "Pane" + j,
124                                                                        /*action*/ ["go", j]
125                                                                );
126                                                        }
127                                                        break;
128                                        }
129                                }, this);
130
131                                // add the first/last classes for styling
132                                query("li:first-child", ul).addClass(_dojoxRotator + "First");
133                                query("li:last-child", ul).addClass(_dojoxRotator + "Last");
134
135                                // set the initial state of the play/pause toggle button
136                                this._togglePlay();
137
138                                this._con = connect.connect(r, "onUpdate", this, "_onUpdate");
139                        }
140                },
141
142                destroy: function(){
143                        // summary:
144                        //              Disconnect from the rotator.
145                        connect.disconnect(this._con);
146                        html.destroy(this._domNode);
147                },
148
149                _togglePlay: function(/*boolean*/playing){
150                        // summary:
151                        //              Toggles the play/pause button, if it exists.
152
153                        var p = this.rotator.playing;
154                        query('.'+_play, this._domNode).style("display", p ? "none" : "");
155                        query('.'+_pause, this._domNode).style("display", p ? "" : "none");
156                },
157
158                _buildInfo: function(/*dojox.widget.Rotator*/r){
159                        // summary:
160                        //              Return a string containing the current pane number and the total number of panes.
161                        return '<span>' + (r.idx+1) + ' / ' + r.panes.length + '</span>'; /*string*/
162                },
163
164                _onUpdate: function(/*string*/type){
165                        // summary:
166                        //              Updates various pager controls when the rotator updates.
167
168                        var r = this.rotator; // no need to test if this is null since _onUpdate is only fired by the rotator
169
170                        switch(type){
171                                case "play":
172                                case "pause":
173                                        this._togglePlay();
174                                        break;
175                                case "onAfterTransition":
176                                        if(this._info){
177                                                this._info.innerHTML = this._buildInfo(r);
178                                        }
179
180                                        // helper function for selecting the current tab
181                                        var s = function(/*NodeList*/n){
182                                                if(r.idx < n.length){
183                                                        html.addClass(n[r.idx], _selected);
184                                                }
185                                        };
186
187                                        s(query('.' + _number, this._domNode).removeClass(_selected));
188                                        s(query('.' + _tab, this._domNode).removeClass(_selected));
189                                        break;
190                        }
191                }
192        });
193});
Note: See TracBrowser for help on using the repository browser.