diff --git a/web/pgadmin/browser/server_groups/servers/databases/templates/databases/js/databases.js b/web/pgadmin/browser/server_groups/servers/databases/templates/databases/js/databases.js index f5cd6a9..aec2612 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/templates/databases/js/databases.js +++ b/web/pgadmin/browser/server_groups/servers/databases/templates/databases/js/databases.js @@ -69,6 +69,11 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) { category: 'drop', priority: 5, label: '{{ _('Disconnect Database...') }}', icon: 'fa fa-chain-broken', enable : 'is_connected' }]); + + _.bindAll(this, 'connection_lost'); + pgBrowser.Events.on( + 'pgadmin:database:connection:lost', this.connection_lost + ); }, can_create_database: function(node, item) { var treeData = this.getTreeNodeHierarchy(item), @@ -82,6 +87,67 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) { is_connected: function(node) { return (node && node.connected == true && node.canDisconn == true); }, + connection_lost: function(i, resp, server_connected) { + if (pgBrowser.tree) { + var t = pgBrowser.tree, + info = i && this.getTreeNodeHierarchy(i), + s = null, + d = i && t.itemData(i), + self = this, + _i = i; + + while (d && d._type != 'database') { + i = t.parent(i); + d = i && t.itemData(i); + } + + if (i && d) { + if (_.isUndefined(d.is_connecting) || !d.is_connecting) { + d.is_connecting = true; + + var disconnect = function(_i, _d) { + if (_d._id == this._id) { + d.is_connecting = false; + pgBrowser.Events.off( + 'pgadmin:database:connect:cancelled', disconnect + ); + _i = _i && t.parent(_i); + _d = _i && t.itemData(_i); + if (_i && _d) { + pgBrowser.Events.trigger( + 'pgadmin:server:disconnect', + {item: _i, data: _d}, false + ); + } + } + }; + + pgBrowser.Events.on( + 'pgadmin:database:connect:cancelled', disconnect + ); + if (server_connected) { + connect(self, d, t, i, true); + return; + } + Alertify.confirm( + '{{ _('Connection lost') }}', + '{{ _('Would you like to reconnect to the database?') }}', + function() { + connect(self, d, t, i, true); + }, + function() { + d.is_connecting = false; + t.unload(i); + t.setInode(i); + t.addIcon(i, {icon: 'icon-database-not-connected'}); + pgBrowser.Events.trigger( + 'pgadmin:database:connect:cancelled', i, d, self + ); + }); + } + } + } + }, callbacks: { /* Connect the database */ connect_database: function(args){ @@ -374,58 +440,91 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) { return null; } }) - }); - function connect_to_database(obj, data, tree, item, interactive) { - connect(obj, data, tree, item) - }; - function connect(obj, data, tree, item) { - var onFailure = function(xhr, status, error, _model, _data, _tree, _item) { - - tree.setInode(_item); - tree.addIcon(_item, {icon: 'icon-database-not-connected'}); - - Alertify.pgNotifier('error', xhr, error, function(msg) { - setTimeout(function() { - Alertify.dlgServerPass( - '{{ _('Connect to database') }}', - msg, _model, _data, _tree, _item - ).resizeTo(); - }, 100); - }); - }, - onSuccess = function(res, model, data, tree, item) { - tree.deselect(item); - tree.setInode(item); - - if (res && res.data) { - if(typeof res.data.connected == 'boolean') { - data.connected = res.data.connected; - } - if (typeof res.data.icon == 'string') { - tree.removeIcon(item); - data.icon = res.data.icon; - tree.addIcon(item, {icon: data.icon}); - } + }); + + function connect_to_database(obj, data, tree, item, interactive) { + connect(obj, data, tree, item) + } - Alertify.success(res.info); - setTimeout(function() {tree.select(item);}, 10); - setTimeout(function() {tree.open(item);}, 100); - } - }, - url = obj.generate_url(item, "connect", data, true); - $.post(url) - .done( - function(res) { - if (res.success == 1) { - return onSuccess(res, obj, data, tree, item); + function connect(obj, data, tree, item, _wasConnected) { + var wasConnected = _wasConnected || data.connected, + onFailure = function( + xhr, status, error, _model, _data, _tree, _item, _status + ) { + if (!_status) { + tree.setInode(_item); + tree.addIcon(_item, {icon: 'icon-database-not-connected'}); } - }) - .fail( - function(xhr, status, error) { - return onFailure(xhr, status, error, obj, data, tree, item); - }); - } + Alertify.pgNotifier('error', xhr, error, function(msg) { + setTimeout(function() { + Alertify.dlgServerPass( + '{{ _('Connect to database') }}', + msg, _model, _data, _tree, _item, _status, + onSuccess, onFailure, onCancel + ).resizeTo(); + }, 100); + }); + }, + onSuccess = function( + res, model, data, tree, item, connected + ) { + data.is_connecting = false; + if (!connected) { + tree.deselect(item); + tree.setInode(item); + } + + if (res && res.data) { + if(typeof res.data.connected == 'boolean') { + data.connected = res.data.connected; + } + if (typeof res.data.icon == 'string') { + tree.removeIcon(item); + data.icon = res.data.icon; + tree.addIcon(item, {icon: data.icon}); + } + + Alertify.success(res.info); + obj.trigger('connected', obj, item, data); + pgBrowser.Events.trigger( + 'pgadmin:database:connected', item, data + ); + + if (!connected) { + setTimeout(function() { + tree.select(item); + tree.open(item); + }, 10); + } + } + }, + onCancel = function(_tree, _item, _data, _status) { + _data.is_connecting = false; + var server = _tree.parent(_item); + _tree.unload(_item); + _tree.setInode(_item); + _tree.removeIcon(_item); + _tree.addIcon(_item, {icon: 'icon-database-not-connected'}); + obj.trigger('connect:cancelled', obj, _item, _data); + pgBrowser.Events.trigger( + 'pgadmin:database:connect:cancelled', _item, _data, obj + ); + _tree.select(server); + }; + + $.post( + obj.generate_url(item, "connect", data, true) + ).done(function(res) { + if (res.success == 1) { + return onSuccess(res, obj, data, tree, item, wasConnected); + } + }).fail(function(xhr, status, error) { + return onFailure( + xhr, status, error, obj, data, tree, item, wasConnected + ); + }); + } } return pgBrowser.Nodes['coll-database']; diff --git a/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js b/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js index ca0c269..e6c92e1 100644 --- a/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js +++ b/web/pgadmin/browser/server_groups/servers/templates/servers/servers.js @@ -102,6 +102,18 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { '{{ _('A grantee must be selected.') }}'; pgBrowser.messages['NO_PRIV_SELECTED'] = '{{ _('At least one privilege should be selected.') }}'; + + pgBrowser.Events.on( + 'pgadmin:server:disconnect', this.callbacks.disconnect_server + ); + pgBrowser.Events.on( + 'pgadmin:server:connect', this.callbacks.connect_server + ); + + _.bindAll(this, 'connection_lost'); + pgBrowser.Events.on( + 'pgadmin:server:connection:lost', this.connection_lost + ); }, is_not_connected: function(node) { return (node && node.connected != true); @@ -158,11 +170,11 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { if (!d) return false; - connect_to_server(obj, d, t, i); + connect_to_server(obj, d, t, i, false); return false; }, /* Disconnect the server */ - disconnect_server: function(args) { + disconnect_server: function(args, notify) { var input = args || {}, obj = this, t = pgBrowser.tree, @@ -172,48 +184,60 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { if (!d) return false; - alertify.confirm( - '{{ _('Disconnect server') }}', - S('{{ _('Are you sure you want to disconnect the server %s?') }}').sprintf(d.label).value(), - function(evt) { - $.ajax({ - url: obj.generate_url(i, 'connect', d, true), - type:'DELETE', - success: function(res) { - if (res.success == 1) { - alertify.success(res.info); - d = t.itemData(i); - t.removeIcon(i); - d.connected = false; - d.icon = 'icon-server-not-connected'; - t.addIcon(i, {icon: d.icon}); - obj.callbacks.refresh.apply(obj, [null, i]); - if (pgBrowser.serverInfo && d._id in pgBrowser.serverInfo) { - delete pgBrowser.serverInfo[d._id] - } - obj.trigger('server-disconnected', obj, i, d); - } - else { - try { - alertify.error(res.errormsg); - } catch (e) {} - t.unload(i); + notify = notify || _.isUndefined(notify) || _.isNull(notify); + + var disconnect = function() { + $.ajax({ + url: obj.generate_url(i, 'connect', d, true), + type:'DELETE', + success: function(res) { + if (res.success == 1) { + alertify.success(res.info); + d = t.itemData(i); + t.removeIcon(i); + d.connected = false; + d.icon = 'icon-server-not-connected'; + t.addIcon(i, {icon: d.icon}); + obj.callbacks.refresh.apply(obj, [null, i]); + if (pgBrowser.serverInfo && d._id in pgBrowser.serverInfo) { + delete pgBrowser.serverInfo[d._id] } - }, - error: function(xhr, status, error) { + obj.trigger('server-disconnected', obj, i, d); + } + else { try { - var err = $.parseJSON(xhr.responseText); - if (err.success == 0) { - alertify.error(err.errormsg); - } + alertify.error(res.errormsg); } catch (e) {} t.unload(i); } + }, + error: function(xhr, status, error) { + try { + var err = $.parseJSON(xhr.responseText); + if (err.success == 0) { + alertify.error(err.errormsg); + } + } catch (e) {} + t.unload(i); + } + }); + }; + + if (notify) { + alertify.confirm( + '{{ _('Disconnect server') }}', + S('{{ _('Are you sure you want to disconnect the server %s?') }}').sprintf( + d.label + ).value(), + function(evt) { + disconnect(); + }, + function(evt) { + return true; }); - }, - function(evt) { - return true; - }); + } else { + disconnect(); + } return false; }, @@ -226,7 +250,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { pgBrowser.tree.addIcon(item, {icon: data.icon}); if (!data.connected) { - connect_to_server(this, data, pgBrowser.tree, item); + connect_to_server(this, data, pgBrowser.tree, item, false); return false; } @@ -409,7 +433,6 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { } }, prepare: function() { - //console.log(this.get('server').user.name); var self = this; // Disable Backup button until user provides Filename this.__internal.buttons[0].element.disabled = true; @@ -487,13 +510,14 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { alertify.changeServerPassword(d).resizeTo('40%','52%'); return false; }, + /* Pause WAL Replay */ pause_wal_replay: function(args) { - var input = args || {}; - obj = this, - t = pgBrowser.tree, - i = input.item || t.selected(), - d = i && i.length == 1 ? t.itemData(i) : undefined; + var input = args || {}, + obj = this, + t = pgBrowser.tree, + i = input.item || t.selected(), + d = i && i.length == 1 ? t.itemData(i) : undefined; if (!d) return false; @@ -528,13 +552,14 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { } }) }, + /* Resume WAL Replay */ resume_wal_replay: function(args) { - var input = args || {}; - obj = this, - t = pgBrowser.tree, - i = input.item || t.selected(), - d = i && i.length == 1 ? t.itemData(i) : undefined; + var input = args || {}, + obj = this, + t = pgBrowser.tree, + i = input.item || t.selected(), + d = i && i.length == 1 ? t.itemData(i) : undefined; if (!d) return false; @@ -709,66 +734,157 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { isConnected: function(model) { return model.get('connected'); } - }) - }); - function connect_to_server(obj, data, tree, item) { - var onFailure = function(xhr, status, error, _model, _data, _tree, _item) { - - tree.setInode(_item); - tree.addIcon(_item, {icon: 'icon-server-not-connected'}); - - alertify.pgNotifier('error', xhr, error, function(msg) { - setTimeout(function() { - alertify.dlgServerPass( - '{{ _('Connect to Server') }}', - msg, _model, _data, _tree, _item - ).resizeTo(); - }, 100); - }); - }, - onSuccess = function(res, model, data, tree, item) { - tree.deselect(item); - tree.setInode(item); + }), + connection_lost: function(i, resp) { + if (pgBrowser.tree) { + var t = pgBrowser.tree, + info = i && this.getTreeNodeHierarchy(i), + s = null, + d = i && t.itemData(i), + self = this; - if (res && res.data) { + while (d && d._type != 'server') { + i = t.parent(i); + d = i && t.itemData(i); + } - if (typeof res.data.icon == 'string') { - tree.removeIcon(item); - data.icon = res.data.icon; - tree.addIcon(item, {icon: data.icon}); + if (i && d && d._type == 'server') { + if (_.isUndefined(d.is_connecting) || !d.is_connecting) { + d.is_connecting = true; + + var disconnect = function(_sid) { + if (d._id == _sid) { + d.is_connecting = false; + // Stop listening to the connection cancellation event + pgBrowser.Events.off( + 'pgadmin:server:connect:cancelled', disconnect + ); + + // Connection to the database will also be cancelled + pgBrowser.Events.trigger( + 'pgadmin:database:connect:cancelled',_sid, + resp.data.database || d.db + ); + + // Make sure - the server is disconnected properly + pgBrowser.Events.trigger( + 'pgadmin:server:disconnect', + {item: _i, data: _d}, false + ); + } + }; + + // Listen for the server connection cancellation event + pgBrowser.Events.on( + 'pgadmin:server:connect:cancelled', disconnect + ); + alertify.confirm( + '{{ _('Connection lost') }}', + '{{ _('Would you like to reconnect to the database?') }}', + function() { + connect_to_server(self, d, t, i, true); + }, + function() { + d.is_connecting = false; + t.unload(i); + t.setInode(i); + t.addIcon(i, {icon: 'icon-database-not-connected'}); + pgBrowser.Events.trigger( + 'pgadmin:server:connect:cancelled', i, d, self + ); + t.select(i); + }); + } } + } + } + }); + function connect_to_server(obj, data, tree, item, reconnect) { + var wasConnected = reconnect || data.connected, + onFailure = function( + xhr, status, error, _node, _data, _tree, _item, _wasConnected + ) { + data.connected = false; + + // It should be attempt to reconnect. + // Let's not change the status of the tree node now. + if (!_wasConnected) { + tree.setInode(_item); + tree.addIcon(_item, {icon: 'icon-server-not-connected'}); + } - // Update 'in_recovery' and 'wal_pause' options at server node - tree.itemData(item).in_recovery=res.data.in_recovery; - tree.itemData(item).wal_pause=res.data.wal_pause; + alertify.pgNotifier('error', xhr, error, function(msg) { + setTimeout(function() { + alertify.dlgServerPass( + '{{ _('Connect to Server') }}', + msg, _node, _data, _tree, _item, _wasConnected + ).resizeTo(); + }, 100); + }); + }, + onSuccess = function(res, node, data, tree, item, _wasConnected) { + if (res && res.data) { + if (typeof res.data.icon == 'string') { + tree.removeIcon(item); + data.icon = res.data.icon; + tree.addIcon(item, {icon: data.icon}); + } - _.extend(data, res.data); + _.extend(data, res.data); + data.is_connecting = false; - var serverInfo = pgBrowser.serverInfo = pgBrowser.serverInfo || {}; - serverInfo[data._id] = _.extend({}, data); + var serverInfo = pgBrowser.serverInfo = + pgBrowser.serverInfo || {}; + serverInfo[data._id] = _.extend({}, data); - alertify.success(res.info); - obj.trigger('server-connected', obj, item, data); + alertify.success(res.info); + obj.trigger('connected', obj, item, data); - setTimeout(function() { - tree.select(item); - tree.open(item); - }, 10); + // Generate the event that server is connected + pgBrowser.Events.trigger( + 'pgadmin:server:connected', data._id, item, data + ); + // Generate the event that database is connected + pgBrowser.Events.trigger( + 'pgadmin:database:connected', data._id, data.db, item, data + ); - } - }; + // We're not reconnecting + if (!_wasConnected) { + tree.setInode(item); + tree.deselect(item); + + setTimeout(function() { + tree.select(item); + tree.open(item); + }, 10); + } else { + // We just need to refresh the tree now. + setTimeout(function() { + node.callbacks.refresh.apply(node, [true]); + }, 10); + } + } + }; // Ask Password and send it back to the connect server if (!alertify.dlgServerPass) { alertify.dialog('dlgServerPass', function factory() { return { - main: function(title, message, model, data, tree, item) { + main: function( + title, message, node, data, tree, item, + _status, _onSuccess, _onFailure, _onCancel + ) { this.set('title', title); this.message = message; this.tree = tree; this.nodeData = data; this.nodeItem = item; - this.nodeModel = model; + this.node= node; + this.connected = _status; + this.onSuccess = _onSuccess || onSuccess; + this.onFailure = _onFailure || onFailure; + this.onCancel = _onCancel || onCancel; }, setup:function() { return { @@ -791,18 +907,24 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { this.setContent(this.message); }, callback: function(closeEvent) { - var _sdata = this.nodeData, - _tree = this.tree, + var _tree = this.tree, _item = this.nodeItem, - _model = this.nodeModel; + _node = this.node, + _data = this.nodeData, + _status = this.connected, + _onSuccess = this.onSuccess, + _onFailure = this.onFailure, + _onCancel = this.onCancel; if (closeEvent.button.text == "{{ _('OK') }}") { - var _url = _model.generate_url(_item, 'connect', _sdata, true); + var _url = _node.generate_url(_item, 'connect', _data, true); - _tree.setLeaf(_item); - _tree.removeIcon(_item); - _tree.addIcon(_item, {icon: 'icon-server-connecting'}); + if (!_status) { + _tree.setLeaf(_item); + _tree.removeIcon(_item); + _tree.addIcon(_item, {icon: 'icon-server-connecting'}); + } $.ajax({ type: 'POST', @@ -810,38 +932,58 @@ function($, _, S, pgAdmin, pgBrowser, alertify) { url: _url, data: $('#frmPassword').serialize(), success: function(res) { - return onSuccess( - res, _model, _sdata, _tree, _item + return _onSuccess( + res, _node, _data, _tree, _item, _status ); }, error: function(xhr, status, error) { - return onFailure( - xhr, status, error, _model, _sdata, _tree, _item + return _onFailure( + xhr, status, error, _node, _data, _tree, _item, _status ); } }); } else { - _tree.setInode(_item); - _tree.removeIcon(_item); - _tree.addIcon(_item, {icon: 'icon-server-not-connected'}); + this.onCancel && typeof(this.onCancel) == 'function' && + this.onCancel(_tree, _item, _data, _status); } } }; }); } + var onCancel = function(_tree, _item, _data, _status) { + _data.is_connecting = false; + _tree.unload(_item); + _tree.setInode(_item); + _tree.removeIcon(_item); + _tree.addIcon(_item, {icon: 'icon-server-not-connected'}); + obj.trigger('connect:cancelled', data._id, data.db, obj, _item, _data); + pgBrowser.Events.trigger( + 'pgadmin:server:connect:cancelled', data._id, _item, _data, obj + ); + pgBrowser.Events.trigger( + 'pgadmin:database:connect:cancelled', data._id, data.db, _item, _data, obj + ); + if (_status) { + _tree.select(_item); + } + }; + + data.is_connecting = true; url = obj.generate_url(item, "connect", data, true); $.post(url) - .done( - function(res) { - if (res.success == 1) { - return onSuccess(res, obj, data, tree, item); - } - }) - .fail( - function(xhr, status, error) { - return onFailure(xhr, status, error, obj, data, tree, item); - }); + .done(function(res) { + if (res.success == 1) { + return onSuccess( + res, obj, data, tree, item, wasConnected + ); + } + }) + .fail(function(xhr, status, error) { + return onFailure( + xhr, status, error, obj, data, tree, item, wasConnected + ); + }); } /* Send PING to indicate that session is alive */ diff --git a/web/pgadmin/browser/templates/browser/js/collection.js b/web/pgadmin/browser/templates/browser/js/collection.js index 12c80b7..487f23e 100644 --- a/web/pgadmin/browser/templates/browser/js/collection.js +++ b/web/pgadmin/browser/templates/browser/js/collection.js @@ -114,14 +114,24 @@ function($, _, S, pgAdmin, Backbone, Alertify, Backform) { // Fetch Data collection.fetch({reset: true}) - .error(function(jqxhr, error, message) { + .error(function(xhr, error, message) { + pgBrowser.Events.trigger( + 'pgadmin:collection:retrieval:error', 'properties', xhr, error, message, item, that + ); + if ( + !Alertify.pgHandleItemError(xhr, error, message, {item: item, info: info}) + ) { Alertify.pgNotifier( - error, jqxhr, + error, xhr, S( "{{ _("Error retrieving properties: %s.") }}" - ).sprintf(message).value() - ); - }); + ).sprintf(message).value(), + function() { + console.log(arguments); + } + ); + } + }); }, generate_url: function(item, type, d) { var url = pgAdmin.Browser.URL + '{TYPE}/{REDIRECT}{REF}', diff --git a/web/pgadmin/browser/templates/browser/js/messages.js b/web/pgadmin/browser/templates/browser/js/messages.js index cac7994..da71596 100644 --- a/web/pgadmin/browser/templates/browser/js/messages.js +++ b/web/pgadmin/browser/templates/browser/js/messages.js @@ -14,8 +14,8 @@ function(_, S, pgAdmin) { 'SQL_TAB': '{{ _('SQL') }}', 'SQL_INCOMPLETE': '{{ _('Incomplete definition') }}', 'SQL_NO_CHANGE': '{{ _('Nothing changed')|safe }}', - 'MUST_BE_INT' : "{{ _("'%s' must be an integer.")|safe }}", - 'MUST_BE_NUM' : "{{ _("'%s' must be a numeric.")|safe }}", + 'MUST_BE_INT' : "{{ _("'%s' must be an integer.") }}", + 'MUST_BE_NUM' : "{{ _("'%s' must be a numeric.") }}", 'MUST_GR_EQ' : "{{ _("'%s' must be greater than or equal to %d.")|safe }}", 'MUST_LESS_EQ' : "{{ _("'%s' must be less than or equal to %d.")|safe }}", 'STATISTICS_LABEL': "{{ _("Statistics") }}", @@ -24,12 +24,13 @@ function(_, S, pgAdmin) { 'NODE_HAS_NO_STATISTICS': "{{ _("No statistics are available for the selected object.") }}", 'TRUE': "{{ _("True") }}", 'FALSE': "{{ _("False") }}", - 'NOTE_CTRL_LABEL': "{{ _("Note") }}" + 'NOTE_CTRL_LABEL': "{{ _("Note") }}", + 'ERR_RETRIEVAL_INFO': "{{ _("Error retrieving the information:
%s") }}", + 'CONNECTION_LOST': "{{ _("Connection to the server has been lost!") }}" }; {% for key in current_app.messages.keys() %} - messages['{{ key|safe }}'] = '{{ current_app.messages[key]|safe }}'; - + messages['{{ key }}'] = '{{ current_app.messages[key] }}'; {% endfor %} return pgBrowser.messages; diff --git a/web/pgadmin/browser/templates/browser/js/node.js b/web/pgadmin/browser/templates/browser/js/node.js index 06152f6..5006232 100644 --- a/web/pgadmin/browser/templates/browser/js/node.js +++ b/web/pgadmin/browser/templates/browser/js/node.js @@ -36,6 +36,14 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) { // Make sure - a child have all the callbacks of the parent. child.callbacks = _.extend({}, parent.callbacks, props.callbacks); + var bindToChild = function(cb) { + if (typeof(child.callbacks[cb]) == 'function') { + child.callbacks[cb] = child.callbacks[cb].bind(child); + } + }, + callbacks = _.keys(child.callbacks); + for(var idx = 0; idx < callbacks.length; idx++) bindToChild(callbacks[idx]); + // Registering the node by calling child.Init(...) function child.Init.apply(child); @@ -174,7 +182,7 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) { // // Used to generate view for the particular node properties, edit, // creation. - getView: function(item, type, el, node, formType, callback, ctx) { + getView: function(item, type, el, node, formType, callback, ctx, cancelFunc) { var that = this; if (!this.type || this.type == '') @@ -263,26 +271,42 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) { } if (!newModel.isNew()) { + var self = this; // This is definetely not in create mode newModel.fetch() - .success(function(res, msg, xhr) { - // We got the latest attributes of the - // object. Render the view now. - view.render(); - if (type != 'properties') { - $(el).focus(); - } - newModel.startNewSession(); - }) - .error(function(jqxhr, error, message) { - // TODO:: We may not want to continue from here + .success(function(res, msg, xhr) { + // We got the latest attributes of the + // object. Render the view now. + view.render(); + if (type != 'properties') { + $(el).focus(); + } + newModel.startNewSession(); + }) + .error(function(xhr, error, message) { + pgBrowser.Events.trigger( + 'pgadmin:node:retrieval:error', 'properties', + xhr, error, message, item, self + ); + if ( + !Alertify.pgHandleItemError( + xhr, error, message, {item: item, info: info} + ) + ) { Alertify.pgNotifier( - error, jqxhr, + error, xhr, S( "{{ _("Error retrieving properties: %s.") }}" - ).sprintf(message).value() - ); - }); + ).sprintf(message).value(), + function() { + console.log(arguments); + } + ); + } + }); + if (cancelFunc) { + cancelFunc(); + } } else { // Yay - render the view now! $(el).focus(); @@ -1039,7 +1063,7 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) { }; // Create a view to edit/create the properties in fieldsets - view = that.getView(item, action, content, data, 'dialog', updateButtons, j); + view = that.getView(item, action, content, data, 'dialog', updateButtons, j, onCancelFunc); if (view) { // Save it to release it later j.data('obj-view', view); @@ -1110,7 +1134,7 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) { if(pnlDependents) $(pnlDependents).removeData('node-prop'); }, - error: function(m, jqxhr) { + error: function(jqxhr) { Alertify.pgNotifier( "error", jqxhr, S( diff --git a/web/pgadmin/misc/depends/static/js/depends.js b/web/pgadmin/misc/depends/static/js/depends.js index 3acd309..648ae01 100644 --- a/web/pgadmin/misc/depends/static/js/depends.js +++ b/web/pgadmin/misc/depends/static/js/depends.js @@ -226,7 +226,29 @@ define( $gridContainer.removeClass('hidden'); // Set the url, fetch the data and update the collection collection.url = url; - collection.fetch({ reset: true }); + collection.fetch({ + reset: true, + error: function(xhr, status, error) { + pgBrowser.Events.trigger( + 'pgadmin:node:retrieval:error', 'depends', xhr, status, error, item, that + ); + if ( + !Alertify.pgHandleItemError(xhr, error, message, { + item: item, info: treeHierarchy + }) + ) { + Alertify.pgNotifier( + status, xhr, + S( + pgBrowser.messages['ERR_RETRIEVAL_INFO'] + ).sprintf(error).value(), + function() { + console.log(arguments); + } + ); + } + } + }); } } if (msg != '') { diff --git a/web/pgadmin/misc/sql/static/js/sql.js b/web/pgadmin/misc/sql/static/js/sql.js index a6db150..7bfcf52 100644 --- a/web/pgadmin/misc/sql/static/js/sql.js +++ b/web/pgadmin/misc/sql/static/js/sql.js @@ -1,6 +1,6 @@ define( - ['underscore', 'jquery', 'pgadmin.browser'], -function(_, $, pgBrowser) { + ['underscore', 'underscore.string', 'jquery', 'pgadmin.browser'], +function(_, S, $, pgBrowser) { pgBrowser.ShowNodeSQL = pgBrowser.ShowNodeSQL || {}; @@ -84,8 +84,25 @@ function(_, $, pgBrowser) { pgAdmin.Browser.editor.setValue(res); } }, - error: function() { - // TODO:: Report this + error: function(xhr, status, error) { + pgBrowser.Events.trigger( + 'pgadmin:node:retrieval:error', 'sql', xhr, status, error, item, that + ); + if ( + !Alertify.pgHandleItemError(xhr, error, message, { + item: item, info: treeHierarchy + }) + ) { + Alertify.pgNotifier( + status, xhr, + S( + pgBrowser.messages['ERR_RETRIEVAL_INFO'] + ).sprintf(error).value(), + function() { + console.log(arguments); + } + ); + } } }); } diff --git a/web/pgadmin/misc/statistics/static/js/statistics.js b/web/pgadmin/misc/statistics/static/js/statistics.js index c1a0327..0beeed3 100644 --- a/web/pgadmin/misc/statistics/static/js/statistics.js +++ b/web/pgadmin/misc/statistics/static/js/statistics.js @@ -1,6 +1,7 @@ -define( - ['underscore', 'jquery', 'pgadmin.browser', 'backgrid', 'wcdocker', 'pgadmin.backgrid'], -function(_, $, pgBrowser, Backgrid) { +define([ + 'underscore', 'underscore.string', 'jquery', 'pgadmin.browser', 'backgrid', + 'wcdocker', 'pgadmin.backgrid' +], function(_, S, $, pgBrowser, Backgrid) { if (pgBrowser.NodeStatistics) return pgBrowser.NodeStatistics; @@ -210,8 +211,25 @@ function(_, $, pgBrowser, Backgrid) { $msgContainer.removeClass('hidden'); } }, - error: function() { - // TODO:: Report this error. + error: function(xhr, status, error) { + pgBrowser.Events.trigger( + 'pgadmin:node:retrieval:error', 'statistics', xhr, status, error, item, that + ); + if ( + !Alertify.pgHandleItemError(xhr, error, message, { + item: item, info: treeHierarchy + }) + ) { + Alertify.pgNotifier( + status, xhr, + S( + pgBrowser.messages['ERR_RETRIEVAL_INFO'] + ).sprintf(error).value(), + function() { + console.log(arguments); + } + ); + } } }); } diff --git a/web/pgadmin/static/js/alertifyjs/pgadmin.defaults.js b/web/pgadmin/static/js/alertifyjs/pgadmin.defaults.js index f1b7472..d7e9614 100644 --- a/web/pgadmin/static/js/alertifyjs/pgadmin.defaults.js +++ b/web/pgadmin/static/js/alertifyjs/pgadmin.defaults.js @@ -95,7 +95,7 @@ function(alertify, S) { if (contentType) { try { - if (contentType.indexOf('text/json') == 0) { + if (contentType.indexOf('application/json') == 0) { resp = $.parseJSON(msg); if (resp.result != null && (!resp.errormsg || resp.errormsg == '') && @@ -104,17 +104,19 @@ function(alertify, S) { } msg = _.escape(resp.result) || _.escape(resp.errormsg) || "Unknown error"; } - } catch (exc) { - } - - if (contentType.indexOf('text/html') == 0) { - alertify.notify( + if (contentType.indexOf('text/html') == 0) { + alertify.notify( S( - '%s

' + window.pgAdmin.Browser.messages.CLICK_FOR_DETAILED_MSG - ).sprintf(promptmsg).value(), type, 0, function() { - alertify.pgIframeDialog().show().set({ frameless: false }).set('pg_msg', msg); + '%s

' + + window.pgAdmin.Browser.messages.CLICK_FOR_DETAILED_MSG + ).sprintf(promptmsg).value(), type, 0, function() { + alertify.pgIframeDialog().show().set({frameless: false}).set( + 'pg_msg', msg + ); }); - return; + return; + } + } catch (exc) { } } alertify.alert().show().set( @@ -185,4 +187,125 @@ function(alertify, S) { this.set('onrestored', alertifyDialogResized); }; + alertify.pgHandleItemError = function(xhr, error, message, args) { + var pgBrowser = window.pgAdmin.Browser; + + if (!xhr || !pgBrowser) { + return; + } + + var msg = xhr.responseText, + contentType = xhr.getResponseHeader('Content-Type'), + msg = xhr.responseText, + jsonResp = contentType && + contentType.indexOf('application/json') == 0 && + $.parseJSON(xhr.responseText); + + if ( + jsonResp && ( + xhr.status == 503 ? ( + jsonResp.info == 'CONNECTION_LOST' && + 'server' in args.info && jsonResp.data.sid >= 0 && + jsonResp.data.sid == args.info.server._id + ) : ( + xhr.status == 428 && + jsonResp.errormsg && + jsonResp.errormsg == pgBrowser.messages.CONNECTION_LOST + ) + ) + ) { + if ( + args.preHandleConnectionLost && + typeof(args.preHandleConnectionLost) == 'function' + ) { + args.preHandleConnectionLost.apply(this, arguments); + } + + // Check the status of the maintenance server connection. + var server = pgBrowser.Nodes['server'], + ctx = { + resp: jsonResp, + xhr: xhr, + args: args + }, + reconnectServer = function() { + var ctx = this, + onServerConnect = function(_sid, _i, _d) { + // Yay - server is reconnected. + if (this.args.info.server._id == _sid) { + pgBrowser.Events.off( + 'pgadmin:server:connected', onServerConnect + ); + pgBrowser.Events.off( + 'pgadmin:server:connect:cancelled', onConnectCancel + ); + + // Do we need to connect the disconnected server now? + if ( + this.resp.data.database && + this.resp.data.database != _d.db + ) { + // Server is connected now, we will need to inform the + // database to connect it now. + pgBrowser.Events.trigger( + 'pgadmin:database:connection:lost', this.args.item, + this.resp, true + ); + } + } + }.bind(ctx), + onConnectCancel = function(_sid, _item, _data) { + // User has cancelled the operation in between. + if (_sid == this.args.info.server.id) { + pgBrowser.Events.off('pgadmin:server:connected', onServerConnect); + pgBrowser.Events.off('pgadmin:server:connect:cancelled', onConnectCancel); + + // Connection to the database will also be cancelled + pgBrowser.Events.trigger( + 'pgadmin:database:connect:cancelled', _sid, + this.resp.data.database || _data.db, _item, _data + ); + } + }.bind(ctx); + + pgBrowser.Events.on('pgadmin:server:connected', onServerConnect); + pgBrowser.Events.on('pgadmin:server:connect:cancelled', onConnectCancel); + + // Connection to the server has been lost, we need to inform the + // server first to take the action first. + pgBrowser.Events.trigger( + 'pgadmin:server:connection:lost', this.args.item, this.resp + ); + }.bind(ctx); + + $.ajax({ + url: server.generate_url( + null, 'connect', args.info.server, true, args.info + ), + dataType: 'json', + success: function(res) { + if (res.success && 'connected' in res.data) { + if (res.data.connected) { + // Server is connected, but - the connection with the + // particular database has been lost. + pgBrowser.Events.trigger( + 'pgadmin:database:connection:lost', args.item, jsonResp + ); + return; + } + } + + // Serever was not connected, we should first try to connect + // the server. + reconnectServer(); + }, + error: function() { + reconnectServer(); + } + }); + return true; + } + return false; + }; + });