Move login nav menu functions into class

Because we need translations, so we use a class where we require the translation class to be in the constructor

Code fix for i18n variable not set check and default fill for the translation class
This commit is contained in:
2025-03-07 15:26:38 +09:00
parent 55972b4ab7
commit f9ffcd3327
7 changed files with 172 additions and 209 deletions

109
src/utils/LoginNavMenu.mjs Normal file
View File

@@ -0,0 +1,109 @@
/*
Description: Login access and menu
Date: 2025//3/6
Creator: Clemens Schwaighofer
*/
export { LoginNavMenu };
import { isObject, getObjectCount } from './JavaScriptHelpers.mjs';
import { exists } from './DomHelpers.mjs';
class LoginNavMenu {
hec;
l10n;
/**
* action box creator
* @param {Object} hec HtmlElementCreator
* @param {Object} l10n l10nTranslation
*/
constructor(hec, l10n)
{
this.hec = hec;
this.l10n = l10n;
}
/**
* create login string and logout button elements
* @param {String} login_string the login string to show on the left
* @param {String} [header_id='mainHeader'] the target for the main element block
* if not set mainHeader is assumed
* this is the target div for the "loginRow"
*/
createLoginRow(login_string, header_id = 'mainHeader')
{
// if header does not exist, we do nothing
if (exists(header_id)) {
// that row must exist already, if not it must be the first in the "mainHeader"
if (!exists('loginRow')) {
$('#' + header_id).html(this.hec.phfo(this.hec.cel('div', 'loginRow', '', ['loginRow', 'flx-spbt'])));
}
// clear out just in case for first entry
// fill with div name & login/logout button
$('#loginRow').html(this.hec.phfo(this.hec.cel('div', 'loginRow-name', login_string)));
$('#loginRow').append(this.hec.phfo(this.hec.cel('div', 'loginRow-info', '')));
$('#loginRow').append(this.hec.phfo(
this.hec.aelx(
// outer div
this.hec.cel('div', 'loginRow-logout'),
// inner element
this.hec.cel('input', 'logout', '', [], {
value: this.l10n.__('Logout'),
type: 'button',
onClick: 'loginLogout()'
})
)
));
}
}
/**
* create the top nav menu that switches physical between pages
* (edit access data based)
* @param {Object} nav_menu the built nav menu with highlight info
* @param {String} [header_id='mainHeader'] the target for the main element block
* if not set mainHeader is assumed
* this is the target div for the "menuRow"
*/
createNavMenu(nav_menu, header_id = 'mainHeader')
{
// must be an object
if (isObject(nav_menu) && getObjectCount(nav_menu) > 1) {
// do we have more than one entry, if not, do not show (single page)
if (!exists('menuRow')) {
$('#' + header_id).html(this.hec.phfo(this.hec.cel('div', 'menuRow', '', ['menuRow', 'flx-s'])));
}
var content = [];
$.each(nav_menu, function(key, item) {
// key is number
// item is object with entries
if (key != 0) {
content.push(this.hec.phfo(this.hec.cel('div', '', '·', ['pd-2'])));
}
// ignore item.popup for now
if (item.enabled) {
// set selected based on window.location.href as the php set will not work
if (window.location.href.indexOf(item.url) != -1) {
item.selected = 1;
}
// create the entry
content.push(this.hec.phfo(
this.hec.aelx(
this.hec.cel('div'),
this.hec.cel('a', '', item.name, ['pd-2'].concat(item.selected ? 'highlight': ''), {
href: item.url
})
)
));
}
});
$('#menuRow').html(content.join(''));
} else {
$('#menuRow').hide();
}
}
}
// __END__