[FIX] Fixed & refactored form
in on_ui_change(), validate() is called first. set_value_from_ui() is only called if field is valid datetimepicker wrongly trigger on_change event when using 'setDate' method openerp on_changes were using widget.value instead of wiget.get_value() thus breaking relational fields on_changes bzr revid: fme@openerp.com-20110620144348-cuuubj3e5acx82zr
This commit is contained in:
parent
e0f94944ad
commit
d7b930e791
|
@ -89,9 +89,6 @@ openerp.base.FormView = openerp.base.View.extend( /** @lends openerp.base.FormV
|
||||||
var field = this.fields[f];
|
var field = this.fields[f];
|
||||||
field.touched = false;
|
field.touched = false;
|
||||||
field.set_value(this.datarecord[f] || false);
|
field.set_value(this.datarecord[f] || false);
|
||||||
// TODO: here we should be able to alter the 'touched' state when setting value
|
|
||||||
// the following line will be removed by fme as soon as refactoring has been done
|
|
||||||
field.touched = false;
|
|
||||||
field.validate();
|
field.validate();
|
||||||
}
|
}
|
||||||
if (!record.id) {
|
if (!record.id) {
|
||||||
|
@ -163,7 +160,7 @@ openerp.base.FormView = openerp.base.View.extend( /** @lends openerp.base.FormV
|
||||||
if (field in argument_replacement) {
|
if (field in argument_replacement) {
|
||||||
args.push(argument_replacement[field]);
|
args.push(argument_replacement[field]);
|
||||||
} else if (self.fields[field]) {
|
} else if (self.fields[field]) {
|
||||||
var value = self.fields[field].value;
|
var value = self.fields[field].get_value();
|
||||||
args.push(value == null ? false : value);
|
args.push(value == null ? false : value);
|
||||||
} else {
|
} else {
|
||||||
args.push(false);
|
args.push(false);
|
||||||
|
@ -194,7 +191,7 @@ openerp.base.FormView = openerp.base.View.extend( /** @lends openerp.base.FormV
|
||||||
if (field) {
|
if (field) {
|
||||||
var value = result.value[f];
|
var value = result.value[f];
|
||||||
processed.push(field.name);
|
processed.push(field.name);
|
||||||
if (field.value != value) {
|
if (field.get_value() != value) {
|
||||||
field.set_value(value);
|
field.set_value(value);
|
||||||
if (_.indexOf(processed, field.name) < 0) {
|
if (_.indexOf(processed, field.name) < 0) {
|
||||||
this.do_onchange(field, processed);
|
this.do_onchange(field, processed);
|
||||||
|
@ -694,11 +691,16 @@ openerp.base.form.Field = openerp.base.form.Widget.extend({
|
||||||
},
|
},
|
||||||
on_ui_change: function() {
|
on_ui_change: function() {
|
||||||
this.touched = this.view.touched = true;
|
this.touched = this.view.touched = true;
|
||||||
this.set_value_from_ui();
|
|
||||||
this.validate();
|
this.validate();
|
||||||
this.view.on_form_changed(this);
|
if (!this.invalid) {
|
||||||
|
this.set_value_from_ui();
|
||||||
|
this.view.on_form_changed(this);
|
||||||
|
} else {
|
||||||
|
this.update_dom();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
validate: function() {
|
validate: function() {
|
||||||
|
this.invalid = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -725,10 +727,11 @@ openerp.base.form.FieldChar = openerp.base.form.Field.extend({
|
||||||
},
|
},
|
||||||
validate: function() {
|
validate: function() {
|
||||||
this.invalid = false;
|
this.invalid = false;
|
||||||
if (this.value === false || this.value === "") {
|
var value = this.$element.find('input').val();
|
||||||
|
if (value === "") {
|
||||||
this.invalid = this.required;
|
this.invalid = this.required;
|
||||||
} else if (this.validation_regex) {
|
} else if (this.validation_regex) {
|
||||||
this.invalid = !this.validation_regex.test(this.value);
|
this.invalid = !this.validation_regex.test(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -781,23 +784,16 @@ openerp.base.form.FieldFloat = openerp.base.form.FieldChar.extend({
|
||||||
this.validation_regex = /^-?\d+(\.\d+)?$/;
|
this.validation_regex = /^-?\d+(\.\d+)?$/;
|
||||||
},
|
},
|
||||||
set_value: function(value) {
|
set_value: function(value) {
|
||||||
if (!value) {
|
this._super.apply(this, [value]);
|
||||||
|
if (value === false || value === undefined) {
|
||||||
// As in GTK client, floats default to 0
|
// As in GTK client, floats default to 0
|
||||||
this.touched = this.view.touched = true;
|
|
||||||
value = 0;
|
value = 0;
|
||||||
}
|
}
|
||||||
this._super.apply(this, [value]);
|
|
||||||
var show_value = value.toFixed(2);
|
var show_value = value.toFixed(2);
|
||||||
this.$element.find('input').val(show_value);
|
this.$element.find('input').val(show_value);
|
||||||
},
|
},
|
||||||
set_value_from_ui: function() {
|
set_value_from_ui: function() {
|
||||||
this.value = this.$element.find('input').val().replace(/,/g, '.');
|
this.value = Number(this.$element.find('input').val().replace(/,/g, '.'));
|
||||||
},
|
|
||||||
validate: function() {
|
|
||||||
this._super.apply(this, arguments);
|
|
||||||
if (!this.invalid) {
|
|
||||||
this.value = Number(this.value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -819,8 +815,10 @@ openerp.base.form.FieldDatetime = openerp.base.form.Field.extend({
|
||||||
if (value == null || value == false) {
|
if (value == null || value == false) {
|
||||||
this.$element.find('input').val('');
|
this.$element.find('input').val('');
|
||||||
} else {
|
} else {
|
||||||
this.value = this.parse(value);
|
this.$element.find('input').unbind('change');
|
||||||
this.$element.find('input')[this.jqueryui_object]('setDate', this.value);
|
// jQuery UI date picker wrongly call on_change event herebelow
|
||||||
|
this.$element.find('input')[this.jqueryui_object]('setDate', this.parse(value));
|
||||||
|
this.$element.find('input').change(this.on_ui_change);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
set_value_from_ui: function() {
|
set_value_from_ui: function() {
|
||||||
|
@ -830,7 +828,7 @@ openerp.base.form.FieldDatetime = openerp.base.form.Field.extend({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
validate: function() {
|
validate: function() {
|
||||||
this.invalid = this.required && this.value === false;
|
this.invalid = this.required && this.$element.find('input')[this.jqueryui_object]('getDate') === '';
|
||||||
},
|
},
|
||||||
parse: openerp.base.parse_datetime,
|
parse: openerp.base.parse_datetime,
|
||||||
format: openerp.base.format_datetime
|
format: openerp.base.format_datetime
|
||||||
|
@ -851,19 +849,17 @@ openerp.base.form.FieldFloatTime = openerp.base.form.FieldChar.extend({
|
||||||
this.validation_regex = /^\d+:\d+$/;
|
this.validation_regex = /^\d+:\d+$/;
|
||||||
},
|
},
|
||||||
set_value: function(value) {
|
set_value: function(value) {
|
||||||
value = value || 0;
|
|
||||||
this._super.apply(this, [value]);
|
this._super.apply(this, [value]);
|
||||||
|
if (value === false || value === undefined) {
|
||||||
|
// As in GTK client, floats default to 0
|
||||||
|
value = 0;
|
||||||
|
}
|
||||||
var show_value = _.sprintf("%02d:%02d", Math.floor(value), Math.round((value % 1) * 60));
|
var show_value = _.sprintf("%02d:%02d", Math.floor(value), Math.round((value % 1) * 60));
|
||||||
this.$element.find('input').val(show_value);
|
this.$element.find('input').val(show_value);
|
||||||
},
|
},
|
||||||
validate: function() {
|
set_value_from_ui: function() {
|
||||||
if (typeof(this.value) == "string") {
|
var time = this.$element.find('input').val().split(':');
|
||||||
this._super.apply(this, arguments);
|
this.set_value(parseInt(time[0], 10) + parseInt(time[1], 10) / 60);
|
||||||
if (!this.invalid) {
|
|
||||||
var time = this.value.split(':');
|
|
||||||
this.set_value(parseInt(time[0], 10) + parseInt(time[1], 10) / 60);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -891,10 +887,11 @@ openerp.base.form.FieldText = openerp.base.form.Field.extend({
|
||||||
},
|
},
|
||||||
validate: function() {
|
validate: function() {
|
||||||
this.invalid = false;
|
this.invalid = false;
|
||||||
if (this.value === false || this.value === "") {
|
var value = this.$element.find('textarea').val();
|
||||||
|
if (value === "") {
|
||||||
this.invalid = this.required;
|
this.invalid = this.required;
|
||||||
} else if (this.validation_regex) {
|
} else if (this.validation_regex) {
|
||||||
this.invalid = !this.validation_regex.test(this.value);
|
this.invalid = !this.validation_regex.test(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -925,7 +922,7 @@ openerp.base.form.FieldBoolean = openerp.base.form.Field.extend({
|
||||||
this.$element.find('input').attr('disabled', this.readonly);
|
this.$element.find('input').attr('disabled', this.readonly);
|
||||||
},
|
},
|
||||||
validate: function() {
|
validate: function() {
|
||||||
this.invalid = this.required && !this.value;
|
this.invalid = this.required && !this.$element.find('input').is(':checked');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -980,22 +977,22 @@ openerp.base.form.FieldSelection = openerp.base.form.Field.extend({
|
||||||
this.$element.find('select').attr('disabled', this.readonly);
|
this.$element.find('select').attr('disabled', this.readonly);
|
||||||
},
|
},
|
||||||
validate: function() {
|
validate: function() {
|
||||||
this.invalid = this.required && this.value === "";
|
this.invalid = this.required && this.$element.find('select').val() === "";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// jquery autocomplete tweak to allow html
|
// jquery autocomplete tweak to allow html
|
||||||
(function(x) {
|
(function() {
|
||||||
var proto = $.ui.autocomplete.prototype,
|
var proto = $.ui.autocomplete.prototype,
|
||||||
initSource = proto._initSource;
|
initSource = proto._initSource;
|
||||||
|
|
||||||
function filter( array, term ) {
|
function filter( array, term ) {
|
||||||
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
|
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
|
||||||
return $.grep( array, function(value) {
|
return $.grep( array, function(value) {
|
||||||
return matcher.test( $( "<div>" ).html( value.label || value.value || value ).text() );
|
return matcher.test( $( "<div>" ).html( value.label || value.value || value ).text() );
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$.extend( proto, {
|
$.extend( proto, {
|
||||||
_initSource: function() {
|
_initSource: function() {
|
||||||
if ( this.options.html && $.isArray(this.options.source) ) {
|
if ( this.options.html && $.isArray(this.options.source) ) {
|
||||||
|
@ -1006,7 +1003,7 @@ openerp.base.form.FieldSelection = openerp.base.form.Field.extend({
|
||||||
initSource.call( this );
|
initSource.call( this );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_renderItem: function( ul, item) {
|
_renderItem: function( ul, item) {
|
||||||
return $( "<li></li>" )
|
return $( "<li></li>" )
|
||||||
.data( "item.autocomplete", item )
|
.data( "item.autocomplete", item )
|
||||||
|
@ -1014,7 +1011,7 @@ openerp.base.form.FieldSelection = openerp.base.form.Field.extend({
|
||||||
.appendTo( ul );
|
.appendTo( ul );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})(null);
|
})();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a new context usable for operations related to fields by merging
|
* Builds a new context usable for operations related to fields by merging
|
||||||
|
@ -1043,7 +1040,7 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
|
||||||
this.$input = this.$element.find("input");
|
this.$input = this.$element.find("input");
|
||||||
this.$drop_down = this.$element.find(".oe-m2o-drop-down-button");
|
this.$drop_down = this.$element.find(".oe-m2o-drop-down-button");
|
||||||
this.$menu_btn = this.$element.find(".oe-m2o-cm-button");
|
this.$menu_btn = this.$element.find(".oe-m2o-cm-button");
|
||||||
|
|
||||||
// context menu
|
// context menu
|
||||||
var bindings = {};
|
var bindings = {};
|
||||||
bindings[this.cm_id + "_search"] = function() {
|
bindings[this.cm_id + "_search"] = function() {
|
||||||
|
@ -1078,7 +1075,7 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// some behavior for input
|
// some behavior for input
|
||||||
this.$input.keyup(function() {
|
this.$input.keyup(function() {
|
||||||
if (self.$input.val() === "") {
|
if (self.$input.val() === "") {
|
||||||
|
@ -1111,7 +1108,7 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.$input.focusout(anyoneLoosesFocus);
|
this.$input.focusout(anyoneLoosesFocus);
|
||||||
|
|
||||||
// autocomplete
|
// autocomplete
|
||||||
this.$input.autocomplete({
|
this.$input.autocomplete({
|
||||||
source: function(req, resp) { self.get_search_result(req, resp); },
|
source: function(req, resp) { self.get_search_result(req, resp); },
|
||||||
|
@ -1138,9 +1135,9 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
|
||||||
get_search_result: function(request, response) {
|
get_search_result: function(request, response) {
|
||||||
var search_val = request.term;
|
var search_val = request.term;
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, []);
|
var dataset = new openerp.base.DataSetStatic(this.session, this.field.relation, []);
|
||||||
|
|
||||||
dataset.name_search([search_val, self.field.domain || [], 'ilike',
|
dataset.name_search([search_val, self.field.domain || [], 'ilike',
|
||||||
build_relation_context(self), this.limit + 1], function(data) {
|
build_relation_context(self), this.limit + 1], function(data) {
|
||||||
self.last_search = data.result;
|
self.last_search = data.result;
|
||||||
|
@ -1148,7 +1145,7 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
|
||||||
var values = _.map(data.result, function(x) {
|
var values = _.map(data.result, function(x) {
|
||||||
return {label: $('<span />').text(x[1]).html(), name:x[1], id:x[0]};
|
return {label: $('<span />').text(x[1]).html(), name:x[1], id:x[0]};
|
||||||
});
|
});
|
||||||
|
|
||||||
// search more... if more results that max
|
// search more... if more results that max
|
||||||
if (values.length > self.limit) {
|
if (values.length > self.limit) {
|
||||||
values = values.slice(0, self.limit);
|
values = values.slice(0, self.limit);
|
||||||
|
@ -1175,7 +1172,7 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
|
||||||
self._change_int_value(null);
|
self._change_int_value(null);
|
||||||
self._search_create_popup("form");
|
self._search_create_popup("form");
|
||||||
}});
|
}});
|
||||||
|
|
||||||
response(values);
|
response(values);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -1247,16 +1244,16 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
},
|
},
|
||||||
start: function() {
|
start: function() {
|
||||||
this._super.apply(this, arguments);
|
this._super.apply(this, arguments);
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.dataset = new openerp.base.DataSetStatic(this.session, this.field.relation);
|
this.dataset = new openerp.base.DataSetStatic(this.session, this.field.relation);
|
||||||
this.dataset.on_unlink.add_last(function(ids) {
|
this.dataset.on_unlink.add_last(function(ids) {
|
||||||
self.dataset.set_ids(_.without.apply(_, [self.dataset.ids].concat(ids)));
|
self.dataset.set_ids(_.without.apply(_, [self.dataset.ids].concat(ids)));
|
||||||
self.on_ui_change();
|
self.on_ui_change();
|
||||||
self.reload_current_view();
|
self.reload_current_view();
|
||||||
});
|
});
|
||||||
|
|
||||||
var modes = this.node.attrs.mode;
|
var modes = this.node.attrs.mode;
|
||||||
modes = !!modes ? modes.split(",") : ["tree", "form"];
|
modes = !!modes ? modes.split(",") : ["tree", "form"];
|
||||||
var views = [];
|
var views = [];
|
||||||
|
@ -1272,7 +1269,7 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
views.push(view);
|
views.push(view);
|
||||||
});
|
});
|
||||||
this.views = views;
|
this.views = views;
|
||||||
|
|
||||||
this.viewmanager = new openerp.base.ViewManager(this.view.session,
|
this.viewmanager = new openerp.base.ViewManager(this.view.session,
|
||||||
this.element_id, this.dataset, views);
|
this.element_id, this.dataset, views);
|
||||||
var reg = new openerp.base.Registry();
|
var reg = new openerp.base.Registry();
|
||||||
|
@ -1280,7 +1277,7 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
reg.add("graph", openerp.base.views.map["graph"]);
|
reg.add("graph", openerp.base.views.map["graph"]);
|
||||||
reg.add("list", "openerp.base.form.One2ManyListView");
|
reg.add("list", "openerp.base.form.One2ManyListView");
|
||||||
this.viewmanager.registry = reg;
|
this.viewmanager.registry = reg;
|
||||||
|
|
||||||
this.viewmanager.on_controller_inited.add_last(function(view_type, controller) {
|
this.viewmanager.on_controller_inited.add_last(function(view_type, controller) {
|
||||||
if (view_type == "list") {
|
if (view_type == "list") {
|
||||||
controller.o2m = self;
|
controller.o2m = self;
|
||||||
|
@ -1290,7 +1287,7 @@ openerp.base.form.FieldOne2Many = openerp.base.form.Field.extend({
|
||||||
self.is_started.resolve();
|
self.is_started.resolve();
|
||||||
});
|
});
|
||||||
this.viewmanager.start();
|
this.viewmanager.start();
|
||||||
|
|
||||||
$.when(this.is_started, this.is_setted).then(function() {
|
$.when(this.is_started, this.is_setted).then(function() {
|
||||||
if (modes[0] == "tree") {
|
if (modes[0] == "tree") {
|
||||||
var view = self.viewmanager.views[self.viewmanager.active_view].controller;
|
var view = self.viewmanager.views[self.viewmanager.active_view].controller;
|
||||||
|
@ -1348,14 +1345,14 @@ openerp.base.form.FieldMany2Many = openerp.base.form.Field.extend({
|
||||||
},
|
},
|
||||||
start: function() {
|
start: function() {
|
||||||
this._super.apply(this, arguments);
|
this._super.apply(this, arguments);
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.dataset = new openerp.base.DataSetStatic(
|
this.dataset = new openerp.base.DataSetStatic(
|
||||||
this.session, this.field.relation);
|
this.session, this.field.relation);
|
||||||
this.dataset.on_unlink.add_last(function(ids) {
|
this.dataset.on_unlink.add_last(function(ids) {
|
||||||
self.list_view.reload_content();
|
self.list_view.reload_content();
|
||||||
|
|
||||||
self.on_ui_change();
|
self.on_ui_change();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue