Skip to content

Commit 92a469a

Browse files
committed
Refactoring: use the same cookie functions for all scripts
1 parent f023089 commit 92a469a

File tree

7 files changed

+134
-143
lines changed

7 files changed

+134
-143
lines changed

deps/jquery/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ SCRIPTS = jquery-$(JQUERY_VERSION).min.js \
1111
jquery.scrollTo-$(SCROLL_VERSION).min.js \
1212
jquery.powertip-$(POWERTIP_VERSION).mod.min.js \
1313
jquery.ui.touch-punch-$(TOUCHPUNCH_VERSION).min.js \
14-
jquery.smartmenus-$(SMARTMENUS_VERSION).min.js
14+
jquery.smartmenus-$(SMARTMENUS_VERSION).min.js \
15+
cookie.js
1516
RESULTS = jquery.js doxmenu-min.css
1617

1718
all: $(RESULTS)

deps/jquery/cookie.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*!
2+
Cookie helper functions
3+
Copyright (c) 2023 Dimitri van Heesch
4+
Released under MIT license.
5+
*/
6+
var Cookie =
7+
{
8+
cookie_namespace: 'doxygen_',
9+
10+
readSetting: function(cookie,defVal) {
11+
if (window.chrome) {
12+
var val = localStorage.getItem(this.cookie_namespace+cookie);
13+
if (val) return val;
14+
} else {
15+
var myCookie = this.cookie_namespace+cookie+"=";
16+
if (document.cookie) {
17+
var index = document.cookie.indexOf(myCookie);
18+
if (index != -1) {
19+
var valStart = index + myCookie.length;
20+
var valEnd = document.cookie.indexOf(";", valStart);
21+
if (valEnd == -1) {
22+
valEnd = document.cookie.length;
23+
}
24+
var val = document.cookie.substring(valStart, valEnd);
25+
return val;
26+
}
27+
}
28+
}
29+
return defVal;
30+
},
31+
32+
writeSetting: function(cookie,val,days=10*365) { // 10 years -> forever
33+
if (window.chrome) {
34+
localStorage.setItem(this.cookie_namespace+cookie,val);
35+
} else {
36+
var date = new Date();
37+
date.setTime(date.getTime()+(days*24*60*60*1000));
38+
expiration = date.toGMTString();
39+
document.cookie = this.cookie_namespace + cookie + "=" +
40+
val + "; SameSite=Lax; expires=" + expiration+"; path=/";
41+
}
42+
},
43+
44+
eraseSetting: function(cookie) {
45+
if (window.chrome) {
46+
var val = localStorage.removeItem(this.cookie_namespace+cookie);
47+
if (val) return val;
48+
} else {
49+
this.writeSetting(cookie,'',-1);
50+
}
51+
},
52+
}

templates/html/darkmode_toggle.js

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ SOFTWARE.
2828
*/
2929

