RotatorContainer.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. define("dojox/layout/RotatorContainer", ["dojo/_base/declare","dojo/_base/html","dojo/_base/connect","dojo/_base/lang","dojo/_base/array",
  2. "dojo/_base/fx","dojo/fx","dijit/_base/manager","dijit/layout/StackContainer","dijit/layout/StackController","dijit/_Widget",
  3. "dijit/_Templated","dijit/_Contained"
  4. ],function(declare,html,connect,lang,array,baseFx,coreFx,manager,
  5. StackContainer,StackController,Widget,Templated,Contained){
  6. /*=====
  7. var Widget = dijit._Widget,
  8. Templated = dijit._Templated,
  9. Contained = dijit._Contained,
  10. StackContainer = dijit.layout.StackContainer,
  11. StackController = dijit.layout.StackController;
  12. =====*/
  13. var RotatorContainer = declare("dojox.layout.RotatorContainer",[StackContainer, Templated], {
  14. // summary:
  15. // Extends a StackContainer to automatically transition between children
  16. // and display navigation in the form of tabs or a pager.
  17. //
  18. // description:
  19. // The RotatorContainer cycles through the children with a transition.
  20. //
  21. // published topics:
  22. // [widgetId]-update - Notifies pager(s) that a child has changed.
  23. // Parameters:
  24. // /*boolean*/ playing - true if playing, false if paused
  25. // /*int*/ current - current selected child
  26. // /*int*/ total - total number of children
  27. //
  28. // example:
  29. // | <div dojoType="dojox.layout.RotatorContainer" id="myRotator" showTabs="true" autoStart="true" transitionDelay="5000">
  30. // | <div id="pane1" dojoType="dijit.layout.ContentPane" title="1">
  31. // | Pane 1!
  32. // | </div>
  33. // | <div id="pane2" dojoType="dijit.layout.ContentPane" title="2">
  34. // | Pane 2!
  35. // | </div>
  36. // | <div id="pane3" dojoType="dijit.layout.ContentPane" title="3" transitionDelay="10000">
  37. // | Pane 3 with overrided transitionDelay!
  38. // | </div>
  39. // | </div>
  40. templateString: '<div class="dojoxRotatorContainer"><div dojoAttachPoint="tabNode"></div><div class="dojoxRotatorPager" dojoAttachPoint="pagerNode"></div><div class="dojoxRotatorContent" dojoAttachPoint="containerNode"></div></div>',
  41. // showTabs: Boolean
  42. // Sets the display of the tabs. The tabs are actually a StackController.
  43. // The child's title is used for the tab's label.
  44. showTabs: true,
  45. // transitionDelay: int
  46. // The delay in milliseconds before transitioning to the next child.
  47. transitionDelay: 5000,
  48. // transition: String
  49. // The type of transition to perform when switching children.
  50. // A null transition will transition instantly.
  51. transition: "fade",
  52. // transitionDuration: int
  53. // The duration of the transition in milliseconds.
  54. transitionDuration: 1000,
  55. // autoStart: Boolean
  56. // Starts the timer to transition children upon creation.
  57. autoStart: true,
  58. // suspendOnHover: Boolean
  59. // Pause the rotator when the mouse hovers over it.
  60. suspendOnHover: false,
  61. // pauseOnManualChange: Boolean
  62. // Pause the rotator when the tab is changed or the pager's next/previous
  63. // buttons are clicked.
  64. pauseOnManualChange: null,
  65. // reverse: Boolean
  66. // Causes the rotator to rotate in reverse order.
  67. reverse: false,
  68. // pagerId: String
  69. // ID the pager widget.
  70. pagerId: "",
  71. // cycles: int
  72. // Number of cycles before pausing.
  73. cycles: -1,
  74. // pagerClass: String
  75. // The declared Class of the Pager used for this Widget
  76. pagerClass: "dojox.layout.RotatorPager",
  77. postCreate: function(){
  78. // summary: Initializes the DOM nodes, tabs, and transition stuff.
  79. this.inherited(arguments);
  80. // force this DOM node to a relative position and make sure the children are absolute positioned
  81. html.style(this.domNode, "position", "relative");
  82. // validate the cycles counter
  83. if(this.cycles-0 == this.cycles && this.cycles != -1){
  84. // we need to add 1 because we decrement cycles before the animation starts
  85. this.cycles++;
  86. }else{
  87. this.cycles = -1;
  88. }
  89. // if they didn't specify the pauseOnManualChange, then we want it to be the opposite of
  90. // the suspendOnHover since it doesn't make sense to do both, unless you really want to
  91. if(this.pauseOnManualChange === null){
  92. this.pauseOnManualChange = !this.suspendOnHover;
  93. }
  94. // create the stack controller if we are using tabs
  95. var id = this.id || "rotator"+(new Date()).getTime(),
  96. sc = new StackController({ containerId:id }, this.tabNode);
  97. this.tabNode = sc.domNode;
  98. this._stackController = sc;
  99. html.style(this.tabNode, "display", this.showTabs ? "" : "none");
  100. // if the controller's tabs are clicked, check if we should pause and reset the cycle counter
  101. this.connect(sc, "onButtonClick","_manualChange");
  102. // set up our topic listeners
  103. this._subscriptions = [
  104. connect.subscribe(this.id+"-cycle", this, "_cycle"),
  105. connect.subscribe(this.id+"-state", this, "_state")
  106. ];
  107. // make sure the transition duration isn't less than the transition delay
  108. var d = Math.round(this.transitionDelay * 0.75);
  109. if(d < this.transitionDuration){
  110. this.transitionDuration = d;
  111. }
  112. // wire up the mouse hover events
  113. if(this.suspendOnHover){
  114. this.connect(this.domNode, "onmouseover", "_onMouseOver");
  115. this.connect(this.domNode, "onmouseout", "_onMouseOut");
  116. }
  117. },
  118. startup: function(){
  119. // summary: Initializes the pagers.
  120. if(this._started){ return; }
  121. // check if the pager is defined within the rotator container
  122. var c = this.getChildren();
  123. for(var i=0, len=c.length; i<len; i++){
  124. if(c[i].declaredClass == this.pagerClass){
  125. this.pagerNode.appendChild(c[i].domNode);
  126. break;
  127. }
  128. }
  129. // process the child widgets
  130. this.inherited(arguments);
  131. // check if we should start automatically
  132. if(this.autoStart){
  133. // start playing
  134. setTimeout(lang.hitch(this, "_play"), 10);
  135. }else{
  136. // update the pagers with the initial state
  137. this._updatePager();
  138. }
  139. },
  140. destroy: function(){
  141. // summary: Unsubscribe to all of our topics
  142. array.forEach(this._subscriptions, connect.unsubscribe);
  143. this.inherited(arguments);
  144. },
  145. _setShowTabsAttr: function(/*anything*/value){
  146. this.showTabs = value;
  147. html.style(this.tabNode, "display", value ? "" : "none");
  148. },
  149. _updatePager: function(){
  150. // summary: Notify the pager's current and total numbers.
  151. var c = this.getChildren();
  152. connect.publish(this.id+"-update", [this._playing, array.indexOf(c, this.selectedChildWidget)+1, c.length]);
  153. },
  154. _onMouseOver: function(){
  155. // summary: Triggered when the mouse is moved over the rotator container.
  156. // temporarily suspend the cycling, but don't officially pause it
  157. this._resetTimer();
  158. this._over = true;
  159. },
  160. _onMouseOut: function(){
  161. // summary: Triggered when the mouse is moved off the rotator container.
  162. this._over = false;
  163. // if we were playing, resume playback in 200ms
  164. // we need to wait because we may be moused over again right away
  165. if(this._playing){
  166. clearTimeout(this._timer);
  167. this._timer = setTimeout(lang.hitch(this, "_play", true), 200);
  168. }
  169. },
  170. _resetTimer: function(){
  171. // summary: Resets the timer used to start the next transition.
  172. clearTimeout(this._timer);
  173. this._timer = null;
  174. },
  175. _cycle: function(/*boolean or int*/next){
  176. // summary: Cycles to the next/previous child.
  177. // if next is an int, then _cycle() was called via a timer
  178. // if next is a boolean, then _cycle() was called via the next/prev buttons, stop playing and reset cycles
  179. if(next instanceof Boolean || typeof next == "boolean"){
  180. this._manualChange();
  181. }
  182. var c = this.getChildren(),
  183. len = c.length,
  184. i = array.indexOf(c, this.selectedChildWidget) + (next === false || (next !== true && this.reverse) ? -1 : 1);
  185. this.selectChild(c[(i < len ? (i < 0 ? len-1 : i) : 0)]);
  186. this._updatePager();
  187. },
  188. _manualChange: function(){
  189. // summary: This function is only called when a manual change occurs in which
  190. // case we may need to stop playing and we need to reset the cycle counter
  191. if(this.pauseOnManualChange){
  192. this._playing = false;
  193. }
  194. this.cycles = -1;
  195. },
  196. _play: function(skip){
  197. // summary: Schedules the next transition.
  198. this._playing = true;
  199. this._resetTimer();
  200. if(skip !== true && this.cycles>0){
  201. this.cycles--;
  202. }
  203. if(this.cycles==0){
  204. this._pause();
  205. }else if((!this.suspendOnHover || !this._over) && this.transitionDelay){
  206. // check if current pane has a delay
  207. this._timer = setTimeout(lang.hitch(this, "_cycle"), this.selectedChildWidget.domNode.getAttribute("transitionDelay") || this.transitionDelay);
  208. }
  209. this._updatePager();
  210. },
  211. _pause: function(){
  212. // summary: Clears the transition timer and pauses the rotator.
  213. this._playing = false;
  214. this._resetTimer();
  215. },
  216. _state: function(playing){
  217. // summary: Fired when the play/pause pager button is toggled.
  218. if(playing){
  219. // since we were manually changed, disable the cycle counter
  220. this.cycles = -1;
  221. this._play();
  222. }else{
  223. this._pause();
  224. }
  225. },
  226. _transition: function(/*dijit._Widget*/ next, /*dijit._Widget*/ prev){
  227. // summary: Dispatches the appropriate transition.
  228. this._resetTimer();
  229. // check if we have anything to transition
  230. if(prev && this.transitionDuration){
  231. switch(this.transition){
  232. case "fade": this._fade(next, prev); return;
  233. }
  234. }
  235. this._transitionEnd();
  236. this.inherited(arguments);
  237. },
  238. _transitionEnd: function(){
  239. if(this._playing){
  240. this._play();
  241. }else{
  242. this._updatePager();
  243. }
  244. },
  245. _fade: function(/*dijit._Widget*/ next, /*dijit._Widget*/ prev){
  246. // summary: Crossfades two children.
  247. this._styleNode(prev.domNode, 1, 1);
  248. this._styleNode(next.domNode, 0, 2);
  249. // show the next child and make sure it's sized properly
  250. this._showChild(next);
  251. if(this.doLayout && next.resize){
  252. next.resize(this._containerContentBox || this._contentBox);
  253. }
  254. // create the crossfade animation
  255. var args = { duration:this.transitionDuration },
  256. anim = coreFx.combine([
  257. baseFx["fadeOut"](lang.mixin({node:prev.domNode}, args)),
  258. baseFx["fadeIn"](lang.mixin({node:next.domNode}, args))
  259. ]);
  260. this.connect(anim, "onEnd", lang.hitch(this,function(){
  261. this._hideChild(prev);
  262. this._transitionEnd();
  263. }));
  264. anim.play();
  265. },
  266. _styleNode: function(/*DOMnode*/node, /*number*/opacity, /*int*/zIndex){
  267. // summary: Helper function to style the children.
  268. html.style(node, "opacity", opacity);
  269. html.style(node, "zIndex", zIndex);
  270. html.style(node, "position", "absolute");
  271. }
  272. });
  273. declare("dojox.layout.RotatorPager", [Widget, Templated, Contained], {
  274. // summary:
  275. // Defines controls used to manipulate a RotatorContainer
  276. //
  277. // description:
  278. // A pager can be defined one of two ways:
  279. // * Externally of the RotatorContainer's template and tell the
  280. // RotatorPager the rotatorId of the RotatorContainer
  281. // * As a direct descendant of the RotatorContainer (i.e. inside the
  282. // RotatorContainer's template)
  283. //
  284. // The pager can contain the following components:
  285. // * Previous button
  286. // - Must be a dijit.form.Button
  287. // - dojoAttachPoint must be named "previous"
  288. // * Next button
  289. // - Must be a dijit.form.Button
  290. // - dojoAttachPoint must be named "next"
  291. // * Play/Pause toggle button
  292. // - Must be a dijit.form.ToggleButton
  293. // - dojoAttachPoint must be named "playPause"
  294. // - Use iconClass to specify toggled state
  295. // * Current child #
  296. // - dojoAttachPoint must be named "current"
  297. // * Total # of children
  298. // - dojoAttachPoint must be named "total"
  299. //
  300. // You can choose to exclude specific controls as well as add elements
  301. // for styling.
  302. //
  303. // Should you need a pager, but don't want to use Dijit buttons, you can
  304. // write your own pager widget and just wire it into the topics. The
  305. // topic names are prefixed with the widget ID of the RotatorContainer.
  306. // Notifications are received from and sent to the RotatorContainer as
  307. // well as other RotatorPagers.
  308. //
  309. // published topics:
  310. // [widgetId]-cycle - Notify that the next or previous button was pressed.
  311. // Parameters:
  312. // /*boolean*/ next - true if next, false if previous
  313. // [widgetId]-state - Notify that the play/pause button was toggled.
  314. // Parameters:
  315. // /*boolean*/ playing - true if playing, false if paused
  316. //
  317. // example:
  318. // A pager with the current/total children and previous/next buttons.
  319. // | <div dojoType="dojox.layout.RotatorPager" rotatorId="myRotator">
  320. // | <button dojoType="dijit.form.Button" dojoAttachPoint="previous">Prev</button>
  321. // | <span dojoAttachPoint="current"></span> / <span dojoAttachPoint="total"></span>
  322. // | <button dojoType="dijit.form.Button" dojoAttachPoint="next">Next</button>
  323. // | </div>
  324. //
  325. // example:
  326. // A pager with only a play/pause toggle button.
  327. // | <div dojoType="dojox.layout.RotatorPager" rotatorId="myRotator">
  328. // | <button dojoType="dijit.form.ToggleButton" dojoAttachPoint="playPause"></button>
  329. // | </div>
  330. //
  331. // example:
  332. // A pager styled with iconClass.
  333. // | <div dojoType="dojox.layout.RotatorPager" class="rotatorIcons" rotatorId="myRotator">
  334. // | <button dojoType="dijit.form.Button" iconClass="previous" dojoAttachPoint="previous">Prev</button>
  335. // | <button dojoType="dijit.form.ToggleButton" iconClass="playPause" dojoAttachPoint="playPause"></button>
  336. // | <button dojoType="dijit.form.Button" iconClass="next" dojoAttachPoint="next">Next</button>
  337. // | <span dojoAttachPoint="current"></span> / <span dojoAttachPoint="total"></span>
  338. // | </div>
  339. widgetsInTemplate: true,
  340. // rotatorId: int
  341. // The ID of the rotator this pager is tied to.
  342. // Only required if defined outside of the RotatorContainer's container.
  343. rotatorId: "",
  344. postMixInProperties: function(){
  345. this.templateString = "<div>" + this.srcNodeRef.innerHTML + "</div>";
  346. },
  347. postCreate: function(){
  348. var p = manager.byId(this.rotatorId) || this.getParent();
  349. if(p && p.declaredClass == "dojox.layout.RotatorContainer"){
  350. if(this.previous){
  351. connect.connect(this.previous, "onClick", function(){
  352. connect.publish(p.id+"-cycle", [false]);
  353. });
  354. }
  355. if(this.next){
  356. connect.connect(this.next, "onClick", function(){
  357. connect.publish(p.id+"-cycle", [true]);
  358. });
  359. }
  360. if(this.playPause){
  361. connect.connect(this.playPause, "onClick", function(){
  362. this.set('label', this.checked ? "Pause" : "Play");
  363. connect.publish(p.id+"-state", [this.checked]);
  364. });
  365. }
  366. this._subscriptions = [
  367. connect.subscribe(p.id+"-state", this, "_state"),
  368. connect.subscribe(p.id+"-update", this, "_update")
  369. ];
  370. }
  371. },
  372. destroy: function(){
  373. // summary: Unsubscribe to all of our topics
  374. array.forEach(this._subscriptions, connect.unsubscribe);
  375. this.inherited(arguments);
  376. },
  377. _state: function(/*boolean*/playing){
  378. // summary: Updates the display of the play/pause button
  379. if(this.playPause && this.playPause.checked != playing){
  380. this.playPause.set("label", playing ? "Pause" : "Play");
  381. this.playPause.set("checked", playing);
  382. }
  383. },
  384. _update: function(/*boolean*/playing, /*int*/current, /*int*/total){
  385. // summary: Updates the pager's play/pause button, current child, and total number of children.
  386. this._state(playing);
  387. if(this.current && current){
  388. this.current.innerHTML = current;
  389. }
  390. if(this.total && total){
  391. this.total.innerHTML = total;
  392. }
  393. }
  394. });
  395. return RotatorContainer;
  396. });