-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathresponsive-tabs.js
More file actions
executable file
·191 lines (143 loc) · 5.48 KB
/
responsive-tabs.js
File metadata and controls
executable file
·191 lines (143 loc) · 5.48 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var fakewaffle = ( function ( $, fakewaffle ) {
'use strict';
fakewaffle.responsiveTabs = function ( collapseDisplayed ) {
fakewaffle.currentPosition = 'tabs';
var tabGroups = $( '.nav-tabs.responsive' );
var hidden = '';
var visible = '';
var activeTab = '';
if ( collapseDisplayed === undefined ) {
collapseDisplayed = [ 'xs', 'sm' ];
}
$.each( collapseDisplayed, function () {
hidden += ' hidden-' + this;
visible += ' visible-' + this;
} );
$.each( tabGroups, function () {
var $tabGroup = $( this );
var tabs = $tabGroup.find( 'li a' );
var collapseDiv = $( '<div></div>', {
'class' : 'panel-group responsive' + visible,
'id' : 'collapse-' + $tabGroup.attr( 'id' )
} );
$.each( tabs, function () {
var $this = $( this );
var oldLinkClass = $this.attr( 'class' ) === undefined ? '' : $this.attr( 'class' );
var newLinkClass = 'accordion-toggle';
var oldParentClass = $this.parent().attr( 'class' ) === undefined ? '' : $this.parent().attr( 'class' );
var newParentClass = 'panel panel-default responsive';
var newHash = $this.get( 0 ).hash.replace( '#', 'collapse-' );
if ( oldLinkClass.length > 0 ) {
newLinkClass += ' ' + oldLinkClass;
}
if ( oldParentClass.length > 0 ) {
oldParentClass = oldParentClass.replace( /\bactive\b/g, '' );
newParentClass += ' ' + oldParentClass;
newParentClass = newParentClass.replace( /\s{2,}/g, ' ' );
newParentClass = newParentClass.replace( /^\s+|\s+$/g, '' );
}
if ( $this.parent().hasClass( 'active' ) ) {
activeTab = '#' + newHash;
}
collapseDiv.append(
$( '<div>' ).attr( 'class', newParentClass ).html(
$( '<div>' ).attr( 'class', 'panel-heading responsive' ).html(
$( '<h4>' ).attr( 'class', 'panel-title responsive' ).html(
$( '<a>', {
'class' : newLinkClass,
'data-toggle' : 'collapse',
'data-parent' : '#collapse-' + $tabGroup.attr( 'id' ),
'href' : '#' + newHash,
'html' : $this.html()
} )
)
)
).append(
$( '<div>', {
'id' : newHash,
'class' : 'panel-collapse collapse responsive'
} )
)
);
} );
$tabGroup.parent().find('.tab-content.responsive').after( collapseDiv );
$tabGroup.addClass( hidden );
$( '.tab-content.responsive' ).addClass( hidden );
} );
fakewaffle.checkResize();
fakewaffle.bindTabToCollapse();
if ( activeTab ) {
$( activeTab ).collapse( 'show' );
}
};
fakewaffle.checkResize = function () {
if ( $( '.panel-group.responsive' ).is( ':visible' ) === true && fakewaffle.currentPosition === 'tabs' ) {
fakewaffle.tabToPanel();
fakewaffle.currentPosition = 'panel';
} else if ( $( '.panel-group.responsive' ).is( ':visible' ) === false && fakewaffle.currentPosition === 'panel' ) {
fakewaffle.panelToTab();
fakewaffle.currentPosition = 'tabs';
}
};
fakewaffle.tabToPanel = function () {
var tabGroups = $( '.nav-tabs.responsive' );
$.each( tabGroups, function ( index, tabGroup ) {
// Find the tab
var tabContents = $( tabGroup ).parent().find('.tab-content.responsive').find( '.tab-pane' );;
$.each( tabContents, function ( index, tabContent ) {
// Find the id to move the element to
var destinationId = $( tabContent ).attr( 'id' ).replace ( /^/, '#collapse-' );
// Convert tab to panel and move to destination
$( tabContent )
.removeClass( 'tab-pane' )
.addClass( 'panel-body responsive' )
.appendTo( $( destinationId ) );
} );
} );
};
fakewaffle.panelToTab = function () {
var panelGroups = $( '.panel-group.responsive' );
$.each( panelGroups, function ( index, panelGroup ) {
var destinationId = $( panelGroup ).attr( 'id' ).replace( 'collapse-', '#' );
var destination = $( destinationId ).next( '.tab-content' )[ 0 ];
// Find the panel contents
var panelContents = $( panelGroup ).find( '.panel-body.responsive' );
// Convert to tab and move to destination
panelContents
.removeClass( 'panel-body responsive' )
.addClass( 'tab-pane' )
.appendTo( $( destination ) );
} );
};
fakewaffle.bindTabToCollapse = function () {
var tabs = $( '.nav-tabs.responsive' ).find( 'li a' );
var collapse = $( '.panel-group.responsive' ).find( '.panel-collapse responsive' );
// Toggle the panels when the associated tab is toggled
tabs.on( 'shown.bs.tab', function ( e ) {
if (fakewaffle.currentPosition === 'tabs') {
var $current = $( e.currentTarget.hash.replace( /#/, '#collapse-' ) );
$current.collapse( 'show' );
if ( e.relatedTarget ) {
var $previous = $( e.relatedTarget.hash.replace( /#/, '#collapse-' ) );
$previous.collapse( 'hide' );
}
}
} );
// Toggle the tab when the associated panel is toggled
collapse.on( 'shown.bs.collapse', function ( e ) {
if (fakewaffle.currentPosition === 'panel') {
// Activate current tabs
var current = $( e.target ).context.id.replace( /collapse-/g, '#' );
$( 'a[href="' + current + '"]' ).tab( 'show' );
// Update the content with active
var panelGroup = $( e.currentTarget ).closest( '.panel-group.responsive' );
$( panelGroup ).find( '.panel-body.responsive' ).removeClass( 'active' );
$( e.currentTarget ).find( '.panel-body.responsive' ).addClass( 'active' );
}
} );
};
$( window ).resize( function () {
fakewaffle.checkResize();
} );
return fakewaffle;
}( window.jQuery, fakewaffle || { } ) );