Skip to content

Commit

Permalink
Add new article on OpenERP 6.1 web client JS hacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Mar 14, 2013
1 parent 3584f35 commit b54d249
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions content/posts/openerp-61-web-javascript-hacks-hide-buttons.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,71 @@
date: 2013-03-14 12:25:36
title: OpenERP 6.1 web client hacks: hide buttons with Javascript
category: English
tags: OpenERP, Javascript, Backbone.js, jQuery, Web

Last year I published two articles on hacking OpenERP 6.0 user interface, one to demonstrate [how to monkey-patch native Javascript behavior](http://kevin.deldycke.com/2012/02/how-to-monkey-patch-openerp-native-javascript/), the other to show how to [tweak widgets](http://kevin.deldycke.com/2012/05/hacking-openerp-60-user-interface-javascript/)

Both methods are based on an injection of inline Javascript code via XML view definition. In OpenERP 6.1, as [highlighted by Timothy](http://kevin.deldycke.com/2012/02/how-to-monkey-patch-openerp-native-javascript/#comment-769313088) you can no longer leverage this dirty trick: Javascript is stripped out of the XML stream.

But I found new ways of hacking OpenERP's web client in 6.1. Following these discoveries, I've created a the [experimental `web_smile_hide_buttons` addon](https://github.com/Smile-SA/smile_openerp_addons_6.1/tree/master/web_smile_hide_buttons/) to hide the hard-coded `create` and `duplicate` buttons on `form` views.

This module is not generic: it just demonstrate how to distribute dirty patches to the web client. It currently:

* Hide `create` buttons on all list views (affect `tree` views and `many2one` search view pop-ups),
* Hide the `save` button on `form` views,
* Hide `create` and `duplicate` button on all read-only `form` views,
* Only apply these transformations to a configurable subset of models.

The core of the module lies in the [`/static/src/js/custom.js`](https://github.com/Smile-SA/smile_openerp_addons_6.1/blob/master/web_smile_hide_buttons/static/src/js/custom.js) file. Here is an excerpt of that file, which sums-up my hacks:

:::javascript
openerp.web_smile_hide_buttons = function(openerp) {

// Models for which we'll hide create and duplicate buttons
var MODELS_TO_HIDE = ['res.partner'];

// Hide the create button on all list views, which affect tree views and many2one pop-up search view
openerp.web.ListView.include({
start: function() {
var self = this;
var ret = this._super.apply(this, arguments);
var res_model = this.dataset.model;
if ($.inArray(res_model, MODELS_TO_HIDE) != -1) {
self.options.addable = false;
};
return ret;
},
});

// Hide the save button on form views
openerp.web.FormView.include({
on_loaded: function(data) {
var self = this;
var ret = this._super.apply(this, arguments);
var res_model = this.dataset.model;
if ($.inArray(res_model, MODELS_TO_HIDE) != -1) {
this.$element.find('button.oe_form_button_save').remove();
};
return ret;
},
});

// Hide the create and duplicate button on all page views (i.e. read-only form views)
openerp.web.PageView.include({
on_loaded: function(data) {
var self = this;
var ret = this._super.apply(this, arguments);
var res_model = this.dataset.model;
if ($.inArray(res_model, MODELS_TO_HIDE) != -1) {
this.$element.find('button.oe_form_button_create').remove();
this.$element.find('button.oe_form_button_duplicate').remove();
};
return ret;
},
});

};

As you can see in the [full version of the code above](https://github.com/Smile-SA/smile_openerp_addons_6.1/blob/master/web_smile_hide_buttons/static/src/js/custom.js), I tried to hide `create` entries of `many2one` context menus, but failed to.

And to make the module really complete, I still have to find a way to hide `create` & `modify` entries of `many2one` drop-down menus. If you have some working code that's doing this, feel free to send me a pull request on GitHub.

0 comments on commit b54d249

Please sign in to comment.