[FIX] web: reduce the number of changes when use handle widgets

This commit is contained in:
Christophe Matthieu
2017-10-23 12:18:29 +02:00
parent 512113b78a
commit b2327b8bf0
3 changed files with 118 additions and 14 deletions
@@ -449,12 +449,21 @@ ListRenderer.include({
_resequence: function (event, ui) {
var self = this;
var movedRecordID = ui.item.data('id');
var rowIDs = _.pluck(this.state.data, 'id');
rowIDs = _.without(rowIDs, movedRecordID);
rowIDs.splice(ui.item.index(), 0, movedRecordID);
var sequences = _.map(this.state.data, function(record) {
return record.data[self.handleField];
});
var rows = this.state.data;
var row = _.findWhere(rows, {id: movedRecordID});
var index0 = rows.indexOf(row);
var index1 = ui.item.index();
rows = rows.slice(Math.min(index0, index1), Math.max(index0, index1) + 1);
rows = _.without(rows, row);
if (index0 > index1) {
rows.unshift(row);
} else {
rows.push(row);
}
var sequences = _.pluck(_.pluck(rows, 'data'), self.handleField);
var rowIDs = _.pluck(rows, 'id');
this.trigger_up('resequence', {
rowIDs: rowIDs,
offset: _.min(sequences),
@@ -2001,7 +2001,7 @@ QUnit.module('relational_fields', {
});
QUnit.test('embedded one2many with handle widget', function (assert) {
assert.expect(8);
assert.expect(7);
this.data.partner.records[0].turtles = [1, 2, 3];
@@ -2044,7 +2044,7 @@ QUnit.module('relational_fields', {
{position: 'top'}
);
assert.verifySteps(["0", "1", "2"],
assert.verifySteps(["0", "1"],
"sequences values should be incremental starting from the previous minimum one");
assert.strictEqual(form.$('td.o_data_cell:not(.o_handle_cell)').text(), "blipyopkawa",
@@ -2104,14 +2104,109 @@ QUnit.module('relational_fields', {
{position: 'top'}
);
assert.strictEqual(turtleOnchange, 3, "should trigger one onchange per line updated");
assert.strictEqual(turtleOnchange, 2, "should trigger one onchange per line updated");
assert.strictEqual(partnerOnchange, 1, "should trigger only one onchange on the parent");
form.destroy();
});
QUnit.test('embedded one2many with handle widget with minimum setValue calls', function (assert) {
var done = assert.async();
assert.expect(20);
this.data.turtle.records[0].turtle_int = 6;
this.data.turtle.records.push({
id: 4,
turtle_int: 20,
turtle_foo: "a1",
}, {
id: 5,
turtle_int: 9,
turtle_foo: "a2",
}, {
id: 6,
turtle_int: 2,
turtle_foo: "a3",
}, {
id: 7,
turtle_int: 11,
turtle_foo: "a4",
});
this.data.partner.records[0].turtles = [1, 2, 3, 4, 5, 6, 7];
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch:'<form string="Partners">' +
'<sheet>' +
'<notebook>' +
'<page string="P page">' +
'<field name="turtles">' +
'<tree default_order="turtle_int">' +
'<field name="turtle_int" widget="handle"/>' +
'<field name="turtle_foo"/>' +
'</tree>' +
'</field>' +
'</page>' +
'</notebook>' +
'</sheet>' +
'</form>',
res_id: 1,
});
testUtils.intercept(form, "field_changed", function (event) {
assert.step(form.model.get(event.data.changes.turtles.id).res_id);
}, true);
form.$buttons.find('.o_form_button_edit').click();
var steps = [];
var positions = [
[6, 0, 'top', [3, 6, 1, 2, 5, 7, 4]], // move the last to the first line
[5, 1, 'top', [7, 6, 1, 2, 5]], // move the penultimate to the second line
[2, 5, 'center', [1, 2, 5, 6]], // move the third to the penultimate line
];
function dragAndDrop() {
var pos = positions.shift();
testUtils.dragAndDrop(
form.$('.ui-sortable-handle').eq(pos[0]),
form.$('tbody tr').eq(pos[1]),
{position: pos[2]}
);
steps = steps.concat(pos[3]);
assert.verifySteps(steps,
"sequences values should be apply from the begin index to the drop index");
if (positions.length) {
setTimeout(dragAndDrop, 10);
} else {
assert.deepEqual(_.pluck(form.model.get(form.handle).data.turtles.data, 'data'), [
{ id: 3, turtle_foo: "kawa", turtle_int: 2 },
{ id: 7, turtle_foo: "a4", turtle_int: 3 },
{ id: 1, turtle_foo: "yop", turtle_int: 4 },
{ id: 2, turtle_foo: "blip", turtle_int: 5 },
{ id: 5, turtle_foo: "a2", turtle_int: 6 },
{ id: 6, turtle_foo: "a3", turtle_int: 7 },
{ id: 4, turtle_foo: "a1", turtle_int: 8 }
], "sequences must be apply correctly");
form.destroy();
done();
}
}
dragAndDrop();
});
QUnit.test('embedded one2many (editable list) with handle widget', function (assert) {
assert.expect(9);
assert.expect(8);
this.data.partner.records[0].p = [1, 2, 4];
var form = createView({
@@ -2153,7 +2248,7 @@ QUnit.module('relational_fields', {
{position: 'top'}
);
assert.verifySteps(["0", "1", "2"],
assert.verifySteps(["0", "1"],
"sequences values should be incremental starting from the previous minimum one");
assert.strictEqual(form.$('td.o_data_cell:not(.o_handle_cell)').text(), "blipMy little Foo Valueyop",
+3 -3
View File
@@ -2703,7 +2703,7 @@ QUnit.module('Views', {
"should write the sequence starting from the lowest current one");
assert.strictEqual(args.field, 'int_field',
"should write the right field as sequence");
assert.deepEqual(args.ids, [1, 4, 2 , 3],
assert.deepEqual(args.ids, [4, 2 , 3],
"should write the sequence in correct order");
return $.when();
}
@@ -2756,7 +2756,7 @@ QUnit.module('Views', {
"should write the sequence starting from the lowest current one");
assert.strictEqual(args.field, 'int_field',
"should write the right field as sequence");
assert.deepEqual(args.ids, [1, 4, 2, 3],
assert.deepEqual(args.ids, [4, 2, 3],
"should write the sequence in correct order");
return $.when();
}
@@ -2816,7 +2816,7 @@ QUnit.module('Views', {
"should write the sequence starting from the lowest current one");
assert.strictEqual(args.field, 'int_field',
"should write the right field as sequence");
assert.deepEqual(args.ids, [1, 4, 2, 3],
assert.deepEqual(args.ids, [4, 2, 3],
"should write the sequence in correct order");
return $.when(def);
}