Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize multiview widget with tryton [CUSTOM] #150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions src/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,8 @@
this.search_count = 0;
this.screen_container = new Sao.ScreenContainer(
attributes.tab_domain);
this._multiview_form = null;
this._multiview_group = null;
this.breadcrumb = attributes.breadcrumb || [];

this.context_screen = null;
Expand Down Expand Up @@ -851,9 +853,6 @@
this.fields_view_tree = {};
this._domain_parser = {};
this.pre_validate = false;
// [Coog specific] used for group_sync
this.parent = null;
// end
this.switch_callback = null;
},
get readonly() {
Expand Down Expand Up @@ -1218,12 +1217,12 @@
if (this.group.parent) {
this.order = null;
}
this.group.add_fields(fields);
if (group && group.length) {
this.current_record = group[0];
} else {
this.current_record = null;
}
this.group.add_fields(fields);
for (name in fields_views) {
var views = fields_views[name];
for (const view of views) {
Expand Down Expand Up @@ -1321,8 +1320,39 @@
}
}
// [Coog specific] multi_mixed_view
if (this.parent && changed){
this.parent.group_sync(this, this.current_record);
if (changed){
this._sync_group();
}
},
_sync_group: function() {
if (!this._multiview_form || (this.current_view.view_type != "tree")) {
return;
}
if (!this.current_record) {
return;
}

var to_sync = [];
let [tree, ...forms] = this._multiview_form.widget_groups[this._multiview_group];
for (const form of forms) {
if (form.screen.current_view.view_type != "form") {
continue;
}
to_sync.push(form);
}

var new_record_fields = {};
for (const form of to_sync) {
for (var fname in form.screen.model.fields) {
if (!(fname in this.current_record.model.fields)) {
new_record_fields[fname] = form.screen.model.fields[fname].description;
}
}
if (new_record_fields) {
this.current_record.group.add_fields(new_record_fields);
}
form.screen.current_record = this.current_record;
form.display();
}
},
load: function(ids, set_cursor=true, modified=false, position=-1) {
Expand Down Expand Up @@ -1929,6 +1959,10 @@
if (this.group.parent) {
promises.push(this.group.parent.root_parent.reload());
}
if (this._multiview_form) {
var root_parent = this.current_record.root_parent;
this._multiview_form.screen.reload([root_parent.id]);
}
return jQuery.when.apply(jQuery, promises).then(() => {
this.display();
});
Expand Down
130 changes: 20 additions & 110 deletions src/view/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ function eval_pyson(value){
this.notebooks = [];
this.expandables = [];
this.containers = [];
this.widget_groups = {};
this.widget_id = 0;
Sao.View.Form._super.init.call(this, view_id, screen, xml);
if (this.attributes.creatable) {
Expand Down Expand Up @@ -3588,15 +3589,27 @@ function eval_pyson(value){
pre_validate: attributes.pre_validate,
breadcrumb: breadcrumb,
});
// [Coog specific]
// > multi_mixed_view see tryton/8fa02ed59d03aa52600fb8332973f6a88d46d8c0
if (attributes.group)
this.screen.parent = this;
this.screen.pre_validate = attributes.pre_validate == 1;

this.screen.windows.push(this);
this.prm = this.screen.switch_view().done(() => {
this.content.append(this.screen.screen_container.el);

// [Coog Specific]
var wgroup;
if (attributes.group) {
this.screen._multiview_form = view;
this.screen._multiview_group = attributes.group;
if (!(attributes.group in view.widget_groups)) {
view.widget_groups[attributes.group] = [];
}
wgroup = view.widget_groups[attributes.group];
if (this.screen.current_view.view_type == 'tree') {
wgroup.unshift(this);
} else {
wgroup.push(this);
}
}
});

if (attributes.add_remove) {
Expand All @@ -3606,108 +3619,6 @@ function eval_pyson(value){

this.but_switch.prop('disabled', this.screen.number_of_views <= 0);
},
// [Coog specific]
// > multi_mixed_view see tryton/8fa02ed59d03aa52600fb8332973f6a88d46d8c0
group_sync: function(screen, current_record){
if (this.attributes.mode == 'form')
return;
if (!this.view || !this.view.widgets)
return;

function is_compatible(screen, record){
if (!screen.current_view)
return false;

return (!(screen.current_view.view_type == 'form' &&
record &&
screen.model_name != record.model.name));
}

var key;
var record;
var widget;
var widgets = this.view.widgets[this.field_name];
var to_sync = [];

for (var j = 0; j < widgets.length; j++){
widget = widgets[j];
if (!widget.hasOwnProperty('attributes')){
return;
}

if (widget == this ||
widget.attributes.group != this.attributes.group ||
!widget.hasOwnProperty('screen')){
continue;
}

if (widget.screen.current_record == current_record){
continue;
}

record = current_record;
if (!is_compatible(widget.screen, record))
record = null;
if (!widget.validate())
return;

to_sync.push({'widget': widget, 'record': record});
}
widget = null;
var to_display = null;
var to_display_prm = jQuery.when();
var record_load_promises, display_prm;

function display_form(widget, record) {
return function () {
widget.display(widget.record, widget.field);
};
}

for (var i = 0; i < to_sync.length; i++){
widget = to_sync[i].widget;
record = to_sync[i].record;
record_load_promises = [];

if (!widget.screen.current_view)
continue;

if (widget.screen.current_view.view_type == 'form' &&
record &&
widget.screen.group.model.name == record.group.model.name){
var fields = widget.screen.group.model.fields;
var ret = [];
for(var name in fields){
ret[name] = fields[name].description;
}
record.group.model.add_fields(ret);

for (var field_name in fields) {
if (!fields.hasOwnProperty(field_name)) {
continue;
}
record_load_promises.push(record.load(field_name));
}
}

widget.screen.current_record = record;
display_prm = jQuery.when.apply(jQuery, record_load_promises);
display_prm.done(display_form(widget, record).bind(this));
if (record){
to_display = widget;
to_display_prm = display_prm;
}
}
if (to_display) {
to_display_prm.done(function() {
for (var j in to_display.view.containers) {
var container = widget.view.containers[j];
container.resize();
}
to_display.display(to_display.record, to_display.field);
});
}
},
get_access: function(type) {
var model = this.attributes.relation;
if (model) {
Expand Down Expand Up @@ -3844,10 +3755,9 @@ function eval_pyson(value){

// [Coog specific]
// > multi_mixed_view see tryton/8fa02ed59d03aa52600fb8332973f6a88d46d8c0
if (this.attributes.group && this.attributes.mode == 'form'){
if (!this.screen.current_record)
this.set_invisible(true);
}else if (new_group && new_group != this.screen.group) {
if (this.attributes.group && this.attributes.mode == 'form') {
this.set_invisible(!this.visible);
} else if (new_group && new_group != this.screen.group) {
this.screen.set_group(new_group);
if ((this.screen.current_view.view_type == 'tree') &&
this.screen.current_view.editable) {
Expand Down