From 87d958d38b827d72f859262ec504b7a799e113b5 Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Fri, 2 Oct 2015 11:49:48 +0200 Subject: [PATCH] allow extra parameters + poc of quick editing --- web_widget_boolean_switch/README.rst | 50 ++++++++++++++++++- .../demo/res_users_view.xml | 9 ++-- .../src/js/web_widget_boolean_switch.js | 36 +++++++++++-- 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/web_widget_boolean_switch/README.rst b/web_widget_boolean_switch/README.rst index 5cc824ad..df31a543 100644 --- a/web_widget_boolean_switch/README.rst +++ b/web_widget_boolean_switch/README.rst @@ -18,9 +18,55 @@ To install this module, you need to: Configuration ============= -To configure this module, you need to: +Example +------- + +```xml + +``` + +Options +------- + + +quick_edit +~~~~~~~~~~ + +extra +~~~~~ +``extra`` is used to set +`bootstrap-switch `_ options. + +Available:: + + * **size**: - default: `null` + * **animate**: - default: `true` + * **indeterminate**: `false` + * **inverse**: `false` + * **radioAllOff**: `false` + * **onColor**: `"primary"` + * **offColor**: `default` + * **onText**: `"ON"`, + * **offText**: `"OFF"`, + * **labelText**: `" "`, + * **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 ===== diff --git a/web_widget_boolean_switch/demo/res_users_view.xml b/web_widget_boolean_switch/demo/res_users_view.xml index 1543c83a..ffe16e9c 100644 --- a/web_widget_boolean_switch/demo/res_users_view.xml +++ b/web_widget_boolean_switch/demo/res_users_view.xml @@ -19,7 +19,8 @@ 20 - + @@ -29,7 +30,8 @@ res.users - + @@ -43,7 +45,8 @@ - + diff --git a/web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js b/web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js index 4e9818f5..2f713a40 100644 --- a/web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js +++ b/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; var readonly = options.hasOwnProperty('readonly') ? options.readonly : false; + var switchOptions = options.hasOwnProperty('extra') ? + options.extra : {}; - var switchOptions = { + + _.extend(switchOptions, { 'readonly': options.hasOwnProperty('readonly') ? options.readonly : readonly, 'disabled': options.hasOwnProperty('disabled') ? options.disabled : !this.quick_edit, - }; + }); if(options.hasOwnProperty('onSwitchChange')){ switchOptions.onSwitchChange = options.onSwitchChange } 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){ // the third parameter tell if we should skip to fire evnets @@ -55,8 +75,16 @@ openerp.web_widget_boolean_switch = function(instance){ var options = { onSwitchChange: _.bind(function(event, state) { - this.internal_set_value(this.$checkbox.is(':checked')); - event.preventDefault(); + // 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(); + } }, this), } _.extend(options, this.modifiers ? this.modifiers : {});