From cc08708f231169ed865f746fb773bcfb537a55c4 Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Sun, 6 Mar 2016 18:50:05 +0530 Subject: [PATCH] Allow to specify special type - 'group' in schema for the browser node, which allows to determine certain group to hide/show the whole tab/fieldset control in the properties, and create dialog. Using this functionality in the database for the 'default privileges - types', which is not allowed before PG < 9.2 using the version check. --- .../templates/databases/js/databases.js | 10 ++-- web/pgadmin/static/js/backform.pgadmin.js | 47 +++++++++++++++++-- 2 files changed, 49 insertions(+), 8 deletions(-) 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 9b86d2f02..0078325e8 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 @@ -273,7 +273,7 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) { min_version: 90200 },{ type: 'nested', control: 'tab', group: '{{ _('Default Privileges') }}', - mode: ['edit', 'create'], + mode: ['edit'], schema:[{ id: 'deftblacl', model: pgAdmin.Browser.Node.PrivilegeRoleModel.extend( {privileges: ['a', 'r', 'w', 'd', 'D', 'x', 't']}), label: '{{ _('Default Privileges: Tables') }}', @@ -295,11 +295,15 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) { },{ id: 'deftypeacl', model: pgAdmin.Browser.Node.PrivilegeRoleModel.extend( {privileges: ['U']}), label: '{{ _('Default Privileges: Types') }}', - editable: false, type: 'collection', group: '{{ _('Types') }}', + editable: false, type: 'collection', group: 'deftypesacl_group', mode: ['edit', 'create'], control: 'unique-col-collection', canAdd: true, canDelete: true, uniqueCol : ['grantee', 'grantor'], min_version: 90200 - }] + },{ + id: 'deftypesacl_group', type: 'group', label: '{{ _('Types') }}', + mode: ['edit', 'create'], min_version: 90200 + } + ] } ], validate: function(keys) { diff --git a/web/pgadmin/static/js/backform.pgadmin.js b/web/pgadmin/static/js/backform.pgadmin.js index 328ef5e46..ead3802ce 100644 --- a/web/pgadmin/static/js/backform.pgadmin.js +++ b/web/pgadmin/static/js/backform.pgadmin.js @@ -464,7 +464,10 @@ controls = this.controls, tmpls = this.template, self = this, - idx=(this.tabIndex * 100); + idx=(this.tabIndex * 100), + evalF = function(f, d, m) { + return (_.isFunction(f) ? !!f.apply(d, [m]) : !!f); + }; this.$el .empty() @@ -478,6 +481,10 @@ .appendTo(this.$el); _.each(this.schema, function(o) { + idx++; + if (!o.version_compatible || !evalF(o.visible, o, m)) { + return; + } var el = $((tmpls['panel'])(_.extend(o, {'tabIndex': idx}))) .appendTo(tabContent) .removeClass('collapse').addClass('collapse'), @@ -493,7 +500,6 @@ el.append(cntr.render().$el); controls.push(cntr); }); - idx++; tabHead.find('a[data-toggle="tab"]').off( 'shown.bs.tab' @@ -558,11 +564,20 @@ 'legendClass': _.result(this, 'legendClass'), 'contentClass': _.result(this, 'contentClass'), 'collapse': _.result(this, 'collapse') + }, + idx=(this.tabIndex * 100), + evalF = function(f, d, m) { + return (_.isFunction(f) ? !!f.apply(d, [m]) : !!f); }; this.$el.empty(); _.each(this.schema, function(o) { + idx++; + if (!o.version_compatible || !evalF(o.visible, o, m)) { + return; + } + if (!o.fields) return; @@ -573,7 +588,8 @@ o.fields.each(function(f) { var cntr = new (f.get("control")) ({ field: f, - model: m + model: m, + tabIndex: idx }); el.append(cntr.render().$el); controls.push(cntr); @@ -1412,7 +1428,8 @@ ) { var proto = (Model && Model.prototype) || Model, schema = subschema || (proto && proto.schema), - pgBrowser = window.pgAdmin.Browser, fields = []; + pgBrowser = window.pgAdmin.Browser, fields = [], + groupInfo = {}; // 'schema' has the information about how to generate the form. if (schema && _.isArray(schema)) { @@ -1427,6 +1444,24 @@ _.each(schema, function(s) { // Do we understand - what control, we're creating // here? + if (s.type == 'group') { + var ver_in_limit = (_.isUndefined(server_info) ? true : + ((_.isUndefined(s.server_type) ? true : + (server_info.type in s.server_type)) && + (_.isUndefined(s.min_version) ? true : + (server_info.version >= s.min_version)) && + (_.isUndefined(s.max_version) ? true : + (server_info.version <= s.max_version)))); + groupInfo[s.id] = { + label: s.label || s.id, + version_compatible: ver_in_limit, + visible: !s.mode || ( + s && s.mode && _.isObject(s.mode) && + _.indexOf(s.mode, mode) != -1) && evalASFunc(s.visible) || true + }; + return; + } + if (!s.mode || (s && s.mode && _.isObject(s.mode) && _.indexOf(s.mode, mode) != -1)) { // Each field is kept in specified group, or in @@ -1516,7 +1551,9 @@ // Create an array from the dictionary with proper required // structure. _.each(groups, function(val, key) { - fields.push({label: key, fields: val}); + fields.push(_.extend({ + label: key, fields: val + }, (groupInfo[key] || {version_compatible: true, visible: true}))); }); }