-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoomanytabs.js
149 lines (120 loc) · 4.93 KB
/
toomanytabs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
(function ($) {
var reservedWidth = 0;
var tabsWidth = 0;
var $tabs;
$.fn.tooManyTabs = function (options) {
var plugin = this;
// default settings
var defaults = {
// moreTab is the element which will be shown when there is not enough space for tabs
moreTabSelector: ".more-tab",
// hidden tabs will be added to this drop down
dropdownSelector: ".more-tab .dropdown-menu",
// selector for tabs which should be readjusted
tabSelector: "ul.nav-tabs>li:not(.action-tab)",
// exclude elements from tabSelector
excludeSelector: ".action-tab:visible"
};
/**
* readjustTabs method does all the logic and actual hiding/showing
*/
plugin.readjustTabs = function () {
// define helper elements
plugin.$moreTab = $(plugin.config.moreTabSelector, this);
plugin.$dropdown = $(plugin.config.dropdownSelector, this);
// get $moreTab width and hide it, because we don't need it just yet
var moreTabWidth = plugin.$moreTab.width();
plugin.$moreTab.hide();
// restore tabs
plugin.$dropdown.find("li")
.insertBefore($(plugin.config.moreTabSelector));
// calculate total width of tabs
tabsWidth = 0;
// @fixme poor performance, fix this
$tabs.each(function () {
tabsWidth += parseInt($(this).width());
});
// calculate width of wrapper element
var outerWidth = $(this).width();
// calculate available space for tabs
var availableSpace = outerWidth - reservedWidth;
// check if there is enough space for tabs in available space
if (tabsWidth > availableSpace) {
// there is not enough space for all tabs,
// let's hide some and show them under $moreTab
// reset moreTab
plugin.$dropdown.html("");
plugin.$moreTab.show();
// reduce available space, because we need room for $moreTab
availableSpace -= moreTabWidth;
// hide tabs that do not fit
$tabs.each(function () {
if (tabsWidth < availableSpace) {
// stop when enough tabs are removed
return false;
} else {
// reduce actual tabs width
tabsWidth -= $(this).width();
// append item to dropbox
plugin.addTabToDropdown($(this));
// hide tab
// $(this).hide();
}
});
} else {
// hide more tab, because there is space for all tabs
plugin.$moreTab.hide();
// restore tabs
plugin.$dropdown.find("li")
.insertBefore($(plugin.config.moreTabSelector));
}
};
/**
* Add tab to dropdown menu (works for twitter bootstrap dropdown plugin)
* This method can be overriden to provide custom logic
* @param $el jQuery element to be be added to the Dropdown menu
*/
plugin.addTabToDropdown = function ($el) {
$el.appendTo(plugin.config.dropdownSelector);
};
/**
* Bind events (resize)
*/
plugin.bindEvents = function () {
var rt;
$(window).resize(function () {
// clear resize timeout
clearTimeout(rt);
// readjusting tabs on every resize event is very costly
// performance wise so we are adding 500ms timeout
rt = setTimeout(function () {
// readjust tabs only if there is no resize event for more
// than 500ms
plugin.readjustTabs();
}, 500);
});
};
/**
* Main init method
* @param options
*/
var init = function (options) {
plugin.config = $.extend({}, defaults, options);
// define $tabs and reverse them, because we want to start hiding from the right side
$tabs = $($(plugin.config.tabSelector).get().reverse());
// calculate reserved space for other elements than tabs
reservedWidth = 0;
$(plugin.config.excludeSelector).each(function () {
reservedWidth += parseInt($(this).width());
});
// calculate total width of tabs
tabsWidth = 0;
$tabs.each(function () {
tabsWidth += parseInt($(this).width());
});
plugin.bindEvents();
plugin.readjustTabs();
};
init(options);
};
})(jQuery);