ScrollingTabController.js.uncompressed.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. require({cache:{
  2. 'url:dijit/layout/templates/ScrollingTabController.html':"<div class=\"dijitTabListContainer-${tabPosition}\" style=\"visibility:hidden\">\n\t<div data-dojo-type=\"dijit.layout._ScrollingTabControllerMenuButton\"\n\t\t\tclass=\"tabStripButton-${tabPosition}\"\n\t\t\tid=\"${id}_menuBtn\"\n\t\t\tdata-dojo-props=\"containerId: '${containerId}', iconClass: 'dijitTabStripMenuIcon',\n\t\t\t\t\tdropDownPosition: ['below-alt', 'above-alt']\"\n\t\t\tdata-dojo-attach-point=\"_menuBtn\" showLabel=\"false\" title=\"\">&#9660;</div>\n\t<div data-dojo-type=\"dijit.layout._ScrollingTabControllerButton\"\n\t\t\tclass=\"tabStripButton-${tabPosition}\"\n\t\t\tid=\"${id}_leftBtn\"\n\t\t\tdata-dojo-props=\"iconClass:'dijitTabStripSlideLeftIcon', showLabel:false, title:''\"\n\t\t\tdata-dojo-attach-point=\"_leftBtn\" data-dojo-attach-event=\"onClick: doSlideLeft\">&#9664;</div>\n\t<div data-dojo-type=\"dijit.layout._ScrollingTabControllerButton\"\n\t\t\tclass=\"tabStripButton-${tabPosition}\"\n\t\t\tid=\"${id}_rightBtn\"\n\t\t\tdata-dojo-props=\"iconClass:'dijitTabStripSlideRightIcon', showLabel:false, title:''\"\n\t\t\tdata-dojo-attach-point=\"_rightBtn\" data-dojo-attach-event=\"onClick: doSlideRight\">&#9654;</div>\n\t<div class='dijitTabListWrapper' data-dojo-attach-point='tablistWrapper'>\n\t\t<div role='tablist' data-dojo-attach-event='onkeypress:onkeypress'\n\t\t\t\tdata-dojo-attach-point='containerNode' class='nowrapTabStrip'></div>\n\t</div>\n</div>",
  3. 'url:dijit/layout/templates/_ScrollingTabControllerButton.html':"<div data-dojo-attach-event=\"onclick:_onClick\" class=\"dijitTabInnerDiv dijitTabContent dijitButtonContents\" data-dojo-attach-point=\"focusNode\">\n\t<img role=\"presentation\" alt=\"\" src=\"${_blankGif}\" class=\"dijitTabStripIcon\" data-dojo-attach-point=\"iconNode\"/>\n\t<span data-dojo-attach-point=\"containerNode,titleNode\" class=\"dijitButtonText\"></span>\n</div>"}});
  4. define("dijit/layout/ScrollingTabController", [
  5. "dojo/_base/array", // array.forEach
  6. "dojo/_base/declare", // declare
  7. "dojo/dom-class", // domClass.add domClass.contains
  8. "dojo/dom-geometry", // domGeometry.contentBox
  9. "dojo/dom-style", // domStyle.style
  10. "dojo/_base/fx", // Animation
  11. "dojo/_base/lang", // lang.hitch
  12. "dojo/on",
  13. "dojo/query", // query
  14. "dojo/sniff", // has("ie"), has("webkit"), has("quirks")
  15. "../registry", // registry.byId()
  16. "dojo/text!./templates/ScrollingTabController.html",
  17. "dojo/text!./templates/_ScrollingTabControllerButton.html",
  18. "./TabController",
  19. "./utils", // marginBox2contextBox, layoutChildren
  20. "../_WidgetsInTemplateMixin",
  21. "../Menu",
  22. "../MenuItem",
  23. "../form/Button",
  24. "../_HasDropDown",
  25. "dojo/NodeList-dom" // NodeList.style
  26. ], function(array, declare, domClass, domGeometry, domStyle, fx, lang, on, query, has,
  27. registry, tabControllerTemplate, buttonTemplate, TabController, layoutUtils, _WidgetsInTemplateMixin,
  28. Menu, MenuItem, Button, _HasDropDown){
  29. // module:
  30. // dijit/layout/ScrollingTabController
  31. var ScrollingTabController = declare("dijit.layout.ScrollingTabController", [TabController, _WidgetsInTemplateMixin], {
  32. // summary:
  33. // Set of tabs with left/right arrow keys and a menu to switch between tabs not
  34. // all fitting on a single row.
  35. // Works only for horizontal tabs (either above or below the content, not to the left
  36. // or right).
  37. // tags:
  38. // private
  39. baseClass: "dijitTabController dijitScrollingTabController",
  40. templateString: tabControllerTemplate,
  41. // useMenu: [const] Boolean
  42. // True if a menu should be used to select tabs when they are too
  43. // wide to fit the TabContainer, false otherwise.
  44. useMenu: true,
  45. // useSlider: [const] Boolean
  46. // True if a slider should be used to select tabs when they are too
  47. // wide to fit the TabContainer, false otherwise.
  48. useSlider: true,
  49. // tabStripClass: [const] String
  50. // The css class to apply to the tab strip, if it is visible.
  51. tabStripClass: "",
  52. widgetsInTemplate: true,
  53. // _minScroll: Number
  54. // The distance in pixels from the edge of the tab strip which,
  55. // if a scroll animation is less than, forces the scroll to
  56. // go all the way to the left/right.
  57. _minScroll: 5,
  58. // Override default behavior mapping class to DOMNode
  59. _setClassAttr: { node: "containerNode", type: "class" },
  60. buildRendering: function(){
  61. this.inherited(arguments);
  62. var n = this.domNode;
  63. this.scrollNode = this.tablistWrapper;
  64. this._initButtons();
  65. if(!this.tabStripClass){
  66. this.tabStripClass = "dijitTabContainer" +
  67. this.tabPosition.charAt(0).toUpperCase() +
  68. this.tabPosition.substr(1).replace(/-.*/, "") +
  69. "None";
  70. domClass.add(n, "tabStrip-disabled")
  71. }
  72. domClass.add(this.tablistWrapper, this.tabStripClass);
  73. },
  74. onStartup: function(){
  75. this.inherited(arguments);
  76. // TabController is hidden until it finishes drawing, to give
  77. // a less visually jumpy instantiation. When it's finished, set visibility to ""
  78. // to that the tabs are hidden/shown depending on the container's visibility setting.
  79. domStyle.set(this.domNode, "visibility", "");
  80. this._postStartup = true;
  81. // changes to the tab button label or iconClass will have changed the width of the
  82. // buttons, so do a resize
  83. this.own(on(this.containerNode, "attrmodified-label, attrmodified-iconclass", lang.hitch(this, function(evt){
  84. if(this._dim){
  85. this.resize(this._dim);
  86. }
  87. })));
  88. },
  89. onAddChild: function(page, insertIndex){
  90. this.inherited(arguments);
  91. // Increment the width of the wrapper when a tab is added
  92. // This makes sure that the buttons never wrap.
  93. // The value 200 is chosen as it should be bigger than most
  94. // Tab button widths.
  95. domStyle.set(this.containerNode, "width",
  96. (domStyle.get(this.containerNode, "width") + 200) + "px");
  97. },
  98. onRemoveChild: function(page, insertIndex){
  99. // null out _selectedTab because we are about to delete that dom node
  100. var button = this.pane2button[page.id];
  101. if(this._selectedTab === button.domNode){
  102. this._selectedTab = null;
  103. }
  104. this.inherited(arguments);
  105. },
  106. _initButtons: function(){
  107. // summary:
  108. // Creates the buttons used to scroll to view tabs that
  109. // may not be visible if the TabContainer is too narrow.
  110. // Make a list of the buttons to display when the tab labels become
  111. // wider than the TabContainer, and hide the other buttons.
  112. // Also gets the total width of the displayed buttons.
  113. this._btnWidth = 0;
  114. this._buttons = query("> .tabStripButton", this.domNode).filter(function(btn){
  115. if((this.useMenu && btn == this._menuBtn.domNode) ||
  116. (this.useSlider && (btn == this._rightBtn.domNode || btn == this._leftBtn.domNode))){
  117. this._btnWidth += domGeometry.getMarginSize(btn).w;
  118. return true;
  119. }else{
  120. domStyle.set(btn, "display", "none");
  121. return false;
  122. }
  123. }, this);
  124. },
  125. _getTabsWidth: function(){
  126. var children = this.getChildren();
  127. if(children.length){
  128. var leftTab = children[this.isLeftToRight() ? 0 : children.length - 1].domNode,
  129. rightTab = children[this.isLeftToRight() ? children.length - 1 : 0].domNode;
  130. return rightTab.offsetLeft + rightTab.offsetWidth - leftTab.offsetLeft;
  131. }else{
  132. return 0;
  133. }
  134. },
  135. _enableBtn: function(width){
  136. // summary:
  137. // Determines if the tabs are wider than the width of the TabContainer, and
  138. // thus that we need to display left/right/menu navigation buttons.
  139. var tabsWidth = this._getTabsWidth();
  140. width = width || domStyle.get(this.scrollNode, "width");
  141. return tabsWidth > 0 && width < tabsWidth;
  142. },
  143. resize: function(dim){
  144. // summary:
  145. // Hides or displays the buttons used to scroll the tab list and launch the menu
  146. // that selects tabs.
  147. // Save the dimensions to be used when a child is renamed.
  148. this._dim = dim;
  149. // Set my height to be my natural height (tall enough for one row of tab labels),
  150. // and my content-box width based on margin-box width specified in dim parameter.
  151. // But first reset scrollNode.height in case it was set by layoutChildren() call
  152. // in a previous run of this method.
  153. this.scrollNode.style.height = "auto";
  154. var cb = this._contentBox = layoutUtils.marginBox2contentBox(this.domNode, {h: 0, w: dim.w});
  155. cb.h = this.scrollNode.offsetHeight;
  156. domGeometry.setContentSize(this.domNode, cb);
  157. // Show/hide the left/right/menu navigation buttons depending on whether or not they
  158. // are needed.
  159. var enable = this._enableBtn(this._contentBox.w);
  160. this._buttons.style("display", enable ? "" : "none");
  161. // Position and size the navigation buttons and the tablist
  162. this._leftBtn.layoutAlign = "left";
  163. this._rightBtn.layoutAlign = "right";
  164. this._menuBtn.layoutAlign = this.isLeftToRight() ? "right" : "left";
  165. layoutUtils.layoutChildren(this.domNode, this._contentBox,
  166. [this._menuBtn, this._leftBtn, this._rightBtn, {domNode: this.scrollNode, layoutAlign: "client"}]);
  167. // set proper scroll so that selected tab is visible
  168. if(this._selectedTab){
  169. if(this._anim && this._anim.status() == "playing"){
  170. this._anim.stop();
  171. }
  172. this.scrollNode.scrollLeft = this._convertToScrollLeft(this._getScrollForSelectedTab());
  173. }
  174. // Enable/disabled left right buttons depending on whether or not user can scroll to left or right
  175. this._setButtonClass(this._getScroll());
  176. this._postResize = true;
  177. // Return my size so layoutChildren() can use it.
  178. // Also avoids IE9 layout glitch on browser resize when scroll buttons present
  179. return {h: this._contentBox.h, w: dim.w};
  180. },
  181. _getScroll: function(){
  182. // summary:
  183. // Returns the current scroll of the tabs where 0 means
  184. // "scrolled all the way to the left" and some positive number, based on #
  185. // of pixels of possible scroll (ex: 1000) means "scrolled all the way to the right"
  186. return (this.isLeftToRight() || has("ie") < 8 || (has("ie") && has("quirks")) || has("webkit")) ? this.scrollNode.scrollLeft :
  187. domStyle.get(this.containerNode, "width") - domStyle.get(this.scrollNode, "width")
  188. + (has("ie") >= 8 ? -1 : 1) * this.scrollNode.scrollLeft;
  189. },
  190. _convertToScrollLeft: function(val){
  191. // summary:
  192. // Given a scroll value where 0 means "scrolled all the way to the left"
  193. // and some positive number, based on # of pixels of possible scroll (ex: 1000)
  194. // means "scrolled all the way to the right", return value to set this.scrollNode.scrollLeft
  195. // to achieve that scroll.
  196. //
  197. // This method is to adjust for RTL funniness in various browsers and versions.
  198. if(this.isLeftToRight() || has("ie") < 8 || (has("ie") && has("quirks")) || has("webkit")){
  199. return val;
  200. }else{
  201. var maxScroll = domStyle.get(this.containerNode, "width") - domStyle.get(this.scrollNode, "width");
  202. return (has("ie") >= 8 ? -1 : 1) * (val - maxScroll);
  203. }
  204. },
  205. onSelectChild: function(/*dijit/_WidgetBase*/ page){
  206. // summary:
  207. // Smoothly scrolls to a tab when it is selected.
  208. var tab = this.pane2button[page.id];
  209. if(!tab || !page){return;}
  210. var node = tab.domNode;
  211. // Save the selection
  212. if(node != this._selectedTab){
  213. this._selectedTab = node;
  214. // Scroll to the selected tab, except on startup, when scrolling is handled in resize()
  215. if(this._postResize){
  216. var sl = this._getScroll();
  217. if(sl > node.offsetLeft ||
  218. sl + domStyle.get(this.scrollNode, "width") <
  219. node.offsetLeft + domStyle.get(node, "width")){
  220. this.createSmoothScroll().play();
  221. }
  222. }
  223. }
  224. this.inherited(arguments);
  225. },
  226. _getScrollBounds: function(){
  227. // summary:
  228. // Returns the minimum and maximum scroll setting to show the leftmost and rightmost
  229. // tabs (respectively)
  230. var children = this.getChildren(),
  231. scrollNodeWidth = domStyle.get(this.scrollNode, "width"), // about 500px
  232. containerWidth = domStyle.get(this.containerNode, "width"), // 50,000px
  233. maxPossibleScroll = containerWidth - scrollNodeWidth, // scrolling until right edge of containerNode visible
  234. tabsWidth = this._getTabsWidth();
  235. if(children.length && tabsWidth > scrollNodeWidth){
  236. // Scrolling should happen
  237. return {
  238. min: this.isLeftToRight() ? 0 : children[children.length-1].domNode.offsetLeft,
  239. max: this.isLeftToRight() ?
  240. (children[children.length-1].domNode.offsetLeft + children[children.length-1].domNode.offsetWidth) - scrollNodeWidth :
  241. maxPossibleScroll
  242. };
  243. }else{
  244. // No scrolling needed, all tabs visible, we stay either scrolled to far left or far right (depending on dir)
  245. var onlyScrollPosition = this.isLeftToRight() ? 0 : maxPossibleScroll;
  246. return {
  247. min: onlyScrollPosition,
  248. max: onlyScrollPosition
  249. };
  250. }
  251. },
  252. _getScrollForSelectedTab: function(){
  253. // summary:
  254. // Returns the scroll value setting so that the selected tab
  255. // will appear in the center
  256. var w = this.scrollNode,
  257. n = this._selectedTab,
  258. scrollNodeWidth = domStyle.get(this.scrollNode, "width"),
  259. scrollBounds = this._getScrollBounds();
  260. // TODO: scroll minimal amount (to either right or left) so that
  261. // selected tab is fully visible, and just return if it's already visible?
  262. var pos = (n.offsetLeft + domStyle.get(n, "width")/2) - scrollNodeWidth/2;
  263. pos = Math.min(Math.max(pos, scrollBounds.min), scrollBounds.max);
  264. // TODO:
  265. // If scrolling close to the left side or right side, scroll
  266. // all the way to the left or right. See this._minScroll.
  267. // (But need to make sure that doesn't scroll the tab out of view...)
  268. return pos;
  269. },
  270. createSmoothScroll: function(x){
  271. // summary:
  272. // Creates a dojo._Animation object that smoothly scrolls the tab list
  273. // either to a fixed horizontal pixel value, or to the selected tab.
  274. // description:
  275. // If an number argument is passed to the function, that horizontal
  276. // pixel position is scrolled to. Otherwise the currently selected
  277. // tab is scrolled to.
  278. // x: Integer?
  279. // An optional pixel value to scroll to, indicating distance from left.
  280. // Calculate position to scroll to
  281. if(arguments.length > 0){
  282. // position specified by caller, just make sure it's within bounds
  283. var scrollBounds = this._getScrollBounds();
  284. x = Math.min(Math.max(x, scrollBounds.min), scrollBounds.max);
  285. }else{
  286. // scroll to center the current tab
  287. x = this._getScrollForSelectedTab();
  288. }
  289. if(this._anim && this._anim.status() == "playing"){
  290. this._anim.stop();
  291. }
  292. var self = this,
  293. w = this.scrollNode,
  294. anim = new fx.Animation({
  295. beforeBegin: function(){
  296. if(this.curve){ delete this.curve; }
  297. var oldS = w.scrollLeft,
  298. newS = self._convertToScrollLeft(x);
  299. anim.curve = new fx._Line(oldS, newS);
  300. },
  301. onAnimate: function(val){
  302. w.scrollLeft = val;
  303. }
  304. });
  305. this._anim = anim;
  306. // Disable/enable left/right buttons according to new scroll position
  307. this._setButtonClass(x);
  308. return anim; // dojo/_base/fx/Animation
  309. },
  310. _getBtnNode: function(/*Event*/ e){
  311. // summary:
  312. // Gets a button DOM node from a mouse click event.
  313. // e:
  314. // The mouse click event.
  315. var n = e.target;
  316. while(n && !domClass.contains(n, "tabStripButton")){
  317. n = n.parentNode;
  318. }
  319. return n;
  320. },
  321. doSlideRight: function(/*Event*/ e){
  322. // summary:
  323. // Scrolls the menu to the right.
  324. // e:
  325. // The mouse click event.
  326. this.doSlide(1, this._getBtnNode(e));
  327. },
  328. doSlideLeft: function(/*Event*/ e){
  329. // summary:
  330. // Scrolls the menu to the left.
  331. // e:
  332. // The mouse click event.
  333. this.doSlide(-1,this._getBtnNode(e));
  334. },
  335. doSlide: function(/*Number*/ direction, /*DomNode*/ node){
  336. // summary:
  337. // Scrolls the tab list to the left or right by 75% of the widget width.
  338. // direction:
  339. // If the direction is 1, the widget scrolls to the right, if it is -1,
  340. // it scrolls to the left.
  341. if(node && domClass.contains(node, "dijitTabDisabled")){return;}
  342. var sWidth = domStyle.get(this.scrollNode, "width");
  343. var d = (sWidth * 0.75) * direction;
  344. var to = this._getScroll() + d;
  345. this._setButtonClass(to);
  346. this.createSmoothScroll(to).play();
  347. },
  348. _setButtonClass: function(/*Number*/ scroll){
  349. // summary:
  350. // Disables the left scroll button if the tabs are scrolled all the way to the left,
  351. // or the right scroll button in the opposite case.
  352. // scroll: Integer
  353. // amount of horizontal scroll
  354. var scrollBounds = this._getScrollBounds();
  355. this._leftBtn.set("disabled", scroll <= scrollBounds.min);
  356. this._rightBtn.set("disabled", scroll >= scrollBounds.max);
  357. }
  358. });
  359. var ScrollingTabControllerButtonMixin = declare("dijit.layout._ScrollingTabControllerButtonMixin", null, {
  360. baseClass: "dijitTab tabStripButton",
  361. templateString: buttonTemplate,
  362. // Override inherited tabIndex: 0 from dijit/form/Button, because user shouldn't be
  363. // able to tab to the left/right/menu buttons
  364. tabIndex: "",
  365. // Similarly, override FormWidget.isFocusable() because clicking a button shouldn't focus it
  366. // either (this override avoids focus() call in FormWidget.js)
  367. isFocusable: function(){ return false; }
  368. });
  369. // Class used in template
  370. declare("dijit.layout._ScrollingTabControllerButton",
  371. [Button, ScrollingTabControllerButtonMixin]);
  372. // Class used in template
  373. declare(
  374. "dijit.layout._ScrollingTabControllerMenuButton",
  375. [Button, _HasDropDown, ScrollingTabControllerButtonMixin],
  376. {
  377. // id of the TabContainer itself
  378. containerId: "",
  379. // -1 so user can't tab into the button, but so that button can still be focused programatically.
  380. // Because need to move focus to the button (or somewhere) before the menu is hidden or IE6 will crash.
  381. tabIndex: "-1",
  382. isLoaded: function(){
  383. // recreate menu every time, in case the TabContainer's list of children (or their icons/labels) have changed
  384. return false;
  385. },
  386. loadDropDown: function(callback){
  387. this.dropDown = new Menu({
  388. id: this.containerId + "_menu",
  389. ownerDocument: this.ownerDocument,
  390. dir: this.dir,
  391. lang: this.lang,
  392. textDir: this.textDir
  393. });
  394. var container = registry.byId(this.containerId);
  395. array.forEach(container.getChildren(), function(page){
  396. var menuItem = new MenuItem({
  397. id: page.id + "_stcMi",
  398. label: page.title,
  399. iconClass: page.iconClass,
  400. disabled: page.disabled,
  401. ownerDocument: this.ownerDocument,
  402. dir: page.dir,
  403. lang: page.lang,
  404. textDir: page.textDir,
  405. onClick: function(){
  406. container.selectChild(page);
  407. }
  408. });
  409. this.dropDown.addChild(menuItem);
  410. }, this);
  411. callback();
  412. },
  413. closeDropDown: function(/*Boolean*/ focus){
  414. this.inherited(arguments);
  415. if(this.dropDown){
  416. this.dropDown.destroyRecursive();
  417. delete this.dropDown;
  418. }
  419. }
  420. });
  421. return ScrollingTabController;
  422. });