Skip to content

Commit 48fae34

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-63321' into BUGS
2 parents 2f08a3c + 471770a commit 48fae34

File tree

2 files changed

+110
-24
lines changed

2 files changed

+110
-24
lines changed

app/code/Magento/Ui/view/base/web/js/dynamic-rows/dynamic-rows.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ define([
167167
* Sets record data to cache
168168
*/
169169
setRecordDataToCache: function (data) {
170-
this.recordDataCache = this.recordDataCache && data.length > this.recordDataCache.length ?
171-
data : this.recordDataCache;
170+
this.recordDataCache = data;
172171
},
173172

174173
/**
@@ -270,10 +269,12 @@ define([
270269
* @param {Number|String} id
271270
*/
272271
deleteHandler: function (index, id) {
272+
var defaultState;
273273
this.setDefaultState();
274+
defaultState = this.defaultPagesState[this.currentPage()];
274275
this.processingDeleteRecord(index, id);
275276
this.pagesChanged[this.currentPage()] =
276-
!compareArrays(this.defaultPagesState[this.currentPage()], this.arrayFilter(this.getChildItems()));
277+
!compareArrays(defaultState, this.arrayFilter(this.getChildItems()));
277278
this.changed(_.some(this.pagesChanged));
278279
},
279280

@@ -327,7 +328,7 @@ define([
327328
},
328329

329330
/**
330-
* Set default dynamic-rows state
331+
* Set default dynamic-rows state or state before changing data
331332
*
332333
* @param {Array} data - defaultState data
333334
*/
@@ -625,6 +626,19 @@ define([
625626
this.pages(pages);
626627
},
627628

629+
/**
630+
* Reinit record data in order to remove deleted values
631+
*
632+
* @return void
633+
*/
634+
reinitRecordData: function () {
635+
this.recordData(
636+
_.filter(this.recordData(), function (elem) {
637+
return elem && elem[this.deleteProperty] !== this.deleteValue;
638+
}, this)
639+
);
640+
},
641+
628642
/**
629643
* Get items to rendering on current page
630644
*
@@ -633,7 +647,6 @@ define([
633647
getChildItems: function (data, page) {
634648
var dataRecord = data || this.relatedData,
635649
startIndex;
636-
637650
this.startIndex = (~~this.currentPage() - 1) * this.pageSize;
638651

639652
startIndex = page || this.startIndex;
@@ -672,9 +685,8 @@ define([
672685
this.bubble('addChild', false);
673686

674687
if (this.relatedData.length && this.relatedData.length % this.pageSize === 0) {
675-
this.clear();
676688
this.pages(this.pages() + 1);
677-
this.currentPage(this.pages());
689+
this.nextPage();
678690
} else if (~~this.currentPage() !== this.pages()) {
679691
this.currentPage(this.pages());
680692
}
@@ -690,11 +702,6 @@ define([
690702
*/
691703
processingDeleteRecord: function (index, recordId) {
692704
this.deleteRecord(index, recordId);
693-
694-
if (this.getChildItems().length <= 0 && this.pages() !== 1) {
695-
this.pages(this.pages() - 1);
696-
this.currentPage(this.pages());
697-
}
698705
},
699706

700707
/**
@@ -717,8 +724,8 @@ define([
717724
return false;
718725
}
719726

720-
this.clear();
721727
this.initChildren();
728+
return true;
722729
},
723730

724731
/**
@@ -743,13 +750,15 @@ define([
743750
* Change page to next
744751
*/
745752
nextPage: function () {
753+
this.clear();
746754
this.currentPage(this.currentPage() + 1);
747755
},
748756

749757
/**
750758
* Change page to previous
751759
*/
752760
previousPage: function () {
761+
this.clear();
753762
this.currentPage(this.currentPage() - 1);
754763
},
755764

@@ -827,24 +836,21 @@ define([
827836
deleteRecord: function (index, recordId) {
828837
var recordInstance,
829838
lastRecord,
830-
recordsData,
831-
childs;
839+
recordsData;
832840

833841
if (this.deleteProperty) {
842+
recordsData = this.recordData();
834843
recordInstance = _.find(this.elems(), function (elem) {
835844
return elem.index === index;
836845
});
837846
recordInstance.destroy();
838847
this.elems([]);
839848
this._updateCollection();
840849
this.removeMaxPosition();
841-
this.recordData()[recordInstance.index][this.deleteProperty] = this.deleteValue;
842-
this.recordData.valueHasMutated();
843-
childs = this.getChildItems();
844-
845-
if (childs.length > this.elems().length) {
846-
this.addChild(false, childs[childs.length - 1][this.identificationProperty], false);
847-
}
850+
recordsData[recordInstance.index][this.deleteProperty] = this.deleteValue;
851+
this.recordData(recordsData);
852+
this.reinitRecordData();
853+
this.reload();
848854
} else {
849855
this.update = true;
850856

@@ -866,11 +872,14 @@ define([
866872
this.update = false;
867873
}
868874

875+
this._reducePages();
876+
this._sort();
877+
},
878+
879+
_reducePages: function() {
869880
if (this.pages() < ~~this.currentPage()) {
870881
this.currentPage(this.pages());
871882
}
872-
873-
this._sort();
874883
},
875884

876885
/**
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint-disable max-nested-callbacks */
7+
define([
8+
'Magento_Ui/js/dynamic-rows/dynamic-rows'
9+
], function (DynamicRows) {
10+
'use strict';
11+
12+
var model,
13+
ElementMock = function (index) {
14+
return {
15+
destroy: function () {},
16+
index: index
17+
};
18+
};
19+
20+
beforeEach(function(done) {
21+
model = new DynamicRows({});
22+
done();
23+
});
24+
25+
describe('Magento_Ui/js/dynamic-rows/dynamic-rows', function () {
26+
it('changePage without Records', function() {
27+
model.recordData = function () {
28+
return {
29+
length: 0
30+
};
31+
};
32+
33+
expect(model.changePage(1)).toBeFalsy();
34+
});
35+
36+
it('changePage with Fake Page', function() {
37+
model.pages = function () {
38+
return 3;
39+
};
40+
41+
expect(model.changePage(4)).toBeFalsy();
42+
});
43+
44+
it('changePage', function() {
45+
model.startIndex = 0;
46+
model.pageSize = 3;
47+
model.relatedData = [
48+
{"a": "b"},
49+
{"b": "c"},
50+
{"v": "g"}
51+
];
52+
53+
model.pages = function () {
54+
return 3;
55+
};
56+
model.changePage(2);
57+
58+
expect(model.templates.record.recordId).toBe(2);//last record number is 3
59+
});
60+
61+
it ('deleteRecord with Delete Property', function () {
62+
var elems,
63+
recordInstanceMock = new ElementMock(1),
64+
elem2 = new ElementMock(2);
65+
spyOn(recordInstanceMock, "destroy");
66+
model.recordData({1: {}});
67+
elems = [
68+
recordInstanceMock,
69+
elem2
70+
];
71+
model.elems(elems);
72+
model.deleteProperty = true;
73+
model.deleteRecord(1, 1);
74+
expect(model.recordData()).toEqual([]);
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)