6 changed files with 139 additions and 97 deletions
@ -1,55 +1,109 @@ |
|||||
// Utility function to apply CSS styles and class modifications
|
/** @odoo-module **/ |
||||
function adjustLayoutOnSidebarToggle(isOpen) { |
|
||||
const actionManager = $(".o_action_manager"); |
import { NavBar } from '@web/webclient/navbar/navbar'; |
||||
const sidebarIcon = $(".side_bar_icon"); |
import { patch } from "@web/core/utils/patch"; |
||||
const navbar = $(".o_main_navbar"); |
|
||||
const topHeading = $(".top_heading"); |
import {Component, onWillDestroy, useExternalListener, onMounted, useRef, onWillUnmount,} from "@odoo/owl"; |
||||
|
|
||||
if (isOpen) { |
patch(NavBar.prototype, { |
||||
$("#sidebar_panel").show(); |
setup() { |
||||
if (window.matchMedia("(min-width: 768px)").matches) { |
super.setup(); |
||||
actionManager.css({ 'margin-left': '200px', 'transition': 'all .1s linear' }); |
|
||||
sidebarIcon.css({ 'margin-left': '200px', 'transition': 'all .1s linear' }); |
this.openElement = useRef("openSidebar"); |
||||
|
this.closeElement = useRef("closeSidebar"); |
||||
|
this.topHeading = useRef("sidebar_icon"); |
||||
|
this.sidebarLinks = useRef("sidebarLinks"); |
||||
|
const sidebarLinkHandler = this.handleSidebarLinkClick.bind(this); |
||||
|
const openSidebarHandler = this.openSidebar.bind(this); |
||||
|
const closeSidebarHandler = this.closeSidebar.bind(this); |
||||
|
onMounted(() => { |
||||
|
const openSidebarElement = this.openElement.el |
||||
|
const closeSidebarElement = this.closeElement.el |
||||
|
const sidebarLinkElements = this.sidebarLinks.el.children; |
||||
|
if (sidebarLinkElements) { |
||||
|
Array.from(sidebarLinkElements).forEach(link => { |
||||
|
link.addEventListener('click', sidebarLinkHandler); |
||||
|
}); |
||||
|
} |
||||
|
if (openSidebarElement) { |
||||
|
openSidebarElement.addEventListener('click', openSidebarHandler); |
||||
|
} |
||||
|
if (closeSidebarElement) { |
||||
|
closeSidebarElement.addEventListener('click', closeSidebarHandler); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
onWillUnmount(() => { |
||||
|
const openSidebarElement = this.openElement.el |
||||
|
const closeSidebarElement = this.closeElement.el |
||||
|
const sidebarLinkElements = this.sidebarLinks.el.children; |
||||
|
if (openSidebarElement) { |
||||
|
openSidebarElement.removeEventListener('click', openSidebarHandler); |
||||
|
} |
||||
|
if (closeSidebarElement) { |
||||
|
closeSidebarElement.removeEventListener('click', closeSidebarHandler); |
||||
|
} |
||||
|
if (sidebarLinkElements) { |
||||
|
Array.from(sidebarLinkElements).forEach(link => { |
||||
|
link.removeEventListener('click', sidebarLinkHandler); |
||||
|
}); |
||||
|
} |
||||
|
}); |
||||
|
}, |
||||
|
|
||||
|
openSidebar() { |
||||
|
this.root.el.nextElementSibling.style.marginLeft = '200px'; |
||||
|
this.root.el.nextElementSibling.style.transition = 'all .1s linear'; |
||||
|
const openSidebarElement = this.openElement.el |
||||
|
const closeSidebarElement = this.closeElement.el |
||||
|
if (openSidebarElement) openSidebarElement.style.display = 'none'; |
||||
|
if (closeSidebarElement) closeSidebarElement.style.display = 'block'; |
||||
|
if (this.root.el.lastChild && this.root.el.lastChild.nodeType === Node.ELEMENT_NODE) { |
||||
|
this.root.el.lastChild.style.display = 'block'; |
||||
|
} |
||||
|
|
||||
|
if (this.topHeading.el && this.topHeading.el.nodeType === Node.ELEMENT_NODE) { |
||||
|
this.topHeading.el.style.marginLeft = '200px'; |
||||
|
this.topHeading.el.style.transition = 'all .1s linear'; |
||||
|
this.topHeading.el.style.width = 'auto'; |
||||
|
} |
||||
|
}, |
||||
|
|
||||
|
closeSidebar() { |
||||
|
console.log('Sidebar closed', this.topHeading); |
||||
|
this.root.el.nextElementSibling.style.marginLeft = '0px'; |
||||
|
this.root.el.nextElementSibling.style.transition = 'all .1s linear'; |
||||
|
const openSidebarElement = this.openElement.el |
||||
|
const closeSidebarElement = this.closeElement.el |
||||
|
if (openSidebarElement) openSidebarElement.style.display = 'block'; |
||||
|
if (closeSidebarElement) closeSidebarElement.style.display = 'none'; |
||||
|
if (this.root.el.lastChild && this.root.el.lastChild.nodeType === Node.ELEMENT_NODE) { |
||||
|
this.root.el.lastChild.style.display = 'none'; |
||||
|
} |
||||
|
if (this.topHeading.el && this.topHeading.el.nodeType === Node.ELEMENT_NODE) { |
||||
|
this.topHeading.el.style.marginLeft = '0px'; |
||||
|
this.topHeading.el.style.width = 'auto'; |
||||
|
} |
||||
|
}, |
||||
|
|
||||
|
handleSidebarLinkClick(event) { |
||||
|
const closeSidebarElement = this.closeElement.el |
||||
|
if (closeSidebarElement) closeSidebarElement.style.display = 'none'; |
||||
|
if (this.topHeading.el && this.topHeading.el.nodeType === Node.ELEMENT_NODE) { |
||||
|
this.topHeading.el.style.marginLeft = '0px'; |
||||
|
} |
||||
|
if (this.topHeading.el && this.topHeading.el.nodeType === Node.ELEMENT_NODE) { |
||||
|
this.topHeading.el.style.marginLeft = '0px'; |
||||
|
this.topHeading.el.style.width = '100%'; |
||||
} |
} |
||||
navbar.addClass("small_nav").addClass(navbar.data("id")); |
const li = event.currentTarget; |
||||
actionManager.addClass("sidebar_margin").addClass(actionManager.data("id")); |
const a = li.firstElementChild; |
||||
topHeading.addClass("sidebar_margin").addClass(topHeading.data("id")); |
const id = a.getAttribute('data-id'); |
||||
} else { |
document.querySelector('header').className = id; |
||||
$("#sidebar_panel").hide(); |
Array.from(this.sidebarLinks.el.children).forEach(li => { |
||||
actionManager.css({ 'margin-left': '0px' }); |
li.firstElementChild.classList.remove('active'); |
||||
sidebarIcon.css({ 'margin-left': '0px' }); |
}); |
||||
navbar.removeClass("small_nav").removeClass(navbar.data("id")); |
a.classList.add('active'); |
||||
actionManager.removeClass("sidebar_margin").removeClass(actionManager.data("id")); |
this.closeSidebar(); |
||||
topHeading.removeClass("sidebar_margin").removeClass(topHeading.data("id")); |
|
||||
} |
} |
||||
} |
}); |
||||
|
|
||||
// Toggle sidebar visibility on click
|
|
||||
$(document).on("click", "#openSidebar", () => { |
|
||||
$("#openSidebar").hide(); |
|
||||
$("#closeSidebar").show(); |
|
||||
adjustLayoutOnSidebarToggle(true); |
|
||||
}); |
|
||||
|
|
||||
$(document).on("click", "#closeSidebar", () => { |
|
||||
$("#closeSidebar").hide(); |
|
||||
$("#openSidebar").show(); |
|
||||
adjustLayoutOnSidebarToggle(false); |
|
||||
}); |
|
||||
|
|
||||
// Handle menu item clicks
|
|
||||
$(document).on("click", ".sidebar a", function () { |
|
||||
const $this = $(this); |
|
||||
const menuItems = $(".sidebar a"); |
|
||||
|
|
||||
menuItems.removeClass("active"); |
|
||||
$this.addClass("active"); |
|
||||
|
|
||||
// Adjust the header to reflect the active menu
|
|
||||
$("header").removeClass().addClass($this.data("id")); |
|
||||
|
|
||||
// Close sidebar after menu item selection
|
|
||||
adjustLayoutOnSidebarToggle(false); |
|
||||
$("#closeSidebar").hide(); |
|
||||
$("#openSidebar").show(); |
|
||||
}); |
|
@ -1,26 +0,0 @@ |
|||||
<?xml version="1.0" encoding="UTF-8"?> |
|
||||
<templates id="template" xml:space="preserve"> |
|
||||
<!-- Extend the web.NavBar template to add a sidebar --> |
|
||||
<t t-name="code_backend_theme_enterprice.sidebar" t-inherit="web.NavBar" t-inherit-mode="extension" owl="1"> |
|
||||
<!-- Add the sidebar panel right after the navbar --> |
|
||||
<xpath expr="//nav[hasclass('o_main_navbar')]" position="after"> |
|
||||
<div class="sidebar_panel" id="sidebar_panel"> |
|
||||
<div class="sidebar"> |
|
||||
<!-- Sidebar close button --> |
|
||||
<div class="sidebar_close"> |
|
||||
<a id="closeSidebar" style="cursor: pointer;"> |
|
||||
<img src="/code_backend_theme_enterprise/static/src/img/icons/close.png"/> |
|
||||
</a> |
|
||||
</div> |
|
||||
|
|
||||
<!-- Sidebar logo --> |
|
||||
<div class="sidebar_logo"> |
|
||||
<img src="/web/binary/company_logo" class="logo_img"/> |
|
||||
</div> |
|
||||
|
|
||||
<!-- Additional sidebar content can go here --> |
|
||||
</div> |
|
||||
</div> |
|
||||
</xpath> |
|
||||
</t> |
|
||||
</templates> |
|
Loading…
Reference in new issue