@@ -13,21 +13,60 @@ class TabsSingleton {
13
13
constructor ( key ) {
14
14
this . key = key ;
15
15
this . tabStrip = document . querySelector ( '.tab-strip--singleton' ) ;
16
+
17
+ // Only tab sets will have a type, init and try to retrieve
18
+ this . type = null ;
19
+ if ( this . tabStrip !== null ) {
20
+ this . type = this . tabStrip . getAttribute ( 'data-tab-preference' ) ;
21
+ }
16
22
}
17
23
18
- get languagePref ( ) {
19
- return window . localStorage . getItem ( this . key ) ;
24
+ /**
25
+ * Return the tabPref object containing preferences for tab sets
26
+ * and page specific prefs. Returns an empty object if it doesn't
27
+ * exist.
28
+ * @returns {object } Tab preference object.
29
+ */
30
+ get tabPref ( ) {
31
+ let pref = JSON . parse ( window . localStorage . getItem ( this . key ) ) ;
32
+
33
+ if ( ! pref ) {
34
+ pref = { } ;
35
+ }
36
+
37
+ return pref ;
20
38
}
21
39
22
- set languagePref ( value ) {
23
- window . localStorage . setItem ( this . key , value ) ;
40
+ /**
41
+ * Sets the tabPref object depending on whether the tab belongs
42
+ * to set (e.g., "drivers") or if it's a one-off page.
43
+ * @param {object } value The "tabId" and optional "type" (tab set)
44
+ */
45
+ set tabPref ( value ) {
46
+ const tabPref = this . tabPref ;
47
+
48
+ // If "type" exists it belongs to a tab set
49
+ if ( this . type ) {
50
+ // Set top-level fields for tab set preferences
51
+ tabPref [ value . type ] = value . tabId ;
52
+ } else if ( tabPref . pages ) {
53
+ // Store one-off pages in the pages embedded document
54
+ tabPref . pages [ window . location . pathname ] = value . tabId ;
55
+ } else {
56
+ // Init pages embedded doc if it doesnt exist and store one-off
57
+ tabPref . pages = { } ;
58
+ tabPref . pages [ window . location . pathname ] = value . tabId ;
59
+ }
60
+
61
+ // Write pref object back to localStorage
62
+ window . localStorage . setItem ( this . key , JSON . stringify ( tabPref ) ) ;
24
63
}
25
64
26
65
/**
27
66
* Return the first singleton tab ID on the page.
28
67
* @returns {string } The first singleton tab ID found.
29
68
*/
30
- getFirstLanguage ( ) {
69
+ getFirstTab ( ) {
31
70
const tabsElement = this . tabStrip . querySelector ( '.tab-strip__element[aria-selected=true]' ) ;
32
71
if ( ! tabsElement ) { return null ; }
33
72
@@ -42,12 +81,18 @@ class TabsSingleton {
42
81
for ( const element of this . tabStrip . querySelectorAll ( '[data-tabid]' ) ) {
43
82
element . onclick = ( e ) => {
44
83
// Get the tab ID of the clicked tab
45
- const currentAttrValue = e . target . getAttribute ( 'data-tabid' ) ;
84
+ const tabId = e . target . getAttribute ( 'data-tabid' ) ;
85
+ const type = this . tabStrip . getAttribute ( 'data-tab-preference' ) ;
86
+
87
+ // Build the pref object to set
88
+ const pref = { } ;
89
+ pref . tabId = tabId ;
90
+ pref . type = type ;
46
91
47
92
// Check to make sure value is not null, i.e., don't do anything on "other"
48
- if ( currentAttrValue ) {
93
+ if ( tabId ) {
49
94
// Save the users preference and re-render
50
- this . languagePref = currentAttrValue ;
95
+ this . tabPref = pref ;
51
96
this . update ( ) ;
52
97
53
98
e . preventDefault ( ) ;
@@ -60,20 +105,27 @@ class TabsSingleton {
60
105
61
106
update ( ) {
62
107
if ( ! this . tabStrip ) { return ; }
63
-
64
- let languagePref = this . languagePref ;
65
- if ( ! languagePref ) {
66
- languagePref = this . getFirstLanguage ( ) ;
67
- } else if ( ! this . tabStrip . querySelector ( `[data-tabid="${ languagePref } "]` ) ) {
68
- // Confirm a tab for their languagePref exists at the top of the page
69
- languagePref = this . getFirstLanguage ( ) ;
108
+ let type = this . type ;
109
+
110
+ let tabPref = this . tabPref ;
111
+
112
+ if ( ! tabPref ) {
113
+ // Display the first tab when there is no pref
114
+ tabPref = this . getFirstTab ( ) ;
115
+ } else if ( tabPref . pages && tabPref . pages [ window . location . pathname ] ) {
116
+ // Check if current page has a one-off page specific pref
117
+ tabPref = tabPref . pages ;
118
+ type = window . location . pathname ;
119
+ } else if ( ! this . tabStrip . querySelector ( `[data-tabid="${ tabPref [ type ] } "]` ) ) {
120
+ // Confirm a tab for their tabPref exists at the top of the page
121
+ tabPref = this . getFirstTab ( ) ;
70
122
}
71
123
72
- if ( ! languagePref ) { return ; }
124
+ if ( ! tabPref ) { return ; }
73
125
74
126
// Show the appropriate tab content and mark the tab as active
75
- showHideTabContent ( languagePref ) ;
76
- this . showHideSelectedTab ( languagePref ) ;
127
+ showHideTabContent ( tabPref [ type ] ) ;
128
+ this . showHideSelectedTab ( tabPref [ type ] ) ;
77
129
}
78
130
79
131
/**
@@ -128,5 +180,5 @@ class TabsSingleton {
128
180
129
181
// Create tab functionality for code examples
130
182
export function setup ( ) {
131
- ( new TabsSingleton ( 'languagePref ' ) ) . setup ( ) ;
183
+ ( new TabsSingleton ( 'tabPref ' ) ) . setup ( ) ;
132
184
}
0 commit comments