Browse Source

manage attrs attribute, espesialy for readonly

pull/236/head
Pierre Verkest 9 years ago
parent
commit
858d554cfc
  1. 20
      web_widget_boolean_switch/README.rst
  2. 5
      web_widget_boolean_switch/demo/res_users_view.xml
  3. 47
      web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js

20
web_widget_boolean_switch/README.rst

@ -22,19 +22,14 @@ Example
``` ```
<field name="active" <field name="active"
widget="boolean_switch" widget="boolean_switch"
attrs="{'readonly': [('login', '=', 'admin')]}"
context="{'fake_parameter': 'foo'}" context="{'fake_parameter': 'foo'}"
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No' }}"/> options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No' }}"/>
``` ```
.. note::
``context`` is sent to the ``write`` method of the field model in case of
special needs with the quick edition.
Options Options
------- -------
quick_edit quick_edit
~~~~~~~~~~ ~~~~~~~~~~
@ -60,7 +55,7 @@ extra
* **wrapperClass**: Container element class(es) - default: `"wrapper"`, * **wrapperClass**: Container element class(es) - default: `"wrapper"`,
..warning::
.. warning::
Those parameters are overwritten by this module or highly discouraged: Those parameters are overwritten by this module or highly discouraged:
@ -71,6 +66,17 @@ extra
* **onInit**: Callback function to execute on initialization - default: `function() {}`, * **onInit**: Callback function to execute on initialization - default: `function() {}`,
* **onSwitchChange**: Callback function to execute on switch state change - default: `function() {}` * **onSwitchChange**: Callback function to execute on switch state change - default: `function() {}`
attrs
-----
This attribute is supported as expected!
context
-------
``context`` is sent to the ``write`` method of the field model in case of
special needs with the quick edition.
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot :alt: Try me on Runbot

5
web_widget_boolean_switch/demo/res_users_view.xml

@ -20,6 +20,7 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="login" position="after"> <field name="login" position="after">
<field name="active" widget="boolean_switch" <field name="active" widget="boolean_switch"
attrs="{'readonly': [('login', '=', 'admin')]}"
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}" options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"
context="{'test': 'test value'}"/> context="{'test': 'test value'}"/>
</field> </field>
@ -31,7 +32,8 @@
<field name="model">res.users</field> <field name="model">res.users</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="active" position="replace"> <field name="active" position="replace">
<field name="active" widget="boolean_switch" readonly="0"
<field name="active" widget="boolean_switch"
attrs="{'readonly': [('login', '=', 'admin')]}"
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}" options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"
context="{'test': 'test value'}"/> context="{'test': 'test value'}"/>
</field> </field>
@ -48,6 +50,7 @@
<field name="login"/> <field name="login"/>
<field name="lang"/> <field name="lang"/>
<field name="active" widget="boolean_switch" <field name="active" widget="boolean_switch"
attrs="{'readonly': [('login', '=', 'admin')]}"
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}" options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"
context="{'test': 'test value'}" /> context="{'test': 'test value'}" />
</tree> </tree>

47
web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js

