forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenuFactory.js
More file actions
263 lines (234 loc) · 8.65 KB
/
MainMenuFactory.js
File metadata and controls
263 lines (234 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2025, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import gettext from 'sources/gettext';
import pgAdmin from 'sources/pgadmin';
import Menu, { MenuItem } from '../../../static/js/helpers/Menu';
import getApiInstance from '../../../static/js/api_instance';
import url_for from 'sources/url_for';
import withCheckPermission from './withCheckPermission';
const MAIN_MENUS = [
{ label: gettext('File'), name: 'file', id: 'mnu_file', index: 0, addSeprator: true, hasDynamicMenuItems: false },
{ label: gettext('Object'), name: 'object', id: 'mnu_obj', index: 1, addSeprator: true, hasDynamicMenuItems: true },
{ label: gettext('Tools'), name: 'tools', id: 'mnu_tools', index: 2, addSeprator: true, hasDynamicMenuItems: false },
{ label: gettext('Help'), name: 'help', id: 'mnu_help', index: 5, addSeprator: false, hasDynamicMenuItems: false }
];
export default class MainMenuFactory {
static electronCallbacks = {};
static toElectron() {
// we support 2 levels of submenu
return pgAdmin.Browser.MainMenus.map((m)=>{
return {
...m.serialize(),
submenu: m.menuItems.map((sm)=>{
const smName = `${m.name}_${sm.name}`;
MainMenuFactory.electronCallbacks[smName] = sm.callback;
return {
...sm.serialize(),
submenu: sm.getMenuItems()?.map((smsm)=>{
MainMenuFactory.electronCallbacks[`${smName}_${smsm.name}`] = smsm.callback;
return {
...smsm.serialize(),
};
})
};
})
};
});
}
static createMainMenus() {
pgAdmin.Browser.MainMenus = [];
MAIN_MENUS.forEach((_menu) => {
let menuObj = Menu.create(_menu.name, _menu.label, _menu.id, _menu.index, _menu.addSeprator, _menu.hasDynamicMenuItems);
pgAdmin.Browser.MainMenus.push(menuObj);
// Don't add menuItems for hasDynamicMenuItems true as it's menuItems get changed on tree selection.
if(!_menu.hasDynamicMenuItems) {
menuObj.clearMenuItems();
menuObj.addMenuItems(MainMenuFactory.createMenuItems(pgAdmin.Browser.all_menus_cache[_menu.name]));
}
});
// enable disable will take care of dynamic menus.
MainMenuFactory.enableDisableMenus();
window.electronUI?.onMenuClick((menuName)=>{
MainMenuFactory.electronCallbacks[menuName]?.();
});
window.electronUI?.setMenus(MainMenuFactory.toElectron());
}
static getSeparator(label, priority) {
return new MenuItem({type: 'separator', label, priority});
}
static createMenuItem(options) {
const 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.callback]) {
options.module[options.callback](options.data, pgAdmin.Browser.tree?.selected());
} else if (options?.callback) {
options.callback(options);
} else if (options.url != '#') {
let api = getApiInstance();
api(
url_for('tools.initialize')
).then(()=>{
window.open(options.url);
}).catch(()=>{
pgAdmin.Browser.notifier.error(gettext('Error in opening window'));
});
}
};
return new MenuItem({...options, callback: withCheckPermission(options, callback)}, (menu, item)=> {
pgAdmin.Browser.Events.trigger('pgadmin:enable-disable-menu-items', menu, item);
window.electronUI?.enableDisableMenuItems(menu?.serialize(), item?.serialize());
});
}
static enableDisableMenus(item) {
let itemData = item ? pgAdmin.Browser.tree.itemData(item) : undefined;
const checkForItems = (items)=>{
items.forEach((mitem) => {
const subItems = mitem.getMenuItems() ?? [];
if(subItems.length > 0) {
checkForItems(subItems);
} else {
mitem.checkAndSetDisabled(itemData, item);
}
});
};
// Non dynamic menus will be required to check whether enabled/disabled.
pgAdmin.Browser.MainMenus.filter((m)=>(!m.hasDynamicMenuItems)).forEach((menu) => {
checkForItems(menu.getMenuItems());
});
pgAdmin.Browser.MainMenus.filter((m)=>(m.hasDynamicMenuItems)).forEach((menu) => {
let menuItemList = MainMenuFactory.getDynamicMenu(menu.name, item, itemData);
menu.setMenuItems(menuItemList);
});
// set the context menu as well
pgAdmin.Browser.BrowserContextMenu = MainMenuFactory.getDynamicMenu('context', item, itemData, true);
window.electronUI?.setMenus(MainMenuFactory.toElectron());
pgAdmin.Browser.Events.trigger('pgadmin:refresh-app-menu');
}
static checkNoMenuOptionForNode(itemData){
if(!itemData) {
return true;
}
let selectedNodeFromNodes=pgAdmin.Browser.Nodes[itemData._type];
let selectedNode=pgAdmin.Browser.tree.selected();
return selectedNodeFromNodes.showMenu?.(itemData, selectedNode) ?? true;
}
static createMenuItems(items, skipDisabled=false, checkAndSetDisabled=()=>true) {
let retVal = [];
let categories = {};
const getNewMenuItem = (i)=>{
const mi = MainMenuFactory.createMenuItem({...i});
checkAndSetDisabled?.(mi);
if(skipDisabled && mi.isDisabled) {
return null;
}
return mi;
};
const getMenuCategory = (catName)=>{
let category = pgAdmin.Browser.menu_categories[catName];
if(!category) {
// generate category on the fly.
category = {
name: catName,
label: catName,
priority: 10,
};
}
let cmi = categories[category.name];
if(!cmi) {
cmi = getNewMenuItem({...category});
// for easily finding again, note down.
categories[category.name] = cmi;
}
return cmi;
};
const applySeparators = (mi)=>{
const newItems = [];
if(mi.above) {
newItems.push(MainMenuFactory.getSeparator(mi.label, mi.priority));
}
newItems.push(mi);
if(mi.below) {
newItems.push(MainMenuFactory.getSeparator(mi.label, mi.priority));
}
return newItems;
};
Object.entries(items).forEach(([k, i])=>{
if('name' in i) {
const mi = getNewMenuItem(i);
if(!mi) return;
if((i.category??'common') != 'common') {
const cmi = getMenuCategory(i.category);
if(cmi) {
cmi.addMenuItems([...applySeparators(mi)]);
} else {
retVal.push(...applySeparators(mi));
}
} else {
retVal.push(...applySeparators(getNewMenuItem(i)));
}
} else {
// Can be a category
const cmi = getMenuCategory(k);
if(cmi) {
cmi.addMenuItems(MainMenuFactory.createMenuItems(i, skipDisabled, checkAndSetDisabled));
}
}
});
// Push the category menus
Object.values(categories).forEach((cmi)=>{
const items = cmi.getMenuItems();
// if there is only one menu in the category, then no need of the category.
if(items.length <= 1 && !cmi.single) {
retVal = retVal.concat(items);
return;
}
retVal.push(...applySeparators(cmi));
});
Menu.sortMenus(retVal ?? []);
return retVal;
}
static getDynamicMenu(name, item, itemData, skipDisabled=false) {
if(!item) {
return [MainMenuFactory.createMenuItem({
name: '',
label: gettext('No object selected'),
category: 'create',
priority: 1,
enable: false,
})];
}
const showMenu = MainMenuFactory.checkNoMenuOptionForNode(itemData);
if(!showMenu){
return [MainMenuFactory.createMenuItem({
enable : false,
label: gettext('No menu available for this object.'),
name:'',
priority: 1,
category: 'create',
})];
} else {
const nodeTypeMenus = pgAdmin.Browser.all_menus_cache[name]?.[itemData._type] ?? [];
const menuItemList = MainMenuFactory.createMenuItems(nodeTypeMenus, skipDisabled, (mi)=>{
return mi.checkAndSetDisabled(itemData, item);
});
if(menuItemList.length == 0) {
return [MainMenuFactory.createMenuItem({
enable : false,
label: gettext('No menu available for this object.'),
name:'',
priority: 1,
category: 'create',
})];
}
return menuItemList;
}
}
}