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); + } }); /**