@ -9,8 +9,8 @@ openerp.web_widget_boolean_switch = function(instance){
instance.web.BooleanSwitchWidget = instance.web.Class.extend({ instance.web.BooleanSwitchWidget = instance.web.Class.extend({
init: function(checkboxes, options, quick_edit_callback){
options = options ? options : {};
init: function(checkboxes, opts, quick_edit_callback){
var options = _.extend({}, opts ? opts : {});
this.checkboxes = checkboxes; this.checkboxes = checkboxes;
this.quick_edit = options.hasOwnProperty('quick_edit') ? this.quick_edit = options.hasOwnProperty('quick_edit') ?
@ -20,12 +20,23 @@ openerp.web_widget_boolean_switch = function(instance){
var switchOptions = options.hasOwnProperty('extra') ? var switchOptions = options.hasOwnProperty('extra') ?
options.extra : {}; options.extra : {};
// in case of domain (using attrs={...}), readonly is set afterward
if(!_.isBoolean(options.readonly)){
options.readonly = false;
}
_.extend(switchOptions, { _.extend(switchOptions, {
'readonly': options.hasOwnProperty('readonly') ? 'readonly': options.hasOwnProperty('readonly') ?
options.readonly : readonly, options.readonly : readonly,
'disabled': options.hasOwnProperty('disabled') ? 'disabled': options.hasOwnProperty('disabled') ?
options.disabled : !this.quick_edit, options.disabled : !this.quick_edit,
}); });
//finnaly if readonly is false, we remove it to init widget
//with the value of its attributes well managed by qweb and
//FieldBooleanSwitch class
if(!switchOptions.readonly){
delete switchOptions.readonly;
}
if(options.hasOwnProperty('onSwitchChange')){ if(options.hasOwnProperty('onSwitchChange')){
switchOptions.onSwitchChange = options.onSwitchChange; switchOptions.onSwitchChange = options.onSwitchChange;
} }
@ -73,7 +84,7 @@ openerp.web_widget_boolean_switch = function(instance){
this.switcher = new openerp.instances.instance0.web.BooleanSwitchWidget( this.switcher = new openerp.instances.instance0.web.BooleanSwitchWidget(
this.$checkbox, options, _.bind(function(event, state) { this.$checkbox, options, _.bind(function(event, state) {
// get in mind that in case of view list editable
// keep in mind that in case of view list editable
// actual_mode is undefined... // actual_mode is undefined...
if(this.view.get('actual_mode') === 'view'){ if(this.view.get('actual_mode') === 'view'){
var id = this.view.dataset.ids[this.view.dataset.index]; var id = this.view.dataset.ids[this.view.dataset.index];
@ -87,8 +98,9 @@ openerp.web_widget_boolean_switch = function(instance){
this.internal_set_value(state, {'silent': true}); this.internal_set_value(state, {'silent': true});
} }
}, this)); }, this));
this.on("change:effective_readonly", this, this.switcher_states);
this.on("change:readonly", this, this.switcher_states);
this._super(); this._super();
this.switcher_states.call(this);
}, },
internal_set_value: function(value_, options) { internal_set_value: function(value_, options) {
var tmp = this.no_rerender; var tmp = this.no_rerender;
@ -97,9 +109,10 @@ openerp.web_widget_boolean_switch = function(instance){
this.no_rerender = tmp; this.no_rerender = tmp;
}, },
switcher_states: function () { switcher_states: function () {
if (this.switcher.quick_edit)
return;
this.switcher.set_disabled(this.get('effective_readonly'));
this.switcher.set_readonly(this.get('readonly'));
if (!this.switcher.quick_edit){
this.switcher.set_disabled(this.get('effective_readonly'));
}
}, },
render_value: function() { render_value: function() {
this.switcher.set_value(this.get('value')); this.switcher.set_value(this.get('value'));
@ -155,10 +168,26 @@ openerp.web_widget_boolean_switch = function(instance){
instance.web.list.columns.add('field.boolean_switch', 'instance.web.list.FieldBooleanSwitch'); instance.web.list.columns.add('field.boolean_switch', 'instance.web.list.FieldBooleanSwitch');
instance.web.list.FieldBooleanSwitch = instance.web.list.Column.extend({ instance.web.list.FieldBooleanSwitch = instance.web.list.Column.extend({
format: function (row_data, options) {
options = options || {};
var attrs = {};
if (options.process_modifiers !== false) {
attrs = this.modifiers_for(row_data);
}
if (attrs.invisible) { return ''; }
if (!row_data[this.id]) {
return options.value_if_empty === undefined ?
'' : options.value_if_empty;
}
var readonly = attrs.hasOwnProperty('readonly') ? attrs.readonly : false;
return this._format(row_data, options, readonly);
},
_format: function (row_data, options) {
return _.str.sprintf('<input type="checkbox" %s readonly="readonly" data-rowid="%d"/>',
_format: function (row_data, options, readonly) {
return _.str.sprintf('<input type="checkbox" %s %s data-rowid="%d"/>',
row_data[this.id].value ? 'checked="checked"' : '', row_data[this.id].value ? 'checked="checked"' : '',
readonly ? 'readonly' : '',
row_data.hasOwnProperty('id') ? row_data.id.value : -1); row_data.hasOwnProperty('id') ? row_data.id.value : -1);
} }
}); });

Loading…
Cancel
Save