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