@@ -30,48 +30,117 @@ function adbcInjectVersionSwitcher() {
30
30
// path;version\npath2;version2;\n...
31
31
// Versions are sorted at generation time
32
32
33
- versions
34
- . trim ( )
35
- . split ( / \n / g)
36
- . map ( ( version ) => version . split ( / ; / ) )
37
- // Most recent on top
38
- . reverse ( )
39
- . forEach ( ( version ) => {
40
- const el = document . createElement ( "a" ) ;
41
- // Variable injected by template
42
- el . setAttribute ( "href" , versionsRoot + "/" + version [ 0 ] ) ;
43
- el . innerText = version [ 1 ] ;
44
- if ( version [ 1 ] === currentVersion ) {
45
- el . classList . toggle ( "active" ) ;
46
- }
47
- const li = document . createElement ( "li" ) ;
48
- li . appendChild ( el ) ;
49
- root . appendChild ( li ) ;
33
+ const parsedVersions = versions
34
+ . trim ( )
35
+ . split ( / \n / g)
36
+ . map ( ( version ) => version . split ( / ; / ) )
37
+ // Most recent on top
38
+ . reverse ( ) ;
39
+ parsedVersions . forEach ( ( version ) => {
40
+ const el = document . createElement ( "a" ) ;
41
+ // Variable injected by template
42
+ el . setAttribute ( "href" , versionsRoot + "/" + version [ 0 ] ) ;
43
+ el . innerText = version [ 1 ] ;
44
+ if ( version [ 1 ] === currentVersion ) {
45
+ el . classList . toggle ( "active" ) ;
46
+ }
47
+ const li = document . createElement ( "li" ) ;
48
+ li . appendChild ( el ) ;
49
+ root . appendChild ( li ) ;
50
50
51
- el . addEventListener ( "click" , ( e ) => {
52
- e . preventDefault ( ) ;
53
- try {
54
- let relativePart = window . location . pathname . replace ( / ^ \/ / , "" ) ;
55
- // Remove the adbc/ prefix
56
- relativePart = relativePart . replace ( / ^ a d b c [ ^ \/ ] + \/ / , "" ) ;
57
- // Remove the version number
58
- relativePart = relativePart . replace ( / ^ [ ^ \/ ] + \/ / , "" ) ;
59
- const newUrl = `${ el . getAttribute ( "href" ) } /${ relativePart } ` ;
60
- window . fetch ( newUrl ) . then ( ( resp ) => {
61
- if ( resp . status === 200 ) {
62
- window . location . href = newUrl ;
63
- } else {
64
- window . location . href = el . getAttribute ( "href" ) ;
65
- }
66
- } , ( ) => {
51
+ el . addEventListener ( "click" , ( e ) => {
52
+ e . preventDefault ( ) ;
53
+ try {
54
+ let relativePart = window . location . pathname . replace ( / ^ \/ / , "" ) ;
55
+ // Remove the adbc/ prefix
56
+ relativePart = relativePart . replace ( / ^ a d b c [ ^ \/ ] + \/ / , "" ) ;
57
+ // Remove the version number
58
+ relativePart = relativePart . replace ( / ^ [ ^ \/ ] + \/ / , "" ) ;
59
+ const newUrl = `${ el . getAttribute ( "href" ) } /${ relativePart } ` ;
60
+ window . fetch ( newUrl ) . then ( ( resp ) => {
61
+ if ( resp . status === 200 ) {
62
+ window . location . href = newUrl ;
63
+ } else {
67
64
window . location . href = el . getAttribute ( "href" ) ;
68
- } ) ;
69
- } catch ( e ) {
65
+ }
66
+ } , ( ) => {
70
67
window . location . href = el . getAttribute ( "href" ) ;
71
- }
72
- return false ;
73
- } ) ;
68
+ } ) ;
69
+ } catch ( e ) {
70
+ window . location . href = el . getAttribute ( "href" ) ;
71
+ }
72
+ return false ;
73
+ } ) ;
74
+ } ) ;
75
+
76
+ // Inject a banner warning if the user is looking at older/development
77
+ // version documentation
78
+
79
+ // If the user has dismissed the popup, don't show it again
80
+ const storageKey = "adbc-ignored-version-warnings" ;
81
+ const ignoreVersionWarnings = new Set ( ) ;
82
+ try {
83
+ const savedVersions = JSON . parse ( window . localStorage [ storageKey ] ) ;
84
+ for ( const version of savedVersions ) {
85
+ ignoreVersionWarnings . add ( version ) ;
86
+ }
87
+ } catch ( e ) {
88
+ // ignore
89
+ }
90
+
91
+ if ( ignoreVersionWarnings . has ( currentVersion ) ) {
92
+ return ;
93
+ }
94
+
95
+ let warningBanner = null ;
96
+ const redirectUrl = `${ versionsRoot } /current` ;
97
+ let redirectText = null ;
98
+ if ( currentVersion . endsWith ( " (dev)" ) ) {
99
+ warningBanner = "This is documentation for an unstable development version." ;
100
+ redirectText = "Switch to stable version" ;
101
+ } else {
102
+ const stableVersions = parsedVersions
103
+ . filter ( v => v [ 0 ] === "current" ) ;
104
+ if ( stableVersions . length > 0 ) {
105
+ const stableVersion = stableVersions [ 0 ] [ 1 ] . match ( / ^ ( .+ ) \( c u r r e n t \) / ) [ 1 ] ;
106
+ if ( currentVersion !== stableVersion ) {
107
+ warningBanner = `This is documentation for version ${ currentVersion } .` ;
108
+ redirectText = `Switch to current stable version` ;
109
+ }
110
+ }
111
+ }
112
+
113
+ if ( warningBanner !== null ) {
114
+ // Generate on the fly instead of depending on the template containing
115
+ // the right elements/styles
116
+ const container = document . createElement ( "div" ) ;
117
+ const text = document . createElement ( "span" ) ;
118
+ text . innerText = warningBanner + " " ;
119
+ const button = document . createElement ( "a" ) ;
120
+ button . setAttribute ( "href" , redirectUrl ) ;
121
+ button . innerText = redirectText ;
122
+ const hide = document . createElement ( "a" ) ;
123
+ hide . innerText = "Hide for this version" ;
124
+ const spacer = document . createTextNode ( " " ) ;
125
+ container . appendChild ( text ) ;
126
+ container . appendChild ( button ) ;
127
+ container . appendChild ( spacer ) ;
128
+ container . appendChild ( hide ) ;
129
+
130
+ hide . addEventListener ( "click" , ( e ) => {
131
+ container . remove ( ) ;
132
+ ignoreVersionWarnings . add ( currentVersion ) ;
133
+ window . localStorage [ storageKey ] =
134
+ JSON . stringify ( Array . from ( ignoreVersionWarnings ) ) ;
74
135
} ) ;
136
+
137
+ container . style . background = "#f8d7da" ;
138
+ container . style . color = "#000" ;
139
+ container . style . padding = "1em" ;
140
+ container . style . textAlign = "center" ;
141
+
142
+ document . body . prepend ( container ) ;
143
+ }
75
144
} ;
76
145
77
146
if ( document . readyState !== "loading" ) {
0 commit comments