StackController.js.uncompressed.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. define("dijit/layout/StackController", [
  2. "dojo/_base/array", // array.forEach array.indexOf array.map
  3. "dojo/_base/declare", // declare
  4. "dojo/dom-class",
  5. "dojo/_base/event", // event.stop
  6. "dojo/keys", // keys
  7. "dojo/_base/lang", // lang.getObject
  8. "dojo/on",
  9. "../focus", // focus.focus()
  10. "../registry", // registry.byId
  11. "../_Widget",
  12. "../_TemplatedMixin",
  13. "../_Container",
  14. "../form/ToggleButton",
  15. "dojo/i18n!../nls/common"
  16. ], function(array, declare, domClass, event, keys, lang, on,
  17. focus, registry, _Widget, _TemplatedMixin, _Container, ToggleButton){
  18. // module:
  19. // dijit/layout/StackController
  20. var StackButton = declare("dijit.layout._StackButton", ToggleButton, {
  21. // summary:
  22. // Internal widget used by StackContainer.
  23. // description:
  24. // The button-like or tab-like object you click to select or delete a page
  25. // tags:
  26. // private
  27. // Override _FormWidget.tabIndex.
  28. // StackContainer buttons are not in the tab order by default.
  29. // Probably we should be calling this.startupKeyNavChildren() instead.
  30. tabIndex: "-1",
  31. // closeButton: Boolean
  32. // When true, display close button for this tab
  33. closeButton: false,
  34. _aria_attr: "aria-selected",
  35. buildRendering: function(/*Event*/ evt){
  36. this.inherited(arguments);
  37. (this.focusNode || this.domNode).setAttribute("role", "tab");
  38. }
  39. });
  40. var StackController = declare("dijit.layout.StackController", [_Widget, _TemplatedMixin, _Container], {
  41. // summary:
  42. // Set of buttons to select a page in a `dijit/layout/StackContainer`
  43. // description:
  44. // Monitors the specified StackContainer, and whenever a page is
  45. // added, deleted, or selected, updates itself accordingly.
  46. baseClass: "dijitStackController",
  47. templateString: "<span role='tablist' data-dojo-attach-event='onkeypress'></span>",
  48. // containerId: [const] String
  49. // The id of the page container that I point to
  50. containerId: "",
  51. // buttonWidget: [const] Constructor
  52. // The button widget to create to correspond to each page
  53. buttonWidget: StackButton,
  54. // buttonWidgetCloseClass: String
  55. // CSS class of [x] close icon, used by event delegation code to tell when close button was clicked
  56. buttonWidgetCloseClass: "dijitStackCloseButton",
  57. constructor: function(params /*===== , srcNodeRef =====*/){
  58. // summary:
  59. // Create the widget.
  60. // params: Object|null
  61. // Hash of initialization parameters for widget, including scalar values (like title, duration etc.)
  62. // and functions, typically callbacks like onClick.
  63. // The hash can contain any of the widget's properties, excluding read-only properties.
  64. // srcNodeRef: DOMNode|String?
  65. // If a srcNodeRef (DOM node) is specified, replace srcNodeRef with my generated DOM tree
  66. this.pane2button = {}; // mapping from pane id to buttons
  67. },
  68. postCreate: function(){
  69. this.inherited(arguments);
  70. // Listen to notifications from StackContainer.
  71. // TODO: do this through bubbled events instead of topics
  72. this.subscribe(this.containerId+"-startup", "onStartup");
  73. this.subscribe(this.containerId+"-addChild", "onAddChild");
  74. this.subscribe(this.containerId+"-removeChild", "onRemoveChild");
  75. this.subscribe(this.containerId+"-selectChild", "onSelectChild");
  76. this.subscribe(this.containerId+"-containerKeyPress", "onContainerKeyPress");
  77. // Listen for click events to select or close tabs.
  78. // No need to worry about ENTER/SPACE key handling: tabs are selected via left/right arrow keys,
  79. // and closed via shift-F10 (to show the close menu).
  80. this.connect(this.containerNode, 'click', function(evt){
  81. var button = registry.getEnclosingWidget(evt.target);
  82. if(button != this.containerNode && !button.disabled && button.page){
  83. for(var target = evt.target; target !== this.containerNode; target = target.parentNode){
  84. if(domClass.contains(target, this.buttonWidgetCloseClass)){
  85. this.onCloseButtonClick(button.page);
  86. break;
  87. }else if(target == button.domNode){
  88. this.onButtonClick(button.page);
  89. break;
  90. }
  91. }
  92. }
  93. });
  94. },
  95. onStartup: function(/*Object*/ info){
  96. // summary:
  97. // Called after StackContainer has finished initializing
  98. // tags:
  99. // private
  100. array.forEach(info.children, this.onAddChild, this);
  101. if(info.selected){
  102. // Show button corresponding to selected pane (unless selected
  103. // is null because there are no panes)
  104. this.onSelectChild(info.selected);
  105. }
  106. // Reflect events like page title changes to tab buttons
  107. var containerNode = registry.byId(this.containerId).containerNode,
  108. pane2button = this.pane2button,
  109. paneToButtonAttr = {
  110. "title": "label",
  111. "showtitle": "showLabel",
  112. "iconclass": "iconClass",
  113. "closable": "closeButton",
  114. "tooltip": "title",
  115. "disabled": "disabled"
  116. },
  117. connectFunc = function(attr, buttonAttr){
  118. return on(containerNode, "attrmodified-" + attr, function(evt){
  119. var button = pane2button[evt.detail && evt.detail.widget && evt.detail.widget.id];
  120. if(button){
  121. button.set(buttonAttr, evt.detail.newValue);
  122. }
  123. });
  124. };
  125. for(var attr in paneToButtonAttr){
  126. this.own(connectFunc(attr, paneToButtonAttr[attr]));
  127. }
  128. },
  129. destroy: function(){
  130. // Since the buttons are internal to the StackController widget, destroy() should remove them, which is
  131. // done by calling onRemoveChild().
  132. for(var pane in this.pane2button){
  133. this.onRemoveChild(registry.byId(pane));
  134. }
  135. // TODO: destroyRecursive() will call destroy() on each child button twice. Once from the above code,
  136. // and once because _WidgetBase.destroyDescendants() deletes anything inside of this.containerNode.
  137. // Probably shouldn't attach that DOMNode as this.containerNode.
  138. this.inherited(arguments);
  139. },
  140. onAddChild: function(/*dijit/_WidgetBase*/ page, /*Integer?*/ insertIndex){
  141. // summary:
  142. // Called whenever a page is added to the container.
  143. // Create button corresponding to the page.
  144. // tags:
  145. // private
  146. // create an instance of the button widget
  147. // (remove typeof buttonWidget == string support in 2.0)
  148. var Cls = lang.isString(this.buttonWidget) ? lang.getObject(this.buttonWidget) : this.buttonWidget;
  149. var button = new Cls({
  150. id: this.id + "_" + page.id,
  151. name: this.id + "_" + page.id,
  152. label: page.title,
  153. disabled: page.disabled,
  154. ownerDocument: this.ownerDocument,
  155. dir: page.dir,
  156. lang: page.lang,
  157. textDir: page.textDir,
  158. showLabel: page.showTitle,
  159. iconClass: page.iconClass,
  160. closeButton: page.closable,
  161. title: page.tooltip,
  162. page: page
  163. });
  164. this.addChild(button, insertIndex);
  165. this.pane2button[page.id] = button;
  166. page.controlButton = button; // this value might be overwritten if two tabs point to same container
  167. if(!this._currentChild){
  168. // If this is the first child then StackContainer will soon publish that it's selected,
  169. // but before that StackContainer calls layout(), and before layout() is called the
  170. // StackController needs to have the proper height... which means that the button needs
  171. // to be marked as selected now. See test_TabContainer_CSS.html for test.
  172. this.onSelectChild(page);
  173. }
  174. },
  175. onRemoveChild: function(/*dijit/_WidgetBase*/ page){
  176. // summary:
  177. // Called whenever a page is removed from the container.
  178. // Remove the button corresponding to the page.
  179. // tags:
  180. // private
  181. if(this._currentChild === page){ this._currentChild = null; }
  182. var button = this.pane2button[page.id];
  183. if(button){
  184. this.removeChild(button);
  185. delete this.pane2button[page.id];
  186. button.destroy();
  187. }
  188. delete page.controlButton;
  189. },
  190. onSelectChild: function(/*dijit/_WidgetBase*/ page){
  191. // summary:
  192. // Called when a page has been selected in the StackContainer, either by me or by another StackController
  193. // tags:
  194. // private
  195. if(!page){ return; }
  196. if(this._currentChild){
  197. var oldButton=this.pane2button[this._currentChild.id];
  198. oldButton.set('checked', false);
  199. oldButton.focusNode.setAttribute("tabIndex", "-1");
  200. }
  201. var newButton=this.pane2button[page.id];
  202. newButton.set('checked', true);
  203. this._currentChild = page;
  204. newButton.focusNode.setAttribute("tabIndex", "0");
  205. var container = registry.byId(this.containerId);
  206. container.containerNode.setAttribute("aria-labelledby", newButton.id);
  207. },
  208. onButtonClick: function(/*dijit/_WidgetBase*/ page){
  209. // summary:
  210. // Called whenever one of my child buttons is pressed in an attempt to select a page
  211. // tags:
  212. // private
  213. var button = this.pane2button[page.id];
  214. // For TabContainer where the tabs are <span>, need to set focus explicitly when left/right arrow
  215. focus.focus(button.focusNode);
  216. if(this._currentChild && this._currentChild.id === page.id) {
  217. //In case the user clicked the checked button, keep it in the checked state because it remains to be the selected stack page.
  218. button.set('checked', true);
  219. }
  220. var container = registry.byId(this.containerId);
  221. container.selectChild(page);
  222. },
  223. onCloseButtonClick: function(/*dijit/_WidgetBase*/ page){
  224. // summary:
  225. // Called whenever one of my child buttons [X] is pressed in an attempt to close a page
  226. // tags:
  227. // private
  228. var container = registry.byId(this.containerId);
  229. container.closeChild(page);
  230. if(this._currentChild){
  231. var b = this.pane2button[this._currentChild.id];
  232. if(b){
  233. focus.focus(b.focusNode || b.domNode);
  234. }
  235. }
  236. },
  237. // TODO: this is a bit redundant with forward, back api in StackContainer
  238. adjacent: function(/*Boolean*/ forward){
  239. // summary:
  240. // Helper for onkeypress to find next/previous button
  241. // tags:
  242. // private
  243. if(!this.isLeftToRight() && (!this.tabPosition || /top|bottom/.test(this.tabPosition))){ forward = !forward; }
  244. // find currently focused button in children array
  245. var children = this.getChildren();
  246. var idx = array.indexOf(children, this.pane2button[this._currentChild.id]),
  247. current = children[idx];
  248. // Pick next/previous non-disabled button to focus on. If we get back to the original button it means
  249. // that all buttons must be disabled, so return current child to avoid an infinite loop.
  250. var child;
  251. do{
  252. idx = (idx + (forward ? 1 : children.length - 1)) % children.length;
  253. child = children[idx];
  254. }while(child.disabled && child != current);
  255. return child; // dijit/_WidgetBase
  256. },
  257. onkeypress: function(/*Event*/ e){
  258. // summary:
  259. // Handle keystrokes on the page list, for advancing to next/previous button
  260. // and closing the current page if the page is closable.
  261. // tags:
  262. // private
  263. if(this.disabled || e.altKey ){ return; }
  264. var forward = null;
  265. if(e.ctrlKey || !e._djpage){
  266. switch(e.charOrCode){
  267. case keys.LEFT_ARROW:
  268. case keys.UP_ARROW:
  269. if(!e._djpage){ forward = false; }
  270. break;
  271. case keys.PAGE_UP:
  272. if(e.ctrlKey){ forward = false; }
  273. break;
  274. case keys.RIGHT_ARROW:
  275. case keys.DOWN_ARROW:
  276. if(!e._djpage){ forward = true; }
  277. break;
  278. case keys.PAGE_DOWN:
  279. if(e.ctrlKey){ forward = true; }
  280. break;
  281. case keys.HOME:
  282. // Navigate to first non-disabled child
  283. var children = this.getChildren();
  284. for(var idx = 0; idx < children.length; idx++){
  285. var child = children[idx];
  286. if(!child.disabled){
  287. this.onButtonClick(child.page);
  288. break;
  289. }
  290. }
  291. event.stop(e);
  292. break;
  293. case keys.END:
  294. // Navigate to last non-disabled child
  295. var children = this.getChildren();
  296. for(var idx = children.length-1; idx >= 0; idx--){
  297. var child = children[idx];
  298. if(!child.disabled){
  299. this.onButtonClick(child.page);
  300. break;
  301. }
  302. }
  303. event.stop(e);
  304. break;
  305. case keys.DELETE:
  306. if(this._currentChild.closable){
  307. this.onCloseButtonClick(this._currentChild);
  308. }
  309. event.stop(e);
  310. break;
  311. default:
  312. if(e.ctrlKey){
  313. if(e.charOrCode === keys.TAB){
  314. this.onButtonClick(this.adjacent(!e.shiftKey).page);
  315. event.stop(e);
  316. }else if(e.charOrCode == "w"){
  317. if(this._currentChild.closable){
  318. this.onCloseButtonClick(this._currentChild);
  319. }
  320. event.stop(e); // avoid browser tab closing.
  321. }
  322. }
  323. }
  324. // handle next/previous page navigation (left/right arrow, etc.)
  325. if(forward !== null){
  326. this.onButtonClick(this.adjacent(forward).page);
  327. event.stop(e);
  328. }
  329. }
  330. },
  331. onContainerKeyPress: function(/*Object*/ info){
  332. // summary:
  333. // Called when there was a keypress on the container
  334. // tags:
  335. // private
  336. info.e._djpage = info.page;
  337. this.onkeypress(info.e);
  338. }
  339. });
  340. StackController.StackButton = StackButton; // for monkey patching
  341. return StackController;
  342. });