11import mixin from '../../globals/js/misc/mixin' ;
22import createComponent from '../../globals/js/mixins/create-component' ;
3- import initComponentBySearch
4- from '../../globals/js/mixins/init-component-by-search' ;
3+ import initComponentBySearch from '../../globals/js/mixins/init-component-by-search' ;
54import eventMatches from '../../globals/js/misc/event-matches' ;
65import toggleClass from '../../globals/js/misc/svg-toggle-class' ;
76
@@ -16,31 +15,37 @@ class InteriorLeftNav extends mixin(createComponent, initComponentBySearch) {
1615 constructor ( element , options ) {
1716 super ( element , options ) ;
1817 this . constructor . components . set ( this . element , this ) ;
18+
19+ this . keepOpen =
20+ this . element . dataset . keepOpen === undefined
21+ ? this . options . keepOpen
22+ : Boolean ( this . element . dataset . keepOpen ) ;
23+
1924 this . hookListItemsEvents ( ) ;
2025 }
2126
2227 hookListItemsEvents = ( ) => {
23- this . element . addEventListener ( 'click' , ( evt ) => {
28+ this . element . addEventListener ( 'click' , evt => {
2429 const leftNavItem = eventMatches (
2530 evt ,
26- this . options . selectorLeftNavListItem ,
31+ this . options . selectorLeftNavListItem
2732 ) ;
2833 const collapseEl = eventMatches (
2934 evt ,
30- this . options . selectorLeftNavCollapse ,
35+ this . options . selectorLeftNavCollapse
3136 ) ;
3237 const collapsedBar = eventMatches (
3338 evt ,
34- `.${ this . options . classLeftNavCollapsed } ` ,
39+ `.${ this . options . classLeftNavCollapsed } `
3540 ) ;
3641
3742 if ( leftNavItem ) {
3843 const childItem = eventMatches (
3944 evt ,
40- this . options . selectorLeftNavNestedListItem ,
45+ this . options . selectorLeftNavNestedListItem
4146 ) ;
4247 const hasChildren = leftNavItem . classList . contains (
43- 'left-nav-list__item--has-children' ,
48+ 'left-nav-list__item--has-children'
4449 ) ;
4550 if ( childItem ) {
4651 this . addActiveListItem ( childItem ) ;
@@ -57,14 +62,14 @@ class InteriorLeftNav extends mixin(createComponent, initComponentBySearch) {
5762 }
5863 } ) ;
5964
60- this . element . addEventListener ( 'keydown' , ( evt ) => {
65+ this . element . addEventListener ( 'keydown' , evt => {
6166 const leftNavItemWithChildren = eventMatches (
6267 evt ,
63- this . options . selectorLeftNavListItemHasChildren ,
68+ this . options . selectorLeftNavListItemHasChildren
6469 ) ;
6570 const leftNavItem = eventMatches (
6671 evt ,
67- this . options . selectorLeftNavListItem ,
72+ this . options . selectorLeftNavListItem
6873 ) ;
6974
7075 if ( leftNavItemWithChildren && evt . which === 13 ) {
@@ -78,16 +83,16 @@ class InteriorLeftNav extends mixin(createComponent, initComponentBySearch) {
7883 addActiveListItem ( item ) {
7984 [
8085 ...this . element . querySelectorAll ( this . options . selectorLeftNavListItem ) ,
81- ] . forEach ( ( currentItem ) => {
86+ ] . forEach ( currentItem => {
8287 if ( ! ( item === currentItem ) ) {
8388 currentItem . classList . remove ( this . options . classActiveLeftNavListItem ) ;
8489 }
8590 } ) ;
8691 [
8792 ...this . element . querySelectorAll (
88- this . options . selectorLeftNavNestedListItem ,
93+ this . options . selectorLeftNavNestedListItem
8994 ) ,
90- ] . forEach ( ( currentItem ) => {
95+ ] . forEach ( currentItem => {
9196 if ( ! ( item === currentItem ) ) {
9297 currentItem . classList . remove ( this . options . classActiveLeftNavListItem ) ;
9398 }
@@ -103,39 +108,45 @@ class InteriorLeftNav extends mixin(createComponent, initComponentBySearch) {
103108 */
104109 handleNestedListClick ( listItem , evt ) {
105110 const allNestedItems = [
106- ...document . querySelectorAll (
107- this . options . selectorLeftNavListItemHasChildren ,
111+ ...this . element . querySelectorAll (
112+ this . options . selectorLeftNavListItemHasChildren
108113 ) ,
109114 ] ;
110115 const isOpen = listItem . classList . contains (
111- this . options . classExpandedLeftNavListItem ,
116+ this . options . classExpandedLeftNavListItem
112117 ) ;
113- allNestedItems . forEach ( ( currentItem ) => {
114- if ( currentItem !== listItem ) {
115- toggleClass (
116- currentItem ,
117- this . options . classExpandedLeftNavListItem ,
118- false ,
119- ) ;
120- }
121- } ) ;
122- if ( ! ( 'InteriorLeftNavItemLink' in evt . target . dataset ) ) {
123- toggleClass ( listItem , this . options . classExpandedLeftNavListItem , ! isOpen ) ;
124- }
125118 const list = listItem . querySelector ( this . options . selectorLeftNavNestedList ) ;
126119 const listItems = [
127120 ...list . querySelectorAll ( this . options . selectorLeftNavNestedListItem ) ,
128121 ] ;
129- listItems . forEach ( ( item ) => {
122+
123+ if ( ! this . keepOpen ) {
124+ allNestedItems . forEach ( currentItem => {
125+ if ( currentItem !== listItem ) {
126+ toggleClass (
127+ currentItem ,
128+ this . options . classExpandedLeftNavListItem ,
129+ false
130+ ) ;
131+ }
132+ } ) ;
133+ }
134+
135+ if ( ! ( 'InteriorLeftNavItemLink' in evt . target . dataset ) ) {
136+ toggleClass ( listItem , this . options . classExpandedLeftNavListItem , ! isOpen ) ;
137+ }
138+
139+ // a11y
140+ listItems . forEach ( item => {
130141 if ( isOpen ) {
131142 // eslint-disable-next-line no-param-reassign
132143 item . querySelector (
133- this . options . selectorLeftNavListItemLink ,
144+ this . options . selectorLeftNavListItemLink
134145 ) . tabIndex = - 1 ;
135146 } else {
136147 // eslint-disable-next-line no-param-reassign
137148 item . querySelector (
138- this . options . selectorLeftNavListItemLink ,
149+ this . options . selectorLeftNavListItemLink
139150 ) . tabIndex = 0 ;
140151 }
141152 } ) ;
@@ -154,38 +165,32 @@ class InteriorLeftNav extends mixin(createComponent, initComponentBySearch) {
154165 this . element . dataset . collapsed = true ;
155166 this . element . classList . add ( this . options . classLeftNavCollapsing ) ;
156167
157- window . setTimeout (
158- ( ) => {
159- this . element . classList . remove ( this . options . classLeftNavCollapsing ) ;
160- this . element . classList . add ( this . options . classLeftNavCollapsed ) ;
161- this . element . dispatchEvent (
162- new CustomEvent ( this . options . eventAfterLeftNavToggled , {
163- bubbles : true ,
164- cancelable : true ,
165- detail : { collapsed : true } ,
166- } ) ,
167- ) ;
168- } ,
169- 250 ,
170- ) ;
168+ window . setTimeout ( ( ) => {
169+ this . element . classList . remove ( this . options . classLeftNavCollapsing ) ;
170+ this . element . classList . add ( this . options . classLeftNavCollapsed ) ;
171+ this . element . dispatchEvent (
172+ new CustomEvent ( this . options . eventAfterLeftNavToggled , {
173+ bubbles : true ,
174+ cancelable : true ,
175+ detail : { collapsed : true } ,
176+ } )
177+ ) ;
178+ } , 250 ) ;
171179 } else {
172180 this . element . dataset . collapsed = false ;
173181 this . element . classList . remove ( this . options . classLeftNavCollapsed ) ;
174182 this . element . classList . add ( this . options . classLeftNavExpanding ) ;
175183
176- window . setTimeout (
177- ( ) => {
178- this . element . classList . remove ( this . options . classLeftNavExpanding ) ;
179- this . element . dispatchEvent (
180- new CustomEvent ( this . options . eventAfterLeftNavToggled , {
181- bubbles : true ,
182- cancelable : true ,
183- detail : { collapsed : false } ,
184- } ) ,
185- ) ;
186- } ,
187- 250 ,
188- ) ;
184+ window . setTimeout ( ( ) => {
185+ this . element . classList . remove ( this . options . classLeftNavExpanding ) ;
186+ this . element . dispatchEvent (
187+ new CustomEvent ( this . options . eventAfterLeftNavToggled , {
188+ bubbles : true ,
189+ cancelable : true ,
190+ detail : { collapsed : false } ,
191+ } )
192+ ) ;
193+ } , 250 ) ;
189194 }
190195 }
191196 } ;
@@ -214,7 +219,8 @@ class InteriorLeftNav extends mixin(createComponent, initComponentBySearch) {
214219 selectorLeftNavListItem : '[data-interior-left-nav-item]' ,
215220 selectorLeftNavListItemLink : '[data-interior-left-nav-item-link]' ,
216221 selectorLeftNavNestedListItem : '[data-interior-left-nav-nested-item]' ,
217- selectorLeftNavListItemHasChildren : '[data-interior-left-nav-with-children]' ,
222+ selectorLeftNavListItemHasChildren :
223+ '[data-interior-left-nav-with-children]' ,
218224 selectorLeftNavCollapse : '[data-interior-left-nav-collapse]' ,
219225 // CSS Class Selectors
220226 classActiveLeftNavListItem : 'left-nav-list__item--active' ,
@@ -225,6 +231,8 @@ class InteriorLeftNav extends mixin(createComponent, initComponentBySearch) {
225231 // Event
226232 eventBeforeLeftNavToggled : 'left-nav-beingtoggled' ,
227233 eventAfterLeftNavToggled : 'left-nav-toggled' ,
234+ // Option
235+ keepOpen : false ,
228236 } ;
229237}
230238
0 commit comments