forked from Techsystech/web
manage attrs attribute, espesialy for readonly
parent
0c41e40ec5
commit
858d554cfc
|
@ -22,19 +22,14 @@ Example
|
|||
```
|
||||
<field name="active"
|
||||
widget="boolean_switch"
|
||||
attrs="{'readonly': [('login', '=', 'admin')]}"
|
||||
context="{'fake_parameter': 'foo'}"
|
||||
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
|
||||
-------
|
||||
|
||||
|
||||
quick_edit
|
||||
~~~~~~~~~~
|
||||
|
||||
|
@ -60,7 +55,7 @@ extra
|
|||
* **wrapperClass**: Container element class(es) - default: `"wrapper"`,
|
||||
|
||||
|
||||
..warning::
|
||||
.. warning::
|
||||
|
||||
Those parameters are overwritten by this module or highly discouraged:
|
||||
|
||||
|
@ -71,6 +66,17 @@ extra
|
|||
* **onInit**: Callback function to execute on initialization - 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
|
||||
:alt: Try me on Runbot
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
<field name="arch" type="xml">
|
||||
<field name="login" position="after">
|
||||
<field name="active" widget="boolean_switch"
|
||||
attrs="{'readonly': [('login', '=', 'admin')]}"
|
||||
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"
|
||||
context="{'test': 'test value'}"/>
|
||||
</field>
|
||||
|
@ -31,7 +32,8 @@
|
|||
<field name="model">res.users</field>
|
||||
<field name="arch" type="xml">
|
||||
<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'}}"
|
||||
context="{'test': 'test value'}"/>
|
||||
</field>
|
||||
|
@ -48,6 +50,7 @@
|
|||
<field name="login"/>
|
||||
<field name="lang"/>
|
||||
<field name="active" widget="boolean_switch"
|
||||
attrs="{'readonly': [('login', '=', 'admin')]}"
|
||||
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"
|
||||
context="{'test': 'test value'}" />
|
||||
</tree>
|
||||
|
|
|
@ -9,8 +9,8 @@ openerp.web_widget_boolean_switch = function(instance){
|
|||
|
||||
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.quick_edit = options.hasOwnProperty('quick_edit') ?
|
||||
|
@ -20,12 +20,23 @@ openerp.web_widget_boolean_switch = function(instance){
|
|||
var switchOptions = options.hasOwnProperty('extra') ?
|
||||
options.extra : {};
|
||||
|
||||
// in case of domain (using attrs={...}), readonly is set afterward
|
||||
if(!_.isBoolean(options.readonly)){
|
||||
options.readonly = false;
|
||||
}
|
||||
_.extend(switchOptions, {
|
||||
'readonly': options.hasOwnProperty('readonly') ?
|
||||
options.readonly : readonly,
|
||||
'disabled': options.hasOwnProperty('disabled') ?
|
||||
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')){
|
||||
switchOptions.onSwitchChange = options.onSwitchChange;
|
||||
}
|
||||
|
@ -73,7 +84,7 @@ openerp.web_widget_boolean_switch = function(instance){
|
|||
|
||||
this.switcher = new openerp.instances.instance0.web.BooleanSwitchWidget(
|
||||
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...
|
||||
if(this.view.get('actual_mode') === 'view'){
|
||||
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));
|
||||
this.on("change:effective_readonly", this, this.switcher_states);
|
||||
this.on("change:readonly", this, this.switcher_states);
|
||||
this._super();
|
||||
this.switcher_states.call(this);
|
||||
},
|
||||
internal_set_value: function(value_, options) {
|
||||
var tmp = this.no_rerender;
|
||||
|
@ -97,9 +109,10 @@ openerp.web_widget_boolean_switch = function(instance){
|
|||
this.no_rerender = tmp;
|
||||
},
|
||||
switcher_states: function () {
|
||||
if (this.switcher.quick_edit)
|
||||
return;
|
||||
this.switcher.set_readonly(this.get('readonly'));
|
||||
if (!this.switcher.quick_edit){
|
||||
this.switcher.set_disabled(this.get('effective_readonly'));
|
||||
}
|
||||
},
|
||||
render_value: function() {
|
||||
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.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 ''; }
|
||||
|
||||
_format: function (row_data, options) {
|
||||
return _.str.sprintf('<input type="checkbox" %s readonly="readonly" data-rowid="%d"/>',
|
||||
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, readonly) {
|
||||
return _.str.sprintf('<input type="checkbox" %s %s data-rowid="%d"/>',
|
||||
row_data[this.id].value ? 'checked="checked"' : '',
|
||||
readonly ? 'readonly' : '',
|
||||
row_data.hasOwnProperty('id') ? row_data.id.value : -1);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue