mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Introduced a event manager for the browser, which will generate certain
known events, when any activity happens on the browser layout.
The following types of events will be triggered through the browser
event manager.
- pgadmin-browser:frame:* [1]
- pgadmin-browser:frame-<name>:* [1]
- pgadmin-browser:panel:* [1]
- pgadmin-browser:panel-<name>:* [1]
- pgadmin-browser:panel
- pgadmin-browser:frame
- pgadmin-browser:tree
- pgadmin-browser:tree:* [2]
[1] The '*' denotes some of the events generated by the wcDocker, which
can be useful to do some operations.
These events are:
+ wcDocker.EVENT.UPDATED
+ wcDocker.EVENT.VISIBILITY_CHANGED
+ wcDocker.EVENT.BEGIN_DOCK
+ wcDocker.EVENT.END_DOCK
+ wcDocker.EVENT.GAIN_FOCUS,
+ wcDocker.EVENT.LOST_FOCUS
+ wcDocker.EVENT.CLOSED
+ wcDocker.EVENT.BUTTON
+ wcDocker.EVENT.ATTACHED
+ wcDocker.EVENT.DETACHED
+ wcDocker.EVENT.MOVE_STARTED
+ wcDocker.EVENT.MOVE_ENDED
+ wcDocker.EVENT.MOVED
+ wcDocker.EVENT.RESIZE_STARTED
+ wcDocker.EVENT.RESIZE_ENDED
+ wcDocker.EVENT.RESIZED
+ wcDocker.EVENT.SCROLLED
[2] The '*' denotes all the events generated by the Browser Tree
(aciTree).
The extension modules can utilize these events to do some operations on
nodes, and panels.
This patch includes showing 'Reversed Engineered Query' for the selected
node (if allowed) using the above approch.
The ShowNodeSQL module looks for two events.
1. SQL Panel Visibility change.
- Based on the visibility of that panel, we start/stop listening the
node selection event.
2. Node Selection in Browser tree
- Based on the selected node type, it will look for 'hasSQL'
parameter of the node, and fetch the Query from the server, and
show it in the SQL editor.
This commit is contained in:
85
web/pgadmin/misc/sql/static/js/sql.js
Normal file
85
web/pgadmin/misc/sql/static/js/sql.js
Normal file
@@ -0,0 +1,85 @@
|
||||
define(
|
||||
['underscore', 'jquery', 'pgadmin.browser'],
|
||||
function(_, $, pgBrowser) {
|
||||
|
||||
pgBrowser.ShowNodeSQL = pgBrowser.ShowNodeSQL || {};
|
||||
|
||||
if (pgBrowser.ShowNodeSQL.initialized) {
|
||||
return pgBrowser.ShowNodeSQL;
|
||||
}
|
||||
|
||||
_.extend(pgBrowser.ShowNodeSQL, {
|
||||
init: function() {
|
||||
if (this.initialized) {
|
||||
return;
|
||||
}
|
||||
this.initialized = true;
|
||||
_.bindAll(this, 'showSQL', 'sqlPanelVisibilityChanged');
|
||||
|
||||
var sqlPanels = pgBrowser.docker.findPanels('sql');
|
||||
|
||||
// We will listend to the visibility change of the SQL panel
|
||||
pgBrowser.Events.on('pgadmin-browser:panel-sql:' + wcDocker.EVENT.VISIBILITY_CHANGED, this.sqlPanelVisibilityChanged);
|
||||
|
||||
// Hmm.. Did we find the SQL panel, and is it visible (openned)?
|
||||
// If that is the case - we need to listen the browser tree selection
|
||||
// events.
|
||||
if ((sqlPanels.length == 1 && sqlPanels[0].isVisible()) || sqlPanels.length != 1) {
|
||||
pgBrowser.Events.on('pgadmin-browser:tree:selected', this.showSQL);
|
||||
}
|
||||
},
|
||||
showSQL: function(item, data, node) {
|
||||
/**
|
||||
* We can't start fetching the SQL immediately, it is possible - the user
|
||||
* is just using keyboards to select the node, and just traversing
|
||||
* through. We will wait for some time before fetching the Reversed
|
||||
* Engineering SQL.
|
||||
**/
|
||||
if (this.timeout) {
|
||||
clearTimeout(this.timeout);
|
||||
}
|
||||
this.timeout = setTimeout(
|
||||
function() {
|
||||
var sql = '-- No object selected!';
|
||||
if (node) {
|
||||
sql = '-- No SQL generated for the selected object.';
|
||||
if (node.hasSQL) {
|
||||
|
||||
sql = '-- Please wait while we fetch the SQL from the server.';
|
||||
var url = node.generate_url(item, 'sql', data, true);
|
||||
|
||||
$.ajax({
|
||||
url: url,
|
||||
type:'GET',
|
||||
success: function(res) {
|
||||
pgAdmin.Browser.editor.setValue(res);
|
||||
},
|
||||
error: function() {
|
||||
// TODO:: Report this
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
pgAdmin.Browser.editor.setValue(sql);
|
||||
}, 400);
|
||||
},
|
||||
sqlPanelVisibilityChanged: function(panel) {
|
||||
if (panel.isVisible()) {
|
||||
var t = pgBrowser.tree,
|
||||
i = t.selected(),
|
||||
d = i && t.itemData(i),
|
||||
n = i && d && pgBrowser.Nodes[d._type];
|
||||
|
||||
pgBrowser.ShowNodeSQL.showSQL.apply(pgBrowser.ShowNodeSQL, [i, d, n]);
|
||||
|
||||
// We will start listening the tree selection event.
|
||||
pgBrowser.Events.on('pgadmin-browser:tree:selected', pgBrowser.ShowNodeSQL.showSQL);
|
||||
} else {
|
||||
// We don't need to listen the tree item selection event.
|
||||
pgBrowser.Events.off('pgadmin-browser:tree:selected', pgBrowser.ShowNodeSQL.showSQL);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return pgBrowser.ShowNodeSQL;
|
||||
});
|
||||
Reference in New Issue
Block a user