MDL-64506 templates: Move BS2 labels to BS4 badges
[moodle.git] / lib / yui / src / dock / js / tabheightmanager.js
CommitLineData
ad3f8cd1
DP
1/* global TABHEIGHTMANAGER, LOGNS */
2
84192d78
SH
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 */
11
12/**
13 * Tab height manager.
14 *
15 * @namespace M.core.dock
16 * @class TabHeightManager
17 * @constructor
1f777e5c 18 * @extends Base
84192d78 19 */
a69a7e89 20TABHEIGHTMANAGER = function() {
84192d78
SH
21 TABHEIGHTMANAGER.superclass.constructor.apply(this, arguments);
22};
23TABHEIGHTMANAGER.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 */
5bb4f444 29 initializer: function() {
84192d78
SH
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 */
5bb4f444 38 checkSizing: function() {
84192d78
SH
39 var dock = this.get('dock'),
40 node = dock.get('dockNode'),
41 items = dock.dockeditems,
abaae2a5
SH
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),
5bb4f444 46 possibleheight = dockheight - controlheight - buffer - (items.length * 2),
84192d78
SH
47 totalheight = 0,
48 id, dockedtitle;
49 if (items.length > 0) {
50 for (id in items) {
a69a7e89 51 if (Y.Lang.isNumber(id) || Y.Lang.isString(id)) {
5bb4f444 52 dockedtitle = Y.one(items[id].get('title')).ancestor('.' + CSS.dockedtitle);
a69a7e89
SH
53 if (dockedtitle) {
54 if (this.get('enabled')) {
55 dockedtitle.setStyle('height', 'auto');
56 }
57 totalheight += dockedtitle.get('offsetHeight') || 0;
84192d78 58 }
84192d78
SH
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 */
5bb4f444 71 enable: function(possibleheight) {
84192d78
SH
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;
82955480 78 Y.log('Enabling the dock tab sizer.', 'debug', LOGNS);
84192d78
SH
79 this.set('enabled', true);
80 for (id in items) {
a69a7e89 81 if (Y.Lang.isNumber(id) || Y.Lang.isString(id)) {
5bb4f444 82 itemtitle = Y.one(items[id].get('title')).ancestor('.' + CSS.dockedtitle);
a69a7e89
SH
83 if (!itemtitle) {
84 continue;
85 }
5bb4f444 86 itemheight = Math.floor((possibleheight - usedheight) / (count - runningcount));
a69a7e89
SH
87 offsetheight = itemtitle.get('offsetHeight');
88 itemtitle.setStyle('overflow', 'hidden');
89 if (offsetheight > itemheight) {
5bb4f444 90 itemtitle.setStyle('height', itemheight + 'px');
a69a7e89
SH
91 usedheight += itemheight;
92 } else {
93 usedheight += offsetheight;
94 }
95 runningcount++;
84192d78 96 }
84192d78
SH
97 }
98 }
99};
100Y.extend(TABHEIGHTMANAGER, Y.Base, TABHEIGHTMANAGER.prototype, {
5bb4f444
DP
101 NAME: 'moodle-core-tabheightmanager',
102 ATTRS: {
84192d78
SH
103 /**
104 * The dock.
105 * @attribute dock
106 * @type DOCK
107 * @writeOnce
108 */
5bb4f444
DP
109 dock: {
110 writeOnce: 'initOnly'
84192d78
SH
111 },
112 /**
113 * True if the item_sizer is being used, false otherwise.
114 * @attribute enabled
115 * @type Bool
116 */
5bb4f444
DP
117 enabled: {
118 value: false
84192d78
SH
119 }
120 }
121});