From 27fbf00ef82b05828a0210a57ab3ab4f78d2ba98 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Tue, 7 Apr 2020 11:33:17 +0200 Subject: [PATCH 1/5] [FIX] when all is undefined, fall back to core's default --- web_m2x_options/static/src/xml/base.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_m2x_options/static/src/xml/base.xml b/web_m2x_options/static/src/xml/base.xml index a4f5ae3c7..d2ac41b8a 100644 --- a/web_m2x_options/static/src/xml/base.xml +++ b/web_m2x_options/static/src/xml/base.xml @@ -5,7 +5,7 @@ - !(widget.nodeOptions.no_open || widget.nodeOptions.no_open_edit) + !(widget.nodeOptions.no_open || widget.nodeOptions.no_open_edit || widget.noOpen) From 8ae6e490cbfd708ecf6cd9910ba916b2444d2079 Mon Sep 17 00:00:00 2001 From: Quentin Groulard Date: Fri, 24 Jan 2020 17:07:20 +0100 Subject: [PATCH 2/5] [ADD] Option open for One2Many fields With 'open' option, clicking on a row of a o2m field will open the view for the record in fullscreen rather than in a popup --- web_m2x_options/readme/USAGE.rst | 2 +- web_m2x_options/static/src/js/form.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/web_m2x_options/readme/USAGE.rst b/web_m2x_options/readme/USAGE.rst index 1da37a228..65ab3e969 100644 --- a/web_m2x_options/readme/USAGE.rst +++ b/web_m2x_options/readme/USAGE.rst @@ -37,7 +37,7 @@ in the field's options dict ``open`` *boolean* (Default: ``False``) - Makes many2many_tags buttons that open the linked resource + Makes many2many_tags and one2many rows buttons that open the linked resource ``no_color_picker`` *boolean* (Default: ``False``) diff --git a/web_m2x_options/static/src/js/form.js b/web_m2x_options/static/src/js/form.js index 1288bcc7b..eb59f8940 100644 --- a/web_m2x_options/static/src/js/form.js +++ b/web_m2x_options/static/src/js/form.js @@ -14,6 +14,7 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { var _t = core._t, FieldMany2ManyTags = relational_fields.FieldMany2ManyTags, FieldMany2One = relational_fields.FieldMany2One, + FieldOne2Many = relational_fields.FieldOne2Many, FormFieldMany2ManyTags = relational_fields.FormFieldMany2ManyTags; var web_m2x_options = rpc.query({ @@ -462,6 +463,29 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { }, }); + FieldOne2Many.include({ + _onOpenRecord: function (ev) { + var self = this; + var open = this.nodeOptions.open; + if (open && self.mode === "readonly") { + ev.stopPropagation(); + var id = ev.data.id; + var res_id = self.record.data[self.name].data.filter( + (line) => line.id === id + )[0].res_id; + self._rpc({ + model: self.field.relation, + method: "get_formview_action", + args: [[res_id]], + }).then(function (action) { + return self.do_action(action); + }); + } else { + return this._super.apply(this, arguments); + } + }, + }); + FormFieldMany2ManyTags.include({ events: _.extend({}, FormFieldMany2ManyTags.prototype.events, { "click .badge": "_onOpenBadge", From 35a284d8a5b1c3aaab2697e7c4f088079ee7114e Mon Sep 17 00:00:00 2001 From: Carlos Roca Date: Mon, 9 Nov 2020 10:19:56 +0100 Subject: [PATCH 3/5] [IMP] web_m2x_options: Efficiency Improved the efficiency of code: With this improvement we achieve to just do an rpc call when the js file is called instead of do the call for each field. --- web_m2x_options/README.rst | 3 +- web_m2x_options/models/ir_config_parameter.py | 5 +- web_m2x_options/readme/CONTRIBUTORS.rst | 1 + web_m2x_options/static/src/js/form.js | 110 +++++++----------- web_m2x_options/static/src/js/ir_options.js | 12 ++ web_m2x_options/views/view.xml | 4 + 6 files changed, 64 insertions(+), 71 deletions(-) create mode 100644 web_m2x_options/static/src/js/ir_options.js diff --git a/web_m2x_options/README.rst b/web_m2x_options/README.rst index fec9de418..95646ee69 100644 --- a/web_m2x_options/README.rst +++ b/web_m2x_options/README.rst @@ -23,7 +23,7 @@ web_m2x_options :target: https://runbot.odoo-community.org/runbot/162/14.0 :alt: Try me on Runbot -|badge1| |badge2| |badge3| |badge4| |badge5| +|badge1| |badge2| |badge3| |badge4| |badge5| This modules modifies "many2one" and "many2manytags" form widgets so as to add some new display control options. @@ -178,6 +178,7 @@ Contributors * Jairo Llopis * David Vidal * Ernesto Tejeda + * Carlos Roca * Bhavesh Odedra * Dhara Solanki (http://www.initos.com) diff --git a/web_m2x_options/models/ir_config_parameter.py b/web_m2x_options/models/ir_config_parameter.py index 8e7228f08..26638d038 100644 --- a/web_m2x_options/models/ir_config_parameter.py +++ b/web_m2x_options/models/ir_config_parameter.py @@ -13,4 +13,7 @@ class IrConfigParameter(models.Model): "web_m2x_options.search_more", "web_m2x_options.m2o_dialog", ] - return self.sudo().search_read([["key", "in", opts]], ["key", "value"]) + return { + res["key"]: res["value"] + for res in self.sudo().search_read([["key", "in", opts]], ["key", "value"]) + } diff --git a/web_m2x_options/readme/CONTRIBUTORS.rst b/web_m2x_options/readme/CONTRIBUTORS.rst index 9838a8c21..c42c7afe7 100644 --- a/web_m2x_options/readme/CONTRIBUTORS.rst +++ b/web_m2x_options/readme/CONTRIBUTORS.rst @@ -9,5 +9,6 @@ * Jairo Llopis * David Vidal * Ernesto Tejeda + * Carlos Roca * Bhavesh Odedra * Dhara Solanki (http://www.initos.com) diff --git a/web_m2x_options/static/src/js/form.js b/web_m2x_options/static/src/js/form.js index eb59f8940..5a3241aa6 100644 --- a/web_m2x_options/static/src/js/form.js +++ b/web_m2x_options/static/src/js/form.js @@ -7,9 +7,10 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { var core = require("web.core"), data = require("web.data"), Dialog = require("web.Dialog"), + FormView = require("web.FormView"), view_dialogs = require("web.view_dialogs"), relational_fields = require("web.relational_fields"), - rpc = require("web.rpc"); + ir_options = require("web_m2x_options.ir_options"); var _t = core._t, FieldMany2ManyTags = relational_fields.FieldMany2ManyTags, @@ -17,10 +18,12 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { FieldOne2Many = relational_fields.FieldOne2Many, FormFieldMany2ManyTags = relational_fields.FormFieldMany2ManyTags; - var web_m2x_options = rpc.query({ - model: "ir.config_parameter", - method: "get_web_m2x_options", - }); + function is_option_set(option) { + if (_.isUndefined(option)) return false; + if (typeof option === "string") return option === "true" || option === "True"; + if (typeof option === "boolean") return option; + return false; + } var M2ODialog = Dialog.extend({ template: "M2ODialog", @@ -94,43 +97,13 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { }); FieldMany2One.include({ - start: function () { - this._super.apply(this, arguments); - return this.get_options(); - }, - - get_options: function () { - var self = this; - if (_.isUndefined(this.ir_options_loaded)) { - this.ir_options_loaded = $.Deferred(); - this.ir_options = {}; - web_m2x_options.then(function (records) { - _(records).each(function (record) { - self.ir_options[record.key] = record.value; - }); - self.ir_options_loaded.resolve(); - }); - } - return $.when(); - }, - - is_option_set: function (option) { - if (_.isUndefined(option)) return false; - if (typeof option === "string") - return option === "true" || option === "True"; - if (typeof option === "boolean") return option; - return false; - }, - _onInputFocusout: function () { var m2o_dialog_opt = - this.is_option_set(this.nodeOptions.m2o_dialog) || + is_option_set(this.nodeOptions.m2o_dialog) || (_.isUndefined(this.nodeOptions.m2o_dialog) && - this.is_option_set( - this.ir_options["web_m2x_options.m2o_dialog"] - )) || + is_option_set(ir_options["web_m2x_options.m2o_dialog"])) || (_.isUndefined(this.nodeOptions.m2o_dialog) && - _.isUndefined(this.ir_options["web_m2x_options.m2o_dialog"])); + _.isUndefined(ir_options["web_m2x_options.m2o_dialog"])); if (this.can_create && this.floating && m2o_dialog_opt) { new M2ODialog(this, this.string, this.$input.val()).open(); } @@ -138,14 +111,12 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { _search: function (search_val) { var self = this; - if (search_val === undefined) { - return this._super.apply(this, arguments); - } + var def = new Promise((resolve) => { // Add options limit used to change number of selections record // returned. - if (!_.isUndefined(self.ir_options["web_m2x_options.limit"])) { - this.limit = parseInt(self.ir_options["web_m2x_options.limit"], 10); + if (!_.isUndefined(ir_options["web_m2x_options.limit"])) { + this.limit = parseInt(ir_options["web_m2x_options.limit"], 10); } if (typeof self.nodeOptions.limit === "number") { @@ -231,14 +202,12 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { // 4- if set globally, apply its value // 5- if not set globally either, check if returned values are more than node's limit if (!_.isUndefined(self.nodeOptions.search_more)) { - var search_more = self.is_option_set( - self.nodeOptions.search_more - ); + var search_more = is_option_set(self.nodeOptions.search_more); } else if ( - !_.isUndefined(self.ir_options["web_m2x_options.search_more"]) + !_.isUndefined(ir_options["web_m2x_options.search_more"]) ) { - var search_more = self.is_option_set( - self.ir_options["web_m2x_options.search_more"] + var search_more = is_option_set( + ir_options["web_m2x_options.search_more"] ); } else { var search_more = values.length > self.limit; @@ -300,13 +269,13 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { var raw_result = _.map(result, function (x) { return x[1]; }); - var quick_create = self.is_option_set(self.nodeOptions.create), + var quick_create = is_option_set(self.nodeOptions.create), quick_create_undef = _.isUndefined(self.nodeOptions.create), m2x_create_undef = _.isUndefined( - self.ir_options["web_m2x_options.create"] + ir_options["web_m2x_options.create"] ), - m2x_create = self.is_option_set( - self.ir_options["web_m2x_options.create"] + m2x_create = is_option_set( + ir_options["web_m2x_options.create"] ); var show_create = (!self.nodeOptions && (m2x_create_undef || m2x_create)) || @@ -333,16 +302,16 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { // Create and edit ... var create_edit = - self.is_option_set(self.nodeOptions.create) || - self.is_option_set(self.nodeOptions.create_edit), + is_option_set(self.nodeOptions.create) || + is_option_set(self.nodeOptions.create_edit), create_edit_undef = _.isUndefined(self.nodeOptions.create) && _.isUndefined(self.nodeOptions.create_edit), m2x_create_edit_undef = _.isUndefined( - self.ir_options["web_m2x_options.create_edit"] + ir_options["web_m2x_options.create_edit"] ), - m2x_create_edit = self.is_option_set( - self.ir_options["web_m2x_options.create_edit"] + m2x_create_edit = is_option_set( + ir_options["web_m2x_options.create_edit"] ); var show_create_edit = (!self.nodeOptions && @@ -382,6 +351,17 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { }); }); this.orderer.add(def); + + // Add options limit used to change number of selections record + // returned. + if (!_.isUndefined(ir_options["web_m2x_options.limit"])) { + this.limit = parseInt(ir_options["web_m2x_options.limit"], 10); + } + + if (typeof this.nodeOptions.limit === "number") { + this.limit = this.nodeOptions.limit; + } + return def; }, }); @@ -397,17 +377,9 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { return result; }, - is_option_set: function (option) { - if (_.isUndefined(option)) return false; - if (typeof option === "string") - return option === "true" || option === "True"; - if (typeof option === "boolean") return option; - return false; - }, - _onOpenBadge: function (event) { var self = this; - var open = self.nodeOptions && self.is_option_set(self.nodeOptions.open); + var open = self.nodeOptions && is_option_set(self.nodeOptions.open); if (open) { var context = self.record.getContext(self.recordParams); var id = parseInt($(event.currentTarget).data("id"), 10); @@ -492,8 +464,8 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) { }), _onOpenBadge: function (event) { - var open = this.is_option_set(this.nodeOptions.open); - var no_color_picker = this.is_option_set(this.nodeOptions.no_color_picker); + var open = is_option_set(this.nodeOptions.open); + var no_color_picker = is_option_set(this.nodeOptions.no_color_picker); this._super.apply(this, arguments); if (!open && !no_color_picker) { this._onOpenColorPicker(event); diff --git a/web_m2x_options/static/src/js/ir_options.js b/web_m2x_options/static/src/js/ir_options.js new file mode 100644 index 000000000..293c66c51 --- /dev/null +++ b/web_m2x_options/static/src/js/ir_options.js @@ -0,0 +1,12 @@ +/* Copyright 2020 Tecnativa - Carlos Roca + * * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ +odoo.define("web_m2x_options.ir_options", function (require) { + "use strict"; + + var rpc = require("web.rpc"); + + return rpc.query({ + model: "ir.config_parameter", + method: "get_web_m2x_options", + }); +}); diff --git a/web_m2x_options/views/view.xml b/web_m2x_options/views/view.xml index d2be9152e..a76af2656 100644 --- a/web_m2x_options/views/view.xml +++ b/web_m2x_options/views/view.xml @@ -10,6 +10,10 @@ type="text/javascript" src="/web_m2x_options/static/src/js/form.js" /> +