Rationalize context usage

issue7072
review43041002
This commit is contained in:
Cédric Krier
2018-03-09 19:10:58 +01:00
parent 760f1b2912
commit bfb2fcaa71
7 changed files with 54 additions and 58 deletions
+5 -13
View File
@@ -393,7 +393,7 @@
}
};
array.context = function() {
var context = jQuery.extend({}, this._context);
var context = jQuery.extend({}, this.model.session.context);
if (this.parent) {
jQuery.extend(context, this.parent.get_context());
if (this.child_name in this.parent.model.fields) {
@@ -1097,13 +1097,10 @@
},
expr_eval: function(expr) {
if (typeof(expr) != 'string') return expr;
var ctx = jQuery.extend({}, this.model.session.context);
ctx.context = jQuery.extend({}, ctx);
jQuery.extend(ctx.context, this.get_context());
jQuery.extend(ctx, this.get_eval());
var ctx = this.get_eval();
ctx.context = this.get_context();
ctx.active_model = this.model.name;
ctx.active_id = this.id;
ctx._user = this.model.session.user_id;
if (this.group.parent && this.group.parent_name) {
var parent_env = Sao.common.EvalEnvironment(this.group.parent);
ctx['_parent_' + this.group.parent_name] = parent_env;
@@ -1409,10 +1406,7 @@
return {};
},
get_context: function(record) {
var context = jQuery.extend({}, record.get_context());
if (record.group.parent) {
jQuery.extend(context, record.group.parent.get_context());
}
var context = record.get_context();
jQuery.extend(context,
record.expr_eval(this.description.context || {}));
return context;
@@ -1643,9 +1637,7 @@
Sao.field.TimeDelta = Sao.class_(Sao.field.Field, {
_default: null,
converter: function(group) {
var ctx = jQuery.extend(
group.model.session.context, group.context());
return ctx[this.description.converter];
return group.context()[this.description.converter];
},
set_client: function(record, value, force_change) {
if (typeof(value) == 'string') {
+4 -3
View File
@@ -416,7 +416,7 @@ var Sao = {};
return Sao.Action.exec_keyword('tree_open', {
'model': screen.model_name,
'id': screen.get_id(),
}, jQuery.extend({}, screen.context), false);
}, jQuery.extend({}, screen.context()), false);
};
Sao.menu = function(preferences) {
@@ -592,7 +592,8 @@ var Sao = {};
var ir_model = new Sao.Model('ir.model');
return ir_model.execute('global_search',
[text, Sao.config.limit, Sao.main_menu_screen.model_name],
Sao.main_menu_screen.context).then(function(s_results) {
Sao.main_menu_screen.context())
.then(function(s_results) {
var results = [];
for (var i=0, len=s_results.length; i < len; i++) {
results.push({
@@ -612,7 +613,7 @@ var Sao = {};
Sao.Action.exec_keyword('tree_open', {
'model': item.model,
'id': item.record_id,
}, Sao.main_menu_screen.context);
}, Sao.main_menu_screen.context());
} else {
var params = {
'model': item.model,
+27 -26
View File
@@ -461,7 +461,7 @@
case 'time':
var format;
var date_format = Sao.common.date_format(
this.screen.context.date_format);
this.screen.context().date_format);
if (field.type == 'date') {
format = date_format;
} else {
@@ -629,8 +629,7 @@
this.views = [];
this.views_preload = attributes.views_preload || {};
this.exclude_field = attributes.exclude_field;
this.context = attributes.context || {};
this.new_group();
this.new_group(attributes.context || {});
this.current_view = null;
this.current_record = null;
this.domain = attributes.domain || [];
@@ -706,7 +705,7 @@
view = this.views_preload[view_type];
} else {
var prm = this.model.execute('fields_view_get',
[view_id, view_type], this.context);
[view_id, view_type], this.context());
return prm.pipe(this.add_view.bind(this));
}
this.add_view(view);
@@ -805,17 +804,17 @@
var context_record = this.context_screen.current_record;
if (context_record &&
!context_record.validate(null, false, null, true)) {
this.new_group([]);
this.new_group();
this.context_screen.display(true);
return jQuery.when();
}
this.context = jQuery.extend({}, this.context,
this.context_screen.get_on_change_value());
this.new_group(jQuery.extend(this.context(),
this.context_screen.get_on_change_value()));
}
var domain = this.search_domain(search_string, true);
if (this.context_domain) {
var decoder = new Sao.PYSON.Decoder(this.context);
var decoder = new Sao.PYSON.Decoder(this.context());
domain = ['AND', domain, decoder.decode(this.context_domain)];
}
var tab_domain = this.screen_container.get_tab_domain();
@@ -824,11 +823,11 @@
}
var grp_prm = this.model.find(domain, this.offset, this.limit,
this.order, this.context);
this.order, this.context());
var count_prm = jQuery.when(this.search_count);
if (!only_ids) {
count_prm = this.model.execute('search_count', [domain],
this.context);
this.context());
}
count_prm.done(function(count) {
this.search_count = count;
@@ -892,13 +891,17 @@
if (tab_domain[2]) {
var domain = ['AND', tab_domain[1], screen_domain];
this.screen_container.set_tab_counter(null, i);
this.group.model.execute('search_count', [domain], this.context)
this.group.model.execute(
'search_count', [domain], this.context())
.then(function(count) {
this.screen_container.set_tab_counter(count, i);
}.bind(this));
}
}.bind(this));
},
context: function() {
return this.group.context();
},
set_group: function(group) {
var fields = {};
if (this.group) {
@@ -929,12 +932,12 @@
}
this.group.add_fields(fields);
},
new_group: function(ids) {
var group = new Sao.Group(this.model, this.context, []);
group.set_readonly(this.attributes.readonly || false);
if (ids) {
group.load(ids);
new_group: function(context) {
if (!context) {
context = this.context();
}
var group = new Sao.Group(this.model, context, []);
group.set_readonly(this.attributes.readonly || false);
this.set_group(group);
},
set_current_record: function(record) {
@@ -1049,7 +1052,7 @@
'model': this.model_name,
'id': this.get_id(),
'ids': [this.get_id()]
}, jQuery.extend({}, this.context), false);
}, jQuery.extend({}, this.context()), false);
} else {
if (!this.modified()) {
this.switch_view('form');
@@ -1267,7 +1270,8 @@
copy: function() {
var dfd = jQuery.Deferred();
var records = this.current_view.selected_records();
this.model.copy(records, this.context).then(function(new_ids) {
this.model.copy(records, this.context())
.then(function(new_ids) {
this.group.load(new_ids);
if (!jQuery.isEmptyObject(new_ids)) {
this.set_current_record(this.group.get(new_ids[0]));
@@ -1297,7 +1301,7 @@
}
if (!(view_id in this.fields_view_tree)) {
view_tree = this.model.execute('fields_view_get', [false, 'tree'],
this.context, false);
this.context(), false);
this.fields_view_tree[view_id] = view_tree;
} else {
view_tree = this.fields_view_tree[view_id];
@@ -1364,11 +1368,8 @@
}
});
var context = jQuery.extend({},
this.model.session.context,
this.context);
domain_parser = new Sao.common.DomainParser(
fields, context);
fields, this.context());
this._domain_parser[view_id] = domain_parser;
return domain_parser;
},
@@ -1501,7 +1502,7 @@
model: this.model_name,
id: this.current_record.id,
ids: ids
}, null, this.context, true);
}, null, this.context(), true);
}
};
@@ -1547,7 +1548,7 @@
var args = record.expr_eval(attributes.change || []);
var values = record._get_on_change_args(args);
record.model.execute(attributes.name, [values],
this.context).then(function(changes) {
this.context()).then(function(changes) {
record.set_on_change(changes);
record.group.root_group().screens.forEach(
function(screen) {
@@ -1556,7 +1557,7 @@
});
} else {
record.save(false).done(function() {
var context = jQuery.extend({}, this.context);
var context = this.context();
context._timestamp = {};
ids = [];
for (i = 0; i < selected_records.length; i++) {
+11 -10
View File
@@ -453,7 +453,7 @@
var screen = this.screen;
var buttons = this.buttons;
var prm = screen.model.execute('view_toolbar_get', [],
screen.context);
screen.context());
prm.done(function(toolbars) {
[
['action', 'glyphicon-cog',
@@ -557,7 +557,7 @@
ids: record_ids
};
Sao.Action.exec_action(exec_action, data,
screen.context);
screen.context());
});
})
.appendTo(menu);
@@ -727,7 +727,7 @@
this.screen.model.execute('read', [[record.id],
fields.map(function(field) {
return field[0];
})], this.screen.context)
})], this.screen.context())
.then(function(result) {
result = result[0];
var message = '';
@@ -763,17 +763,18 @@
(revision < revisions[revisions.length - 1][0])) {
revision = revisions[revisions.length - 1][0];
}
if (revision != this.screen.context._datetime) {
// Update screen context that will be propagated by
if (revision != this.screen.context()._datetime) {
// Update group context that will be propagated by
// recreating new group
this.screen.context._datetime = revision;
this.screen.group._context._datetime = revision;
if (this.screen.current_view.view_type != 'form') {
this.screen.search_filter(
this.screen.screen_container
.search_entry.val());
} else {
// Test if record exist in revisions
this.screen.new_group([current_id]);
this.screen.new_group();
this.screen.group.load([current_id]);
}
this.screen.display(true);
this.update_revision();
@@ -786,14 +787,14 @@
return record.id;
});
this.screen.model.execute('history_revisions',
[ids], this.screen.context)
[ids], this.screen.context())
.then(function(revisions) {
new Sao.Window.Revision(revisions, set_revision(revisions));
});
}.bind(this));
},
update_revision: function() {
var revision = this.screen.context._datetime;
var revision = this.screen.context()._datetime;
var label;
if (revision) {
var date_format = Sao.common.date_format();
@@ -922,7 +923,7 @@
return r.id;
}),
this.screen.current_view.get_fields(),
this.screen.context);
this.screen.context());
},
import: function(){
new Sao.Window.Import(this.screen);
+2 -1
View File
@@ -2211,7 +2211,8 @@ function eval_pyson(value){
var screen = this.get_screen();
var m2o_id =
this.id_from_value(record.field_get(this.field_name));
screen.new_group([m2o_id]);
screen.new_group();
screen.group.load([m2o_id]);
callback = function(result) {
if (result) {
var rec_name_prm = screen.current_record.rec_name();
+4 -4
View File
@@ -195,7 +195,7 @@
if ((type == 'date') || (type == 'datetime')) {
var format_func, date_format, time_format;
date_format = Sao.common.date_format(
this.view.screen.context.date_format);
this.view.screen.context().date_format);
time_format = '%X';
if (type == 'datetime') {
format_func = function(dt) {
@@ -238,7 +238,7 @@
},
action: function(data, element) {
var ids = this.ids[this._action_key(data)];
var ctx = jQuery.extend({}, this.view.screen.context);
var ctx = jQuery.extend({}, this.view.screen.context());
delete ctx.active_ids;
delete ctx.active_id;
Sao.Action.exec_keyword('graph_open', {
@@ -292,7 +292,7 @@
var type = this.xfield.type;
if ((type == 'date') || (type == 'datetime')) {
var date_format = Sao.common.date_format(
this.view.screen.context.date_format);
this.view.screen.context().date_format);
var datetime_format = date_format + ' %X';
if (type == 'datetime') {
format_func = function(dt) {
@@ -322,7 +322,7 @@
var type = this.xfield.type;
if ((type == 'date') || (type == 'datetime')) {
var date_format = Sao.common.date_format(
this.view.screen.context.date_format);
this.view.screen.context().date_format);
var datetime_format = date_format + ' %X';
if (type == 'datetime') {
key = Sao.common.format_datetime(datetime_format, key);
+1 -1
View File
@@ -324,7 +324,7 @@
}
var inversion = new Sao.common.DomainInversion();
domain = inversion.simplify(domain);
var decoder = new Sao.PYSON.Decoder(this.screen.context);
var decoder = new Sao.PYSON.Decoder(this.screen.context());
this.columns.forEach(function(column) {
var name = column.attributes.name;
if (!name) {