diff --git a/web_m2x_options/README.rst b/web_m2x_options/README.rst index e3681641..be6e7603 100644 --- a/web_m2x_options/README.rst +++ b/web_m2x_options/README.rst @@ -47,6 +47,22 @@ New options Number of displayed record in drop-down panel +``search_more`` *boolean* + + Used to force disable/enable search more button. + +``field_color`` *string* + + A string to define the field used to define color. + This option has to be used with colors. + +``colors`` *dictionary* + + A dictionary to link field value with a HTML color. + This option has to be used with field_color. + + + ir.config_parameter options --------------------------- @@ -61,15 +77,25 @@ If you disable one option, you can enable it for particular field by setting "cr Whether to display "Create and Edit..." entry in dropdown panel for all fields in the odoo instance. +``web_m2x_options.m2o_dialog`` *boolean* (Default: depends if user have create rights) + + Whether to display the many2one dialog in case of validation error for all fields in the odoo instance. + ``web_m2x_options.limit`` *int* (Default: openerp default value is ``7``) Number of displayed record in drop-down panel for all fields in the odoo instance +``web_m2x_options.search_more`` *boolean* (Default: default value is ``False``) + + Whether the field should always show "Search more..." entry or not. + To add these parameters go to Configuration -> Technical -> Parameters -> System Parameters and add new parameters like: - web_m2x_options.create: False - web_m2x_options.create_edit: False +- web_m2x_options.m2o_dialog: False - web_m2x_options.limit: 10 +- web_m2x_options.search_more: True Example @@ -78,7 +104,7 @@ Example Your XML form view definition could contain:: ... - + ... Note diff --git a/web_m2x_options/__openerp__.py b/web_m2x_options/__openerp__.py index 66732118..37b7dbf4 100644 --- a/web_m2x_options/__openerp__.py +++ b/web_m2x_options/__openerp__.py @@ -2,7 +2,7 @@ { "name": 'web_m2x_options', - "version": "0.1", + "version": "8.0.0.2", "depends": [ 'base', 'web', diff --git a/web_m2x_options/static/src/js/form.js b/web_m2x_options/static/src/js/form.js index 4e3801eb..bc4d3beb 100644 --- a/web_m2x_options/static/src/js/form.js +++ b/web_m2x_options/static/src/js/form.js @@ -9,42 +9,60 @@ openerp.web_m2x_options = function (instance) { _lt = instance.web._lt; var OPTIONS = ['web_m2x_options.create', - 'web_m2x_options.create_edit', - 'web_m2x_options.limit',]; + 'web_m2x_options.create_edit', + 'web_m2x_options.limit', + 'web_m2x_options.search_more', + 'web_m2x_options.m2o_dialog',]; - instance.web.form.FieldMany2One.include({ + instance.web.form.FieldMany2One = instance.web.form.FieldMany2One.extend({ - start: function() { - this._super.apply(this, arguments); - return this.get_options(); - }, - - get_options: function() { - var self = this; - if (!_.isUndefined(this.view) && _.isUndefined(this.view.ir_options_loaded)) { - this.view.ir_options_loaded = $.Deferred(); - this.view.ir_options = {}; - (new instance.web.Model("ir.config_parameter")) - .query(["key", "value"]).filter([['key', 'in', OPTIONS]]) - .all().then(function(records) { - _(records).each(function(record) { - self.view.ir_options[record.key] = record.value; - }); - self.view.ir_options_loaded.resolve(); - }); - return this.view.ir_options_loaded; - } - return $.when(); - }, - - show_error_displayer: function () { - if ((typeof this.options.m2o_dialog === 'undefined' && this.can_create) || - this.options.m2o_dialog) { + start: function() { + this._super.apply(this, arguments); + return this.get_options(); + }, + + get_options: function() { + var self = this; + if (!_.isUndefined(this.view) && _.isUndefined(this.view.ir_options_loaded)) { + this.view.ir_options_loaded = $.Deferred(); + this.view.ir_options = {}; + (new instance.web.Model("ir.config_parameter")) + .query(["key", "value"]).filter([['key', 'in', OPTIONS]]) + .all().then(function(records) { + _(records).each(function(record) { + self.view.ir_options[record.key] = record.value; + }); + self.view.ir_options_loaded.resolve(); + }); + return this.view.ir_options_loaded; + } + return $.when(); + }, + + is_option_set: function(option) { + if (_.isUndefined(option)) { + return false + } + var is_string = typeof option === 'string' + var is_bool = typeof option === 'boolean' + if (is_string) { + return option === 'true' || option === 'True' + } else if (is_bool) { + return option + } + return false + }, + + show_error_displayer: function () { + if(this.is_option_set(this.options.m2o_dialog) || + _.isUndefined(this.options.m2o_dialog) && this.is_option_set(this.view.ir_options['web_m2x_options.m2o_dialog']) || + this.can_create && _.isUndefined(this.options.m2o_dialog) && _.isUndefined(this.view.ir_options['web_m2x_options.m2o_dialog'])) { new instance.web.form.M2ODialog(this).open(); } }, get_search_result: function (search_val) { + var Objects = new instance.web.Model(this.field.relation); var def = $.Deferred(); var self = this; // add options limit used to change number of selections record @@ -59,6 +77,15 @@ openerp.web_m2x_options = function (instance) { this.limit = this.options.limit; } + // add options search_more to force enable or disable search_more button + if (this.is_option_set(this.options.search_more) || _.isUndefined(this.options.search_more) && this.is_option_set(self.view.ir_options['web_m2x_options.search_more'])) { + this.search_more = true + } + + // add options field_color and colors to color item(s) depending on field_color value + this.field_color = this.options.field_color + this.colors = this.options.colors + var dataset = new instance.web.DataSet(this, this.field.relation, self.build_context()); var blacklist = this.get_search_blacklist(); @@ -92,10 +119,39 @@ openerp.web_m2x_options = function (instance) { id: x[0], }; }); + + // Search result value colors + + if (self.colors && self.field_color) { + var value_ids = []; + for (var index in values) { + value_ids.push(values[index].id); + } + + // RPC request to get field_color from Objects + Objects.query([self.field_color]) + .filter([['id', 'in', value_ids]]) + .all().done(function (objects) { + for (var index in objects) { + for (var index_value in values) { + if (values[index_value].id == objects[index].id) { + // Find value in values by comparing ids + var value = values[index_value]; + + // Find color with field value as key + var color = self.colors[objects[index][self.field_color]] || 'black'; + value.label = ''+value.label+''; + break; + } + } + } + def.resolve(values); + }); + } // search more... if more results that max - if (values.length > self.limit) { + if (values.length > self.limit || self.search_more) { values = values.slice(0, self.limit); values.push({ label: _t("Search More..."), @@ -151,8 +207,11 @@ openerp.web_m2x_options = function (instance) { classname: 'oe_m2o_dropdown_option' }); } - - def.resolve(values); + + // Check if colors specified to wait for RPC + if (!(self.field_color && self.colors)){ + def.resolve(values); + } }); return def;