1- /** @odoo -module **/
21/* Copyright 2018 Tecnativa - Jairo Llopis
32 * Copyright 2021 ITerra - Sergey Shebanin
43 * Copyright 2023 Onestein - Anjeel Haria
54 * Copyright 2023 Taras Shabaranskyi
65 * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */
76
87import { Component , onWillStart , useState } from "@odoo/owl" ;
9- import { session } from "@web/session" ;
108import { useBus , useService } from "@web/core/utils/hooks" ;
119import { AppMenuItem } from "@web_responsive/components/apps_menu_item/apps_menu_item.esm" ;
1210import { AppsMenuSearchBar } from "@web_responsive/components/menu_searchbar/searchbar.esm" ;
1311import { NavBar } from "@web/webclient/navbar/navbar" ;
1412import { WebClient } from "@web/webclient/webclient" ;
13+ import { browser } from "@web/core/browser/browser" ;
1514import { patch } from "@web/core/utils/patch" ;
15+ import { router } from "@web/core/browser/router" ;
16+ import { session } from "@web/session" ;
1617import { useHotkey } from "@web/core/hotkeys/hotkey_hook" ;
17- import { browser } from "@web/core/browser/browser" ;
18+ import { user } from "@web/core/user" ;
19+
20+ /* global document */
21+ /* global location */
22+ /* global window */
1823
1924// Patch WebClient to show AppsMenu instead of default app
2025patch ( WebClient . prototype , {
2126 setup ( ) {
2227 super . setup ( ) ;
28+
2329 useBus ( this . env . bus , "APPS_MENU:STATE_CHANGED" , ( { detail : state } ) => {
2430 document . body . classList . toggle ( "o_apps_menu_opened" , state ) ;
2531 } ) ;
26- this . user = useService ( "user" ) ;
2732 onWillStart ( async ( ) => {
2833 const is_redirect_home = await this . orm . searchRead (
2934 "res.users" ,
30- [ [ "id" , "=" , this . user . userId ] ] ,
35+ [ [ "id" , "=" , user . userId ] ] ,
3136 [ "is_redirect_home" ]
3237 ) ;
33- this . env . services . user . updateContext ( {
38+ user . updateContext ( {
3439 is_redirect_to_home : is_redirect_home [ 0 ] ?. is_redirect_home ,
3540 } ) ;
3641 } ) ;
3742 this . redirect = false ;
3843 } ,
3944 _loadDefaultApp ( ) {
40- if ( this . env . services . user . context . is_redirect_to_home ) {
45+ if ( user . context . is_redirect_to_home ) {
4146 this . env . bus . trigger ( "APPS_MENU:STATE_CHANGED" , true ) ;
4247 } else {
4348 super . _loadDefaultApp ( ) ;
@@ -52,9 +57,9 @@ export class AppsMenu extends Component {
5257 this . theme = session . apps_menu . theme || "milk" ;
5358 this . menuService = useService ( "menu" ) ;
5459 browser . localStorage . setItem ( "redirect_menuId" , "" ) ;
55- if ( this . env . services . user . context . is_redirect_to_home ) {
56- this . router = useService ( "router" ) ;
57- const menuId = Number ( this . router . current . hash . menu_id || 0 ) ;
60+
61+ if ( user . context . is_redirect_to_home ) {
62+ const menuId = Number ( router . current . menu_id || 0 ) ;
5863 this . state = useState ( { open : menuId === 0 } ) ;
5964 }
6065 useBus ( this . env . bus , "ACTION_MANAGER:UI-UPDATED" , ( ) => {
@@ -75,98 +80,75 @@ export class AppsMenu extends Component {
7580 const repeatable = {
7681 allowRepeat : true ,
7782 } ;
78- useHotkey (
79- "ArrowRight" ,
80- ( ) => {
81- this . _onWindowKeydown ( "next" ) ;
82- } ,
83- repeatable
84- ) ;
85- useHotkey (
86- "ArrowLeft" ,
87- ( ) => {
88- this . _onWindowKeydown ( "prev" ) ;
89- } ,
90- repeatable
91- ) ;
92- useHotkey (
93- "ArrowDown" ,
94- ( ) => {
95- this . _onWindowKeydown ( "next" ) ;
96- } ,
97- repeatable
98- ) ;
99- useHotkey (
100- "ArrowUp" ,
101- ( ) => {
102- this . _onWindowKeydown ( "prev" ) ;
103- } ,
104- repeatable
105- ) ;
83+
84+ const keyActions = [
85+ { key : "ArrowRight" , action : "next" } ,
86+ { key : "ArrowLeft" , action : "prev" } ,
87+ { key : "ArrowDown" , action : "next" } ,
88+ { key : "ArrowUp" , action : "prev" } ,
89+ ] ;
90+
91+ keyActions . forEach ( ( { key, action} ) => {
92+ useHotkey (
93+ key ,
94+ ( ) => {
95+ this . _onWindowKeydown ( action ) ;
96+ } ,
97+ repeatable
98+ ) ;
99+ } ) ;
100+
106101 useHotkey ( "Escape" , ( ) => {
107102 this . env . bus . trigger ( "ACTION_MANAGER:UI-UPDATED" ) ;
108103 } ) ;
109104 }
110105
111106 _onWindowKeydown ( direction ) {
112- const focusableInputElements = document . querySelectorAll ( ".o-app-menu-item" ) ;
107+ const focusableInputElements = Array . from (
108+ document . querySelectorAll ( ".o-app-menu-item" )
109+ ) ;
110+ const currentIndex = focusableInputElements . indexOf ( document . activeElement ) ;
111+
113112 if ( focusableInputElements . length ) {
114- const focusable = [ ... focusableInputElements ] ;
115- const index = focusable . indexOf ( document . activeElement ) ;
113+ const lastIndex = focusableInputElements . length - 1 ;
114+
116115 let nextIndex = 0 ;
117- if ( direction === "prev" && index >= 0 ) {
118- if ( index > 0 ) {
119- nextIndex = index - 1 ;
120- } else {
121- nextIndex = focusable . length - 1 ;
122- }
123- } else if ( direction === "next" ) {
124- if ( index + 1 < focusable . length ) {
125- nextIndex = index + 1 ;
126- } else {
127- nextIndex = 0 ;
128- }
116+ if ( direction === "next" ) {
117+ nextIndex = currentIndex < lastIndex ? currentIndex + 1 : 0 ;
118+ } else if ( direction === "prev" ) {
119+ nextIndex = currentIndex > 0 ? currentIndex - 1 : lastIndex ;
129120 }
121+
130122 focusableInputElements [ nextIndex ] . focus ( ) ;
131123 }
132124 }
133125
134126 onMenuClick ( ) {
135- if ( ! this . env . services . user . context . is_redirect_to_home ) {
136- this . setOpenState ( ! this . state . open ) ;
137- } else {
138- const redirect_menuId =
139- browser . localStorage . getItem ( "redirect_menuId" ) || "" ;
140- if ( ! redirect_menuId ) {
141- this . setOpenState ( true ) ;
142- } else {
143- this . setOpenState ( ! this . state . open ) ;
144- }
145- const { href, hash} = location ;
146- const menuId = this . router . current . hash . menu_id ;
147- if ( menuId && menuId != redirect_menuId ) {
148- console . log ( this . router . current . hash . menu_id ) ;
149- browser . localStorage . setItem (
150- "redirect_menuId" ,
151- this . router . current . hash . menu_id
152- ) ;
127+ const isRedirect = user . context . is_redirect_to_home ;
128+ const redirectMenuId = browser . localStorage . getItem ( "redirect_menuId" ) || "" ;
129+ const { href, hash} = location ;
130+
131+ if ( isRedirect ) {
132+ const shouldOpenState = ! redirectMenuId || ! this . state . open ;
133+ this . setOpenState ( shouldOpenState ) ;
134+
135+ const currentMenuId = router . current . menu_id ;
136+ if ( currentMenuId && currentMenuId !== redirectMenuId ) {
137+ browser . localStorage . setItem ( "redirect_menuId" , currentMenuId ) ;
153138 }
154139
155140 if ( href . includes ( hash ) ) {
156141 window . history . replaceState ( null , "" , href . replace ( hash , "" ) ) ;
157142 }
143+ } else {
144+ this . setOpenState ( ! this . state . open ) ;
158145 }
159146 }
160147}
161148
162- Object . assign ( AppsMenu , {
163- template : "web_responsive.AppsMenu" ,
164- props : {
165- slots : {
166- type : Object ,
167- optional : true ,
168- } ,
169- } ,
170- } ) ;
149+ AppsMenu . template = "web_responsive.AppsMenu" ;
150+ AppsMenu . props = {
151+ slots : { type : Object , optional : true } ,
152+ } ;
171153
172154Object . assign ( NavBar . components , { AppsMenu, AppMenuItem, AppsMenuSearchBar} ) ;
0 commit comments