diff --git a/addons/base/static/src/js/form.js b/addons/base/static/src/js/form.js index 3ea5697c77f..3a6ef174c8d 100644 --- a/addons/base/static/src/js/form.js +++ b/addons/base/static/src/js/form.js @@ -530,34 +530,19 @@ openerp.base.form.WidgetButton = openerp.base.form.Widget.extend({ } }, on_confirmed: function() { - var attrs = this.node.attrs; - if (attrs.special) { - this.on_button_action({ - result : { type: 'ir.actions.act_window_close' } + var self = this; + + this.execute_action( + this.node.attrs, this.view.dataset, this.session.action_manager, + this.view.datarecord.id, function (result) { + self.log("Button returned", result); + self.view.reload(); }); - } else { - var type = attrs.type || 'workflow'; - var context = _.extend({}, this.view.dataset.context, attrs.context || {}); - switch(type) { - case 'object': - return this.view.dataset.call(attrs.name, [this.view.datarecord.id], [context], this.on_button_action); - case 'action': - return this.rpc('/base/action/load', { action_id: parseInt(attrs.name) }, this.on_button_action); - default: - return this.view.dataset.exec_workflow(this.view.datarecord.id, attrs.name, this.on_button_action); - } - } - }, - on_button_action: function(r) { - console.log("Got reesonse button", r) - if (r.result && r.result.constructor == Object) { - this.session.action_manager.do_action(r.result); - } else { - this.log("Button returned", r.result); - this.view.reload(); - } } }); +// let WidgetButton execute actions +_.extend(openerp.base.form.WidgetButton.prototype, + openerp.base.ActionExecutor); openerp.base.form.WidgetLabel = openerp.base.form.Widget.extend({ init: function(view, node) { diff --git a/addons/base/static/src/js/views.js b/addons/base/static/src/js/views.js index 7af2e6b38e9..92609e166be 100644 --- a/addons/base/static/src/js/views.js +++ b/addons/base/static/src/js/views.js @@ -63,6 +63,58 @@ openerp.base.ActionManager = openerp.base.Controller.extend({ } }); +/** + * Mixin for action-executing objects, provides handling of OpenERP actions to + * all clients. + * + * Mix into existing classes via ``_.extend`` of the class's prototype. + * + * @class + */ +openerp.base.ActionExecutor = +/** + * @lends openerp.base.ActionExecutor# + */ { + /** + * Fetches and executes the action identified by ``action_data``. + * + * @param {Object} action_data the action descriptor data + * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it + * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``) + * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'`` + * @param {Object} [action_data.context=null] additional action context, to add to the current context + * @param {openerp.base.DataSet} dataset a dataset object used to communicate with the server + * @param {openerp.base.ActionManager} action_manager object able to actually execute the action, if any is fetched + * @param {Number} [record_id] the identifier of the object on which the action is to be applied + * @param {Function} on_no_action callback to execute if the action does not generate any result (no new action) + */ + execute_action: function (action_data, dataset, action_manager, record_id, on_no_action) { + var handler = function (r) { + if (r.result && r.result.constructor == Object) { + action_manager.do_action(r.result); + } else { + on_no_action(r.result); + } + }; + + if (action_data.special) { + handler({ + result : { type: 'ir.actions.act_window_close' } + }); + } else { + var context = _.extend({}, dataset.context, action_data.context || {}); + switch(action_data.type) { + case 'object': + return dataset.call(action_data.name, [record_id], [context], handler); + case 'action': + return this.rpc('/base/action/load', { action_id: parseInt(action_data.name, 10) }, handler); + default: + return dataset.exec_workflow(record_id, action_data.name, handler); + } + } + } +}; + /** * Registry for all the main views */