mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Extract the generate_url(..) function from node.js, and collection.js
This commit is contained in:
committed by
Ashesh Vashi
parent
e9b80dae9c
commit
d527769bf8
@@ -1,8 +1,8 @@
|
|||||||
define([
|
define([
|
||||||
'sources/gettext', 'jquery', 'underscore', 'underscore.string', 'sources/pgadmin',
|
'sources/gettext', 'jquery', 'underscore', 'underscore.string', 'sources/pgadmin',
|
||||||
'backbone', 'alertify', 'backform', 'backgrid', 'pgadmin.backform', 'pgadmin.backgrid',
|
'backbone', 'alertify', 'backform', 'backgrid', 'sources/browser/generate_url', 'pgadmin.backform', 'pgadmin.backgrid',
|
||||||
'pgadmin.browser.node'
|
'pgadmin.browser.node'
|
||||||
], function(gettext, $, _, S, pgAdmin, Backbone, Alertify, Backform, Backgrid) {
|
], function(gettext, $, _, S, pgAdmin, Backbone, Alertify, Backform, Backgrid, generateUrl) {
|
||||||
|
|
||||||
var pgBrowser = pgAdmin.Browser = pgAdmin.Browser || {};
|
var pgBrowser = pgAdmin.Browser = pgAdmin.Browser || {};
|
||||||
|
|
||||||
@@ -135,41 +135,22 @@ define([
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
generate_url: function(item, type, d) {
|
generate_url: function(item, type) {
|
||||||
var url = pgAdmin.Browser.URL + '{TYPE}/{REDIRECT}{REF}',
|
/*
|
||||||
/*
|
* Using list, and collection functions of a node to get the nodes
|
||||||
* Using list, and collection functions of a node to get the nodes
|
* under the collection, and properties of the collection respectively.
|
||||||
* under the collection, and properties of the collection respectively.
|
*/
|
||||||
*/
|
var opURL = {
|
||||||
opURL = {
|
'properties': 'obj', 'children': 'nodes',
|
||||||
'properties': 'obj', 'children': 'nodes'
|
|
||||||
},
|
},
|
||||||
ref = '', self = this;
|
self = this;
|
||||||
|
var collectionPickFunction = function (treeInfoValue, treeInfoKey) {
|
||||||
_.each(
|
return (treeInfoKey != self.type);
|
||||||
_.sortBy(
|
|
||||||
_.values(
|
|
||||||
_.pick(
|
|
||||||
this.getTreeNodeHierarchy(item), function(v, k, o) {
|
|
||||||
return (k != self.type);
|
|
||||||
})
|
|
||||||
),
|
|
||||||
function(o) { return o.priority; }
|
|
||||||
),
|
|
||||||
function(o) {
|
|
||||||
ref = S('%s/%s').sprintf(ref, encodeURI(o._id)).value();
|
|
||||||
});
|
|
||||||
|
|
||||||
var args = {
|
|
||||||
'TYPE': self.node,
|
|
||||||
'REDIRECT': (type in opURL ? opURL[type] : type),
|
|
||||||
'REF': S('%s/').sprintf(ref).value()
|
|
||||||
};
|
};
|
||||||
|
var treeInfo = this.getTreeNodeHierarchy(item);
|
||||||
return url.replace(/{(\w+)}/g, function(match, arg) {
|
var actionType = type in opURL ? opURL[type] : type;
|
||||||
return args[arg];
|
return generateUrl.generate_url(pgAdmin.Browser.URL, treeInfo, actionType, self.node, collectionPickFunction);
|
||||||
});
|
},
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return pgBrowser.Collection;
|
return pgBrowser.Collection;
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ define(
|
|||||||
'pgadmin.browser.node', [
|
'pgadmin.browser.node', [
|
||||||
'sources/gettext', 'jquery', 'underscore', 'underscore.string', 'sources/pgadmin',
|
'sources/gettext', 'jquery', 'underscore', 'underscore.string', 'sources/pgadmin',
|
||||||
'pgadmin.browser.menu', 'backbone', 'pgadmin.alertifyjs', 'pgadmin.browser.datamodel',
|
'pgadmin.browser.menu', 'backbone', 'pgadmin.alertifyjs', 'pgadmin.browser.datamodel',
|
||||||
'backform', 'pgadmin.browser.utils', 'pgadmin.backform'
|
'backform', 'sources/browser/generate_url', 'pgadmin.browser.utils', 'pgadmin.backform'
|
||||||
], function(gettext, $, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) {
|
], function(gettext, $, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform, generateUrl) {
|
||||||
|
|
||||||
var wcDocker = window.wcDocker,
|
var wcDocker = window.wcDocker,
|
||||||
keyCode = {
|
keyCode = {
|
||||||
@@ -1428,26 +1428,27 @@ define(
|
|||||||
* depends, statistics
|
* depends, statistics
|
||||||
*/
|
*/
|
||||||
generate_url: function(item, type, d, with_id, info) {
|
generate_url: function(item, type, d, with_id, info) {
|
||||||
var url = pgBrowser.URL + '{TYPE}/{REDIRECT}{REF}',
|
|
||||||
opURL = {
|
var opURL = {
|
||||||
'create': 'obj', 'drop': 'obj', 'edit': 'obj',
|
'create': 'obj', 'drop': 'obj', 'edit': 'obj',
|
||||||
'properties': 'obj', 'statistics': 'stats'
|
'properties': 'obj', 'statistics': 'stats'
|
||||||
},
|
}, self = this,
|
||||||
ref = '', self = this,
|
|
||||||
priority = -Infinity;
|
priority = -Infinity;
|
||||||
|
|
||||||
info = (_.isUndefined(item) || _.isNull(item)) ?
|
var treeInfo = (_.isUndefined(item) || _.isNull(item)) ?
|
||||||
info || {} : this.getTreeNodeHierarchy(item);
|
info || {} : this.getTreeNodeHierarchy(item);
|
||||||
|
var actionType = type in opURL ? opURL[type] : type;
|
||||||
|
var itemID = with_id && d._type == self.type ? encodeURIComponent(d._id) : '';
|
||||||
|
|
||||||
if (self.parent_type) {
|
if (self.parent_type) {
|
||||||
if (_.isString(self.parent_type)) {
|
if (_.isString(self.parent_type)) {
|
||||||
var p = info[self.parent_type];
|
var p = treeInfo[self.parent_type];
|
||||||
if (p) {
|
if (p) {
|
||||||
priority = p.priority;
|
priority = p.priority;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_.each(self.parent_type, function(o) {
|
_.each(self.parent_type, function(o) {
|
||||||
var p = info[o];
|
var p = treeInfo[o];
|
||||||
if (p) {
|
if (p) {
|
||||||
if (priority < p.priority) {
|
if (priority < p.priority) {
|
||||||
priority = p.priority;
|
priority = p.priority;
|
||||||
@@ -1456,34 +1457,10 @@ define(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var nodePickFunction = function (treeInfoValue) {
|
||||||
_.each(
|
return (treeInfoValue.priority <= priority);
|
||||||
_.sortBy(
|
|
||||||
_.values(
|
|
||||||
_.pick(info,
|
|
||||||
function(v, k, o) {
|
|
||||||
return (v.priority <= priority);
|
|
||||||
})
|
|
||||||
),
|
|
||||||
function(o) { return o.priority; }
|
|
||||||
),
|
|
||||||
function(o) {
|
|
||||||
ref = S('%s/%s').sprintf(ref, encodeURIComponent(o._id)).value();
|
|
||||||
});
|
|
||||||
|
|
||||||
ref = S('%s/%s').sprintf(
|
|
||||||
ref, with_id && d._type == self.type ? encodeURIComponent(d._id) : ''
|
|
||||||
).value();
|
|
||||||
|
|
||||||
var args = {
|
|
||||||
'TYPE': self.type,
|
|
||||||
'REDIRECT': (type in opURL ? opURL[type] : type),
|
|
||||||
'REF': ref
|
|
||||||
};
|
};
|
||||||
|
return generateUrl.generate_url(pgBrowser.URL, treeInfo, actionType, self.type, nodePickFunction, itemID);
|
||||||
return url.replace(/{(\w+)}/g, function(match, arg) {
|
|
||||||
return args[arg];
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
// Base class for Node Data Collection
|
// Base class for Node Data Collection
|
||||||
Collection: pgBrowser.DataCollection,
|
Collection: pgBrowser.DataCollection,
|
||||||
|
|||||||
23
web/pgadmin/static/js/browser/generate_url.js
Normal file
23
web/pgadmin/static/js/browser/generate_url.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import _ from 'underscore';
|
||||||
|
|
||||||
|
function generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction, itemDataID) {
|
||||||
|
let ref = '';
|
||||||
|
_.each(
|
||||||
|
_.sortBy(
|
||||||
|
_.pick(treeInfo, pickFunction),
|
||||||
|
function (treeInfoItems) {
|
||||||
|
return treeInfoItems.priority;
|
||||||
|
}
|
||||||
|
),
|
||||||
|
function (treeInfoItems) {
|
||||||
|
ref = `${ref}/${encodeURI(treeInfoItems._id)}`;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
ref = itemDataID ? `${ref}/${itemDataID}` : `${ref}/`;
|
||||||
|
|
||||||
|
return `${baseUrl}${nodeType}/${actionType}${ref}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
generate_url: generate_url,
|
||||||
|
};
|
||||||
95
web/regression/javascript/browser/generate_url_spec.js
Normal file
95
web/regression/javascript/browser/generate_url_spec.js
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import {generate_url} from 'sources/browser/generate_url';
|
||||||
|
|
||||||
|
describe('generate_url', () => {
|
||||||
|
describe('in collection', () => {
|
||||||
|
let baseUrl, treeInfo, actionType, nodeType, pickFunction;
|
||||||
|
beforeEach(() => {
|
||||||
|
baseUrl = 'http://base/and-extension/';
|
||||||
|
treeInfo = {
|
||||||
|
treeNode1: {
|
||||||
|
_id: 'an_id',
|
||||||
|
priority: 1000,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
actionType = 'actionType';
|
||||||
|
nodeType = 'nodeType';
|
||||||
|
pickFunction = () => {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
it('returns a correctly formatted URL', () => {
|
||||||
|
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction);
|
||||||
|
|
||||||
|
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/an_id/');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('given there are multiple treeInfoItems', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
treeInfo['treeNode2'] = {
|
||||||
|
_id: 'another_id',
|
||||||
|
priority: 500,
|
||||||
|
};
|
||||||
|
treeInfo['treeNode3'] = {
|
||||||
|
_id: 'a_third_id',
|
||||||
|
priority: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
pickFunction = (value, key) => {
|
||||||
|
return key != 'treeNode2';
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('chooses the correct treeInfo', () => {
|
||||||
|
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction);
|
||||||
|
|
||||||
|
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/a_third_id/an_id/');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('in node', () => {
|
||||||
|
let baseUrl, treeInfo, actionType, nodeType, pickFunction, itemDataID;
|
||||||
|
beforeEach(() => {
|
||||||
|
baseUrl = 'http://base/and-extension/';
|
||||||
|
treeInfo = {
|
||||||
|
treeNode1: {
|
||||||
|
_id: 'an_id',
|
||||||
|
priority: 1000,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
actionType = 'actionType';
|
||||||
|
nodeType = 'nodeType';
|
||||||
|
pickFunction = () => {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
itemDataID = 'item1';
|
||||||
|
});
|
||||||
|
it('returns a correctly formatted URL', () => {
|
||||||
|
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction, itemDataID);
|
||||||
|
|
||||||
|
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/an_id/item1');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('given there are multiple treeInfoItems', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
treeInfo['treeNode2'] = {
|
||||||
|
_id: 'another_id',
|
||||||
|
priority: 500,
|
||||||
|
};
|
||||||
|
treeInfo['treeNode3'] = {
|
||||||
|
_id: 'a_third_id',
|
||||||
|
priority: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
pickFunction = (value) => {
|
||||||
|
return value.priority > 100;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('chooses the correct treeInfo', () => {
|
||||||
|
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction, itemDataID);
|
||||||
|
|
||||||
|
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/another_id/an_id/item1');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user