Controller.js 5.9 KB

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