Debian Codegraph @

dijit/layout/ScrollingTabController.js

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