Controller.js 5.6 KB

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