File size: 13,945 Bytes
ae01f49 | 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | /////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import _ from 'lodash';
import pgAdmin from '../../../static/js/pgadmin';
import hotkeys from 'hotkeys-js';
import * as commonUtils from '../../../static/js/utils';
import gettext from 'sources/gettext';
import pgWindow from 'sources/window';
import usePreferences from '../../../preferences/static/js/store';
const pgBrowser = pgAdmin.Browser = pgAdmin.Browser || {};
pgBrowser.keyboardNavigation = pgBrowser.keyboardNavigation || {};
hotkeys.filter = function () {
return true;
};
_.extend(pgBrowser.keyboardNavigation, {
iframeEventsChannel:new BroadcastChannel('iframe-events'),
init: function() {
this.iframeEventsChannel.onmessage = (ev) =>{
hotkeys.trigger(ev.data);
};
usePreferences.subscribe((prefStore)=>{
hotkeys.unbind();
if (prefStore.version > 0) {
this.keyboardShortcut = {
...(prefStore.getPreferences('browser', 'main_menu_file')?.value) && {'file_shortcut': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'main_menu_file')?.value)},
...(prefStore.getPreferences('browser', 'main_menu_object')?.value) && {'object_shortcut': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'main_menu_object')?.value)},
...(prefStore.getPreferences('browser', 'main_menu_tools')?.value) && {'tools_shortcut': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'main_menu_tools')?.value)},
...(prefStore.getPreferences('browser', 'main_menu_help')?.value) && {'help_shortcut': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'main_menu_help')?.value)},
'left_tree_shortcut': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'browser_tree')?.value),
'tabbed_panel_backward': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'tabbed_panel_backward')?.value),
'tabbed_panel_forward': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'tabbed_panel_forward')?.value),
'sub_menu_query_tool': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'sub_menu_query_tool')?.value),
'sub_menu_view_data': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'sub_menu_view_data')?.value),
'sub_menu_search_objects': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'sub_menu_search_objects')?.value),
'sub_menu_properties': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'sub_menu_properties')?.value),
'sub_menu_create': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'sub_menu_create')?.value),
'sub_menu_delete': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'sub_menu_delete')?.value),
'sub_menu_refresh': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'sub_menu_refresh')?.value),
'context_menu': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'context_menu')?.value),
'direct_debugging': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'direct_debugging')?.value),
'add_grid_row': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'add_grid_row')?.value),
'open_quick_search': commonUtils.parseShortcutValue(prefStore.getPreferences('browser', 'open_quick_search')?.value),
};
this.shortcutMethods = {
...(prefStore.getPreferences('browser', 'main_menu_file')?.value) && {'bindMainMenu': {
'shortcuts': [this.keyboardShortcut.file_shortcut,
this.keyboardShortcut.object_shortcut, this.keyboardShortcut.tools_shortcut,
this.keyboardShortcut.help_shortcut],
}}, // Main menu
'bindRightPanel': {'shortcuts': [this.keyboardShortcut.tabbed_panel_backward, this.keyboardShortcut.tabbed_panel_forward]}, // Main window panels
'bindLeftTree': {'shortcuts': this.keyboardShortcut.left_tree_shortcut}, // Main menu,
'bindSubMenuQueryTool': {'shortcuts': this.keyboardShortcut.sub_menu_query_tool}, // Sub menu - Open Query Tool,
'bindSubMenuViewData': {'shortcuts': this.keyboardShortcut.sub_menu_view_data}, // Sub menu - Open View Data,
'bindSubMenuSearchObjects': {'shortcuts': this.keyboardShortcut.sub_menu_search_objects}, // Sub menu - Open search objects,
'bindSubMenuProperties': {'shortcuts': this.keyboardShortcut.sub_menu_properties}, // Sub menu - Edit Properties,
'bindSubMenuCreate': {'shortcuts': this.keyboardShortcut.sub_menu_create}, // Sub menu - Create Object,
'bindSubMenuDelete': {'shortcuts': this.keyboardShortcut.sub_menu_delete}, // Sub menu - Delete object,
'bindSubMenuRefresh': {'shortcuts': this.keyboardShortcut.sub_menu_refresh, 'bindElem': '#tree'}, // Sub menu - Refresh object,
'bindContextMenu': {'shortcuts': this.keyboardShortcut.context_menu}, // Sub menu - Open context menu,
'bindDirectDebugging': {'shortcuts': this.keyboardShortcut.direct_debugging}, // Sub menu - Direct Debugging
'bindAddGridRow': {'shortcuts': this.keyboardShortcut.add_grid_row}, // Subnode Grid Add Row
'bindOpenQuickSearch': {'shortcuts': this.keyboardShortcut.open_quick_search}, // Subnode Grid Refresh Row
};
this.shortcutsString=Object.values(this.shortcutMethods).map(i=>i.shortcuts).join(',');
// Checks if the tab is iframe or not, if iframe then calls the function 'setupIframeEventsBroadcast'
if (window.self != window.top) {
this.setupIframeEventsBroadcast();
} else {
this.bindShortcuts();
}
}
});
},
//Sends the pressed keyboard shortcut from iframe to parent
triggerIframeEventsBroadcast: function(event,checkShortcuts=false){
const shortcut = {
alt:event?.altKey,
shift:event?.shiftKey,
control:event?.ctrlKey,
key:{
char:event?.key
}
};
const currShortcutString = commonUtils.parseShortcutValue(shortcut);
if (checkShortcuts && !this.shortcutsString.split(',').includes(currShortcutString)){
return;
}
this.iframeEventsChannel.postMessage(currShortcutString);
},
//listens to keyboard events and triggers the 'triggerIframeEventsBroadcast' for shortcuts
setupIframeEventsBroadcast:function() {
const self=this;
hotkeys(self.shortcutsString,(event)=>{
this.triggerIframeEventsBroadcast(event);
});
},
bindShortcuts: function() {
const self = this;
_.each(self.shortcutMethods, (keyCombo, callback) => {
self._bindWithHotkeys(keyCombo.shortcuts, self[callback]);
});
},
_bindWithHotkeys: function(shortcuts, callback) {
const self = this;
hotkeys(shortcuts.toString(), function (event, combo) {
if(!combo){
combo = this;
}
callback.apply(self, [event, combo]);
});
},
bindMainMenu: function(event, combo) {
const shortcut_obj = this.keyboardShortcut;
let menuLabel = null;
switch (combo.key) {
case shortcut_obj.file_shortcut:
menuLabel = gettext('File');
break;
case shortcut_obj.object_shortcut:
menuLabel = gettext('Object');
break;
case shortcut_obj.tools_shortcut:
menuLabel = gettext('Tools');
break;
case shortcut_obj.help_shortcut:
menuLabel = gettext('Help');
break;
default:
break;
}
if(menuLabel) {
document.querySelector(`div[data-test="app-menu-bar"] button[data-label="${menuLabel}"]`)?.click();
}
},
bindRightPanel: function(event, combo) {
const self = this;
let dockLayoutTabs = document.activeElement?.closest('.dock-layout')?.querySelectorAll('.dock-tab-btn');
let shortcut_obj = this.keyboardShortcut;
//if the focus is on the tab button
if (document.activeElement.closest('.dock-tab-btn')) {
let currDockTab = document.activeElement?.closest('.dock-tab-btn');
if(dockLayoutTabs?.length > 1 && currDockTab) {
for(let i=0; i<dockLayoutTabs.length; i++) {
if(dockLayoutTabs[i] == currDockTab) {
let activeTabIdx = i;
self._focusTab(dockLayoutTabs, activeTabIdx, shortcut_obj, combo);
break;
}
}
}
//if the tab is a iframe or the focus is within the content of tab
} else if (document.activeElement.nodeName === 'IFRAME' || document.activeElement.closest('.dock-tabpane.dock-tabpane-active')?.id) {
let activeTabId = '';
//if the tab is a iframe
if (document.activeElement.nodeName === 'IFRAME'){
dockLayoutTabs = document.activeElement?.closest('#root')?.querySelectorAll('.dock-tab-btn');
activeTabId = document.activeElement?.id;
//if the focus is within the content of tab
} else if (document.activeElement.closest('.dock-tabpane.dock-tabpane-active')?.id){
activeTabId = document.activeElement.closest('.dock-tabpane.dock-tabpane-active')?.id;
}
if(dockLayoutTabs?.length > 1 && activeTabId) {
for(let i=0; i<dockLayoutTabs.length; i++) {
let tabIdx = i;
let tabId = dockLayoutTabs[tabIdx].id?.slice(14);
if (tabId == activeTabId) {
self._focusTab(dockLayoutTabs, tabIdx, shortcut_obj, combo);
break;
}
}
}
//if the focus is on the body or on the menu bar
} else if (document.activeElement === document.body || document.querySelector('div[data-test="app-menu-bar"]')) {
pgAdmin.Browser.docker.navigatePanel();
}
},
_focusTab: function(dockLayoutTabs, activeTabIdx, shortcut_obj, combo){
if (combo.key === shortcut_obj.tabbed_panel_backward) activeTabIdx = (activeTabIdx + dockLayoutTabs.length - 1) % dockLayoutTabs.length;
else if (combo.key === shortcut_obj.tabbed_panel_forward) activeTabIdx = (activeTabIdx + 1) % dockLayoutTabs.length;
dockLayoutTabs[activeTabIdx]?.click();
dockLayoutTabs[activeTabIdx]?.focus();
},
bindLeftTree: function() {
const tree = this.getTreeDetails();
document.querySelector('[id="id-object-explorer"]').focus();
tree.t.select(tree.i);
},
bindSubMenuQueryTool: function() {
const tree = this.getTreeDetails();
if (!tree.d)
return;
// Call data grid method to render query tool
pgAdmin.Tools.SQLEditor.showQueryTool('', tree.i);
},
bindSubMenuViewData: function() {
const tree = this.getTreeDetails();
if (!tree.d)
return;
// Call data grid method to render view data
pgAdmin.Tools.SQLEditor.showViewData({'mnuid': 1}, tree.i);
},
bindSubMenuSearchObjects: function() {
const tree = this.getTreeDetails();
if (!tree.d)
return;
// Call show search object to open the search object dialog.
pgAdmin.Tools.SearchObjects.show_search_objects('', tree.i);
},
bindSubMenuProperties: function() {
const tree = this.getTreeDetails();
if (!tree.d || pgAdmin.Browser.Nodes[tree.t.itemData(tree.i)._type].collection_node === true)
return;
// Open properties dialog in edit mode
pgAdmin.Browser.Node.callbacks.show_obj_properties.call(
pgAdmin.Browser.Nodes[tree.t.itemData(tree.i)._type], {action: 'edit'}
);
},
bindSubMenuCreate: function() {
const tree = this.getTreeDetails();
let node_obj = pgAdmin.Browser.Nodes[tree.t.itemData(tree.i)._type];
if (!tree.d){
return;
} else if(node_obj.collection_node === true) {
if(node_obj.node) {
node_obj = pgAdmin.Browser.Nodes[node_obj.node];
} else {
return;
}
}
// Open properties dialog in edit mode
pgAdmin.Browser.Node.callbacks.show_obj_properties.call(
node_obj, {action: 'create', item: tree.i}
);
},
bindSubMenuDelete: function() {
const tree = this.getTreeDetails();
if (!tree.d || pgAdmin.Browser.Nodes[tree.t.itemData(tree.i)._type].collection_node === true)
return;
// Call delete object callback
pgAdmin.Browser.Node.callbacks.delete_obj.call(pgAdmin.Browser.Nodes[tree.t.itemData(tree.i)._type]);
},
bindSubMenuRefresh: function(event) {
event?.preventDefault();
const tree = pgBrowser.keyboardNavigation.getTreeDetails();
// Call refresh object callback
pgAdmin.Browser.Node.callbacks.refresh.call(pgAdmin.Browser.Nodes[tree.t.itemData(tree.i)._type]);
},
bindDirectDebugging: function() {
const tree = this.getTreeDetails();
const type = tree.t.itemData(tree.i)._type;
if (!tree.d || (type !== 'function' && type !== 'procedure'))
return;
if (pgAdmin.Tools.Debugger.can_debug(tree.d, tree.i, {'debug_type': 'direct'})) {
// Call debugger callback
pgAdmin.Tools.Debugger.get_function_information(pgAdmin.Browser.Nodes[type]);
}
},
isPropertyPanelVisible: function() {
let isPanelVisible = false;
_.each(pgAdmin.Browser.docker.findPanels(), (panel) => {
if (panel._type === 'properties')
isPanelVisible = panel.isVisible();
});
return isPanelVisible;
},
getTreeDetails: function() {
const tree = pgAdmin.Browser.tree;
const selectedTreeNode = tree.selected() ? tree.selected() : tree.first();
const selectedTreeNodeData = selectedTreeNode ? tree.itemData(selectedTreeNode) : undefined;
return {
t: tree,
i: selectedTreeNode,
d: selectedTreeNodeData,
};
},
bindOpenQuickSearch: function() {
pgWindow.pgAdmin.Browser.all_menus_cache.help.mnu_quick_search_help.callback();
},
});
module.exports = pgAdmin.Browser.keyboardNavigation;
|