Skip to content

Commit

Permalink
Make session storage theme-specific.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Jan 3, 2025
1 parent fd49afb commit f8df2d6
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 25 deletions.
1 change: 1 addition & 0 deletions module/VuFindTheme/src/VuFindTheme/ThemeInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ protected function loadThemeConfig($theme)
{
// Load theme configuration...
$this->allThemeInfo[$theme] = include $this->getThemeConfig($theme);
$this->allThemeInfo[$theme]['themeName'] = $theme;
// ..and if there are mixins, load those too!
if (isset($this->allThemeInfo[$theme]['mixins'])) {
foreach ($this->allThemeInfo[$theme]['mixins'] as $mix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function testStandardCompilation()

// Did the configuration merge correctly?
$expectedConfig = [
'themeName' => 'child',
'extends' => false,
'css' => ['child.css'],
'js' => ['hello.js', 'extra.js'],
Expand Down Expand Up @@ -171,6 +172,7 @@ public function testStandardCompilationWithMixin()

// Did the configuration merge correctly?
$expectedConfig = [
'themeName' => 'mixin_user',
'extends' => false,
'css' => ['child.css'],
'js' => ['hello.js', 'extra.js', 'mixin.js'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public function testGetThemeInfo()
$ti = $this->getThemeInfo();
$ti->setTheme('child');
$expectedChild = include "{$this->fixturePath}/child/theme.config.php";
$expectedChild['themeName'] = 'child';
$expectedParent = include "{$this->fixturePath}/parent/theme.config.php";
$expectedParent['themeName'] = 'parent';
$this->assertEquals('parent', $expectedChild['extends']);
$this->assertEquals(false, $expectedParent['extends']);
$this->assertEquals(
Expand All @@ -129,10 +131,12 @@ public function testGetThemeInfoWithMixin()
$ti = $this->getThemeInfo();
$ti->setTheme('mixin_user');
$expectedChild = include "{$this->fixturePath}/child/theme.config.php";
$expectedChild['themeName'] = 'child';
$expectedParent = include "{$this->fixturePath}/parent/theme.config.php";
$expectedParent['themeName'] = 'parent';
$expectedMixin = include "{$this->fixturePath}/mixin/mixin.config.php";
$expectedMixinUser
= include "{$this->fixturePath}/mixin_user/theme.config.php";
$expectedMixinUser = include "{$this->fixturePath}/mixin_user/theme.config.php";
$expectedMixinUser['themeName'] = 'mixin_user';
$this->assertEquals('parent', $expectedChild['extends']);
$this->assertEquals(false, $expectedParent['extends']);
$this->assertEquals(
Expand Down Expand Up @@ -307,7 +311,7 @@ public function testGetMergedConfigNoKey()
$config = $ti->getMergedConfig();
$this->assertEquals('HTML5', $config['doctype']);
$this->assertEqualsCanonicalizing(
['doctype', 'extends', 'js', 'helpers'],
['doctype', 'extends', 'js', 'helpers', 'themeName'],
array_keys($config)
);
}
Expand Down
44 changes: 35 additions & 9 deletions themes/bootstrap3/js/account_ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,42 @@ VuFind.register('account', function Account() {

var _submodules = [];
var _clearCaches = false;
var _sessionDataPrefix = "vf-account-status-";
var _sessionDataPrefix = "vf-account-status-2-";

var _save = function _save(module) {
var _getStorageKey = function _getStorageKey(module) {
return _sessionDataPrefix + module;
};

var _loadSessionData = function _loadSessionData(module) {
var theme = VuFind.getTheme();
var json = sessionStorage.getItem(_getStorageKey(module));
if (null !== json) {
var data = JSON.parse(json);
if (typeof data[theme] !== 'undefined') {
return data[theme];
}
}
return null;
};

var _saveSessionData = function _saveSessionData(module) {
var theme = VuFind.getTheme();
var json = sessionStorage.getItem(_getStorageKey(module));
var data = {};
if (null !== json) {
data = JSON.parse(json) || {};
}
data[theme] = _statuses[module];
sessionStorage.setItem(
_sessionDataPrefix + module,
JSON.stringify(_statuses[module])
_getStorageKey(module),
JSON.stringify(data)
);
};

var _clearSessionData = function _clearSessionData(module) {
sessionStorage.removeItem(_getStorageKey(module));
};

// Forward declaration for clearAllCaches
var clearAllCaches = function clearAllCachesForward() {};

Expand All @@ -40,7 +67,7 @@ VuFind.register('account', function Account() {
if (typeof name === "undefined" || name === '') {
clearAllCaches();
} else {
sessionStorage.removeItem(_sessionDataPrefix + name);
_clearSessionData(name);
}
};

Expand Down Expand Up @@ -119,21 +146,20 @@ VuFind.register('account', function Account() {
_statuses[module] = MISSING;
})
.always(function ajaxLookupAlways() {
_save(module);
_saveSessionData(module);
_render();
});
};

var _load = function _load(module) {
if (_clearCaches) {
sessionStorage.removeItem(_sessionDataPrefix + module);
_clearSessionData(module);
}
var $element = $(_submodules[module].selector);
if (!$element) {
_statuses[module] = INACTIVE;
} else {
var json = sessionStorage.getItem(_sessionDataPrefix + module);
var session = typeof json === "undefined" ? null : JSON.parse(json);
var session = _loadSessionData(module);
if (
session === null ||
session === LOADING ||
Expand Down
13 changes: 12 additions & 1 deletion themes/bootstrap3/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var VuFind = (function VuFind() {
var _submodules = [];
var _cspNonce = '';
var _searchId = null;
var _theme = null;

var _icons = {};
var _translations = {};
Expand Down Expand Up @@ -439,6 +440,14 @@ var VuFind = (function VuFind() {
_searchId = searchId;
};

var getTheme = function getTheme() {
return _theme;
};

var setTheme = function setTheme(theme) {
_theme = theme;
};

function setupQRCodeLinks(_container) {
var container = _container || document.body;
var qrcodeLinks = container.querySelectorAll('a.qrcodeLink');
Expand Down Expand Up @@ -623,7 +632,9 @@ var VuFind = (function VuFind() {
restoreTransitions: restoreTransitions,
inURLSearchParams: inURLSearchParams,
deleteKeyValueFromURLSearchParams: deleteKeyValueFromURLSearchParams,
deleteParamsFromURLSearchParams: deleteParamsFromURLSearchParams
deleteParamsFromURLSearchParams: deleteParamsFromURLSearchParams,
getTheme,
setTheme
};
})();

Expand Down
6 changes: 5 additions & 1 deletion themes/bootstrap3/templates/layout/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
// Collect small scripts together and append as one block:
$appendScripts = [];

if (!empty($this->themeConfig()['stickyElements'])) {
$themeConfig = $this->themeConfig();
if (!empty($themeConfig['stickyElements'])) {
$this->headScript()->appendFile('sticky_elements.js');
}

Expand Down Expand Up @@ -98,6 +99,8 @@
$dsb = DEFAULT_SEARCH_BACKEND;
$cspNonce = $this->csp()->getNonce();
$searchId = json_encode($this->layout()->searchId);
$theme = json_encode($themeConfig['themeName']);

$appendScripts[] = <<<JS
VuFind.path = '{$root}';
VuFind.defaultSearchBackend = '{$dsb}';
Expand All @@ -106,6 +109,7 @@
VuFind.addIcons({$icons});
VuFind.setCspNonce('{$cspNonce}');
VuFind.setCurrentSearchId($searchId);
VuFind.setTheme($theme);
JS;

if ($domain = $this->cookieManager()->getDomain()) {
Expand Down
44 changes: 35 additions & 9 deletions themes/bootstrap5/js/account_ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,42 @@ VuFind.register('account', function Account() {

var _submodules = [];
var _clearCaches = false;
var _sessionDataPrefix = "vf-account-status-";
var _sessionDataPrefix = "vf-account-status-2-";

var _save = function _save(module) {
var _getStorageKey = function _getStorageKey(module) {
return _sessionDataPrefix + module;
};

var _loadSessionData = function _loadSessionData(module) {
var theme = VuFind.getTheme();
var json = sessionStorage.getItem(_getStorageKey(module));
if (null !== json) {
var data = JSON.parse(json);
if (typeof data[theme] !== 'undefined') {
return data[theme];
}
}
return null;
};

var _saveSessionData = function _saveSessionData(module) {
var theme = VuFind.getTheme();
var json = sessionStorage.getItem(_getStorageKey(module));
var data = {};
if (null !== json) {
data = JSON.parse(json) || {};
}
data[theme] = _statuses[module];
sessionStorage.setItem(
_sessionDataPrefix + module,
JSON.stringify(_statuses[module])
_getStorageKey(module),
JSON.stringify(data)
);
};

var _clearSessionData = function _clearSessionData(module) {
sessionStorage.removeItem(_getStorageKey(module));
};

// Forward declaration for clearAllCaches
var clearAllCaches = function clearAllCachesForward() {};

Expand All @@ -40,7 +67,7 @@ VuFind.register('account', function Account() {
if (typeof name === "undefined" || name === '') {
clearAllCaches();
} else {
sessionStorage.removeItem(_sessionDataPrefix + name);
_clearSessionData(name);
}
};

Expand Down Expand Up @@ -120,21 +147,20 @@ VuFind.register('account', function Account() {
_statuses[module] = MISSING;
})
.always(function ajaxLookupAlways() {
_save(module);
_saveSessionData(module);
_render();
});
};

var _load = function _load(module) {
if (_clearCaches) {
sessionStorage.removeItem(_sessionDataPrefix + module);
_clearSessionData(module);
}
var $element = $(_submodules[module].selector);
if (!$element) {
_statuses[module] = INACTIVE;
} else {
var json = sessionStorage.getItem(_sessionDataPrefix + module);
var session = typeof json === "undefined" ? null : JSON.parse(json);
var session = _loadSessionData(module);
if (
session === null ||
session === LOADING ||
Expand Down
13 changes: 12 additions & 1 deletion themes/bootstrap5/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var VuFind = (function VuFind() {
var _submodules = [];
var _cspNonce = '';
var _searchId = null;
var _theme = null;

var _icons = {};
var _translations = {};
Expand Down Expand Up @@ -439,6 +440,14 @@ var VuFind = (function VuFind() {
_searchId = searchId;
};

var getTheme = function getTheme() {
return _theme;
};

var setTheme = function setTheme(theme) {
_theme = theme;
};

function setupQRCodeLinks(_container) {
var container = _container || document.body;
var qrcodeLinks = container.querySelectorAll('a.qrcodeLink');
Expand Down Expand Up @@ -623,7 +632,9 @@ var VuFind = (function VuFind() {
restoreTransitions: restoreTransitions,
inURLSearchParams: inURLSearchParams,
deleteKeyValueFromURLSearchParams: deleteKeyValueFromURLSearchParams,
deleteParamsFromURLSearchParams: deleteParamsFromURLSearchParams
deleteParamsFromURLSearchParams: deleteParamsFromURLSearchParams,
getTheme,
setTheme
};
})();

Expand Down
6 changes: 5 additions & 1 deletion themes/bootstrap5/templates/layout/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
// Collect small scripts together and append as one block:
$appendScripts = [];

if (!empty($this->themeConfig()['stickyElements'])) {
$themeConfig = $this->themeConfig();
if (!empty($themeConfig['stickyElements'])) {
$this->headScript()->appendFile('sticky_elements.js');
}

Expand Down Expand Up @@ -98,6 +99,8 @@
$dsb = DEFAULT_SEARCH_BACKEND;
$cspNonce = $this->csp()->getNonce();
$searchId = json_encode($this->layout()->searchId);
$theme = json_encode($themeConfig['themeName']);

$appendScripts[] = <<<JS
VuFind.path = '{$root}';
VuFind.defaultSearchBackend = '{$dsb}';
Expand All @@ -106,6 +109,7 @@
VuFind.addIcons({$icons});
VuFind.setCspNonce('{$cspNonce}');
VuFind.setCurrentSearchId($searchId);
VuFind.setTheme($theme);
JS;

if ($domain = $this->cookieManager()->getDomain()) {
Expand Down

0 comments on commit f8df2d6

Please sign in to comment.