2022-12-06 18:16:36 +05:30
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
2023-01-02 11:53:55 +05:30
// Copyright (C) 2013 - 2023, The pgAdmin Development Team
2022-12-06 18:16:36 +05:30
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import gettext from 'sources/gettext' ;
import pgAdmin from 'sources/pgadmin' ;
2022-12-22 14:25:18 +05:30
import Menu , { MenuItem } from '../../../static/js/helpers/Menu' ;
2022-12-09 11:50:53 +05:30
import getApiInstance from '../../../static/js/api_instance' ;
import url _for from 'sources/url_for' ;
import Notifier from '../../../static/js/helpers/Notifier' ;
2023-01-17 17:18:35 +05:30
import { getBrowser } from '../../../static/js/utils' ;
import { isMac } from '../../../static/js/keyboard_shortcuts' ;
2022-12-06 18:16:36 +05:30
2022-12-22 14:25:18 +05:30
const MAIN _MENUS = [
2022-12-06 18:16:36 +05:30
{ label : gettext ( 'File' ) , name : 'file' , id : 'mnu_file' , index : 0 , addSepratior : true } ,
{ label : gettext ( 'Object' ) , name : 'object' , id : 'mnu_obj' , index : 1 , addSepratior : true } ,
{ label : gettext ( 'Tools' ) , name : 'tools' , id : 'mnu_tools' , index : 2 , addSepratior : true } ,
{ label : gettext ( 'Help' ) , name : 'help' , id : 'mnu_help' , index : 5 , addSepratior : false }
] ;
2023-01-17 17:18:35 +05:30
let { name : browser } = getBrowser ( ) ;
if ( browser == 'Nwjs' ) {
let controlKey = isMac ( ) ? 'cmd' : 'ctrl' ;
let fullScreenKey = isMac ( ) ? 'F' : 'F10' ;
const RUNTIME _MENUS _OPTIONS = {
runtime : {
label : gettext ( 'Runtime' ) ,
name : 'runtime' ,
priority : 999 ,
submenus : {
configure : { label : gettext ( 'Configure...' ) , name : 'configure' , priority : 0 , enable : true } ,
view _log : { label : gettext ( 'View log...' ) , name : 'view_log' , priority : 1 , enable : true } ,
enter _full _screen : { label : gettext ( 'Enter Full Screen' ) , name : 'enter_full_screen' , enable : true , priority : 2 , key : fullScreenKey , modifiers : isMac ( ) ? ` ${ controlKey } +ctrl ` : controlKey } ,
actual _size : { label : gettext ( 'Actual Size' ) , name : 'actual_size' , priority : 3 , enable : true , key : '0' , modifiers : controlKey } ,
zoom _in : { label : gettext ( 'Zoom In' ) , name : 'zoom_in' , priority : 4 , enable : true , key : '+' , modifiers : controlKey } ,
zoom _out : { label : gettext ( 'Zoom Out' ) , name : 'zoom_out' , enable : true , priority : 5 , key : '-' , modifiers : controlKey } ,
}
}
} ;
pgAdmin . Browser . RUNTIME _MENUS _OPTIONS = RUNTIME _MENUS _OPTIONS ;
}
2022-12-22 14:25:18 +05:30
export default class MainMenuFactory {
static createMainMenus ( ) {
pgAdmin . Browser . MainMenus = [ ] ;
MAIN _MENUS . forEach ( ( _menu ) => {
let menuObj = Menu . create ( _menu . name , _menu . label , _menu . id , _menu . index , _menu . addSepratior ) ;
pgAdmin . Browser . MainMenus . push ( menuObj ) ;
// Don't add menuItems for Object menu as it's menuItems get changed on tree selection.
if ( _menu . name !== 'object' ) {
menuObj . addMenuItems ( Object . values ( pgAdmin . Browser . all _menus _cache [ _menu . name ] ) ) ;
menuObj . getMenuItems ( ) . forEach ( ( menuItem , index ) => {
menuItem ? . getMenuItems ( ) ? . forEach ( ( item , indx ) => {
item . below && menuItem ? . getMenuItems ( ) . splice ( indx + 1 , 0 , MainMenuFactory . getSeparator ( ) ) ;
} ) ;
if ( menuItem . below ) {
menuObj . addMenuItem ( MainMenuFactory . getSeparator ( ) , index + 1 ) ;
}
2022-12-07 18:47:38 +05:30
} ) ;
2022-12-22 14:25:18 +05:30
}
} ) ;
2022-12-06 18:16:36 +05:30
2022-12-22 14:25:18 +05:30
pgAdmin . Browser . enable _disable _menus ( ) ;
}
static getSeparator ( label , priority ) {
return new MenuItem ( { type : 'separator' , label , priority } ) ;
}
2022-12-07 18:47:38 +05:30
2022-12-22 14:25:18 +05:30
static refreshMainMenuItems ( menu , menuItems ) {
2022-12-06 18:16:36 +05:30
menu . setMenuItems ( menuItems ) ;
pgAdmin . Browser . Events . trigger ( 'pgadmin:nw-refresh-menu-item' , menu ) ;
}
2022-12-22 14:25:18 +05:30
static createMenuItem ( options ) {
2022-12-06 18:16:36 +05:30
return new MenuItem ( { ... options , callback : ( ) => {
// Some callbacks registered in 'callbacks' check and call specifiec callback function
if ( options . module && 'callbacks' in options . module && options . module . callbacks [ options . callback ] ) {
options . module . callbacks [ options . callback ] . apply ( options . module , [ options . data , pgAdmin . Browser . tree . selected ( ) ] ) ;
} else if ( options . module && options . module [ options . callback ] ) {
options . module [ options . callback ] . apply ( options . module , [ options . data , pgAdmin . Browser . tree . selected ( ) ] ) ;
} else if ( options ? . callback ) {
options . callback ( options ) ;
} else {
if ( options . url != '#' ) {
2022-12-09 11:50:53 +05:30
let api = getApiInstance ( ) ;
api (
url _for ( 'tools.initialize' )
) . then ( ( ) => {
window . open ( options . url ) ;
} ) . catch ( ( ) => {
Notifier . error ( gettext ( 'Error in opening window' ) ) ;
} ) ;
2022-12-06 18:16:36 +05:30
}
}
} } , ( menu , item ) => {
pgAdmin . Browser . Events . trigger ( 'pgadmin:nw-enable-disable-menu-items' , menu , item ) ;
2022-12-09 14:08:35 +05:30
} , ( item ) => {
pgAdmin . Browser . Events . trigger ( 'pgadmin:nw-update-checked-menu-item' , item ) ;
2022-12-06 18:16:36 +05:30
} ) ;
}
2022-12-22 14:25:18 +05:30
2023-01-02 10:51:13 +05:30
static getContextMenu ( menuList ) {
2022-12-22 14:25:18 +05:30
Menu . sortMenus ( menuList ) ;
2023-01-02 10:51:13 +05:30
return menuList ;
2022-12-22 14:25:18 +05:30
}
2023-03-02 11:02:15 +05:30
static checkNoMenuOptionForNode ( d ) {
let selectedNodeFromNodes = pgAdmin . Browser . Nodes [ d . _type ] ;
let selectedNode = pgAdmin . Browser . tree . selected ( ) ;
let flag = ! _ . isUndefined ( selectedNodeFromNodes . showMenu ) ;
if ( flag ) {
var showMenu = selectedNodeFromNodes . showMenu ( d , selectedNode ) ;
return { flag : showMenu ? false : flag , showMenu } ;
} else {
return { flag , showMenu : undefined } ;
}
}
}