[imp] improved AbstractFormPopup

bzr revid: nicolas.vanhoren@openerp.com-20120516130142-quyvtv0s35uvbuug
This commit is contained in:
niv-openerp 2012-05-16 15:01:42 +02:00
parent 57894a5361
commit ab21c9ac81
2 changed files with 69 additions and 93 deletions

View File

@ -1079,41 +1079,44 @@ instance.web.ProxyDataSet = instance.web.DataSetSearch.extend({
this.create_function = null; this.create_function = null;
this.write_function = null; this.write_function = null;
this.read_function = null; this.read_function = null;
this.default_get_function = null;
this.unlink_function = null;
}, },
read_ids: function () { read_ids: function (ids, fields, options) {
if (this.read_function) { if (this.read_function) {
return this.read_function.apply(null, arguments); return this.read_function(ids, fields, options, this._super);
} else { } else {
return this._super.apply(this, arguments); return this._super.apply(this, arguments);
} }
}, },
default_get: function(fields, options) { default_get: function(fields, options) {
return this._super(fields, options).then(this.on_default_get); if (this.default_get_function) {
return this.default_get_function(fields, options, this._super);
} else {
return this._super.apply(this, arguments);
}
}, },
on_default_get: function(result) {},
create: function(data, callback, error_callback) { create: function(data, callback, error_callback) {
this.on_create(data);
if (this.create_function) { if (this.create_function) {
return this.create_function(data, callback, error_callback); return this.create_function(data, this._super).then(callback, error_callback);
} else { } else {
return $.async_when({"result": undefined}).then(callback); return this._super.apply(this, arguments);
} }
}, },
on_create: function(data) {}, write: function (id, data, options, callback, error_callback) {
write: function (id, data, options, callback) {
this.on_write(id, data);
if (this.write_function) { if (this.write_function) {
return this.write_function(id, data, options, callback); return this.write_function(id, data, options, this._super).then(callback, error_callback);
} else { } else {
return $.async_when({"result": true}).then(callback); return this._super.apply(this, arguments);
} }
}, },
on_write: function(id, data) {},
unlink: function(ids, callback, error_callback) { unlink: function(ids, callback, error_callback) {
this.on_unlink(ids); if (this.unlink_function) {
return $.async_when({"result": true}).then(callback); return this.unlink_function(ids, this._super).then(callback, error_callback);
} else {
return this._super.apply(this, arguments);
}
}, },
on_unlink: function(ids) {}
}); });
instance.web.CompoundContext = instance.web.Class.extend({ instance.web.CompoundContext = instance.web.Class.extend({

View File

@ -2970,7 +2970,11 @@ instance.web.form.One2ManyViewManager = instance.web.ViewManager.extend({
var pop = new instance.web.form.FormOpenPopup(self.o2m.view); var pop = new instance.web.form.FormOpenPopup(self.o2m.view);
pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(), { pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(), {
title: _t("Open: ") + self.name, title: _t("Open: ") + self.name,
auto_write: false, write_function: function(id, data, options) {
return self.o2m.dataset.write(id, data, {}).then(function() {
self.o2m.reload_current_view();
});
},
alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined, alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
parent_view: self.o2m.view, parent_view: self.o2m.view,
child_name: self.o2m.name, child_name: self.o2m.name,
@ -2980,11 +2984,6 @@ instance.web.form.One2ManyViewManager = instance.web.ViewManager.extend({
form_view_options: {'not_interactible_on_create':true}, form_view_options: {'not_interactible_on_create':true},
readonly: self.o2m.get("effective_readonly") readonly: self.o2m.get("effective_readonly")
}); });
pop.on_write.add(function(id, data) {
self.o2m.dataset.write(id, data, {}, function(r) {
self.o2m.reload_current_view();
});
});
}, },
}); });
@ -3003,7 +3002,6 @@ instance.web.form.One2ManyListView = instance.web.ListView.extend({
} else { } else {
var self = this; var self = this;
var pop = new instance.web.form.SelectCreatePopup(this); var pop = new instance.web.form.SelectCreatePopup(this);
pop.on_default_get.add(self.dataset.on_default_get);
pop.select_element( pop.select_element(
self.o2m.field.relation, self.o2m.field.relation,
{ {
@ -3036,7 +3034,11 @@ instance.web.form.One2ManyListView = instance.web.ListView.extend({
var pop = new instance.web.form.FormOpenPopup(self.o2m.view); var pop = new instance.web.form.FormOpenPopup(self.o2m.view);
pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(), { pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(), {
title: _t("Open: ") + self.name, title: _t("Open: ") + self.name,
auto_write: false, write_function: function(id, data) {
return self.o2m.dataset.write(id, data, {}, function(r) {
self.o2m.reload_current_view();
});
},
alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined, alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
parent_view: self.o2m.view, parent_view: self.o2m.view,
child_name: self.o2m.name, child_name: self.o2m.name,
@ -3046,11 +3048,6 @@ instance.web.form.One2ManyListView = instance.web.ListView.extend({
form_view_options: {'not_interactible_on_create':true}, form_view_options: {'not_interactible_on_create':true},
readonly: self.o2m.get("effective_readonly") readonly: self.o2m.get("effective_readonly")
}); });
pop.on_write.add(function(id, data) {
self.o2m.dataset.write(id, data, {}, function(r) {
self.o2m.reload_current_view();
});
});
}, },
do_button_action: function (name, id, callback) { do_button_action: function (name, id, callback) {
var self = this; var self = this;
@ -3365,6 +3362,7 @@ instance.web.form.AbstractFormPopup = instance.web.OldWidget.extend({
template: "AbstractFormPopup.render", template: "AbstractFormPopup.render",
/** /**
* options: * options:
* -readonly: only applicable when not in creation mode, default to false
*/ */
init_popup: function(model, row_id, domain, context, options) { init_popup: function(model, row_id, domain, context, options) {
this.row_id = row_id; this.row_id = row_id;
@ -3375,6 +3373,34 @@ instance.web.form.AbstractFormPopup = instance.web.OldWidget.extend({
_.defaults(this.options, { _.defaults(this.options, {
}); });
}, },
init_dataset: function() {
var self = this;
this.dataset = new instance.web.ProxyDataSet(this, this.model, this.context);
this.dataset.read_function = this.options.read_function;
this.dataset.create_function = function(data, sup) {
var fct = self.options.create_function || sup;
return fct.call(this, data).then(function(r) {
self.created_elements.push(r.result);
});
};
this.dataset.write_function = function(id, data, options, sup) {
var fct = self.options.write_function || sup;
return sup.call(this, id, data, options).then(self.on_write_completed);
};
this.dataset.parent_view = this.options.parent_view;
this.dataset.child_name = this.options.child_name;
},
display_popup: function() {
this.renderElement();
instance.web.form.dialog(this.$element, {
close: function() {
self.check_exit();
},
title: this.options.title || ""
});
this.start();
},
on_write_completed: function() {},
setup_form_view: function() { setup_form_view: function() {
var self = this; var self = this;
var options = _.clone(self.options.form_view_options) || {}; var options = _.clone(self.options.form_view_options) || {};
@ -3442,43 +3468,22 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend
* - read_function * - read_function
*/ */
select_element: function(model, options, domain, context) { select_element: function(model, options, domain, context) {
this.init_popup(model, null, domain, context, options);
var self = this; var self = this;
this.init_popup(model, null, domain, context, _.extend({}, options, {create_mode:true}));
_.defaults(this.options, { _.defaults(this.options, {
"initial_view": "search", initial_view: "search",
"create_function": function() { create_function: function() {
return self.create_row.apply(self, arguments); return self.create_row.apply(self, arguments);
}, },
read_function: null, read_function: null,
}); });
this.initial_ids = this.options.initial_ids; this.initial_ids = this.options.initial_ids;
this.created_elements = []; this.created_elements = [];
this.renderElement(); this.display_popup();
instance.web.form.dialog(this.$element, {
close: function() {
self.check_exit();
},
title: options.title || ""
});
this.start();
}, },
start: function() { start: function() {
this._super();
var self = this; var self = this;
this.dataset = new instance.web.ProxyDataSet(this, this.model, this.init_dataset();
this.context);
this.dataset.create_function = function() {
return self.options.create_function.apply(null, arguments).then(function(r) {
self.created_elements.push(r.result);
});
};
this.dataset.write_function = function() {
return self.write_row.apply(self, arguments);
};
this.dataset.read_function = this.options.read_function;
this.dataset.parent_view = this.options.parent_view;
this.dataset.child_name = this.options.child_name;
this.dataset.on_default_get.add(this.on_default_get);
if (this.options.initial_view == "search") { if (this.options.initial_view == "search") {
self.rpc('/web/session/eval_domain_and_context', { self.rpc('/web/session/eval_domain_and_context', {
domains: [], domains: [],
@ -3565,13 +3570,6 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend
wdataset.child_name = this.options.child_name; wdataset.child_name = this.options.child_name;
return wdataset.create.apply(wdataset, arguments); return wdataset.create.apply(wdataset, arguments);
}, },
write_row: function() {
var self = this;
var wdataset = new instance.web.DataSetSearch(this, this.model, this.context, this.domain);
wdataset.parent_view = this.options.parent_view;
wdataset.child_name = this.options.child_name;
return wdataset.write.apply(wdataset, arguments);
},
on_select_elements: function(element_ids) { on_select_elements: function(element_ids) {
}, },
on_click_element: function(ids) { on_click_element: function(ids) {
@ -3596,9 +3594,8 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend
if (this.created_elements.length > 0) { if (this.created_elements.length > 0) {
this.on_select_elements(this.created_elements); this.on_select_elements(this.created_elements);
} }
this.destroy(); this._super();
}, },
on_default_get: function(res) {}
}); });
instance.web.form.SelectCreateListView = instance.web.ListView.extend({ instance.web.form.SelectCreateListView = instance.web.ListView.extend({
@ -3623,50 +3620,26 @@ instance.web.form.FormOpenPopup = instance.web.form.AbstractFormPopup.extend(/**
/** /**
* options: * options:
* - alternative_form_view * - alternative_form_view
* - auto_write (default true) * - write_function
* - read_function * - read_function
* - parent_view * - parent_view
* - child_name * - child_name
* - form_view_options * - form_view_options
* - readonly
*/ */
show_element: function(model, row_id, context, options) { show_element: function(model, row_id, context, options) {
this.init_popup(model, row_id, [], context, _.extend({}, options, {create_mode:false})); this.init_popup(model, row_id, [], context, options);
_.defaults(this.options, { _.defaults(this.options, {
"auto_write": true, auto_write: true,
}); });
this.renderElement(); this.display_popup();
instance.web.dialog(this.$element, {
title: options.title || '',
modal: true,
width: 960,
height: 600
});
this.start();
}, },
start: function() { start: function() {
this._super(); this._super();
this.dataset = new instance.web.ProxyDataSet(this, this.model, this.context); this.init_dataset();
this.dataset.read_function = this.options.read_function;
this.dataset.ids = [this.row_id]; this.dataset.ids = [this.row_id];
this.dataset.index = 0; this.dataset.index = 0;
this.dataset.parent_view = this.options.parent_view;
this.dataset.child_name = this.options.child_name;
this.dataset.on_write.add(this.write_row);
this.setup_form_view(); this.setup_form_view();
}, },
write_row: function(id, data) {
if (!this.options.auto_write)
return;
var self = this;
var wdataset = new instance.web.DataSetSearch(this, this.model, this.context, this.domain);
wdataset.parent_view = this.options.parent_view;
wdataset.child_name = this.options.child_name;
wdataset.write(id, data, {}, function(r) {
self.on_write_completed();
});
},
on_write_completed: function() {},
}); });
instance.web.form.FieldReference = instance.web.form.AbstractField.extend(_.extend({}, instance.web.form.ReinitializeFieldMixin, { instance.web.form.FieldReference = instance.web.form.AbstractField.extend(_.extend({}, instance.web.form.ReinitializeFieldMixin, {