diff --git a/addons/web/static/src/js/views/list/list_editable_renderer.js b/addons/web/static/src/js/views/list/list_editable_renderer.js index 1a2fe3bd624..1fcadf0d348 100644 --- a/addons/web/static/src/js/views/list/list_editable_renderer.js +++ b/addons/web/static/src/js/views/list/list_editable_renderer.js @@ -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), diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 492e2b3bcce..bc6d0d0cea0 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -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:'
', + 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", diff --git a/addons/web/static/tests/views/list_tests.js b/addons/web/static/tests/views/list_tests.js index a7368c75325..e318a936b46 100644 --- a/addons/web/static/tests/views/list_tests.js +++ b/addons/web/static/tests/views/list_tests.js @@ -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); }