@@ -12,12 +12,12 @@ function showHideTabContent(currentAttrValue) {
12
12
class TabsSingleton {
13
13
constructor ( key ) {
14
14
this . key = key ;
15
- this . tabStrip = document . querySelector ( '.tab-strip--singleton' ) ;
15
+ this . tabStrips = document . querySelectorAll ( '.tab-strip--singleton' ) ;
16
16
17
17
// Only tab sets will have a type, init and try to retrieve
18
18
this . type = null ;
19
- if ( this . tabStrip !== null ) {
20
- this . type = this . tabStrip . getAttribute ( 'data-tab-preference' ) ;
19
+ if ( this . tabStrips !== null ) {
20
+ this . type = this . tabStrips [ 0 ] . getAttribute ( 'data-tab-preference' ) ;
21
21
}
22
22
}
23
23
@@ -61,44 +61,47 @@ class TabsSingleton {
61
61
* @returns {string } The first singleton tab ID found.
62
62
*/
63
63
getFirstTab ( ) {
64
- const tabsElement = this . tabStrip . querySelector ( '.tab-strip__element[aria-selected=true]' ) ;
64
+ const tabsElement = this . tabStrips [ 0 ] .
65
+ querySelector ( '.tab-strip__element[aria-selected=true]' ) ;
65
66
if ( ! tabsElement ) { return null ; }
66
67
67
68
return tabsElement . getAttribute ( 'data-tabid' ) ;
68
69
}
69
70
70
71
setup ( ) {
71
- if ( ! this . tabStrip ) { return ; }
72
+ if ( ! this . tabStrips ) { return ; }
72
73
73
74
this . hideTabBars ( ) ;
74
75
75
- for ( const element of this . tabStrip . querySelectorAll ( '[data-tabid]' ) ) {
76
- element . onclick = ( e ) => {
77
- // Get the tab ID of the clicked tab
78
- const tabId = e . target . getAttribute ( 'data-tabid' ) ;
79
- const type = this . tabStrip . getAttribute ( 'data-tab-preference' ) ;
80
-
81
- // Build the pref object to set
82
- const pref = { } ;
83
- pref . tabId = tabId ;
84
- pref . type = type ;
85
-
86
- // Check to make sure value is not null, i.e., don't do anything on "other"
87
- if ( tabId ) {
88
- // Save the users preference and re-render
89
- this . tabPref = pref ;
90
- this . update ( ) ;
91
-
92
- e . preventDefault ( ) ;
93
- }
94
- } ;
76
+ for ( const tabStrip of this . tabStrips ) {
77
+ for ( const element of tabStrip . querySelectorAll ( '[data-tabid]' ) ) {
78
+ element . onclick = ( e ) => {
79
+ // Get the tab ID of the clicked tab
80
+ const tabId = e . target . getAttribute ( 'data-tabid' ) ;
81
+ const type = this . tabStrips [ 0 ] . getAttribute ( 'data-tab-preference' ) ;
82
+
83
+ // Build the pref object to set
84
+ const pref = { } ;
85
+ pref . tabId = tabId ;
86
+ pref . type = type ;
87
+
88
+ // Check to make sure value is not null, i.e., don't do anything on "other"
89
+ if ( tabId ) {
90
+ // Save the users preference and re-render
91
+ this . tabPref = pref ;
92
+ this . update ( ) ;
93
+
94
+ e . preventDefault ( ) ;
95
+ }
96
+ } ;
97
+ }
95
98
}
96
99
97
100
this . update ( ) ;
98
101
}
99
102
100
103
update ( ) {
101
- if ( ! this . tabStrip ) { return ; }
104
+ if ( ! this . tabStrips ) { return ; }
102
105
let type = this . type ;
103
106
104
107
let tabPref = this . tabPref ;
@@ -107,7 +110,7 @@ class TabsSingleton {
107
110
// Check if current page has a one-off page specific pref
108
111
tabPref = tabPref . pages ;
109
112
type = window . location . pathname ;
110
- } else if ( ! this . tabStrip . querySelector ( `[data-tabid="${ tabPref [ type ] } "]` ) ) {
113
+ } else if ( ! this . tabStrips [ 0 ] . querySelector ( `[data-tabid="${ tabPref [ type ] } "]` ) ) {
111
114
// If their tabPref does not exist at the top of the page use the first tab
112
115
tabPref [ type ] = this . getFirstTab ( ) ;
113
116
}
@@ -125,29 +128,31 @@ class TabsSingleton {
125
128
* @returns {void }
126
129
*/
127
130
showHideSelectedTab ( currentAttrValue ) {
128
- // Get the <a>, <li> and <ul> of the selected tab
129
- const tabLink = $ ( this . tabStrip . querySelector ( `[data-tabid="${ currentAttrValue } "]` ) ) ;
130
- const tabList = tabLink . parent ( 'ul' ) ;
131
-
132
- // Get the dropdown <a> and <li> for active and label management
133
- const dropdownLink = $ ( this . tabStrip . querySelector ( '.dropdown-toggle' ) ) ;
134
- const dropdownListItem = $ ( this . tabStrip . querySelector ( '.dropdown' ) ) ;
135
-
136
- // Set the active tab, if it's on the dropdown set it to active and change label
137
- if ( tabList . hasClass ( 'dropdown-menu' ) ) {
138
- // Use first so text doesn't repeat if more than one set of tabs
139
- dropdownLink . text ( `${ tabLink . first ( ) . text ( ) } ` ) . append ( '<span class="caret"></span>' ) ;
140
- dropdownListItem .
141
- attr ( 'aria-selected' , true ) .
142
- siblings ( ) .
143
- attr ( 'aria-selected' , false ) ;
144
- } else {
145
- // Set a non-dropdown tab to active, and change the dropdown label back to "Other"
146
- tabLink .
147
- attr ( 'aria-selected' , true ) .
148
- siblings ( ) .
149
- attr ( 'aria-selected' , false ) ;
150
- dropdownLink . text ( 'Other ' ) . append ( '<span class="caret"></span>' ) ;
131
+ for ( const tabStrip of this . tabStrips ) {
132
+ // Get the <a>, <li> and <ul> of the selected tab
133
+ const tabLink = $ ( tabStrip . querySelector ( `[data-tabid="${ currentAttrValue } "]` ) ) ;
134
+ const tabList = tabLink . parent ( 'ul' ) ;
135
+
136
+ // Get the dropdown <a> and <li> for active and label management
137
+ const dropdownLink = $ ( tabStrip . querySelector ( '.dropdown-toggle' ) ) ;
138
+ const dropdownListItem = $ ( tabStrip . querySelector ( '.dropdown' ) ) ;
139
+
140
+ // Set the active tab, if it's on the dropdown set it to active and change label
141
+ if ( tabList . hasClass ( 'dropdown-menu' ) ) {
142
+ // Use first so text doesn't repeat if more than one set of tabs
143
+ dropdownLink . text ( `${ tabLink . first ( ) . text ( ) } ` ) . append ( '<span class="caret"></span>' ) ;
144
+ dropdownListItem .
145
+ attr ( 'aria-selected' , true ) .
146
+ siblings ( ) .
147
+ attr ( 'aria-selected' , false ) ;
148
+ } else {
149
+ // Set a non-dropdown tab to active, and change the dropdown label back to "Other"
150
+ tabLink .
151
+ attr ( 'aria-selected' , true ) .
152
+ siblings ( ) .
153
+ attr ( 'aria-selected' , false ) ;
154
+ dropdownLink . text ( 'Other ' ) . append ( '<span class="caret"></span>' ) ;
155
+ }
151
156
}
152
157
}
153
158
@@ -156,16 +161,19 @@ class TabsSingleton {
156
161
* @returns {void }
157
162
*/
158
163
hideTabBars ( ) {
159
- const tabBars = $ ( '.tab-strip--singleton' ) ;
160
- const mainTabBar = tabBars . first ( ) ;
161
- // Remove any additional tab bars
162
- tabBars . slice ( 1 ) .
163
- detach ( ) ;
164
- // Position the main tab bar after the page title
165
- mainTabBar .
166
- detach ( ) .
167
- insertAfter ( 'h1' ) .
168
- first ( ) ;
164
+ const isTop = document . querySelector ( '.tabs-top' ) ;
165
+ if ( isTop ) {
166
+ const tabBars = $ ( '.tab-strip--singleton' ) ;
167
+ const mainTabBar = tabBars . first ( ) ;
168
+ // Remove any additional tab bars
169
+ tabBars . slice ( 1 ) .
170
+ detach ( ) ;
171
+ // Position the main tab bar after the page title
172
+ mainTabBar .
173
+ detach ( ) .
174
+ insertAfter ( 'h1' ) .
175
+ first ( ) ;
176
+ }
169
177
}
170
178
}
171
179
0 commit comments