From 2c6ca7d82ce403fe64652b2ea92fb5b66286a634 Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Sat, 21 May 2016 15:41:35 +0530 Subject: [PATCH] Changed the Select2Control to work more consistently. --- web/pgadmin/browser/static/js/node.ui.js | 104 +------------------ web/pgadmin/static/css/overrides.css | 5 + web/pgadmin/static/js/backform.pgadmin.js | 117 ++++++++++++++++++++-- 3 files changed, 114 insertions(+), 112 deletions(-) diff --git a/web/pgadmin/browser/static/js/node.ui.js b/web/pgadmin/browser/static/js/node.ui.js index 7e8493512..90c9ada56 100644 --- a/web/pgadmin/browser/static/js/node.ui.js +++ b/web/pgadmin/browser/static/js/node.ui.js @@ -4,23 +4,6 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { var pgBrowser = pgAdmin.Browser; - // Store value in DOM as stringified JSON. - var StringOrJSONFormatter = function() {}; - _.extend(StringOrJSONFormatter.prototype, { - fromRaw: function(rawData, model) { - return JSON.stringify(_.escape(rawData)); - }, - toRaw: function(formattedData, model) { - if (typeof(formattedData) == 'string') { - return _.unescape(formattedData); - } - if (formattedData instanceof Array) { - return JSON.stringify(JSON.parse(JSON.stringify(formattedData))); - } - return JSON.parse(formattedData); - } - }); - /* * NodeAjaxOptionsControl * This control will fetch the options required to render the select @@ -34,38 +17,22 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { * data to proper 'label', 'value' format. */ var NodeAjaxOptionsControl = Backform.NodeAjaxOptionsControl = - Backform.SelectControl.extend({ - defaults: _.extend(Backform.SelectControl.prototype.defaults, { + Backform.Select2Control.extend({ + defaults: _.extend(Backform.Select2Control.prototype.defaults, { url: undefined, transform: undefined, url_with_id: false, - first_empty: false, - empty_value: '-- None --', select2: { allowClear: true, placeholder: 'Select from the list', width: 'style' } }), - template: _.template([ - '', - '
', - ' ', - '
'].join("\n")), - formatter: StringOrJSONFormatter, initialize: function() { /* * Initialization from the original control. */ - Backform.SelectControl.prototype.initialize.apply(this, arguments); + Backform.Select2Control.prototype.initialize.apply(this, arguments); /* * We're about to fetch the options required for this control. @@ -140,41 +107,6 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { self.field.set('options', data); } } - }, - render: function() { - - if(this.$sel && this.$sel.select2) { - this.$sel.select2('destroy') - } - - /* - * Let SelectControl render it, we will do our magic on the - * select control in it. - */ - Backform.SelectControl.prototype.render.apply(this, arguments); - - var d = this.field.toJSON(), - select2_opts = _.defaults({}, d.select2, this.defaults.select2), - evalF = function(f, d, m) { - return (_.isFunction(f) ? !!f.apply(d, [m]) : !!f); - }; - - /* - * If select2 options do not have any disabled property on this field - * 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) - }); - } - - /* - * Add empty option as Select2 requires any empty '')); - this.$sel = $select.select2(select2Opts); + this.$sel = this.$el.find("select").select2(select2Opts); + + this.updateInvalid(); return this; + }, + getValueFromDOM: function() { + return Backform.SelectControl.prototype.getValueFromDOM.apply( + this, arguments + ); } });