Browse Source

manage states (readonly/disabled) on form view

pull/236/head
Pierre Verkest 9 years ago
parent
commit
9ce5e96ad8
  1. 63
      web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js

63
web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js

@ -13,63 +13,64 @@ openerp.web_widget_boolean_switch = function(instance){
var options = options ? options : {}; var options = options ? options : {};
this.checkboxes = checkboxes; this.checkboxes = checkboxes;
var switchOptions = { var switchOptions = {
'readonly': options.hasOwnProperty('readonly') ? options.readonly : true,
'readonly': options.hasOwnProperty('readonly') ? options.readonly : false,
'disabled': options.hasOwnProperty('disabled') ? options.disabled : false,
}; };
if(options.hasOwnProperty('readonly')){
if(options.hasOwnProperty('onSwitchChange')){
switchOptions.onSwitchChange = options.onSwitchChange switchOptions.onSwitchChange = options.onSwitchChange
} }
'onSwitchChange'
this.checkboxes.bootstrapSwitch(switchOptions); this.checkboxes.bootstrapSwitch(switchOptions);
}, },
start: function(){
debugger;
},
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
this.checkboxes.bootstrapSwitch('state', value, false);
this.checkboxes.bootstrapSwitch('state', value, true);
},
set_readonly: function(value){
this.checkboxes.bootstrapSwitch('readonly', value);
},
set_disabled: function(value){
this.checkboxes.bootstrapSwitch('disabled', value);
}, },
// set_readonly: function(value){
// // the third parameter tell if we should skip to fire evnets
// this.checkboxes.bootstrapSwitch({'readonly': value});
// },
}); });
// Form view // Form view
instance.web.form.FieldBooleanSwitch = instance.web.form.AbstractField.extend(
instance.web.form.ReinitializeFieldMixin, {
instance.web.form.FieldBooleanSwitch = instance.web.form.AbstractField.extend({
template: 'FieldBooleanSwitch', template: 'FieldBooleanSwitch',
init: function(field_manager, node){
this._super(field_manager, node);
},
start: function(){ start: function(){
var self = this;
//TODO: Get options from xmlview to init widget
this.$checkbox = $("input", this.$el); this.$checkbox = $("input", this.$el);
this.widget = new openerp.instances.instance0.web.BooleanSwitchWidget(
var readonly = this.modifiers &&
this.modifiers.hasOwnProperty('readonly') ?
this.modifiers.readonly : false;
this.quick_edit = this.options &&
this.options.hasOwnProperty('quick_edit') ?
this.options.quick_edit : false;
var disabled = !this.quick_edit;
this.switcher = new openerp.instances.instance0.web.BooleanSwitchWidget(
this.$checkbox, { this.$checkbox, {
'readonly': readonly,
'disabled': disabled,
onSwitchChange: _.bind(function(event, state) { onSwitchChange: _.bind(function(event, state) {
this.internal_set_value(this.$checkbox.is(':checked')); this.internal_set_value(this.$checkbox.is(':checked'));
event.preventDefault(); event.preventDefault();
}, this) }, this)
}); });
this.setupFocus(this.$checkbox);
//TODO: use initialize_content to change
// var check_readonly = function() {
// self.$checkbox.prop('disabled', self.get("effective_readonly"));
// };
// this.on("change:effective_readonly", this, check_readonly);
// check_readonly.call(this);
this._super.apply(this, arguments);
this.on("change:effective_readonly", this, this.switcher_states);
this._super();
},
switcher_states: function () {
if (this.quick_edit)
return;
this.switcher.set_disabled(this.get('effective_readonly'))
}, },
render_value: function() { render_value: function() {
this.widget.set_value(this.get('value'));
//this.$checkbox.bootstrapSwitch('state', this.get('value'), true);
this.switcher.set_value(this.get('value'));
}, },
focus: function() {
var input = this.$checkbox && this.$checkbox[0];
return input ? input.focus() : false;
}
}); });
// List view // List view

Loading…
Cancel
Save