8000 Call row/column removed callbacks on removeRows/removeColumns. · hbcodeXCI/NativeScript@21c74bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 21c74bd

Browse files
committed
Call row/column removed callbacks on removeRows/removeColumns.
Changed callback names to _onRowRemoved, etc and changed visibility to public to work around a TypeScript "protected" limitation.
1 parent de748d4 commit 21c74bd

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

apps/tests/ui/layouts/grid-layout-tests.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,27 @@ import commonTests = require("./common-layout-tests");
1212

1313
var DELTA = 1;
1414

15-
export class GridLayoutTest extends testModule.UITest<GridLayout> {
15+
class RemovalTrackingGridLayout extends GridLayout {
16+
public removedRows = 0;
17+
public removedCols = 0;
1618

17-
public create(): GridLayout {
18-
return new GridLayout();
19+
public _onRowRemoved(itemSpec: ItemSpec, index: number) {
20+
console.log("_onRowRemoved");
21+
this.removedRows++;
22+
super._onRowRemoved(itemSpec, index);
23+
}
24+
25+
public _onColumnRemoved(itemSpec: ItemSpec, index: number) {
26+
console.log("_onColumnRemoved");
27+
this.removedCols++;
28+
super._onColumnRemoved(itemSpec, index);
29+
}
30+
}
31+
32+
export class GridLayoutTest extends testModule.UITest<RemovalTrackingGridLayout> {
33+
34+
public create(): RemovalTrackingGridLayout {
35+
return new RemovalTrackingGridLayout();
1936
}
2037

2138
private row(view: view.View): number {
@@ -194,16 +211,20 @@ export class GridLayoutTest extends testModule.UITest<GridLayout> {
194211

195212
public test_removeColumns() {
196213
this.prepareGridLayout(false);
197-
TKUnit.assertTrue(this.testView.getColumns().length > 0, "There should be columns.");
214+
const colsBefore = this.testView.getColumns().length;
215+
TKUnit.assertTrue(colsBefore > 0, "There should be columns.");
198216
this.testView.removeColumns();
199217
TKUnit.assertTrue(this.testView.getColumns().length === 0, "Columns should be empty.");
218+
TKUnit.assertTrue(this.testView.removedCols === colsBefore, "_onColumnRemoved called for each column.");
200219
}
201220

202221
public test_removeRows() {
203222
this.prepareGridLayout(false);
204-
TKUnit.assertTrue(this.testView.getRows().length > 0, "There should be rows.");
223+
const rowsBefore = this.testView.getRows().length;
224+
TKUnit.assertTrue(rowsBefore > 0, "There should be rows.");
205225
this.testView.removeRows();
206226
TKUnit.assertTrue(this.testView.getRows().length === 0, "Rows should be empty.");
227+
TKUnit.assertTrue(this.testView.removedRows === rowsBefore, "_onRowRemoved called for each row.");
207228
}
208229

209230
public test_removeChildren() {

ui/layouts/grid-layout/grid-layout-common.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ export class GridLayout extends LayoutBase implements definition.GridLayout, App
169169
GridLayout.validateItemSpec(itemSpec);
170170
itemSpec.owner = this;
171171
this._rows.push(itemSpec);
172-
this.onRowAdded(itemSpec);
172+
this._onRowAdded(itemSpec);
173173
this.invalidate();
174174
}
175175

176176
public addColumn(itemSpec: ItemSpec) {
177177
GridLayout.validateItemSpec(itemSpec);
178178
itemSpec.owner = this;
179179
this._cols.push(itemSpec);
180-
this.onColumnAdded(itemSpec);
180+
this._onColumnAdded(itemSpec);
181181
this.invalidate();
182182
}
183183

@@ -193,7 +193,7 @@ export class GridLayout extends LayoutBase implements definition.GridLayout, App
193193

194194
itemSpec.index = -1;
195195
this._rows.splice(index, 1);
196-
this.onRowRemoved(itemSpec, index);
196+
this._onRowRemoved(itemSpec, index);
197197
this.invalidate();
198198
}
199199

@@ -209,21 +209,25 @@ export class GridLayout extends LayoutBase implements definition.GridLayout, App
209209

210210
itemSpec.index = -1;
211211
this._cols.splice(index, 1);
212-
this.onColumnRemoved(itemSpec, index);
212+
this._onColumnRemoved(itemSpec, index);
213213
this.invalidate();
214214
}
215215

216216
public removeColumns() {
217-
for (var i = 0; i < this._cols.length; i++) {
218-
this._cols[i].index = -1;
217+
for (var i = this._cols.length - 1; i >= 0; i--) {
218+
const colSpec = this._cols[i];
219+
this._onColumnRemoved(colSpec, i);
220+
colSpec.index = -1;
219221
}
220222
this._cols.length = 0;
221223
this.invalidate();
222224
}
223225

224226
public removeRows() {
225-
for (var i = 0; i < this._rows.length; i++) {
226-
this._rows[i].index = -1;
227+
for (var i = this._rows.length - 1; i >= 0; i--) {
228+
const rowSpec = this._rows[i];
229+
this._onRowRemoved(rowSpec, i);
230+
rowSpec.index = -1;
2272 8000 31
}
228232
this._rows.length = 0;
229233
this.invalidate();
@@ -245,19 +249,19 @@ export class GridLayout extends LayoutBase implements definition.GridLayout, App
245249
this.invalidate();
246250
}
247251

248-
protected onRowAdded(itemSpec: ItemSpec) {
252+
public _onRowAdded(itemSpec: ItemSpec) {
249253
//
250254
}
251255

252-
protected onColumnAdded(itemSpec: ItemSpec) {
256+
public _onColumnAdded(itemSpec: ItemSpec) {
253257
//
254258
}
255259

256-
protected onRowRemoved(itemSpec: ItemSpec, index: number) {
260+
public _onRowRemoved(itemSpec: ItemSpec, index: number): void {
257261
//
258262
}
259263

260-
protected onColumnRemoved(itemSpec: ItemSpec, index: number) {
264+
public _onColumnRemoved(itemSpec: ItemSpec, index: number): void {
261265
//
262266
}
263267

@@ -388,4 +392,4 @@ export class GridLayout extends LayoutBase implements definition.GridLayout, App
388392
this.addRow(rows[i]);
389393
}
390394
}
391-
}
395+
}

ui/layouts/grid-layout/grid-layout.android.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,34 @@ export class GridLayout extends common.GridLayout {
8585
this._layout = new org.nativescript.widgets.GridLayout(this._context);
8686

8787
// Update native GridLayout
88-
this.getRows().forEach((itemSpec: ItemSpec, index, rows) => { this.onRowAdded(itemSpec); }, this);
89-
this.getColumns().forEach((itemSpec: ItemSpec, index, rows) => { this.onColumnAdded(itemSpec); }, this);
88+
this.getRows().forEach((itemSpec: ItemSpec, index, rows) => { this._onRowAdded(itemSpec); }, this);
89+
this.getColumns().forEach((itemSpec: ItemSpec, index, rows) => { this._onColumnAdded(itemSpec); }, this);
9090
}
9191

92-
protected onRowAdded(itemSpec: ItemSpec) {
92+
public _onRowAdded(itemSpec: ItemSpec) {
9393
if (this._layout) {
9494
var nativeSpec = createNativeSpec(itemSpec);
9595
itemSpec.nativeSpec = nativeSpec;
9696
this._layout.addRow(nativeSpec);
9797
}
9898
}
9999

100-
protected onColumnAdded(itemSpec: ItemSpec) {
100+
public _onColumnAdded(itemSpec: ItemSpec) {
101101
if (this._layout) {
102102
var nativeSpec = createNativeSpec(itemSpec);
103103
itemSpec.nativeSpec = nativeSpec;
104104
this._layout.addColumn(nativeSpec);
105105
}
106106
}
107107

108-
protected onRowRemoved(itemSpec: ItemSpec, index: number) {
108+
public _onRowRemoved(itemSpec: ItemSpec, index: number) {
109109
itemSpec.nativeSpec = null;
110110
if (this._layout) {
111111
this._layout.removeRowAt(index);
112112
}
113113
}
114114

115-
protected onColumnRemoved(itemSpec: ItemSpec, index: number) {
115+
public _onColumnRemoved(itemSpec: ItemSpec, index: number) {
116116
itemSpec.nativeSpec = null;
117117
if (this._layout) {
118118
this._layout.removeColumnAt(index);
@@ -122,4 +122,4 @@ export class GridLayout extends common.GridLayout {
122122
protected invalidate(): void {
123123
// No need to request layout for android because it will be done in the native call.
124124
}
125-
}
125+
}

ui/layouts/grid-layout/grid-layout.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,12 @@
175175
* Gets array of row specifications defined on this instance of GridLayout.
176176
*/
177177
public getRows(): Array<ItemSpec>;
178+
179+
//@private
180+
public _onRowAdded(itemSpec: ItemSpec): void;
181+
public _onColumnAdded(itemSpec: ItemSpec): void;
182+
public _onRowRemoved(itemSpec: ItemSpec, index: number): void;
183+
public _onColumnRemoved(itemSpec: ItemSpec, index: number): void;
184+
//@endprivate
178185
}
179-
}
186+
}

ui/layouts/grid-layout/grid-layout.ios.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ export class GridLayout extends common.GridLayout {
1616
this.helper = new MeasureHelper(this);
1717
}
1818

19-
protected onRowAdded(itemSpec: common.ItemSpec) {
19+
public _onRowAdded(itemSpec: common.ItemSpec) {
2020
this.helper.rows.push(new ItemGroup(itemSpec));
2121
}
2222

23-
protected onColumnAdded(itemSpec: common.ItemSpec) {
23+
public _onColumnAdded(itemSpec: common.ItemSpec) {
2424
this.helper.columns.push(new ItemGroup(itemSpec));
2525
}
2626

27-
protected onRowRemoved(itemSpec: common.ItemSpec, index: number) {
27+
public _onRowRemoved(itemSpec: common.ItemSpec, index: number) {
2828
this.helper.rows[index].children.length = 0;
2929
this.helper.rows.splice(index, 1);
3030
}
3131

32-
protected onColumnRemoved(itemSpec: common.ItemSpec, index: number) {
32+
public _onColumnRemoved(itemSpec: common.ItemSpec, index: number) {
3333
this.helper.columns[index].children.length = 0;
3434
this.helper.columns.splice(index, 1);
3535
}

0 commit comments

Comments
 (0)
0