RadioGroup.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. if (!dojo._hasResource["dojox.layout.RadioGroup"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dojox.layout.RadioGroup"] = true;
  7. dojo.provide("dojox.layout.RadioGroup");
  8. dojo.experimental("dojox.layout.RadioGroup");
  9. //
  10. // dojox.layout.RadioGroup - an experimental (probably poorly named) Layout
  11. // widget extending StackContainer
  12. // that accepts ContentPanes as children, and applies aesthetically pleasing
  13. // responsive transition animations
  14. // attached to :hover of the Buttons created.
  15. //
  16. // FIXME: take the Buttons out of the root template, and allow layoutAlign
  17. // or similar attrib to use a different
  18. // template, or build the template dynamically?
  19. //
  20. dojo.require("dijit._Widget");
  21. dojo.require("dijit._Templated");
  22. dojo.require("dijit._Container");
  23. dojo.require("dijit.layout.StackContainer");
  24. dojo.require("dojox.fx.easing");
  25. dojo.declare("dojox.layout.RadioGroup", [dijit.layout.StackContainer,
  26. dijit._Templated], {
  27. // summary: A Container that turns its Layout Children into a
  28. // single Pane and transitions between states
  29. // onHover of the button
  30. //
  31. // duration: Int
  32. // used for Fade and Slide RadioGroup's, the duration to run the
  33. // transition animation. does not affect anything
  34. // in default RadioGroup
  35. duration : 750,
  36. // hasButtons: Boolean
  37. // toggles internal button making on or off
  38. hasButtons : true,
  39. // templateString: String
  40. // the template for our container
  41. templateString : '<div class="dojoxRadioGroup">'
  42. + ' <div dojoAttachPoint="buttonHolder" style="display:none;">'
  43. + ' <table class="dojoxRadioButtons"><tbody><tr class="dojoxRadioButtonRow" dojoAttachPoint="buttonNode"></tr></tbody></table>'
  44. + ' </div>'
  45. + ' <div class="dojoxRadioView" dojoAttachPoint="containerNode"></div>'
  46. + '</div>',
  47. startup : function() {
  48. // summary: scan the container for children, and make "tab
  49. // buttons" for them
  50. this.inherited("startup", arguments);
  51. this._children = this.getChildren();
  52. this._buttons = this._children.length;
  53. this._size = dojo.coords(this.containerNode);
  54. if (this.hasButtons) {
  55. dojo.style(this.buttonHolder, "display", "block");
  56. dojo.forEach(this._children, this._makeButton, this);
  57. }
  58. },
  59. // private:
  60. _makeButton : function(/* DomNode */n) {
  61. // summary: creates a hover button for a child node of the
  62. // RadioGroup
  63. dojo.style(n.domNode, "position", "absolute");
  64. var tmp = document.createElement('td');
  65. this.buttonNode.appendChild(tmp);
  66. var tmpt = tmp.appendChild(document.createElement('div'));
  67. var tmpw = new dojox.layout._RadioButton({
  68. label : n.title,
  69. page : n
  70. }, tmpt);
  71. tmpw.startup();
  72. },
  73. // FIXME: shouldn't have to rewriting these, need to take
  74. // styling out of _showChild and _hideChild
  75. // and use classes on the domNode in _transition or something
  76. // similar (in StackContainer)
  77. _transition : function(/* Widget */newWidget, /* Widget */
  78. oldWidget) {
  79. // summary: called when StackContainer receives a
  80. // selectChild call, used to transition the panes.
  81. this._showChild(newWidget);
  82. if (oldWidget) {
  83. this._hideChild(oldWidget);
  84. }
  85. // Size the new widget, in case this is the first time it's
  86. // being shown,
  87. // or I have been resized since the last time it was shown.
  88. // page must be visible for resizing to work
  89. if (this.doLayout && newWidget.resize) {
  90. newWidget.resize(this._containerContentBox
  91. || this._contentBox);
  92. }
  93. },
  94. _showChild : function(/* Widget */page) {
  95. // summary: show the selected child widget
  96. var children = this.getChildren();
  97. page.isFirstChild = (page == children[0]);
  98. page.isLastChild = (page == children[children.length - 1]);
  99. page.selected = true;
  100. page.domNode.style.display = "";
  101. if (page._loadCheck) {
  102. page._loadCheck(); // trigger load in ContentPane
  103. }
  104. if (page.onShow) {
  105. page.onShow();
  106. }
  107. },
  108. _hideChild : function(/* Widget */page) {
  109. // summary: hide the specified child widget
  110. page.selected = false;
  111. page.domNode.style.display = "none";
  112. if (page.onHide) {
  113. page.onHide();
  114. }
  115. }
  116. });
  117. dojo.declare("dojox.layout.RadioGroupFade", dojox.layout.RadioGroup, {
  118. // summary: An extension on a stock RadioGroup, that fades the
  119. // panes.
  120. _hideChild : function(page) {
  121. // summary: hide the specified child widget
  122. dojo.fadeOut({
  123. node : page.domNode,
  124. duration : this.duration,
  125. onEnd : this.inherited("_hideChild", arguments)
  126. }).play();
  127. },
  128. _showChild : function(page) {
  129. // summary: show the specified child widget
  130. this.inherited("_showChild", arguments);
  131. dojo.style(page.domNode, "opacity", 0);
  132. dojo.fadeIn({
  133. node : page.domNode,
  134. duration : this.duration
  135. }).play();
  136. }
  137. });
  138. dojo.declare("dojox.layout.RadioGroupSlide", dojox.layout.RadioGroup, {
  139. // summary: A Sliding Radio Group
  140. // description:
  141. // An extension on a stock RadioGroup widget, sliding the pane
  142. // into view from being hidden. The entry direction is
  143. // randomized
  144. // on each view
  145. //
  146. // easing: dojo._Animation.easing
  147. // A hook to override the default easing of the pane slides.
  148. easing : dojox.fx.easing.easeOut,
  149. startup : function() {
  150. // summary: on startup, set each of the panes off-screen
  151. // (_showChild is called later)
  152. this.inherited("startup", arguments);
  153. dojo.forEach(this._children, this._positionChild, this);
  154. },
  155. _positionChild : function(page) {
  156. // summary: randomly set the child out of view
  157. // description:
  158. var rA = Math.round(Math.random());
  159. var rB = Math.round(Math.random());
  160. dojo.style(page.domNode, rA ? "top" : "left", (rB
  161. ? "-"
  162. : "")
  163. + this._size[rA ? "h" : "w"] + "px");
  164. },
  165. _showChild : function(page) {
  166. // summary: Slide in the selected child widget
  167. this.inherited("_showChild", arguments);
  168. if (this._anim && this._anim.status() == "playing") {
  169. this._anim.gotoPercent(100, true);
  170. }
  171. this._anim = dojo.animateProperty({
  172. node : page.domNode,
  173. properties : {
  174. // take a performance hit determinging one
  175. // of these doesn't get modified
  176. // but it's better this way than an extra
  177. // call to mixin in think?
  178. left : {
  179. end : 0,
  180. unit : "px"
  181. },
  182. top : {
  183. end : 0,
  184. unit : "px"
  185. }
  186. },
  187. duration : this.duration,
  188. easing : this.easing
  189. });
  190. this._anim.play();
  191. },
  192. _hideChild : function(page) {
  193. // summary: reset the position of the hidden pane out of
  194. // sight
  195. this.inherited("_hideChild", arguments);
  196. this._positionChild(page);
  197. }
  198. });
  199. dojo.declare("dojox.layout._RadioButton", [dijit._Widget, dijit._Templated,
  200. dijit._Contained], {
  201. // summary: The Buttons for a RadioGroup
  202. //
  203. // description: A private widget used to manipulate the
  204. // StackContainer (RadioGroup*). Don't create directly.
  205. //
  206. // label: String
  207. // the Text Label of the button
  208. label : "",
  209. // domNode to tell parent to select
  210. page : null,
  211. templateString : '<div dojoAttachPoint="focusNode" class="dojoxRadioButton"><span dojoAttachPoint="titleNode" class="dojoxRadioButtonLabel">${label}</span></div>',
  212. startup : function() {
  213. // summary: start listening to mouseOver
  214. this.connect(this.domNode, "onmouseover", "_onMouse");
  215. },
  216. _onMouse : function(/* Event */e) {
  217. // summary: set the selected child on hover, and set our
  218. // hover state class
  219. this.getParent().selectChild(this.page);
  220. this._clearSelected();
  221. dojo.addClass(this.domNode, "dojoxRadioButtonSelected");
  222. },
  223. _clearSelected : function() {
  224. // summary: remove hover state class from sibling Buttons.
  225. // This is easier (and more reliable)
  226. // than setting up an additional connection to onMouseOut
  227. // FIXME: this relies on the template being
  228. // [div][span]node[/span][/div]
  229. dojo.query(".dojoxRadioButtonSelected",
  230. this.domNode.parentNode.parentNode).forEach(
  231. function(n) {
  232. dojo.removeClass(n, "dojoxRadioButtonSelected");
  233. });
  234. }
  235. });
  236. }