[ADD] Plugged form save events

bzr revid: fme@openerp.com-20110405143425-iqpibce75hri3pdb
This commit is contained in:
Fabien Meghazi 2011-04-05 16:34:25 +02:00
parent 052ee7bb31
commit ed1625a04c
4 changed files with 154 additions and 87 deletions

View File

@ -406,7 +406,14 @@ body.openerp {
.openerp .required.error { .openerp .required.error {
border: 1px solid #900; border: 1px solid #900;
} }
.openerp .oe_form_pager, .openerp .oe_list_pager { .openerp .oe_form_buttons {
float: left;
}
.openerp .oe_form_pager {
float: right;
}
.openerp .oe_list_pager {
text-align: right; text-align: right;
} }
@ -440,9 +447,13 @@ body.openerp {
border-bottom-width: 1px; border-bottom-width: 1px;
margin: 6px 4px 6px 1px; margin: 6px 4px 6px 1px;
} }
.openerp input[required=required], .openerp select[required=required] { .openerp td.required input, .openerp td.required select {
background-color: #D2D2FF; background-color: #D2D2FF;
} }
.openerp td.invalid input, .openerp td.invalid select {
background-color: #F66;
border: 1px solid #D00;
}
/* jQuery UI override */ /* jQuery UI override */
.openerp .ui-widget { .openerp .ui-widget {

View File

@ -77,6 +77,7 @@ openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base.
}); });
}, },
fetch_ids: function (ids, fields, callback) { fetch_ids: function (ids, fields, callback) {
var self = this;
this.rpc('/base/dataset/get', { this.rpc('/base/dataset/get', {
model: this.model, model: this.model,
ids: ids, ids: ids,
@ -135,6 +136,14 @@ openerp.base.DataRecord = openerp.base.Controller.extend({
on_change: function() { on_change: function() {
}, },
on_reload: function() { on_reload: function() {
},
save: function(callback) {
console.log("datarecord", this.values)
this.rpc('/base/datarecord/save', {
model: this.model,
id: this.id,
data: this.values
}, callback);
} }
}); });

View File

@ -25,7 +25,7 @@ openerp.base.FormView = openerp.base.Controller.extend( /** @lends openerp.base
this.widgets = {}; this.widgets = {};
this.widgets_counter = 0; this.widgets_counter = 0;
this.fields = {}; this.fields = {};
this.datarecord = {}; this.datarecord = null;
this.ready = false; this.ready = false;
}, },
start: function() { start: function() {
@ -46,10 +46,15 @@ openerp.base.FormView = openerp.base.Controller.extend( /** @lends openerp.base
var action = $(this).data('pager-action'); var action = $(this).data('pager-action');
self.on_pager_action(action); self.on_pager_action(action);
}); });
this.$element.find('div.oe_form_buttons button.oe_form_button_save').click(this.do_save);
this.$element.find('div.oe_form_buttons button.oe_form_button_save_edit').click(this.do_save_edit);
this.$element.find('div.oe_form_buttons button.oe_form_button_cancel').click(this.do_cancel);
// sidebar stuff // sidebar stuff
if (this.view_manager.sidebar) if (this.view_manager.sidebar) {
this.view_manager.sidebar.load_multi_actions(); this.view_manager.sidebar.load_multi_actions();
}
}, },
on_record_loaded: function(record) { on_record_loaded: function(record) {
if (record.length) { if (record.length) {
@ -81,21 +86,23 @@ openerp.base.FormView = openerp.base.Controller.extend( /** @lends openerp.base
return false; return false;
} }
var invalid = false; var invalid = false;
var values = {};
for (var f in this.fields) { for (var f in this.fields) {
f = this.fields[f]; f = this.fields[f];
if (f.invalid) { if (f.invalid) {
invalid = true; invalid = true;
} else { } else if (f.touched) {
values[f.name] = f.value; this.datarecord.values[f.name] = f.get_value();
} }
} }
if (invalid) { if (invalid) {
this.on_invalid(); this.on_invalid();
} else { } else {
console.log("Save form", values); this.datarecord.save(this.on_saved);
// TODO: save values via datarecord }
// rpc - save.callbacl on_saved },
do_save_edit: function() {
if (this.do_save()) {
this.switch_readonly();
} }
}, },
do_show: function () { do_show: function () {
@ -129,9 +136,16 @@ openerp.base.FormView = openerp.base.Controller.extend( /** @lends openerp.base
} }
this.dataset.fetch_index(this.fields_view.fields, this.on_record_loaded); this.dataset.fetch_index(this.fields_view.fields, this.on_record_loaded);
}, },
on_invalid: function() { switch_readonly: function() {
}, },
on_saved: function() { switch_editable: function() {
},
on_invalid: function() {
console.info("Form invalid");
},
on_saved: function(r) {
console.log("Responsed from save", r)
debugger;
// Check response for exceptions, display error // Check response for exceptions, display error
}, },
do_search: function (domains, contexts, groupbys) { do_search: function (domains, contexts, groupbys) {
@ -378,20 +392,23 @@ openerp.base.form.Field = openerp.base.form.Widget.extend({
this.readonly = (node.attrs.readonly == '1'); this.readonly = (node.attrs.readonly == '1');
this.required = (node.attrs.required == '1'); this.required = (node.attrs.required == '1');
this.invalid = false; this.invalid = false;
this.touched = false;
}, },
set_value: function(value) { set_value: function(value) {
this.value = value; this.value = value;
}, },
get_value: function(value) { get_value: function() {
return value; return this.value;
}, },
update_dom: function() { update_dom: function() {
this._super.apply(this, arguments); this._super.apply(this, arguments);
this.$element.toggleClass('disabled', this.readonly); this.$element.toggleClass('disabled', this.readonly);
this.$element.toggleClass('required', this.required); this.$element.toggleClass('required', this.required);
this.$element.toggleClass('invalid', this.invalid);
}, },
on_ui_change: function() { on_ui_change: function() {
this.view.on_form_changed(this); this.view.on_form_changed(this);
this.touched = true;
} }
}); });
@ -409,14 +426,9 @@ openerp.base.form.FieldChar = openerp.base.form.Field.extend({
var show_value = (value != null && value !== false) ? value : ''; var show_value = (value != null && value !== false) ? value : '';
this.$element.find('input').val(show_value); this.$element.find('input').val(show_value);
}, },
get_value: function() {
},
update_dom: function() { update_dom: function() {
this._super.apply(this, arguments); this._super.apply(this, arguments);
this.$element.find('input').attr({ this.$element.find('input').attr('disabled', this.readonly);
'disabled' : this.readonly,
'required' : this.required
});
}, },
on_ui_change: function() { on_ui_change: function() {
this.value = this.$element.find('input').val(); this.value = this.$element.find('input').val();
@ -431,29 +443,59 @@ openerp.base.form.FieldEmail = openerp.base.form.FieldChar.extend({
openerp.base.form.FieldUrl = openerp.base.form.FieldChar.extend({ openerp.base.form.FieldUrl = openerp.base.form.FieldChar.extend({
}); });
openerp.base.form.FieldFloat = openerp.base.form.Field.extend({ openerp.base.form.FieldFloat = openerp.base.form.FieldChar.extend({
init: function(view, node) {
this._super(view, node);
this.template = "FieldChar";
},
start: function() {
this._super.apply(this, arguments);
this.$element.find('input').change(this.on_ui_change);
},
set_value: function(value) { set_value: function(value) {
this._super.apply(this, arguments); this._super.apply(this, arguments);
var show_value = (value != null && value !== false) ? value.toFixed(2) : ''; var show_value = (value != null && value !== false) ? value.toFixed(2) : '';
this.$element.find('input').val(value); this.$element.find('input').val(value);
}, },
get_value: function() { on_ui_change: function() {
}, this.value = parseFloat(this.$element.find('input').val()); // TODO: do it well
update_dom: function() { this.invalid = this.required && this.value == "";
this._super.apply(this, arguments); this._super.apply(this, arguments);
this.$element.find('input').attr({ }
'disabled' : this.readonly, });
'required' : this.required
openerp.base.form.FieldDate = openerp.base.form.FieldChar.extend({
init: function(view, node) {
this._super(view, node);
this.template = "FieldDate";
},
start: function() {
this._super.apply(this, arguments);
this.$element.find('input').change(this.on_ui_change).datepicker({
dateFormat: 'yy-mm-dd'
}); });
}, },
set_value: function(value) {
this._super.apply(this, arguments);
var show_value = (value != null && value !== false) ? value : '';
this.$element.find('input').val(show_value);
},
on_ui_change: function() {
this.value = this.$element.find('input').val();
this.invalid = this.required && this.value == "";
this._super.apply(this, arguments);
}
});
openerp.base.form.FieldDatetime = openerp.base.form.FieldChar.extend({
init: function(view, node) {
this._super(view, node);
this.template = "FieldDatetime";
},
start: function() {
this._super.apply(this, arguments);
this.$element.find('input').change(this.on_ui_change).datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm:ss'
});
},
set_value: function(value) {
this._super.apply(this, arguments);
var show_value = (value != null && value !== false) ? value : '';
this.$element.find('input').val(show_value);
},
on_ui_change: function() { on_ui_change: function() {
this.value = this.$element.find('input').val(); this.value = this.$element.find('input').val();
this.invalid = this.required && this.value == ""; this.invalid = this.required && this.value == "";
@ -466,13 +508,23 @@ openerp.base.form.FieldText = openerp.base.form.Field.extend({
this._super(view, node); this._super(view, node);
this.template = "FieldText"; this.template = "FieldText";
}, },
start: function() {
this._super.apply(this, arguments);
this.$element.find('textarea').change(this.on_ui_change);
},
set_value: function(value) { set_value: function(value) {
this._super.apply(this, arguments); this._super.apply(this, arguments);
var show_value = (value != null && value !== false) ? value : ''; var show_value = (value != null && value !== false) ? value : '';
this.$element.find('textarea').val(show_value); this.$element.find('textarea').val(show_value);
}, },
get_value: function() { update_dom: function() {
return this.$element.find('textarea').val(); this._super.apply(this, arguments);
this.$element.find('textarea').attr('disabled', this.readonly);
},
on_ui_change: function() {
this.value = this.$element.find('textarea').val();
this.invalid = this.required && this.value == "";
this._super.apply(this, arguments);
} }
}); });
@ -481,53 +533,27 @@ openerp.base.form.FieldBoolean = openerp.base.form.Field.extend({
this._super(view, node); this._super(view, node);
this.template = "FieldBoolean"; this.template = "FieldBoolean";
}, },
start: function() {
var self = this;
this._super.apply(this, arguments);
this.$element.find('input').click(function() {
if ($(this)[0].checked != this.value) {
self.on_ui_change();
}
});
},
set_value: function(value) { set_value: function(value) {
this._super.apply(this, arguments); this._super.apply(this, arguments);
this.$element.find('input')[0].checked = value; this.$element.find('input')[0].checked = value;
}, },
get_value: function() { update_dom: function() {
return this.$element.find('input').is(':checked');
}
});
openerp.base.form.FieldDate = openerp.base.form.FieldChar.extend({
init: function(view, node) {
this._super(view, node);
this.template = "FieldDate";
},
start: function() {
this._super.apply(this, arguments); this._super.apply(this, arguments);
this.$element.find('input').datepicker({ this.$element.find('textarea').attr('disabled', this.readonly);
dateFormat: 'yy-mm-dd'
});
}, },
set_value: function(value) { on_ui_change: function() {
this.value = this.$element.find('input').is(':checked');
this.invalid = this.required && !this.value;
this._super.apply(this, arguments); this._super.apply(this, arguments);
var show_value = (value != null && value !== false) ? value : '';
this.$element.find('input').val(show_value);
},
get_value: function() {
}
});
openerp.base.form.FieldDatetime = openerp.base.form.FieldChar.extend({
init: function(view, node) {
this._super(view, node);
this.template = "FieldDatetime";
},
start: function() {
this._super.apply(this, arguments);
this.$element.find('input').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm:ss'
});
},
set_value: function(value) {
this._super.apply(this, arguments);
var show_value = (value != null && value !== false) ? value : '';
this.$element.find('input').val(show_value);
},
get_value: function() {
} }
}); });
@ -540,6 +566,10 @@ openerp.base.form.FieldSelection = openerp.base.form.Field.extend({
this._super(view, node); this._super(view, node);
this.template = "FieldSelection"; this.template = "FieldSelection";
}, },
start: function() {
this._super.apply(this, arguments);
this.$element.find('select').change(this.on_ui_change);
},
set_value: function(value) { set_value: function(value) {
this._super.apply(this, arguments); this._super.apply(this, arguments);
if (value != null && value !== false) { if (value != null && value !== false) {
@ -548,8 +578,14 @@ openerp.base.form.FieldSelection = openerp.base.form.Field.extend({
this.$element.find('select')[0].selectedIndex = 0; this.$element.find('select')[0].selectedIndex = 0;
} }
}, },
get_value: function() { update_dom: function() {
return this.$element.find('select').val(); this._super.apply(this, arguments);
this.$element.find('select').attr('disabled', this.readonly);
},
on_ui_change: function() {
this.value = this.$element.find('select').val();
this.invalid = this.required && this.value == "";
this._super.apply(this, arguments);
} }
}); });
@ -560,7 +596,11 @@ openerp.base.form.FieldMany2One = openerp.base.form.Field.extend({
}, },
set_value: function(value) { set_value: function(value) {
this._super.apply(this, arguments); this._super.apply(this, arguments);
var show_value = (value != null && value !== false) ? value[1] : ''; var show_value = ''
if (value != null && value !== false) {
show_value = value[1];
this.value = value[0];
}
this.$element.find('input').val(show_value); this.$element.find('input').val(show_value);
} }
}); });

View File

@ -156,14 +156,21 @@
</t> </t>
<t t-name="FormView"> <t t-name="FormView">
<h2 class="oe_view_title"><t t-esc="view.fields_view.arch.attrs.string"/></h2> <h2 class="oe_view_title"><t t-esc="view.fields_view.arch.attrs.string"/></h2>
<div class="oe_form_pager"> <div class="oe_form_header">
<button type="button" data-pager-action="first">First</button> <div class="oe_form_buttons">
<button type="button" data-pager-action="previous">&lt;&lt;</button> <button type="button" class="oe_form_button_save">Save</button>
<button type="button" class="oe_form_button_save_edit">Save &amp; Edit</button>
<button type="button" class="oe_form_button_cancel">Cancel</button>
</div>
<div class="oe_form_pager">
<button type="button" data-pager-action="first">First</button>
<button type="button" data-pager-action="previous">&lt;&lt;</button>
<span class="oe_pager_index">0</span> / <span class="oe_pager_count">0</span> <span class="oe_pager_index">0</span> / <span class="oe_pager_count">0</span>
<button type="button" data-pager-action="next">&gt;&gt;</button> <button type="button" data-pager-action="next">&gt;&gt;</button>
<button type="button" data-pager-action="last">Last</button> <button type="button" data-pager-action="last">Last</button>
</div>
</div> </div>
<t t-raw="frame.render()"/> <t t-raw="frame.render()"/>
<div style="text-align:right;"> <div style="text-align:right;">