MDL-64506 templates: Move BS2 labels to BS4 badges
[moodle.git] / lib / yui / src / dock / js / tabheightmanager.js
1 /* global TABHEIGHTMANAGER, LOGNS */
3 /**
4  * Dock JS.
5  *
6  * This file contains the tab height manager.
7  * The tab height manager is responsible for ensure all tabs are visible all the time.
8  *
9  * @module moodle-core-dock
10  */
12 /**
13  * Tab height manager.
14  *
15  * @namespace M.core.dock
16  * @class TabHeightManager
17  * @constructor
18  * @extends Base
19  */
20 TABHEIGHTMANAGER = function() {
21     TABHEIGHTMANAGER.superclass.constructor.apply(this, arguments);
22 };
23 TABHEIGHTMANAGER.prototype = {
24     /**
25      * Initialises the dock sizer which then attaches itself to the required
26      * events in order to monitor the dock
27      * @method initializer
28      */
29     initializer: function() {
30         var dock = this.get('dock');
31         dock.on('dock:itemschanged', this.checkSizing, this);
32         Y.on('windowresize', this.checkSizing, this);
33     },
34     /**
35      * Check if the size dock items needs to be adjusted
36      * @method checkSizing
37      */
38     checkSizing: function() {
39         var dock = this.get('dock'),
40             node = dock.get('dockNode'),
41             items = dock.dockeditems,
42             containermargin = parseInt(node.one('.dockeditem_container').getStyle('marginTop').replace('/[^0-9]+$/', ''), 10),
43             dockheight = node.get('offsetHeight') - containermargin,
44             controlheight = node.one('.controls').get('offsetHeight'),
45             buffer = (dock.get('bufferPanel') * 3),
46             possibleheight = dockheight - controlheight - buffer - (items.length * 2),
47             totalheight = 0,
48             id, dockedtitle;
49         if (items.length > 0) {
50             for (id in items) {
51                 if (Y.Lang.isNumber(id) || Y.Lang.isString(id)) {
52                     dockedtitle = Y.one(items[id].get('title')).ancestor('.' + CSS.dockedtitle);
53                     if (dockedtitle) {
54                         if (this.get('enabled')) {
55                             dockedtitle.setStyle('height', 'auto');
56                         }
57                         totalheight += dockedtitle.get('offsetHeight') || 0;
58                     }
59                 }
60             }
61             if (totalheight > possibleheight) {
62                 this.enable(possibleheight);
63             }
64         }
65     },
66     /**
67      * Enables the dock sizer and resizes where required.
68      * @method enable
69      * @param {Number} possibleheight
70      */
71     enable: function(possibleheight) {
72         var dock = this.get('dock'),
73             items = dock.dockeditems,
74             count = dock.count,
75             runningcount = 0,
76             usedheight = 0,
77             id, itemtitle, itemheight, offsetheight;
78         Y.log('Enabling the dock tab sizer.', 'debug', LOGNS);
79         this.set('enabled', true);
80         for (id in items) {
81             if (Y.Lang.isNumber(id) || Y.Lang.isString(id)) {
82                 itemtitle = Y.one(items[id].get('title')).ancestor('.' + CSS.dockedtitle);
83                 if (!itemtitle) {
84                     continue;
85                 }
86                 itemheight = Math.floor((possibleheight - usedheight) / (count - runningcount));
87                 offsetheight = itemtitle.get('offsetHeight');
88                 itemtitle.setStyle('overflow', 'hidden');
89                 if (offsetheight > itemheight) {
90                     itemtitle.setStyle('height', itemheight + 'px');
91                     usedheight += itemheight;
92                 } else {
93                     usedheight += offsetheight;
94                 }
95                 runningcount++;
96             }
97         }
98     }
99 };
100 Y.extend(TABHEIGHTMANAGER, Y.Base, TABHEIGHTMANAGER.prototype, {
101     NAME: 'moodle-core-tabheightmanager',
102     ATTRS: {
103         /**
104          * The dock.
105          * @attribute dock
106          * @type DOCK
107          * @writeOnce
108          */
109         dock: {
110             writeOnce: 'initOnly'
111         },
112         /**
113          * True if the item_sizer is being used, false otherwise.
114          * @attribute enabled
115          * @type Bool
116          */
117         enabled: {
118             value: false
119         }
120     }
121 });