3030
class DarkModeToggle extends HTMLElement {
31+
32+
static darkmode_cookie_name = 'prefers-dark';
33+
static lightmode_cookie_name = 'prefers-light';
34+
3135
static icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" height="1em" width="1em"><g fill="none" fill-rule="evenodd"><path d="M0 0h24v24H0z"></path><rect width="1" height="3" x="12" fill="currentColor" rx=".5"></rect><rect width="1" height="3" x="12" y="21" fill="currentColor" rx=".5"></rect><rect width="1" height="3" x="22" y="10.5" fill="currentColor" rx=".5" transform="rotate(90 22.5 12)"></rect><rect width="1" height="3" x="1" y="10.5" fill="currentColor" rx=".5" transform="rotate(90 1.5 12)"></rect><rect width="1" height="3" x="19" y="3" fill="currentColor" rx=".5" transform="rotate(-135 19.5 4.5)"></rect><rect width="1" height="3" x="19" y="18" fill="currentColor" rx=".5" transform="rotate(135 19.5 19.5)"></rect><rect width="1" height="3" x="4" y="3" fill="currentColor" rx=".5" transform="scale(1 -1) rotate(45 15.37 0)"></rect><rect width="1" height="3" x="4" y="18" fill="currentColor" rx=".5" transform="scale(1 -1) rotate(-45 -42.57 0)"></rect><circle cx="12" cy="12" r="6.5" stroke="currentColor"></circle><path fill="currentColor" stroke="currentColor" d="M12.5 18.48V5.52a6.5 6.5 0 010 12.96z"></path></g></svg>';
3236
static title = "Toggle Light/Dark Mode"
3337

@@ -159,54 +163,29 @@ class DarkModeToggle extends HTMLElement {
159163
}
160164

161165
static get prefersDarkModeInLightMode() {
162-
if (window.chrome) { // Chrome supports localStorage in combination with file:// but not cookies
163-
return localStorage.getItem(DarkModeToggle.prefersDarkModeInLightModeKey)
164-
} else { // Other browsers support cookies in combination with file:// but not localStorage
165-
return DarkModeToggle.readCookie('doxygen_prefers_dark')=='1'
166-
}
166+
return Cookie.readSetting(DarkModeToggle.darkmode_cookie_name,'0')=='1';
167167
}
168168

169169
static set prefersDarkModeInLightMode(preference) {
170-
if (window.chrome) {
171-
if (preference) {
172-
localStorage.setItem(DarkModeToggle.prefersDarkModeInLightModeKey, true)
173-
} else {
174-
localStorage.removeItem(DarkModeToggle.prefersDarkModeInLightModeKey)
175-
}
170+
if (preference) {
171+
Cookie.writeSetting(DarkModeToggle.darkmode_cookie_name,'1');
176172
} else {
177-
if (preference) {
178-
DarkModeToggle.createCookie('doxygen_prefers_dark','1',365)
179-
} else {
180-
DarkModeToggle.eraseCookie('doxygen_prefers_dark')
181-
}
173+
Cookie.eraseSetting(DarkModeToggle.darkmode_cookie_name);
182174
}
183175
}
184176

185177
static get prefersLightModeInDarkMode() {
186-
if (window.chrome) { // Chrome supports localStorage in combination with file:// but not cookies
187-
return localStorage.getItem(DarkModeToggle.prefersLightModeInDarkModeKey)
188-
} else { // Other browsers support cookies in combination with file:// but not localStorage
189-
return DarkModeToggle.readCookie('doxygen_prefers_light')=='1'
190-
}
178+
return Cookie.readSetting(DarkModeToggle.lightmode_cookie_name,'0')=='1'
191179
}
192180

193181
static set prefersLightModeInDarkMode(preference) {
194-
if (window.chrome) {
195-
if (preference) {
196-
localStorage.setItem(DarkModeToggle.prefersLightModeInDarkModeKey, true)
197-
} else {
198-
localStorage.removeItem(DarkModeToggle.prefersLightModeInDarkModeKey)
199-
}
182+
if (preference) {
183+
Cookie.writeSetting(DarkModeToggle.lightmode_cookie_name,'1');
200184
} else {
201-
if (preference) {
202-
DarkModeToggle.createCookie('doxygen_prefers_light','1',365)
203-
} else {
204-
DarkModeToggle.eraseCookie('doxygen_prefers_light')
205-
}
185+
Cookie.eraseSetting(DarkModeToggle.lightmode_cookie_name);
206186
}
207187
}
208188

209-
210189
/**
211190
* @returns `true` for dark-mode, `false` for light-mode user preference
212191
*/

templates/html/jquery.js

Lines changed: 47 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/html/navtree.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
var navTreeSubIndices = new Array();
2626
var arrowDown = '&#9660;';
2727
var arrowRight = '&#9658;';
28+
var navpath_cookie_name = 'navpath';
2829

2930
function getData(varName)
3031
{
@@ -61,37 +62,21 @@ function pathName()
6162
return $(location).attr('pathname').replace(/[^-A-Za-z0-9+&@#/%?=~_|!:,.;\(\)]/g, '');
6263
}
6364

64-
function localStorageSupported()
65-
{
66-
try {
67-
return 'localStorage' in window && window['localStorage'] !== null && window.localStorage.getItem;
68-
}
69-
catch(e) {
70-
return false;
71-
}
72-
}
73-
7465
function storeLink(link)
7566
{
76-
if (!$("#nav-sync").hasClass('sync') && localStorageSupported()) {
77-
window.localStorage.setItem('navpath',link);
67+
if (!$("#nav-sync").hasClass('sync')) {
68+
Cookie.writeSetting(navpath_cookie_name,link);
7869
}
7970
}
8071

8172
function deleteLink()
8273
{
83-
if (localStorageSupported()) {
84-
window.localStorage.setItem('navpath','');
85-
}
74+
Cookie.writeSetting(navpath_cookie_name,'');
8675
}
8776

8877
function cachedLink()
8978
{
90-
if (localStorageSupported()) {
91-
return window.localStorage.getItem('navpath');
92-
} else {
93-
return '';
94-
}
79+
return Cookie.readSetting(navpath_cookie_name,'');
9580
}
9681

9782
function getScript(scriptName,func)
@@ -506,16 +491,14 @@ function initNavTree(toroot,relpath)
506491
o.node.plus_img.className = 'arrow';
507492
o.node.plus_img.innerHTML = arrowRight;
508493

509-
if (localStorageSupported()) {
510-
var navSync = $('#nav-sync');
511-
if (cachedLink()) {
512-
showSyncOff(navSync,relpath);
513-
navSync.removeClass('sync');
514-
} else {
515-
showSyncOn(navSync,relpath);
516-
}
517-
navSync.click(function(){ toggleSyncButton(relpath); });
494+
var navSync = $('#nav-sync');
495+
if (cachedLink()) {
496+
showSyncOff(navSync,relpath);
497+
navSync.removeClass('sync');
498+
} else {
499+
showSyncOn(navSync,relpath);
518500
}
501+
navSync.click(function(){ toggleSyncButton(relpath); });
519502

520503
if (loadTriggered) { // load before ready
521504
navTo(o,toroot,hashUrl(),relpath);

templates/html/resize.js

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,12 @@
2222
2323
@licend The above is the entire license notice for the JavaScript code in this file
2424
*/
25+
2526
var once=1;
2627
function initResizable()
2728
{
28-
var cookie_namespace = 'doxygen';
2929
var sidenav,navtree,content,header,barWidth=6,desktop_vp=768,titleHeight;
30-
31-
function readSetting(cookie)
32-
{
33-
if (window.chrome) {
34-
var val = localStorage.getItem(cookie_namespace+'_width');
35-
if (val) return val;
36-
} else {
37-
var myCookie = cookie_namespace+"_"+cookie+"=";
38-
if (document.cookie) {
39-
var index = document.cookie.indexOf(myCookie);
40-
if (index != -1) {
41-
var valStart = index + myCookie.length;
42-
var valEnd = document.cookie.indexOf(";", valStart);
43-
if (valEnd == -1) {
44-
valEnd = document.cookie.length;
45-
}
46-
var val = document.cookie.substring(valStart, valEnd);
47-
return val;
48-
}
49-
}
50-
}
51-
return $TREEVIEW_WIDTH;
52-
}
53-
54-
function writeSetting(cookie, val)
55-
{
56-
if (window.chrome) {
57-
localStorage.setItem(cookie_namespace+"_width",val);
58-
} else {
59-
var date = new Date();
60-
date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
61-
expiration = date.toGMTString();
62-
document.cookie = cookie_namespace + "_" + cookie + "=" + val + "; SameSite=Lax; expires=" + expiration+"; path=/";
63-
}
64-
}
30+
var resize_cookie_name = 'width';
6531

6632
function resizeWidth()
6733
{
@@ -71,7 +37,7 @@ function initResizable()
7137
if (typeof page_layout!=='undefined' && page_layout==1) {
7238
footer.css({marginLeft:parseInt(sidenavWidth)+"px"});
7339
}
74-
writeSetting('width',sidenavWidth-barWidth);
40+
Cookie.writeSetting(resize_cookie_name,sidenavWidth-barWidth);
7541
}
7642

7743
function restoreWidth(navWidth)
@@ -114,12 +80,12 @@ function initResizable()
11480
newWidth=0;
11581
}
11682
else {
117-
var width = readSetting('width');
83+
var width = Cookie.readSetting(resize_cookie_name,$TREEVIEW_WIDTH);
11884
newWidth = (width>$TREEVIEW_WIDTH && width<$(window).width()) ? width : $TREEVIEW_WIDTH;
11985
}
12086
restoreWidth(newWidth);
12187
var sidenavWidth = $(sidenav).outerWidth();
122-
writeSetting('width',sidenavWidth-barWidth);
88+
Cookie.writeSetting(resize_cookie_name,sidenavWidth-barWidth);
12389
}
12490

12591
header = $("#top");
@@ -138,7 +104,7 @@ function initResizable()
138104
$('#nav-sync').css({ right:'34px' });
139105
barWidth=20;
140106
}
141-
var width = readSetting('width');
107+
var width = Cookie.readSetting(resize_cookie_name,$TREEVIEW_WIDTH);
142108
if (width) { restoreWidth(width); } else { resizeWidth(); }
143109
resizeHeight();
144110
var url = location.href;

templates/html/search.js

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
2323
@licend The above is the entire license notice for the JavaScript code in this file
2424
*/
25-
var cookie_namespace = 'doxygen';
26-
var cookie_name = '_search_grp';
25+
var search_cookie_name = 'search_grp';
2726

2827
function convertToId(search)
2928
{
@@ -274,7 +273,7 @@ function SearchBox(name, resultsPath, extension)
274273
if (j==id)
275274
{
276275
node.innerHTML='&#8226;';
277-
writeSetting(cookie_name, child.childNodes[1].nodeValue)
276+
Cookie.writeSetting(search_cookie_name, child.childNodes[1].nodeValue)
278277
}
279278
else
280279
{
@@ -830,45 +829,10 @@ function createResults(resultsPath)
830829
}
831830
}
832831

833-
function writeSetting(cookie, val)
834-
{
835-
if (window.chrome) {
836-
localStorage.setItem(cookie_namespace+cookie,val);
837-
} else {
838-
var date = new Date();
839-
date.setTime(date.getTime()+(1*60*60*1000)); // default expiration is one hour
840-
expiration = date.toGMTString();
841-
document.cookie = cookie_namespace + cookie + "=" + val + "; SameSite=Lax; expires=" + expiration+"; path=/";
842-
}
843-
}
844-
845832
function init_search()
846833
{
847834
var results = document.getElementById("MSearchSelectWindow");
848835

849-
function readSetting(cookie)
850-
{
851-
if (window.chrome) {
852-
var val = localStorage.getItem(cookie_namespace+cookie);
853-
if (val) return val;
854-
} else {
855-
var myCookie = cookie_namespace+cookie+"=";
856-
if (document.cookie) {
857-
var index = document.cookie.indexOf(myCookie);
858-
if (index != -1) {
859-
var valStart = index + myCookie.length;
860-
var valEnd = document.cookie.indexOf(";", valStart);
861-
if (valEnd == -1) {
862-
valEnd = document.cookie.length;
863-
}
864-
var val = document.cookie.substring(valStart, valEnd);
865-
return val;
866-
}
867-
}
868-
}
869-
return 0;
870-
}
871-
872836
results.tabIndex=0;
873837
for (var key in indexSectionLabels)
874838
{
@@ -894,7 +858,7 @@ function init_search()
894858
}
895859
}
896860
});
897-
var name = readSetting(cookie_name);
861+
var name = Cookie.readSetting(search_cookie_name,0);
898862
var id = searchBox.GetSelectionIdByName(name);
899863
searchBox.OnSelectItem(id);
900864
}

0 commit comments

Comments
 (0)