diff --git a/web/pgadmin/browser/templates/browser/js/browser.js b/web/pgadmin/browser/templates/browser/js/browser.js index 151d05b..0ac4616 100644 --- a/web/pgadmin/browser/templates/browser/js/browser.js +++ b/web/pgadmin/browser/templates/browser/js/browser.js @@ -820,6 +820,11 @@ function(require, $, _, S, Bootstrap, pgAdmin, Alertify, CodeMirror) { ctx.b._findTreeChildNode( ctx.i, d, ctx ); + + // Only server-group node will not have any parent + if (!_data._pid) { + addNodeWithNoParent.apply(ctx, arguments); + } } return true; }.bind(ctx), @@ -958,7 +963,7 @@ function(require, $, _, S, Bootstrap, pgAdmin, Alertify, CodeMirror) { }); }.bind(ctx); - if (!ctx.t.isInode(ctx.i) && ctx.d.inode) { + if (!ctx.t.isInode(ctx.i) && _data && _data.inode) { ctx.t.setInode(ctx.i, {success: _append}); } else { _append(); @@ -997,6 +1002,94 @@ function(require, $, _, S, Bootstrap, pgAdmin, Alertify, CodeMirror) { // We can find the collection node using _findTreeChildNode // indirectly. findChildNode(selectNode, addNode); + }.bind(ctx), + addNodeWithNoParent = function() { + // Append the new data in the tree at appropriate postition. + var ctx = this, + items = ctx.t.children(), + s = 0, e = items.length - 1, i, + linearSearch = function() { + while (e >= s) { + i = items.eq(s); + d = ctx.t.itemData(i); + if (d.label > _data.label) + return true; + s++; + } + if (e != items.length - 1) { + i = items.eq(e); + return true; + } + i = null; + return false; + }, + binarySearch = function() { + var d, m; + // Binary search only outperforms Linear search for n > 44. + // Reference: + // https://en.wikipedia.org/wiki/Binary_search_algorithm#cite_note-30 + // + // We will try until it's half. + while (e - s > 22) { + i = items.eq(s); + d = ctx.t.itemData(i); + if (d.label > _data.label) + return true; + i = items.eq(e); + d = ctx.t.itemData(i); + if (d.label < _data.label) + return true; + m = Math.round((e - s) / 2); + i = items.eq(e); + d = ctx.t.itemData(i); + if (d.label < _data.label) { + s = m + 1; + e--; + } else { + s++; + e = m - 1; + } + } + return linearSearch(); + }; + + if (binarySearch()) { + ctx.t.before(i, { + itemData: _data, + success: function() { + if ( + ctx.o && ctx.o.success && typeof(ctx.o.success) == 'function' + ) { + ctx.o.success.apply(ctx.t, [i, _data]); + } + }, + fail: function() { + if ( + ctx.o && ctx.o.fail && typeof(ctx.o.fail) == 'function' + ) { + ctx.o.fail.apply(ctx.t, [i, _data]); + } + } + }); + } else { + ctx.t.append(ctx.i, { + itemData: _data, + success: function() { + if ( + ctx.o && ctx.o.success && typeof(ctx.o.success) == 'function' + ) { + ctx.o.success.apply(ctx.t, [ctx.i, _old, _new]); + } + }, + fail: function() { + if ( + ctx.o && ctx.o.fail && typeof(ctx.o.fail) == 'function' + ) { + ctx.o.fail.apply(ctx.t, [ctx.i, _old, _new]); + } + } + }); + } }.bind(ctx); if (!ctx.t.wasInit() || !_data) {