From 9b9126da15f0284e64d7f051f01fc54a403eb332 Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Tue, 12 Apr 2016 16:50:43 +0530 Subject: [PATCH] Improvised Select2Cell, and SwitchCell. Removed the use of separate editor for both of these cell types. There were two instance of select2 were getting created in the Select2Cell, one in the Select2Cell itself, and another in Select2CellEditor. And, loosing the focus mysteriously, and making the scrollbar in the property dialog non-responsive. Also, modified the NodeAjaxOptionsCell to use the above logic, and removed its own version of render function to make it consitent across the system. This patch [changes sent by Murtuza] also includes improvisation in the DeleteCell, and ObjectCell, which will honour now 'canRemoveRow', and ''canEditRow' respective properties of Column. --- web/pgadmin/browser/static/js/node.ui.js | 85 +---- .../static/js/backgrid/backgrid.pgadmin.js | 296 +++++++++--------- 2 files changed, 150 insertions(+), 231 deletions(-) diff --git a/web/pgadmin/browser/static/js/node.ui.js b/web/pgadmin/browser/static/js/node.ui.js index 7ee0ef8bf..c3968136e 100644 --- a/web/pgadmin/browser/static/js/node.ui.js +++ b/web/pgadmin/browser/static/js/node.ui.js @@ -314,10 +314,16 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { allowClear: true, placeholder: 'Select from the list', width: 'style' - } + }, + opt: { + label: null, + value: null, + image: null, + selected: false + } }), template: _.template( - '' + '' ), initialize: function () { Backgrid.Extension.Select2Cell.prototype.initialize.apply(this, arguments); @@ -390,81 +396,6 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { } column.set('options_cached', true); } - }, - render: function() { - /* - * Let SelectCell render it, we will do our magic on the - * select control in it. - */ - - var col = _.defaults(this.column.toJSON(), this.defaults), - model = this.model, column = this.column, - editable = Backgrid.callByNeed(col.editable, column, model), - optionValues = _.clone(this.optionValues || - _.isFunction(this.column.get('options')) ? - (this.column.get('options'))(this) : - this.column.get('options')), - select2_opts = _.defaults({}, col.select2, this.defaults.select2), - evalF = function(f, col, m) { - return (_.isFunction(f) ? !!f.apply(col, [m]) : !!f); - }; - - this.$el.empty(); - - if (!_.isArray(optionValues)) throw new TypeError("optionValues must be an array"); - - /* - * Add empty option as Select2 requires any empty ''].join(''), - null,{ - variable: null - }), - - initialize: function () { - Backgrid.SelectCellEditor.prototype.initialize.apply(this, arguments); - this.close = _.bind(this.close, this); - }, - /** - Renders a `select2` select box instead of the default `", {tabIndex: -1}).appendTo(this.$el); + selectedValues = model.get(this.column.get("name")), + self = this, + $select = self.$select = $('').appendTo(this.$el); for (var i = 0; i < optionValues.length; i++) { - var optionValue = optionValues[i]; + var opt = optionValues[i]; - if (_.isArray(optionValue)) { - optionText = optionValue[0]; - optionValue = optionValue[1]; + if (_.isArray(opt)) { + optionText = opt[0]; + optionValue = opt[1]; - this.$select.append( - this.template({ - text: optionText, - value: optionValue, - selected: (selectedValues == optionValue) || - (_.indexOf(selectedValues, optionValue) > -1) + $select.append( + self.template({ + text: optionText, + value: optionValue, + selected: (selectedValues == optionValue) || + (_.indexOf(selectedValues, optionValue) > -1) })); - } else { - throw new TypeError("optionValues elements must be a name-value pair."); + } else { + opt = _.defaults(opt, { + selected: ((selectedValues == opt.value) || + (_.indexOf(selectedValues, opt.value) > -1)) + }, self.defaults.opt); + $select.append(self.template(opt)); } } + + var select2_opts = _.extend( + {openOnEnter: false}, + col.select2, this.defaults.select2 + ); + + if(col && _.has(col.disabled)) { + _.extend(select2_opts, { + disabled: evalF(col.disabled, col, model) + }); + } else { + _.extend(select2_opts, {disabled: !editable}); + } + // Initialize select2 control. - this.$select.select2( - _.defaults( - {'disabled': !editable}, - col.select2, - this.defaults.select2 - )); + this.$select.select2(select2_opts).on('change', self.onSave); this.delegateEvents(); @@ -542,8 +522,16 @@ onSave: function (e) { var model = this.model; var column = this.column; + model.set(column.get("name"), this.$select.val()); - } + }, + + remove: function() { + this.$select.off('change', this.onSave); + this.$select.select2('destroy'); + this.$el.empty(); + Backgrid.SelectCell.prototype.remove.apply(this, arguments); + } }); /**