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.
This commit is contained in:
Ashesh Vashi
2016-04-08 11:00:48 +05:30
parent 4ec4b4f43e
commit 944bdbb96c
3 changed files with 35 additions and 33 deletions

View File

@@ -102,7 +102,7 @@
// override only once. // override only once.
if (opts && opts.column && if (opts && opts.column &&
opts.column instanceof Backbone.Model && opts.column instanceof Backbone.Model &&
opts.column.has('orig_options')) { opts.column.get('options_cached')) {
override_opts = false; override_opts = false;
} }
Backgrid.Extension.NodeListByNameCell.prototype.initialize.apply( Backgrid.Extension.NodeListByNameCell.prototype.initialize.apply(
@@ -112,8 +112,7 @@
// Let's override the options // Let's override the options
if (override_opts) { if (override_opts) {
var opts = self.column.get('options'); var opts = self.column.get('options');
self.column.set('options', self.omit_selected_roles); self.column.set('options', self.omit_selected_roles.bind(self, opts));
self.column.set('orig_options', opts);
} }
var rerender = function (m) { var rerender = function (m) {
@@ -135,17 +134,16 @@
this.listenTo(self.model.collection, "remove", rerender, this); this.listenTo(self.model.collection, "remove", rerender, this);
}, },
// Remove all the selected roles (though- not mine). // Remove all the selected roles (though- not mine).
omit_selected_roles: function() { omit_selected_roles: function(opts, cell) {
var self = this, var res = opts(cell),
opts = self.column.get('orig_options'),
res = opts.apply(this),
selected = {}, selected = {},
cid = self.model.cid, model = cell.model,
curr_user = self.model.top.node_info.server.user.name; cid = model.cid,
curr_user = model.top.node_info.server.user.name;
var idx = 0; var idx = 0;
this.model.collection.each(function(m) { model.collection.each(function(m) {
var grantee = m.get('grantee'); var grantee = m.get('grantee');
if (m.cid != cid && !_.isUndefined(grantee) && if (m.cid != cid && !_.isUndefined(grantee) &&
@@ -158,8 +156,6 @@
return !(o.value in selected); return !(o.value in selected);
}); });
this.model.collection.available_roles = {};
return res; return res;
} }
}), }),

View File

@@ -153,8 +153,7 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
* and schema has disabled property then we need to apply it * and schema has disabled property then we need to apply it
*/ */
if(!_.has(select2_opts, 'disabled') && (d && d.disabled)) { if(!_.has(select2_opts, 'disabled') && (d && d.disabled)) {
_.extend(select2_opts, { _.extend(select2_opts, {disabled: evalF(d.disabled, d, this.model)
disabled: evalF(d.disabled, d, this.model)
}); });
} }
@@ -178,9 +177,11 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
if(!optimage){ if(!optimage){
return opt.text; return opt.text;
} else { } else {
return $( return $('<span></span>').append(
'<span><span class="wcTabIcon ' + optimage + '"/>' + opt.text + '</span>' $('<span></span>', {class: "wcTabIcon " + optimage})
); ).append(
$('<span></span>').text(opt.text)
);
} }
}; };
@@ -383,7 +384,7 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
// We will transform the data later, when rendering. // We will transform the data later, when rendering.
// It will allow us to generate different data based on the // It will allow us to generate different data based on the
// dependencies. // dependencies.
column.set('options', transform.bind(self, data)); column.set('options', transform.bind(column, data));
} else { } else {
column.set('options', data); column.set('options', data);
} }
@@ -401,7 +402,7 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
editable = Backgrid.callByNeed(col.editable, column, model), editable = Backgrid.callByNeed(col.editable, column, model),
optionValues = _.clone(this.optionValues || optionValues = _.clone(this.optionValues ||
_.isFunction(this.column.get('options')) ? _.isFunction(this.column.get('options')) ?
this.column.get('options').apply(this) : (this.column.get('options'))(this) :
this.column.get('options')), this.column.get('options')),
select2_opts = _.defaults({}, col.select2, this.defaults.select2), select2_opts = _.defaults({}, col.select2, this.defaults.select2),
evalF = function(f, col, m) { evalF = function(f, col, m) {
@@ -443,13 +444,12 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
(_.indexOf(selectedValues, optionValue) > -1) (_.indexOf(selectedValues, optionValue) > -1)
})); }));
} }
// Initialize select2 control. // Initialize select2 control.
this.$select.select2( this.$select.select2(
_.defaults( _.defaults(
{'disabled': !editable}, {'disabled': !editable}, col.select2, this.defaults.select2
col.select2, ));
this.defaults.select2
));
/* /*
* If select2 options do not have any disabled property on this cell * 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) disabled: evalF(col.disabled, col, this.model)
}); });
} }
this.$el.find("select").select2(select2_opts); this.$el.find("select").select2(select2_opts);
this.delegateEvents(); this.delegateEvents();
@@ -474,8 +473,8 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
defaults: _.extend({}, NodeAjaxOptionsCell.prototype.defaults, { defaults: _.extend({}, NodeAjaxOptionsCell.prototype.defaults, {
url: 'nodes', url: 'nodes',
filter: undefined, filter: undefined,
transform: function(rows) { transform: function(rows, control) {
var self = this, var self = control || this,
node = self.column.get('schema_node'), node = self.column.get('schema_node'),
res = [], res = [],
filter = self.column.get('filter') || function() { return true; }; filter = self.column.get('filter') || function() { return true; };
@@ -518,8 +517,8 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
defaults: _.extend({}, NodeAjaxOptionsCell.prototype.defaults, { defaults: _.extend({}, NodeAjaxOptionsCell.prototype.defaults, {
url: 'nodes', url: 'nodes',
filter: undefined, filter: undefined,
transform: function(rows) { transform: function(rows, control) {
var self = this, var self = control || this,
node = self.column.get('schema_node'), node = self.column.get('schema_node'),
res = [], res = [],
filter = self.column.get('filter') || function() { return true; }; filter = self.column.get('filter') || function() { return true; };

View File

@@ -339,11 +339,11 @@
// Evaluation the options // Evaluation the options
if (_.isFunction(data.options)) { if (_.isFunction(data.options)) {
try { try {
data.options = data.options.apply(this) data.options = data.options(this)
} catch(e) { } catch(e) {
// Do nothing // Do nothing
data.options = [] 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() { render: function() {
Backform.SelectControl.prototype.render.apply(this, arguments); 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 '<option><option>' for * Add empty option as Select2 requires any empty '<option><option>' for
* some of its functionality to work and initialize select2 control. * some of its functionality to work and initialize select2 control.
*/ */
this.$el.find("select").prepend($('<option></option>')).select2(col.select2); var $select = this.$el.find("select");
$select.prepend($('<option></option>'));
$select.select2(select2Opts);
return this; return this;
} }
}); });