N°8318 - Managing menu in itop portal new look (#710)

This commit is contained in:
Benjamin Dalsass
2025-04-14 16:38:36 +02:00
committed by GitHub
parent 5e6d96019d
commit adc8b47945
8 changed files with 152 additions and 12 deletions

View File

@@ -0,0 +1,21 @@
# Copyright (C) 2010-2024 Combodo SAS
#
# This file is part of iTop.
#
# iTop is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iTop is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with iTop. If not, see <http://www.gnu.org/licenses/>
p_preferences_set_preference:
path: '/preferences/setPreference'
defaults:
_controller: 'Combodo\iTop\Portal\Controller\PreferencesController::SetPreferenceAction'

View File

@@ -245,6 +245,10 @@ class NavigationMenuElement extends HTMLElement {
}
Expand(bSaveUserPreference = false) {
// save user preference
if (bSaveUserPreference) {
oUserPreferences.setPreference('portal.navigation_menu.expanded', 'expanded');
}
// sync attribute
if (this.getAttribute(NavigationMenuElement.DATA_EXPANDED_STATE) !== 'expanded') {
this.setAttribute(NavigationMenuElement.DATA_EXPANDED_STATE, 'expanded');
@@ -257,13 +261,13 @@ class NavigationMenuElement extends HTMLElement {
// dispatch events
window.dispatchEvent(new Event('resize')); // do layout
this.dispatchEvent(new CustomEvent("state", {detail: 'expanded'}));
// save user preference
if (bSaveUserPreference) {
SetUserPreference('portal.navigation_menu.expanded', 'expanded', true);
}
}
Collapse(bSaveUserPreference = false) {
// save user preference
if (bSaveUserPreference) {
oUserPreferences.setPreference('portal.navigation_menu.expanded', 'collapsed');
}
// sync attribute
if (this.getAttribute(NavigationMenuElement.DATA_EXPANDED_STATE) !== 'collapsed') {
this.setAttribute(NavigationMenuElement.DATA_EXPANDED_STATE, 'collapsed');
@@ -276,10 +280,6 @@ class NavigationMenuElement extends HTMLElement {
// dispatch events
window.dispatchEvent(new Event('resize')); // do layout
this.dispatchEvent(new CustomEvent("state", {detail: 'collapsed'}));
// save user preference
if (bSaveUserPreference) {
SetUserPreference('portal.navigation_menu.expanded', 'collapsed', true);
}
}
IsExpanded() {

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2013-2024 Combodo SAS
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
class UserPreferences {
constructor(sUrl) {
this.sUrl = sUrl;
}
setPreference(key, value) {
let $data = new FormData();
$data.append("key", key);
$data.append("value", value);
fetch(this.sUrl, {
method: "POST",
body: $data,
}).then(
(response) => {
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
return response.json();
}
).catch(
(error) => {
console.error('Unable to set user preference:', error);
}
);
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* Copyright (C) 2013-2024 Combodo SAS
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Portal\Controller;
use appUserPreferences;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SymfonyAbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Class PreferencesController
*
* @package Combodo\iTop\Portal\Controller
* @since 3.3.0
*/
class PreferencesController extends SymfonyAbstractController
{
/**
* Set a preference for the current user.
*
* @param \Symfony\Component\HttpFoundation\Request $oRequest
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function SetPreferenceAction(Request $oRequest): JsonResponse
{
$sStatus = 'success';
// retrieve the parameters from the request
$sKey = $oRequest->request->get('key');
$sValue = $oRequest->request->get('value');
// set user preference
try{
appUserPreferences::SetPref($sKey, $sValue);
}
catch(Exception){
$sStatus = 'error';
}
return new JsonResponse([
'status' => $sStatus,
]);
}
}

View File

@@ -160,6 +160,8 @@
<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/clipboard/dist/clipboard.min.js'|add_itop_version }}"></script>
<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/clipboardwidget.js'|add_itop_version }}"></script>
<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/portal-clipboard.js'|add_itop_version }}"></script>
{# User Preferences #}
<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/user_preferences.js'|add_itop_version }}"></script>
{# custom elements #}
<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/custom_elements/base_element.js'|add_itop_version }}"></script>
<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/custom_elements/tile_element.js'|add_itop_version }}"></script>
@@ -279,8 +281,8 @@
{
return '{{ app['url_generator'].generate('p_session_message_add')|raw }}';
};
// used to allow SetUserPreferences to be called from the UI
const oUserPreferences = JSON.parse('{{ user_preferences|json_encode|e('js') }}');
// user preferences object
const oUserPreferences = new UserPreferences('{{ app['url_generator'].generate('p_preferences_set_preference')|raw }}');
/**
* @param sUrl {string} The URL to append the new param to
* @param sParamName {string} Name of the parameter

View File

@@ -25,6 +25,7 @@ return array(
'Combodo\\iTop\\Portal\\Controller\\DefaultController' => $baseDir . '/src/Controller/DefaultController.php',
'Combodo\\iTop\\Portal\\Controller\\ManageBrickController' => $baseDir . '/src/Controller/ManageBrickController.php',
'Combodo\\iTop\\Portal\\Controller\\ObjectController' => $baseDir . '/src/Controller/ObjectController.php',
'Combodo\\iTop\\Portal\\Controller\\PreferencesController' => $baseDir . '/src/Controller/PreferencesController.php',
'Combodo\\iTop\\Portal\\Controller\\SessionMessageController' => $baseDir . '/src/Controller/SessionMessageController.php',
'Combodo\\iTop\\Portal\\Controller\\UserProfileBrickController' => $baseDir . '/src/Controller/UserProfileBrickController.php',
'Combodo\\iTop\\Portal\\DataCollector\\PortalCollector' => $baseDir . '/src/DataCollector/PortalCollector.php',

View File

@@ -45,6 +45,7 @@ class ComposerStaticInitCombodo_ItopPortalBase_Portal
'Combodo\\iTop\\Portal\\Controller\\DefaultController' => __DIR__ . '/../..' . '/src/Controller/DefaultController.php',
'Combodo\\iTop\\Portal\\Controller\\ManageBrickController' => __DIR__ . '/../..' . '/src/Controller/ManageBrickController.php',
'Combodo\\iTop\\Portal\\Controller\\ObjectController' => __DIR__ . '/../..' . '/src/Controller/ObjectController.php',
'Combodo\\iTop\\Portal\\Controller\\PreferencesController' => __DIR__ . '/../..' . '/src/Controller/PreferencesController.php',
'Combodo\\iTop\\Portal\\Controller\\SessionMessageController' => __DIR__ . '/../..' . '/src/Controller/SessionMessageController.php',
'Combodo\\iTop\\Portal\\Controller\\UserProfileBrickController' => __DIR__ . '/../..' . '/src/Controller/UserProfileBrickController.php',
'Combodo\\iTop\\Portal\\DataCollector\\PortalCollector' => __DIR__ . '/../..' . '/src/DataCollector/PortalCollector.php',

View File

@@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '6d8d747575dc9cd4b53dc9592eb646b692d3ea86',
'reference' => 'c203329236dfb3c05aa02ac73ab12dd1fe52143e',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
@@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '6d8d747575dc9cd4b53dc9592eb646b692d3ea86',
'reference' => 'c203329236dfb3c05aa02ac73ab12dd1fe52143e',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),