@@ -28,41 +28,40 @@ export default Base.extend({
2828
2929 var self = this ;
3030
31- self . $nav = $ ( "<nav/>" )
32- . attr ( "aria-label" , "Tab Navigation" )
31+ const $nav = $ ( "<nav/>" )
32+ . attr ( "aria-label" , "Tab Navigation" ) ;
3333
34- self . $toc = $ ( "<ul/>" )
34+ const $toc = $ ( "<ul/>" )
3535 . addClass ( self . options . classTOCName )
36- . appendTo ( self . $nav ) ;
36+ . appendTo ( $nav ) ;
3737
3838 if ( self . options . prependTo ) {
39- self . $nav . prependTo ( self . options . prependTo ) ;
39+ $nav . prependTo ( self . options . prependTo ) ;
4040 } else if ( self . options . appendTo ) {
41- self . $nav . appendTo ( self . options . appendTo ) ;
41+ $nav . appendTo ( self . options . appendTo ) ;
4242 } else {
43- self . $nav . prependTo ( self . $el ) ;
43+ $nav . prependTo ( self . $el ) ;
4444 }
4545
46-
4746 if ( self . options . className ) {
4847 self . $el . addClass ( self . options . className ) ;
4948 }
5049
5150 $ ( self . options . section , self . $el )
5251 . addClass ( self . options . classSectionName )
5352 . attr ( "role" , "tabpanel" )
54- . attr ( "tabindex" , "0" )
53+ . attr ( "tabindex" , "0" ) ;
5554
5655 var asTabs = self . $el . hasClass ( "autotabs" ) ;
5756
5857 if ( asTabs ) {
59- self . $toc . addClass ( "nav-tabs" ) ;
58+ $toc . addClass ( "nav-tabs" ) ;
6059 self . $contentArea = $ ( "<div/>" ) . addClass ( self . options . classContentAreaName ) ;
61- self . $contentArea . insertAfter ( self . $nav ) ;
60+ self . $contentArea . insertAfter ( $nav ) ;
6261 $ ( self . options . section , self . $el ) . appendTo ( self . $contentArea ) ;
6362 $ ( self . options . section , self . $el ) . find ( "legend" ) . hide ( ) ;
6463 } else {
65- self . $toc . addClass ( [
64+ $toc . addClass ( [
6665 "flex-column" ,
6766 "float-end" ,
6867 "border" ,
@@ -72,45 +71,51 @@ export default Base.extend({
7271 "ms-3" ,
7372 "py-2" ,
7473 "px-0"
75- ] )
74+ ] ) ;
7675 }
7776
7877 var activeId = null ;
7978
8079 $ ( self . options . levels , self . $el ) . each ( function ( i ) {
81- const section = this . closest ( self . options . section ) ;
8280 const $level = $ ( this ) ;
83- let id = $level . prop ( "id" ) ? $level . prop ( "id" ) : $ ( section ) . prop ( "id" ) ;
81+ const $section = $level . closest ( self . options . section ) ;
82+ let sectionId = $section . prop ( "id" ) ;
83+ let sectionHash = `#${ sectionId } ` ;
84+ const tabId = `${ self . options . IDPrefix } ${ self . name } -${ i } ` ;
85+ const tabHash = `#${ tabId } ` ;
86+ const levelId = `${ tabId } -pane` ;
87+ const levelHash = `#${ levelId } ` ;
8488
85- if ( ! id || $ ( "#" + id ) . length > 0 ) {
86- id = self . options . IDPrefix + self . name + "-" + i ;
89+ if ( ! asTabs ) {
90+ $level . attr ( "id" , levelId ) . attr ( "aria-labelledby" , tabId ) ;
91+ } else {
92+ $section . attr ( "aria-labelledby" , tabId ) ;
93+ if ( ! sectionId ) {
94+ // sections without ID get auto generated "<tabId>-pane"
95+ sectionId = levelId ;
96+ sectionHash = `#${ levelId } ` ;
97+ $section . attr ( "id" , levelId ) ;
98+ }
8799 }
88100
89- const tabId = `${ id } -tab` ;
90- $ ( section ) . attr ( "id" , id ) . attr ( "aria-labelledby" , tabId ) ;
91-
92- if ( window . location . hash === "#" + id ) {
93- activeId = tabId ;
94- }
95- if ( activeId === null && $level . hasClass ( self . options . classActiveName ) ) {
101+ if ( activeId === null && ( tabHash . indexOf ( window . location . hash ) != - 1 || $level . hasClass ( self . options . classActiveName ) ) ) {
96102 activeId = tabId ;
97103 }
98- $level . data ( "navref" , id ) ;
99104
100105 const $navItem = $ ( "<li/>" ) ;
101106 $navItem
102107 . addClass ( "nav-item" )
103108 . attr ( "role" , "presentation" )
104- . appendTo ( self . $toc ) ;
109+ . appendTo ( $toc ) ;
105110
106111 const $nav = $ ( "<a/>" ) ;
107112 $nav . appendTo ( $navItem )
108- . text ( $level . text ( ) )
113+ . html ( $level . html ( ) )
109114 . attr ( "id" , tabId )
110- . attr ( "href" , "#" + id )
111- . attr ( "aria-controls" , id )
115+ . attr ( "href" , asTabs ? sectionHash : levelHash )
116+ . attr ( "aria-controls" , asTabs ? sectionId : levelId )
112117 . attr ( "data-bs-toggle" , "tab" )
113- . attr ( "data-bs-target" , `# ${ id } ` )
118+ . attr ( "data-bs-target" , asTabs ? sectionHash : levelHash )
114119 . addClass ( [
115120 "nav-link" ,
116121 self . options . classLevelPrefixName + self . getLevel ( $level ) ,
@@ -142,39 +147,29 @@ export default Base.extend({
142147 $ ( this ) . trigger ( "clicked" ) ;
143148 if ( ! options . skipHash ) {
144149 if ( window . history && window . history . pushState ) {
145- window . history . pushState ( { } , "" , `# ${ id } ` ) ;
150+ window . history . pushState ( { } , "" , tabHash ) ;
146151 }
147152 }
148153 } ) ;
154+
149155 if ( ! asTabs ) {
150156 $nav . addClass ( [
151157 "text-decoration-underline" ,
152158 "p-0" ,
153159 "mx-3" ,
154160 ] ) ;
155161 }
156- $level . data ( "autotoc-trigger-id" , id ) ;
157162
158163 self . tabs . push ( {
159- section : section ,
160- id : id ,
164+ section : $ section[ 0 ] ,
165+ id : levelId ,
161166 nav : $nav [ 0 ] ,
162167 } ) ;
163168 } ) ;
164169
165- if ( activeId ) {
166- const activeTabButton = self . $toc . find ( "a#" + activeId ) [ 0 ] ;
167- if ( activeTabButton ) {
168- const tab = Tab . getOrCreateInstance ( activeTabButton ) ;
169- tab . show ( ) ;
170- }
171- } else {
172- const firstTabButton = self . $toc . find ( "a" ) . first ( ) [ 0 ] ;
173- if ( firstTabButton ) {
174- const tab = Tab . getOrCreateInstance ( firstTabButton ) ;
175- tab . show ( ) ;
176- }
177- }
170+ const activeTabButton = activeId ? $toc . find ( "a#" + activeId ) [ 0 ] : $toc . find ( "a" ) . first ( ) [ 0 ] ;
171+ const tab = Tab . getOrCreateInstance ( activeTabButton ) ;
172+ tab . show ( ) ;
178173
179174 // After DOM tree is built, initialize eventual validation
180175 this . initialize_validation ( ) ;
0 commit comments