mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
Brought back the viewModule.getAncestor function.
Extended it, so you can pass actual type instances instead of strings.
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import TKUnit = require("../TKUnit");
|
||||
import view = require("ui/core/view");
|
||||
import builder = require("ui/builder");
|
||||
import page = require("ui/page");
|
||||
import buttonModule = require("ui/button");
|
||||
import switchModule = require("ui/switch");
|
||||
import textFieldModule = require("ui/text-field");
|
||||
@ -13,6 +12,7 @@ import fileSystemAccess = require("file-system/file-system-access");
|
||||
import observable = require("data/observable");
|
||||
import stackLayoutModule = require("ui/layouts/stack-layout");
|
||||
import {Label} from "ui/label";
|
||||
import {Page} from "ui/page";
|
||||
import myCustomControlWithoutXml = require("./mymodule/MyControl");
|
||||
import listViewModule = require("ui/list-view");
|
||||
import helper = require("../ui/helper");
|
||||
@ -52,9 +52,9 @@ export function test_loadWithOptionsNoXML() {
|
||||
};
|
||||
|
||||
export function test_loadWithOptionsNoXML_CSSIsApplied() {
|
||||
var newPage: page.Page;
|
||||
var pageFactory = function (): page.Page {
|
||||
newPage = new page.Page();
|
||||
var newPage: Page;
|
||||
var pageFactory = function (): Page {
|
||||
newPage = new Page();
|
||||
|
||||
newPage.content = builder.load({
|
||||
path: "~/xml-declaration/mymodule",
|
||||
@ -83,6 +83,12 @@ export function test_loadInheritedPageAndResolveFromChild() {
|
||||
|
||||
let discoveredPage = contentLabel.page;
|
||||
TKUnit.assert(page === discoveredPage);
|
||||
|
||||
let discoveredAncestorByBaseType = viewModule.getAncestor(contentLabel, Page);
|
||||
TKUnit.assert(page === discoveredAncestorByBaseType);
|
||||
|
||||
let discoveredAncestorByInheritedTypeName = viewModule.getAncestor(contentLabel, "InheritedPage");
|
||||
TKUnit.assert(page === discoveredAncestorByInheritedTypeName);
|
||||
});
|
||||
}
|
||||
|
||||
@ -96,9 +102,9 @@ export function test_loadWithOptionsWithXML() {
|
||||
};
|
||||
|
||||
export function test_loadWithOptionsWithXML_CSSIsApplied() {
|
||||
var newPage: page.Page;
|
||||
var pageFactory = function (): page.Page {
|
||||
newPage = new page.Page();
|
||||
var newPage: Page;
|
||||
var pageFactory = function (): Page {
|
||||
newPage = new Page();
|
||||
|
||||
newPage.content = builder.load({
|
||||
path: "~/xml-declaration/mymodulewithxml",
|
||||
@ -173,7 +179,7 @@ export function test_parse_ShouldFindEventHandlersInExports() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldSetGridAttachedProperties() {
|
||||
var p = <page.Page>builder.parse("<Page><GridLayout><Label row='1' col='2' rowSpan='3' colSpan='4' /></GridLayout></Page>");
|
||||
var p = <Page>builder.parse("<Page><GridLayout><Label row='1' col='2' rowSpan='3' colSpan='4' /></GridLayout></Page>");
|
||||
var grid = <gridLayoutModule.GridLayout>p.content;
|
||||
var child = grid.getChildAt(0);
|
||||
|
||||
@ -191,7 +197,7 @@ export function test_parse_ShouldSetGridAttachedProperties() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldSetCanvasAttachedProperties() {
|
||||
var p = <page.Page>builder.parse("<Page><AbsoluteLayout><Label left='1' top='2' right='3' bottom='4' /></AbsoluteLayout></Page>");
|
||||
var p = <Page>builder.parse("<Page><AbsoluteLayout><Label left='1' top='2' right='3' bottom='4' /></AbsoluteLayout></Page>");
|
||||
var grid = <gridLayoutModule.GridLayout>p.content;
|
||||
var child = grid.getChildAt(0);
|
||||
|
||||
@ -203,34 +209,34 @@ export function test_parse_ShouldSetCanvasAttachedProperties() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseNumberProperties() {
|
||||
var p = <page.Page>builder.parse("<Page width='100' />");
|
||||
var p = <Page>builder.parse("<Page width='100' />");
|
||||
|
||||
TKUnit.assert(p.width === 100, "Expected result: 100; Actual result: " + p.width + "; type: " + typeof (p.width));
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBooleanProperties() {
|
||||
var p = <page.Page>builder.parse("<Page><Switch checked='true' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><Switch checked='true' /></Page>");
|
||||
var sw = <switchModule.Switch>p.content;
|
||||
|
||||
TKUnit.assert(sw.checked === true, "Expected result: true; Actual result: " + sw.checked + "; type: " + typeof (sw.checked));
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBooleanPropertiesIgnoreCase() {
|
||||
var p = <page.Page>builder.parse("<Page><Switch checked='False' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><Switch checked='False' /></Page>");
|
||||
var sw = <switchModule.Switch>p.content;
|
||||
|
||||
TKUnit.assert(sw.checked === false, "Expected result: false; Actual result: " + sw.checked + "; type: " + typeof (sw.checked));
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBooleanPropertiesIgnoreCaseInverted() {
|
||||
var p = <page.Page>builder.parse("<Page><TextField editable='False' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><TextField editable='False' /></Page>");
|
||||
var tf = <textFieldModule.TextField>p.content;
|
||||
|
||||
TKUnit.assert(tf.editable === false, "Expected result: false; Actual result: " + tf.editable + "; type: " + typeof (tf.editable));
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParsePlatformSpecificProperties() {
|
||||
var p = <page.Page>builder.parse("<Page><TextField ios:editable='False' android:editable='True' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><TextField ios:editable='False' android:editable='True' /></Page>");
|
||||
var tf = <textFieldModule.TextField>p.content;
|
||||
|
||||
if (platform.device.os === platform.platformNames.ios) {
|
||||
@ -241,7 +247,7 @@ export function test_parse_ShouldParsePlatformSpecificProperties() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParsePlatformSpecificComponents() {
|
||||
var p = <page.Page>builder.parse("<Page><ios><TextField /></ios><android><Label /></android></Page>");
|
||||
var p = <Page>builder.parse("<Page><ios><TextField /></ios><android><Label /></android></Page>");
|
||||
if (platform.device.os === platform.platformNames.ios) {
|
||||
TKUnit.assert(p.content instanceof textFieldModule.TextField, "Expected result: TextField; Actual result: " + p.content);
|
||||
}
|
||||
@ -262,7 +268,7 @@ export function test_parse_ThrowErrorWhenNestingPlatforms() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindings() {
|
||||
var p = <page.Page>builder.parse("<Page><Switch checked='{{ myProp }}' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><Switch checked='{{ myProp }}' /></Page>");
|
||||
p.bindingContext = { myProp: true };
|
||||
var sw = <switchModule.Switch>p.content;
|
||||
|
||||
@ -270,7 +276,7 @@ export function test_parse_ShouldParseBindings() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsWithObservable() {
|
||||
var p = <page.Page>builder.parse("<Page><Switch checked='{{ myProp }}' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><Switch checked='{{ myProp }}' /></Page>");
|
||||
var obj = new observable.Observable();
|
||||
obj.set("myProp", true);
|
||||
p.bindingContext = obj;
|
||||
@ -284,7 +290,7 @@ export function test_parse_ShouldParseBindingsWithObservable() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsToEvents() {
|
||||
var p = <page.Page>builder.parse("<Page><Button tap='{{ myTap }}' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><Button tap='{{ myTap }}' /></Page>");
|
||||
p.bindingContext = {
|
||||
myTap: function (args) {
|
||||
//
|
||||
@ -296,7 +302,7 @@ export function test_parse_ShouldParseBindingsToEvents() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsToGestures() {
|
||||
var p = <page.Page>builder.parse("<Page><Label tap='{{ myTap }}' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><Label tap='{{ myTap }}' /></Page>");
|
||||
var context = {
|
||||
myTap: function (args) {
|
||||
//
|
||||
@ -313,7 +319,7 @@ export function test_parse_ShouldParseBindingsToGestures() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseSubProperties() {
|
||||
var p = <page.Page>builder.parse("<Page><Switch style.visibility='collapsed' checked='{{ myProp }}' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><Switch style.visibility='collapsed' checked='{{ myProp }}' /></Page>");
|
||||
var obj = new observable.Observable();
|
||||
obj.set("myProp", true);
|
||||
p.bindingContext = obj;
|
||||
@ -347,7 +353,7 @@ export function test_parse_ShouldParseBindingsWithCommaInsideDoubleQuote() {
|
||||
};
|
||||
|
||||
export function test_parse_CanBindBackgroundImage() {
|
||||
var p = <page.Page>builder.parse("<Page><StackLayout backgroundImage='{{ myProp }}' /></Page>");
|
||||
var p = <Page>builder.parse("<Page><StackLayout backgroundImage='{{ myProp }}' /></Page>");
|
||||
var expected = "~/logo.png"
|
||||
var obj = new observable.Observable();
|
||||
obj.set("myProp", expected);
|
||||
@ -358,7 +364,7 @@ export function test_parse_CanBindBackgroundImage() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseLowerCaseDashedComponentDeclaration() {
|
||||
var p = <page.Page>builder.parse('<page><stack-layout><label text="Label" /><segmented-bar><segmented-bar.items><segmented-bar-item title="test" /></segmented-bar.items></segmented-bar></stack-layout></page>');
|
||||
var p = <Page>builder.parse('<page><stack-layout><label text="Label" /><segmented-bar><segmented-bar.items><segmented-bar-item title="test" /></segmented-bar.items></segmented-bar></stack-layout></page>');
|
||||
var ctrl = <stackLayoutModule.StackLayout>p.content;
|
||||
|
||||
TKUnit.assert(ctrl instanceof stackLayoutModule.StackLayout, "Expected result: StackLayout!; Actual result: " + ctrl);
|
||||
@ -367,28 +373,28 @@ export function test_parse_ShouldParseLowerCaseDashedComponentDeclaration() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithoutXml() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodule"><customControls:MyControl /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodule"><customControls:MyControl /></Page>');
|
||||
var ctrl = p.content;
|
||||
|
||||
TKUnit.assert(ctrl instanceof myCustomControlWithoutXml.MyControl, "Expected result: custom control is defined!; Actual result: " + ctrl);
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithoutXmlFromTNSModules() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns' + ':customControls="tns_modules/ui/label"><customControls:Label /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns' + ':customControls="tns_modules/ui/label"><customControls:Label /></Page>');
|
||||
var ctrl = p.content;
|
||||
|
||||
TKUnit.assert(ctrl instanceof Label, "Expected result: custom control is defined!; Actual result: " + ctrl);
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithoutXmlFromTNSModulesWhenNotSpecified() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns' + ':customControls="ui/label"><customControls:Label /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns' + ':customControls="ui/label"><customControls:Label /></Page>');
|
||||
var ctrl = p.content;
|
||||
|
||||
TKUnit.assert(ctrl instanceof Label, "Expected result: custom control is defined!; Actual result: " + ctrl);
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithXml() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:MyControl /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:MyControl /></Page>');
|
||||
var panel = <stackLayoutModule.StackLayout>p.content;
|
||||
var lbl = <Label>panel.getChildAt(0);
|
||||
|
||||
@ -396,21 +402,21 @@ export function test_parse_ShouldParseCustomComponentWithXml() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithXml_WithAttributes() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:MyControl visibility="collapsed" /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:MyControl visibility="collapsed" /></Page>');
|
||||
var panel = <stackLayoutModule.StackLayout>p.content;
|
||||
|
||||
TKUnit.assertEqual(panel.visibility, "collapsed", "panel.visibility");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithXml_WithCustomAttributes() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:MyControl myProperty="myValue" /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:MyControl myProperty="myValue" /></Page>');
|
||||
var panel = <stackLayoutModule.StackLayout>p.content;
|
||||
|
||||
TKUnit.assertEqual(panel["myProperty"], "myValue", "customControl.myProperty");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithXmlNoJS() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:my-control-no-js /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:my-control-no-js /></Page>');
|
||||
var panel = <stackLayoutModule.StackLayout>p.content;
|
||||
var lbl = <Label>panel.getChildAt(0);
|
||||
|
||||
@ -418,21 +424,21 @@ export function test_parse_ShouldParseCustomComponentWithXmlNoJS() {
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithXmlNoJS_WithAttributes() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:my-control-no-js visibility="collapsed" /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:my-control-no-js visibility="collapsed" /></Page>');
|
||||
var panel = <stackLayoutModule.StackLayout>p.content;
|
||||
|
||||
TKUnit.assertEqual(panel.visibility, "collapsed", "panel.visibility");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithXmlNoJS_WithCustomAttributes() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:my-control-no-js myProperty="myValue" /></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodulewithxml"><customControls:my-control-no-js myProperty="myValue" /></Page>');
|
||||
var panel = <stackLayoutModule.StackLayout>p.content;
|
||||
|
||||
TKUnit.assertEqual(panel["myProperty"], "myValue", "customControl.myProperty");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseCustomComponentWithoutXmlInListViewTemplate() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodule"><ListView items="{{ items }}" itemLoading="{{ itemLoading }}"><ListView.itemTemplate><customControls:MyControl /></ListView.itemTemplate></ListView></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns:customControls="xml-declaration/mymodule"><ListView items="{{ items }}" itemLoading="{{ itemLoading }}"><ListView.itemTemplate><customControls:MyControl /></ListView.itemTemplate></ListView></Page>');
|
||||
|
||||
function testAction(views: Array<viewModule.View>) {
|
||||
var ctrl;
|
||||
@ -460,7 +466,7 @@ export function test_parse_ShouldParseCustomComponentWithoutXmlInListViewTemplat
|
||||
}
|
||||
|
||||
export function test_parse_ShouldParseNestedListViewInListViewTemplate() {
|
||||
var p = <page.Page>builder.parse('<Page xmlns="http://www.nativescript.org/tns.xsd"><ListView items="{{ items }}" itemLoading="{{ itemLoading }}"><ListView.itemTemplate><ListView items="{{ subItems }}" /></ListView.itemTemplate></ListView></Page>');
|
||||
var p = <Page>builder.parse('<Page xmlns="http://www.nativescript.org/tns.xsd"><ListView items="{{ items }}" itemLoading="{{ itemLoading }}"><ListView.itemTemplate><ListView items="{{ subItems }}" /></ListView.itemTemplate></ListView></Page>');
|
||||
|
||||
function testAction(views: Array<viewModule.View>) {
|
||||
var ctrl;
|
||||
|
@ -55,6 +55,21 @@ export function eachDescendant(view: definition.View, callback: (child: View) =>
|
||||
view._eachChildView(localCallback);
|
||||
}
|
||||
|
||||
export function getAncestor(view: View, criterion: string | Function): definition.View {
|
||||
let matcher: (view: definition.View) => boolean = null;
|
||||
if (typeof criterion === "string")
|
||||
matcher = (view: definition.View) => view.typeName === criterion;
|
||||
else
|
||||
matcher = (view: definition.View) => view instanceof criterion;
|
||||
|
||||
for (let parent: definition.View = view.parent; parent != null; parent = parent.parent) {
|
||||
if (matcher(parent))
|
||||
return parent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
var viewIdCounter = 0;
|
||||
|
||||
function onCssClassPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||
|
8
ui/core/view.d.ts
vendored
8
ui/core/view.d.ts
vendored
@ -22,6 +22,14 @@ declare module "ui/core/view" {
|
||||
*/
|
||||
export function eachDescendant(view: View, callback: (child: View) => boolean);
|
||||
|
||||
/**
|
||||
* Gets an ancestor from a given type.
|
||||
* @param view - Starting view (child view).
|
||||
* @param criterion - The type of ancestor view we are looking for. Could be a string containing a class name or an actual type.
|
||||
* Returns an instance of a view (if found), otherwise undefined.
|
||||
*/
|
||||
export function getAncestor(view: View, criterion: string | Function): View;
|
||||
|
||||
/**
|
||||
* Defines interface for an optional parameter used to create a view.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user