Skip to content

Commit

Permalink
Javascript persistency improvements
Browse files Browse the repository at this point in the history
- Use session storage for Chromium based browsers when using session cookies for Firefox.
- Store per project cookies (based on PROJECT_NAME + PROJECT_NUMBER setting in the config file)
  • Loading branch information
doxygen committed Oct 31, 2023
1 parent 173c944 commit 2f3a72e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 17 deletions.
16 changes: 12 additions & 4 deletions deps/jquery/cookie.js
Expand Up @@ -8,7 +8,8 @@ let Cookie = {

readSetting(cookie,defVal) {
if (window.chrome) {
const val = localStorage.getItem(this.cookie_namespace+cookie);
const val = localStorage.getItem(this.cookie_namespace+cookie) ||
sessionStorage.getItem(this.cookie_namespace+cookie);
if (val) return val;
} else {
let myCookie = this.cookie_namespace+cookie+"=";
Expand All @@ -29,7 +30,11 @@ let Cookie = {

writeSetting(cookie,val,days=10*365) { // default days='forever', 0=session cookie, -1=delete
if (window.chrome) {
localStorage.setItem(this.cookie_namespace+cookie,val);
if (days==0) {
sessionStorage.setItem(this.cookie_namespace+cookie,val);
} else {
localStorage.setItem(this.cookie_namespace+cookie,val);
}
} else {
let date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
Expand All @@ -41,8 +46,11 @@ let Cookie = {

eraseSetting(cookie) {
if (window.chrome) {
const val = localStorage.removeItem(this.cookie_namespace+cookie);
if (val) return val;
if (localStorage.getItem(this.cookie_namespace+cookie)) {
localStorage.removeItem(this.cookie_namespace+cookie);
} else if (sessionStorage.getItem(this.cookie_namespace+cookie)) {
sessionStorage.removeItem(this.cookie_namespace+cookie);
}
} else {
this.writeSetting(cookie,'',-1);
}
Expand Down
6 changes: 5 additions & 1 deletion src/ftvhelp.cpp
Expand Up @@ -835,6 +835,7 @@ static void generateJSNavTree(const FTVNodes &nodeList)
t << "\nvar SYNCONMSG = '" << theTranslator->trPanelSynchronisationTooltip(FALSE) << "';";
t << "\nvar SYNCOFFMSG = '" << theTranslator->trPanelSynchronisationTooltip(TRUE) << "';";
}

ResourceMgr::instance().copyResource("navtree.js",htmlOutput);
}

Expand Down Expand Up @@ -870,7 +871,10 @@ void FTVHelp::generateTreeViewScripts()
if (f.is_open())
{
TextStream t(&f);
t << substitute(mgr.getAsString("resize.js"), "$TREEVIEW_WIDTH", QCString().setNum(Config_getInt(TREEVIEW_WIDTH)));
t << substitute(
substitute(mgr.getAsString("resize.js"),
"$TREEVIEW_WIDTH", QCString().setNum(Config_getInt(TREEVIEW_WIDTH))),
"$PROJECTID", getProjectId());
}
}
{
Expand Down
3 changes: 2 additions & 1 deletion src/htmlgen.cpp
Expand Up @@ -1219,7 +1219,8 @@ void HtmlGenerator::init()
if (f.is_open())
{
TextStream t(&f);
t << replaceColorMarkers(mgr.getAsString("darkmode_toggle.js"));
t << substitute(replaceColorMarkers(mgr.getAsString("darkmode_toggle.js")),
"$PROJECTID",getProjectId());
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/util.cpp
Expand Up @@ -6987,3 +6987,13 @@ QCString detab(const QCString &s,int &refIndent)
out.addChar(0);
return out.get();
}

QCString getProjectId()
{
QCString projectName = Config_getString(PROJECT_NAME)+" "+Config_getString(PROJECT_NUMBER);
uint8_t md5_sig[16];
char sigStr[33];
MD5Buffer(projectName.data(),projectName.length(),md5_sig);
MD5SigToString(md5_sig,sigStr);
return sigStr;
}
2 changes: 2 additions & 0 deletions src/util.h
Expand Up @@ -472,4 +472,6 @@ inline QCString fixSpaces(const QCString &s) { return substitute(s," ","&#160;")

QCString detab(const QCString &s,int &refIndent);

QCString getProjectId();

#endif
4 changes: 2 additions & 2 deletions templates/html/darkmode_toggle.js
Expand Up @@ -29,8 +29,8 @@ SOFTWARE.

class DarkModeToggle extends HTMLElement {

static darkmode_cookie_name = 'prefers-dark';
static lightmode_cookie_name = 'prefers-light';
static darkmode_cookie_name = '$PROJECTID'+'_prefers-dark';
static lightmode_cookie_name = '$PROJECTID'+'_prefers-light';

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>';
static title = "Toggle Light/Dark Mode"
Expand Down
16 changes: 12 additions & 4 deletions templates/html/jquery.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions templates/html/resize.js
Expand Up @@ -26,15 +26,15 @@
let once=1;
function initResizable() {
let sidenav,navtree,content,header,footer,barWidth=6;
const resize_cookie_name = 'width';
const RESIZE_COOKIE_NAME = '$PROJECTID'+'_width';

function resizeWidth() {
const sidenavWidth = $(sidenav).outerWidth();
content.css({marginLeft:parseInt(sidenavWidth)+"px"});
if (typeof page_layout!=='undefined' && page_layout==1) {
footer.css({marginLeft:parseInt(sidenavWidth)+"px"});
}
Cookie.writeSetting(resize_cookie_name,sidenavWidth-barWidth);
Cookie.writeSetting(RESIZE_COOKIE_NAME,sidenavWidth-barWidth);
}

function restoreWidth(navWidth) {
Expand Down Expand Up @@ -72,12 +72,12 @@ function initResizable() {
if (sidenav.width()>0) {
newWidth=0;
} else {
const width = Cookie.readSetting(resize_cookie_name,$TREEVIEW_WIDTH);
const width = Cookie.readSetting(RESIZE_COOKIE_NAME,$TREEVIEW_WIDTH);
newWidth = (width>$TREEVIEW_WIDTH && width<$(window).width()) ? width : $TREEVIEW_WIDTH;
}
restoreWidth(newWidth);
const sidenavWidth = $(sidenav).outerWidth();
Cookie.writeSetting(resize_cookie_name,sidenavWidth-barWidth);
Cookie.writeSetting(RESIZE_COOKIE_NAME,sidenavWidth-barWidth);
}

header = $("#top");
Expand All @@ -96,7 +96,7 @@ function initResizable() {
$('#nav-sync').css({ right:'34px' });
barWidth=20;
}
const width = Cookie.readSetting(resize_cookie_name,$TREEVIEW_WIDTH);
const width = Cookie.readSetting(RESIZE_COOKIE_NAME,$TREEVIEW_WIDTH);
if (width) { restoreWidth(width); } else { resizeWidth(); }
resizeHeight();
const url = location.href;
Expand Down

0 comments on commit 2f3a72e

Please sign in to comment.