Browse Source

allow extra parameters + poc of quick editing

pull/236/head
Pierre Verkest 9 years ago
parent
commit
87d958d38b
  1. 50
      web_widget_boolean_switch/README.rst
  2. 9
      web_widget_boolean_switch/demo/res_users_view.xml
  3. 34
      web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js

50
web_widget_boolean_switch/README.rst

@ -18,9 +18,55 @@ To install this module, you need to:
Configuration Configuration
============= =============
To configure this module, you need to:
Example
-------
```xml
<field name="active"
widget="boolean_switch"
options="{'quick_edit': True, extra: {'onText': 'Yes', 'offText': 'No' }"/>
```
Options
-------
quick_edit
~~~~~~~~~~
extra
~~~~~
``extra`` is used to set
`bootstrap-switch <http://www.bootstrap-switch.org/options.html>`_ options.
Available::
* **size**: - default: `null`
* **animate**: - default: `true`
* **indeterminate**: `false`
* **inverse**: `false`
* **radioAllOff**: `false`
* **onColor**: `"primary"`
* **offColor**: `default`
* **onText**: `"ON"`,
* **offText**: `"OFF"`,
* **labelText**: `"&nbsp;"`,
* **handleWidth**: `"auto"`,
* **labelWidth**: `"auto"`,
* **baseClass**: `"bootstrap-switch"`,
* **wrapperClass**: `"wrapper"`,
.. warning::
Those parameters are overwritten by this module or highly discourage::
* **state**: true,
* **disabled**: `false`
* **readonly**: `false`
* **onInit**: `function() {}`,
* **onSwitchChange**: `function() {}`
* go to ...
Usage Usage
===== =====

9
web_widget_boolean_switch/demo/res_users_view.xml

@ -19,7 +19,8 @@
<field name="sequence">20</field> <field name="sequence">20</field>
<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" options="{'quick_edit': True}"/>
<field name="active" widget="boolean_switch"
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"/>
</field> </field>
</field> </field>
</record> </record>
@ -29,7 +30,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" options="{'quick_edit': True}"/>
<field name="active" widget="boolean_switch" readonly="0"
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"/>
</field> </field>
</field> </field>
</record> </record>
@ -43,7 +45,8 @@
<field name="name"/> <field name="name"/>
<field name="login"/> <field name="login"/>
<field name="lang"/> <field name="lang"/>
<field name="active" widget="boolean_switch" options="{'quick_edit': True}"/>
<field name="active" widget="boolean_switch"
options="{'quick_edit': True, 'extra': {'onText': 'Yes', 'offText': 'No'}}"/>
</tree> </tree>
</field> </field>
</record> </record>

34
web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js

@ -17,17 +17,37 @@ openerp.web_widget_boolean_switch = function(instance){
options.quick_edit : false; options.quick_edit : false;
var readonly = options.hasOwnProperty('readonly') ? var readonly = options.hasOwnProperty('readonly') ?
options.readonly : false; options.readonly : false;
var switchOptions = options.hasOwnProperty('extra') ?
options.extra : {};
var 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,
};
});
if(options.hasOwnProperty('onSwitchChange')){ if(options.hasOwnProperty('onSwitchChange')){
switchOptions.onSwitchChange = options.onSwitchChange switchOptions.onSwitchChange = options.onSwitchChange
} }
this.checkboxes.bootstrapSwitch(switchOptions); this.checkboxes.bootstrapSwitch(switchOptions);
if(this.quick_edit){
this.checkboxes.on('switchChange.bootstrapSwitch',
function(event, state) {
var model_name = 'res.users';
var id = 4;
var values = {};
values['active'] = state;
var some_context = {};
var model = new openerp.instances.instance0.web.Model(model_name);
model.call('write', [[id], values],
{context: some_context}).then(function (result) {
console.log('success');
});
});
}
}, },
set_value: function(value){ set_value: function(value){
// the third parameter tell if we should skip to fire evnets // the third parameter tell if we should skip to fire evnets
@ -55,8 +75,16 @@ openerp.web_widget_boolean_switch = function(instance){
var options = { var options = {
onSwitchChange: _.bind(function(event, state) { onSwitchChange: _.bind(function(event, state) {
this.internal_set_value(this.$checkbox.is(':checked'));
// Test effective_readonly in case we are using quick_edit,
// and we are not in edit mode.
// We could use this.view.get('actual_mode') which sons
// semantically better, possible values are
// at least `view`, `edit`, `create`, ...? to avoid doupt
// using bool seems safer!
if(!this.get('effective_readonly')){
this.internal_set_value(state);
event.preventDefault(); event.preventDefault();
}
}, this), }, this),
} }
_.extend(options, this.modifiers ? this.modifiers : {}); _.extend(options, this.modifiers ? this.modifiers : {});

Loading…
Cancel
Save