Skip to content

Commit d6cc796

Browse files
committed
Merge pull request NativeScript#463 from hdeshev/layout-insert-child-at
Layout child API extensions
2 parents 0a4f3ee + 489edfc commit d6cc796

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

apps/tests/layouts/stack-layout-tests.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import page = require("ui/page");
2-
import layout = require("ui/layouts/stack-layout");
3-
import button = require("ui/button");
1+
import {Page} from "ui/page";
2+
import {StackLayout} from "ui/layouts/stack-layout";
3+
import {Button} from "ui/button";
44
import TKUnit = require("../TKUnit");
55
import helper = require("./layout-helper");
66
import navHelper = require("../ui/helper");
@@ -9,7 +9,7 @@ import utils = require("utils/utils");
99

1010
var ASYNC = 2;
1111

12-
export class MyStackLayout extends layout.StackLayout {
12+
export class MyStackLayout extends StackLayout {
1313
public measureCount: number = 0;
1414
public arrangeCount: number = 0;
1515

@@ -32,16 +32,16 @@ export class MyStackLayout extends layout.StackLayout {
3232
}
3333
}
3434

35-
var tmp: button.Button;
36-
var newPage: page.Page;
35+
var tmp: Button;
36+
var newPage: Page;
3737
var rootLayout: MyStackLayout;
3838
var btn1: helper.MyButton;
3939
var btn2: helper.MyButton;
4040

4141
export function setUpModule() {
4242
var pageFactory = function () {
43-
newPage = new page.Page();
44-
tmp = new button.Button();
43+
newPage = new Page();
44+
tmp = new Button();
4545
tmp.text = "Loading test";
4646
newPage.content = tmp;
4747
return newPage;
@@ -65,7 +65,7 @@ export function setUp() {
6565
btn1.text = "btn1";
6666
rootLayout.addChild(btn1);
6767
btn2 = new helper.MyButton();
68-
btn2.text = "btn 2";
68+
btn2.text = "btn2";
6969
rootLayout.addChild(btn2);
7070
newPage.content = rootLayout;
7171
}
@@ -206,11 +206,37 @@ export function test_StackLayout_Padding_Horizontal() {
206206
helper.assertLayout(btn2, 60, 20, 50, 240, "btn2");
207207
}
208208

209+
function assertChildTexts(expected, layout, message) {
210+
let texts: Array<string> = [];
211+
layout._eachChildView((child: {text: string}) => texts.push(child.text));
212+
TKUnit.assertEqual(expected, texts.join('|'), message);
213+
}
214+
215+
export function test_insertChildAtPosition() {
216+
assertChildTexts("btn1|btn2", rootLayout, "initial 2 buttons");
217+
218+
let newChild = new Button();
219+
newChild.text = 'in-between';
220+
rootLayout.insertChild(1, newChild);
221+
222+
assertChildTexts("btn1|in-between|btn2", rootLayout, "button inserted at correct location");
223+
}
224+
225+
export function test_getChildIndex() {
226+
let lastChildIndex = rootLayout.getChildrenCount() - 1;
227+
let lastButton = <Button>rootLayout.getChildAt(lastChildIndex);
228+
TKUnit.assertEqual("btn2", lastButton.text);
229+
TKUnit.assertEqual(lastChildIndex, rootLayout.getChildIndex(lastButton));
230+
231+
let nonChild = new Button();
232+
TKUnit.assertEqual(-1, rootLayout.getChildIndex(nonChild));
233+
}
234+
209235
export function test_codesnippets() {
210236
// <snippet module="ui/layouts/stack-layout" title="stack-layout">
211237
// ### Create StackLayout
212238
// ``` JavaScript
213-
var stackLayout = new layout.StackLayout();
239+
var stackLayout = new StackLayout();
214240
// ```
215241

216242
// ### Declaring a StackLayout.
@@ -225,7 +251,7 @@ export function test_codesnippets() {
225251

226252
// ### Add child view to layout
227253
// ``` JavaScript
228-
var btn = new button.Button();
254+
var btn = new Button();
229255
stackLayout.addChild(btn);
230256
// ```
231257

@@ -240,4 +266,4 @@ export function test_codesnippets() {
240266
// ```
241267

242268
// </snippet>
243-
};
269+
};

ui/layouts/layout.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@
1717
*/
1818
getChildAt(index: number): view.View;
1919

20+
/**
21+
* Returns the position of the child view
22+
* @param child The child view that we are looking for.
23+
*/
24+
getChildIndex(child: view.View): number;
25+
2026
/**
2127
* Adds the view to children array.
2228
* @param view The view to be added to the end of the children array.
2329
*/
2430
addChild(view: view.View);
2531

32+
/**
33+
* Inserts the view to children array at the specified index.
34+
* @param atIndex The insertion index.
35+
* @param view The view to be added to the end of the children array.
36+
*/
37+
insertChild(atIndex: number, child: view.View);
38+
2639
/**
2740
* Removes the specified view from the children array.
2841
* @param view The view to remove from the children array.
@@ -49,4 +62,4 @@
4962
*/
5063
paddingTop: number;
5164
}
52-
}
65+
}

ui/layouts/layout.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export class Layout extends view.CustomLayoutView implements definition.Layout,
2828
return this._subViews[index];
2929
}
3030

31+
getChildIndex(child: view.View): number {
32+
return this._subViews.indexOf(child);
33+
}
34+
3135
public getChildById(id: string) {
3236
return view.getViewById(this, id);
3337
}
@@ -38,6 +42,11 @@ export class Layout extends view.CustomLayoutView implements definition.Layout,
3842
this._subViews.push(child);
3943
}
4044

45+
public insertChild(atIndex: number, child: view.View) {
46+
this._addView(child);
47+
this._subViews.splice(atIndex, 0, child);
48+
}
49+
4150
public removeChild(child: view.View) {
4251
this._removeView(child);
4352

@@ -98,4 +107,4 @@ export class Layout extends view.CustomLayoutView implements definition.Layout,
98107
(<UIView>nativeView).clipsToBounds = value;
99108
}
100109
}
101-
}
110+
}

0 commit comments

Comments
 (0)