From 944bdbb96c3b360c856d7586144b835e44afac43 Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Fri, 8 Apr 2016 11:00:48 +0530 Subject: [PATCH] Improvised the 'transform/options' function usage with the Select2Cell. The current implementaton binds the cell/control object, and the ajax data in the asychronous Cells/Controls with the 'options' functions extended from the Select2Cell. The problem starts when we try to fetch the current model from that options/transform/filter function to do some operation, which does not require in most of the cases. Except the privileges control - where we needed the current model for omitting the existing selected object during transformation, and filtering. In order resolved the issue, we need a common object, which is shared among the Cell. In backgrid, the 'Column' object is mong the cell, hence - implementation logic has been changed to bid the 'Column' object with the 'options' function and, passed the 'Cell' object as an arguments. Because - we do use the common function 'transform' between 'Control' and 'Cell', we needed make changes in the Select2Control to pass the Control object as an arguments. And, make the changes in the privileges control to use the new implementation. The same logic is also required in some of the operations, we will be/are working on the table/column nodes. --- .../servers/static/js/privilege.js | 20 +++++------ web/pgadmin/browser/static/js/node.ui.js | 33 +++++++++---------- web/pgadmin/static/js/backform.pgadmin.js | 15 ++++++--- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/web/pgadmin/browser/server_groups/servers/static/js/privilege.js b/web/pgadmin/browser/server_groups/servers/static/js/privilege.js index 0a99fa372..b63f6f6f6 100644 --- a/web/pgadmin/browser/server_groups/servers/static/js/privilege.js +++ b/web/pgadmin/browser/server_groups/servers/static/js/privilege.js @@ -102,7 +102,7 @@ // override only once. if (opts && opts.column && opts.column instanceof Backbone.Model && - opts.column.has('orig_options')) { + opts.column.get('options_cached')) { override_opts = false; } Backgrid.Extension.NodeListByNameCell.prototype.initialize.apply( @@ -112,8 +112,7 @@ // Let's override the options if (override_opts) { var opts = self.column.get('options'); - self.column.set('options', self.omit_selected_roles); - self.column.set('orig_options', opts); + self.column.set('options', self.omit_selected_roles.bind(self, opts)); } var rerender = function (m) { @@ -135,17 +134,16 @@ this.listenTo(self.model.collection, "remove", rerender, this); }, // Remove all the selected roles (though- not mine). - omit_selected_roles: function() { - var self = this, - opts = self.column.get('orig_options'), - res = opts.apply(this), + omit_selected_roles: function(opts, cell) { + var res = opts(cell), selected = {}, - cid = self.model.cid, - curr_user = self.model.top.node_info.server.user.name; + model = cell.model, + cid = model.cid, + curr_user = model.top.node_info.server.user.name; var idx = 0; - this.model.collection.each(function(m) { + model.collection.each(function(m) { var grantee = m.get('grantee'); if (m.cid != cid && !_.isUndefined(grantee) && @@ -158,8 +156,6 @@ return !(o.value in selected); }); - this.model.collection.available_roles = {}; - return res; } }), diff --git a/web/pgadmin/browser/static/js/node.ui.js b/web/pgadmin/browser/static/js/node.ui.js index 4d69e1f01..7ee0ef8bf 100644 --- a/web/pgadmin/browser/static/js/node.ui.js +++ b/web/pgadmin/browser/static/js/node.ui.js @@ -153,8 +153,7 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { * and schema has disabled property then we need to apply it */ if(!_.has(select2_opts, 'disabled') && (d && d.disabled)) { - _.extend(select2_opts, { - disabled: evalF(d.disabled, d, this.model) + _.extend(select2_opts, {disabled: evalF(d.disabled, d, this.model) }); } @@ -178,9 +177,11 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { if(!optimage){ return opt.text; } else { - return $( - '' + opt.text + '' - ); + return $('').append( + $('', {class: "wcTabIcon " + optimage}) + ).append( + $('').text(opt.text) + ); } }; @@ -383,7 +384,7 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { // We will transform the data later, when rendering. // It will allow us to generate different data based on the // dependencies. - column.set('options', transform.bind(self, data)); + column.set('options', transform.bind(column, data)); } else { column.set('options', data); } @@ -401,7 +402,7 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { editable = Backgrid.callByNeed(col.editable, column, model), optionValues = _.clone(this.optionValues || _.isFunction(this.column.get('options')) ? - this.column.get('options').apply(this) : + (this.column.get('options'))(this) : this.column.get('options')), select2_opts = _.defaults({}, col.select2, this.defaults.select2), evalF = function(f, col, m) { @@ -443,13 +444,12 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { (_.indexOf(selectedValues, optionValue) > -1) })); } + // Initialize select2 control. this.$select.select2( - _.defaults( - {'disabled': !editable}, - col.select2, - this.defaults.select2 - )); + _.defaults( + {'disabled': !editable}, col.select2, this.defaults.select2 + )); /* * If select2 options do not have any disabled property on this cell @@ -460,7 +460,6 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { disabled: evalF(col.disabled, col, this.model) }); } - this.$el.find("select").select2(select2_opts); this.delegateEvents(); @@ -474,8 +473,8 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { defaults: _.extend({}, NodeAjaxOptionsCell.prototype.defaults, { url: 'nodes', filter: undefined, - transform: function(rows) { - var self = this, + transform: function(rows, control) { + var self = control || this, node = self.column.get('schema_node'), res = [], filter = self.column.get('filter') || function() { return true; }; @@ -518,8 +517,8 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { defaults: _.extend({}, NodeAjaxOptionsCell.prototype.defaults, { url: 'nodes', filter: undefined, - transform: function(rows) { - var self = this, + transform: function(rows, control) { + var self = control || this, node = self.column.get('schema_node'), res = [], filter = self.column.get('filter') || function() { return true; }; diff --git a/web/pgadmin/static/js/backform.pgadmin.js b/web/pgadmin/static/js/backform.pgadmin.js index b85a2cb58..8eaed6dda 100644 --- a/web/pgadmin/static/js/backform.pgadmin.js +++ b/web/pgadmin/static/js/backform.pgadmin.js @@ -339,11 +339,11 @@ // Evaluation the options if (_.isFunction(data.options)) { try { - data.options = data.options.apply(this) + data.options = data.options(this) } catch(e) { // Do nothing data.options = [] - this.model.trigger('pgadmin-view:transform:error', m, self.field, e); + this.model.trigger('pgadmin-view:transform:error', this.model, this.field, e); } } @@ -1732,12 +1732,19 @@ render: function() { Backform.SelectControl.prototype.render.apply(this, arguments); - var col = _.defaults(this.field.toJSON(), this.defaults) + var opts = this.field.toJSON(); + var select2Opts = _.defaults( + {}, opts.select2, (this.defaults && this.defaults.select2) || {} + ); + /* * Add empty option as Select2 requires any empty '')).select2(col.select2); + var $select = this.$el.find("select"); + $select.prepend($('')); + $select.select2(select2Opts); + return this; } });