This repository has been archived by the owner on Nov 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
plusTabs.js
153 lines (148 loc) · 6.34 KB
/
plusTabs.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
150
151
152
153
/*
* plusTabs version 1.0
*
* Author: Jason Day @iamjasonday
*
* (c) 2012, Jason Day
*
* Dual licensed under the MIT and GPL licenses
*
* Dependencies: jQuery v1.6+, jQuery UI 1.8+
*
* Description:
* plusTabs extends jQuery UI tabs to include a dropdown for when tabs break to the next line
*
* Usage:
*
* Simple:
* $("#selector").plusTabs();
*
* Options:
* $("#selector".plusTabs({
* className: "plusTabs", //classname for css scoping
* seeMore: true, //initiate "see more" behavior
* seeMoreText: "More", //set see more text
* showCount: false, //show drop down count
* expandIcon: "▼ ", //icon/caret - if using image, specify image width
* dropWidth: "66%", //width of dropdown
* sizeTweak: 0 //adjust size of active tab to tweak "see more" layout
* });
*
*/
(function($) {
var methods = {
init: function(options) {
if (options === undefined) options = {};
//Merge defaults and options
options = $.extend({}, $.fn.plusTabs.defaults, options);
return this.each(function() {
var o = options,
$plusTabs = $(this);
// add class plusTabs for tabs styling
(o.className != '') && $plusTabs.addClass(o.className);
//initiate jQuery UI Tabs
$plusTabs.tabs();
function showActiveTab() {
var allTabsLength = "";
if (o.showCount === true) {
allTabsLength = " (" + $plusTabs.tabs("length") + ")";
}
//hide all tabs, show selected tab
$uiTabsNav.find("li").hide();
$uiTabsNav.find("li.ui-state-active").show();
//add "see more" tab
if ($plusTabs.find("li.seeMore").length == 0) {
$uiTabsNav.append("<li class='ui-state-default ui-corner-top seeMore'><a href='javascript: void(0);'>" + o.expandIcon + o.seeMoreText + allTabsLength + "</a></li>");
} else {
$plusTabs.find("li.seeMore").show();
}
// set active tab width
var seeMoreWidth = $(".seeMore").outerWidth(),
moreActiveTab = ATBwidth - seeMoreWidth - o.sizeTweak;
$uiTabsNav.find("li.ui-state-active").css("width", moreActiveTab);
// position .allTabs
var uiTabsHeight = $uiTabsNav.outerHeight(),
$allTabs = $plusTabs.find(".allTabs");
$allTabs.css({
"top": uiTabsHeight,
"width": o.dropWidth
});
//$plusTabs.find(".allTabs").css("width", moreActiveTab);
// Highlight active tab in allTabs dropdown
$allTabs.find("a").removeClass("highlight");
var selectedText = $uiTabsNav.find("li.ui-state-active a").text(),
allTabsSelected = $allTabs.find('a:contains("' + selectedText + '")');
$plusTabs.find(allTabsSelected).addClass("highlight");
}
var $uiTabsNav = $plusTabs.find('.ui-tabs-nav');
// get total width of all tabs of current product
var ATBwidth = $plusTabs.outerWidth();
var tabsWidth = 0;
$uiTabsNav.find("li").each(function(index, tabs) {
tabsWidth += $(tabs).outerWidth();
});
// Check if product, initialize "see more" behavior
if (o.seeMore === true && tabsWidth >= ATBwidth) {
var allTabsNav = $('<div class="allTabs" />').appendTo($plusTabs);
//clone tabs/behavior append to allTabs
$uiTabsNav.find('a').clone().click(function(event) {
//stop hash to behavior
event.preventDefault();
// mimic tab select
$plusTabs.tabs('select', $(this).index());
// show active tab on selection
showActiveTab();
//hide "see more tabs"
$plusTabs.find('.allTabs').slideUp('fast');
}).appendTo(allTabsNav);
showActiveTab();
} /*end if o.seeMore === "true" */
// "see more" functionality
// show all tabs drop down with a timer
var timeout, $allTabs = $plusTabs.find(".allTabs");
$plusTabs.find(".seeMore a").click(function() {
clearTimeout(timeout);
$allTabs.slideDown();
timeout = setTimeout(function() {
$allTabs.slideUp();
clearTimeout(timeout);
}, 3000)
});
$plusTabs.find(".seeMore a").keydown(function(e) {
if (e.which === 13) {
$allTabs.find("a:first").focus();
}
});
$allTabs.mouseenter(function() {
clearTimeout(timeout);
});
$allTabs.mouseleave(function() {
clearTimeout(timeout);
var $this = $(this)
timeout = setTimeout(function() {
$this.slideUp()
}, 1000)
});
});
}
};
$.fn.plusTabs = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on $.plusTabs');
}
};
// Default settings
$.fn.plusTabs.defaults = {
className: "plusTabs",
seeMore: true,
seeMoreText: "More",
showCount: false,
expandIcon: "▼ ",
dropWidth: "66%",
sizeTweak: 0
};
})(jQuery);