mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 12:57:42 +08:00
Merge pull request #463 from hdeshev/layout-insert-child-at
Layout child API extensions
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import page = require("ui/page");
|
import {Page} from "ui/page";
|
||||||
import layout = require("ui/layouts/stack-layout");
|
import {StackLayout} from "ui/layouts/stack-layout";
|
||||||
import button = require("ui/button");
|
import {Button} from "ui/button";
|
||||||
import TKUnit = require("../TKUnit");
|
import TKUnit = require("../TKUnit");
|
||||||
import helper = require("./layout-helper");
|
import helper = require("./layout-helper");
|
||||||
import navHelper = require("../ui/helper");
|
import navHelper = require("../ui/helper");
|
||||||
@ -9,7 +9,7 @@ import utils = require("utils/utils");
|
|||||||
|
|
||||||
var ASYNC = 2;
|
var ASYNC = 2;
|
||||||
|
|
||||||
export class MyStackLayout extends layout.StackLayout {
|
export class MyStackLayout extends StackLayout {
|
||||||
public measureCount: number = 0;
|
public measureCount: number = 0;
|
||||||
public arrangeCount: number = 0;
|
public arrangeCount: number = 0;
|
||||||
|
|
||||||
@ -32,16 +32,16 @@ export class MyStackLayout extends layout.StackLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmp: button.Button;
|
var tmp: Button;
|
||||||
var newPage: page.Page;
|
var newPage: Page;
|
||||||
var rootLayout: MyStackLayout;
|
var rootLayout: MyStackLayout;
|
||||||
var btn1: helper.MyButton;
|
var btn1: helper.MyButton;
|
||||||
var btn2: helper.MyButton;
|
var btn2: helper.MyButton;
|
||||||
|
|
||||||
export function setUpModule() {
|
export function setUpModule() {
|
||||||
var pageFactory = function () {
|
var pageFactory = function () {
|
||||||
newPage = new page.Page();
|
newPage = new Page();
|
||||||
tmp = new button.Button();
|
tmp = new Button();
|
||||||
tmp.text = "Loading test";
|
tmp.text = "Loading test";
|
||||||
newPage.content = tmp;
|
newPage.content = tmp;
|
||||||
return newPage;
|
return newPage;
|
||||||
@ -65,7 +65,7 @@ export function setUp() {
|
|||||||
btn1.text = "btn1";
|
btn1.text = "btn1";
|
||||||
rootLayout.addChild(btn1);
|
rootLayout.addChild(btn1);
|
||||||
btn2 = new helper.MyButton();
|
btn2 = new helper.MyButton();
|
||||||
btn2.text = "btn 2";
|
btn2.text = "btn2";
|
||||||
rootLayout.addChild(btn2);
|
rootLayout.addChild(btn2);
|
||||||
newPage.content = rootLayout;
|
newPage.content = rootLayout;
|
||||||
}
|
}
|
||||||
@ -206,11 +206,37 @@ export function test_StackLayout_Padding_Horizontal() {
|
|||||||
helper.assertLayout(btn2, 60, 20, 50, 240, "btn2");
|
helper.assertLayout(btn2, 60, 20, 50, 240, "btn2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function assertChildTexts(expected, layout, message) {
|
||||||
|
let texts: Array<string> = [];
|
||||||
|
layout._eachChildView((child: {text: string}) => texts.push(child.text));
|
||||||
|
TKUnit.assertEqual(expected, texts.join('|'), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function test_insertChildAtPosition() {
|
||||||
|
assertChildTexts("btn1|btn2", rootLayout, "initial 2 buttons");
|
||||||
|
|
||||||
|
let newChild = new Button();
|
||||||
|
newChild.text = 'in-between';
|
||||||
|
rootLayout.insertChild(1, newChild);
|
||||||
|
|
||||||
|
assertChildTexts("btn1|in-between|btn2", rootLayout, "button inserted at correct location");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function test_getChildIndex() {
|
||||||
|
let lastChildIndex = rootLayout.getChildrenCount() - 1;
|
||||||
|
let lastButton = <Button>rootLayout.getChildAt(lastChildIndex);
|
||||||
|
TKUnit.assertEqual("btn2", lastButton.text);
|
||||||
|
TKUnit.assertEqual(lastChildIndex, rootLayout.getChildIndex(lastButton));
|
||||||
|
|
||||||
|
let nonChild = new Button();
|
||||||
|
TKUnit.assertEqual(-1, rootLayout.getChildIndex(nonChild));
|
||||||
|
}
|
||||||
|
|
||||||
export function test_codesnippets() {
|
export function test_codesnippets() {
|
||||||
// <snippet module="ui/layouts/stack-layout" title="stack-layout">
|
// <snippet module="ui/layouts/stack-layout" title="stack-layout">
|
||||||
// ### Create StackLayout
|
// ### Create StackLayout
|
||||||
// ``` JavaScript
|
// ``` JavaScript
|
||||||
var stackLayout = new layout.StackLayout();
|
var stackLayout = new StackLayout();
|
||||||
// ```
|
// ```
|
||||||
|
|
||||||
// ### Declaring a StackLayout.
|
// ### Declaring a StackLayout.
|
||||||
@ -225,7 +251,7 @@ export function test_codesnippets() {
|
|||||||
|
|
||||||
// ### Add child view to layout
|
// ### Add child view to layout
|
||||||
// ``` JavaScript
|
// ``` JavaScript
|
||||||
var btn = new button.Button();
|
var btn = new Button();
|
||||||
stackLayout.addChild(btn);
|
stackLayout.addChild(btn);
|
||||||
// ```
|
// ```
|
||||||
|
|
||||||
@ -240,4 +266,4 @@ export function test_codesnippets() {
|
|||||||
// ```
|
// ```
|
||||||
|
|
||||||
// </snippet>
|
// </snippet>
|
||||||
};
|
};
|
||||||
|
15
ui/layouts/layout.d.ts
vendored
15
ui/layouts/layout.d.ts
vendored
@ -17,12 +17,25 @@
|
|||||||
*/
|
*/
|
||||||
getChildAt(index: number): view.View;
|
getChildAt(index: number): view.View;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the position of the child view
|
||||||
|
* @param child The child view that we are looking for.
|
||||||
|
*/
|
||||||
|
getChildIndex(child: view.View): number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the view to children array.
|
* Adds the view to children array.
|
||||||
* @param view The view to be added to the end of the children array.
|
* @param view The view to be added to the end of the children array.
|
||||||
*/
|
*/
|
||||||
addChild(view: view.View);
|
addChild(view: view.View);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts the view to children array at the specified index.
|
||||||
|
* @param atIndex The insertion index.
|
||||||
|
* @param view The view to be added to the end of the children array.
|
||||||
|
*/
|
||||||
|
insertChild(atIndex: number, child: view.View);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the specified view from the children array.
|
* Removes the specified view from the children array.
|
||||||
* @param view The view to remove from the children array.
|
* @param view The view to remove from the children array.
|
||||||
@ -49,4 +62,4 @@
|
|||||||
*/
|
*/
|
||||||
paddingTop: number;
|
paddingTop: number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,10 @@ export class Layout extends view.CustomLayoutView implements definition.Layout,
|
|||||||
return this._subViews[index];
|
return this._subViews[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getChildIndex(child: view.View): number {
|
||||||
|
return this._subViews.indexOf(child);
|
||||||
|
}
|
||||||
|
|
||||||
public getChildById(id: string) {
|
public getChildById(id: string) {
|
||||||
return view.getViewById(this, id);
|
return view.getViewById(this, id);
|
||||||
}
|
}
|
||||||
@ -38,6 +42,11 @@ export class Layout extends view.CustomLayoutView implements definition.Layout,
|
|||||||
this._subViews.push(child);
|
this._subViews.push(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public insertChild(atIndex: number, child: view.View) {
|
||||||
|
this._addView(child);
|
||||||
|
this._subViews.splice(atIndex, 0, child);
|
||||||
|
}
|
||||||
|
|
||||||
public removeChild(child: view.View) {
|
public removeChild(child: view.View) {
|
||||||
this._removeView(child);
|
this._removeView(child);
|
||||||
|
|
||||||
@ -98,4 +107,4 @@ export class Layout extends view.CustomLayoutView implements definition.Layout,
|
|||||||
(<UIView>nativeView).clipsToBounds = value;
|
(<UIView>nativeView).clipsToBounds = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user