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