Merge pull request #3396 from NativeScript/action-bar-test-refactor

Action bar test refactor
This commit is contained in:
Alexander Vakrilov
2017-01-03 17:50:10 +02:00
committed by GitHub
13 changed files with 286 additions and 297 deletions

View File

@@ -43,7 +43,8 @@
"sourceMaps": true,
"diagnosticLogging": false,
"emulator": false,
"rebuild": false
"rebuild": false,
"stopOnEntry": true
},
{
"name": "Launch on Android",

View File

@@ -55,7 +55,7 @@ allTests["CONNECTIVITY"] = require("./connectivity-tests");
// allTests["PROXY-VIEW-CONTAINER"] = require("./ui/proxy-view-container/proxy-view-container-tests")
allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests");
// allTests["ACTION-BAR"] = require("./ui/action-bar/action-bar-tests");
allTests["ACTION-BAR"] = require("./ui/action-bar/action-bar-tests");
// allTests["XML-DECLARATION"] = require("./xml-declaration/xml-declaration-tests");
allTests["DOCKLAYOUT"] = require("./ui/layouts/dock-layout-tests");
allTests["WRAPLAYOUT"] = require("./ui/layouts/wrap-layout-tests");

View File

@@ -1,11 +1,11 @@
import * as TKUnit from "../../TKUnit";
import * as LabelModule from "ui/label";
import * as helper from "../helper";
import * as builder from "ui/builder";
import * as button from "ui/button";
import * as PageModule from "ui/page";
import * as viewModule from "ui/core/view";
import * as fs from "file-system";
import { Label } from "ui/label";
import { Button } from "ui/button";
import { Page } from "ui/page";
import { View } from "ui/core/view";
import { Observable } from "data/observable";
// >> actionbar-common-require
@@ -13,14 +13,14 @@ import * as actionBarModule from "ui/action-bar";
// << actionbar-common-require
export function test_actionItem_inherit_bindingContext() {
var page: PageModule.Page;
var label: LabelModule.Label;
var context = { text: "item" };
let page: Page;
let label: Label;
const context = { text: "item" };
var pageFactory = function (): PageModule.Page {
page = new PageModule.Page();
const pageFactory = function (): Page {
page = new Page();
page.bindingContext = context;
var actionItem = new actionBarModule.ActionItem();
const actionItem = new actionBarModule.ActionItem();
actionItem.bind({
sourceProperty: "text",
@@ -29,7 +29,7 @@ export function test_actionItem_inherit_bindingContext() {
page.actionBar.actionItems.addItem(actionItem);
label = new LabelModule.Label();
label = new Label();
label.text = "Text";
page.content = label;
return page;
@@ -41,7 +41,7 @@ export function test_actionItem_inherit_bindingContext() {
}
export function test_actionBar_inherit_bindingContext_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar title=\"{{ myProp }} \" /> </Page.actionBar> </Page>");
p.bindingContext = { myProp: "success" };
@@ -49,52 +49,52 @@ export function test_actionBar_inherit_bindingContext_inXML() {
};
export function test_actionItem_inherit_bindingContext_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionBar.actionItems>" +
"<ActionItem text=\"{{ myProp }} \" />" +
"</ActionBar.actionItems> </ActionBar> </Page.actionBar> </Page>");
p.bindingContext = { myProp: "success" };
var actionItem = p.actionBar.actionItems.getItemAt(0);
const actionItem = p.actionBar.actionItems.getItemAt(0);
TKUnit.assertEqual(actionItem.text, "success", "actionItem.text");
};
export function test_actionItem_page_property_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionBar.actionItems>" +
"<ActionItem text=\"test\" />" +
"</ActionBar.actionItems> </ActionBar> </Page.actionBar> </Page>");
var actionItem = p.actionBar.actionItems.getItemAt(0);
const actionItem = p.actionBar.actionItems.getItemAt(0);
TKUnit.assertEqual(actionItem.page, p, "actionItem.page");
};
export function test_actionItem_actionView_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionItem> <ActionItem.actionView>" +
"<Label/>" +
"</ActionItem.actionView> </ActionItem> </ActionBar> </Page.actionBar> </Page>");
var label = <LabelModule.Label>p.actionBar.actionItems.getItemAt(0).actionView;
TKUnit.assert(label instanceof LabelModule.Label, "ActionItem.actionView not loaded correctly");
const label = <Label>p.actionBar.actionItems.getItemAt(0).actionView;
TKUnit.assert(label instanceof Label, "ActionItem.actionView not loaded correctly");
};
export function test_actionItem_actionView_inherit_bindingContext_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionItem> <ActionItem.actionView>" +
"<Label text=\"{{ myProp }} \" />" +
"</ActionItem.actionView> </ActionItem> </ActionBar> </Page.actionBar> </Page>");
p.bindingContext = { myProp: "success" };
var label = <LabelModule.Label>p.actionBar.actionItems.getItemAt(0).actionView;
TKUnit.assert(label instanceof LabelModule.Label, "ActionItem.actionView not loaded correctly");
const label = <Label>p.actionBar.actionItems.getItemAt(0).actionView;
TKUnit.assert(label instanceof Label, "ActionItem.actionView not loaded correctly");
TKUnit.assertEqual(label.text, "success", "ActionItem.actionView");
};
export function test_ActionBar_is_not_empty_when_actionItem_actionView_is_set() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionItem> <ActionItem.actionView>" +
"<Label text=\"test\" />" +
"</ActionItem.actionView> </ActionItem> </ActionBar> </Page.actionBar> </Page>");
@@ -103,50 +103,50 @@ export function test_ActionBar_is_not_empty_when_actionItem_actionView_is_set()
};
export function test_navigationButton_inherit_bindingContext_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar>" +
"<NavigationButton text=\"{{ myProp }} \" />" +
"</ActionBar> </Page.actionBar> </Page>");
p.bindingContext = { myProp: "success" };
var navButton = p.actionBar.navigationButton;
const navButton = p.actionBar.navigationButton;
TKUnit.assertEqual(navButton.text, "success", "actionItem.text");
};
export function test_titleView_inherit_bindingContext_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionBar.titleView>" +
"<Button text=\"{{ myProp }} \" />" +
"</ActionBar.titleView> </ActionBar> </Page.actionBar> </Page>");
p.bindingContext = { myProp: "success" };
var centerBtn = <button.Button>p.actionBar.titleView;
TKUnit.assert(centerBtn instanceof button.Button, "titleView not loaded correctly");
const centerBtn = <Button>p.actionBar.titleView;
TKUnit.assert(centerBtn instanceof Button, "titleView not loaded correctly");
TKUnit.assertEqual(centerBtn.text, "success", "actionItem.text");
};
export function test_titleView_inXML() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionBar.titleView>" +
"<Button/>" +
"</ActionBar.titleView> </ActionBar> </Page.actionBar> </Page>");
var centerBtn = <button.Button>p.actionBar.titleView;
TKUnit.assert(centerBtn instanceof button.Button, "titleView not loaded correctly");
const centerBtn = <Button>p.actionBar.titleView;
TKUnit.assert(centerBtn instanceof Button, "titleView not loaded correctly");
};
export function test_titleView_inXML_short_definition() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar>" +
"<Button/>" +
"</ActionBar> </Page.actionBar> </Page>");
var centerBtn = <button.Button>p.actionBar.titleView;
TKUnit.assert(centerBtn instanceof button.Button, "titleView not loaded correctly");
const centerBtn = <Button>p.actionBar.titleView;
TKUnit.assert(centerBtn instanceof Button, "titleView not loaded correctly");
};
export function test_ActionBar_is_not_empty_when_titleView_is_set() {
var p = <PageModule.Page>builder.parse(
const p = <Page>builder.parse(
"<Page> <Page.actionBar> <ActionBar> <ActionBar.titleView>" +
"<Button text=\"test\" />" +
"</ActionBar.titleView> </ActionBar> </Page.actionBar> </Page>");
@@ -155,18 +155,18 @@ export function test_ActionBar_is_not_empty_when_titleView_is_set() {
};
export function test_ActionBarItemBindingToEvent() {
var p = <PageModule.Page>builder.parse('<Page><Page.actionBar><ActionBar><ActionBar.actionItems><ActionItem tap="{{ test }}"/></ActionBar.actionItems></ActionBar></Page.actionBar></Page>');
const p = <Page>builder.parse('<Page><Page.actionBar><ActionBar><ActionBar.actionItems><ActionItem tap="{{ test }}"/></ActionBar.actionItems></ActionBar></Page.actionBar></Page>');
var testAction = function (views: Array<viewModule.View>) {
var page = <PageModule.Page>views[0];
var firstHandlerCallCounter = 0;
var secondHandlerCallCounter = 0;
var firstHandler = function () { firstHandlerCallCounter++; };
var secondHandler = function () { secondHandlerCallCounter++; };
const testAction = function (views: Array<View>) {
const page = <Page>views[0];
let firstHandlerCallCounter = 0;
let secondHandlerCallCounter = 0;
const firstHandler = function () { firstHandlerCallCounter++; };
const secondHandler = function () { secondHandlerCallCounter++; };
page.bindingContext = new Observable({ "test": firstHandler });
var actionBarItem = page.actionBar.actionItems.getItemAt(0);
const actionBarItem = page.actionBar.actionItems.getItemAt(0);
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"].length, 1, "There should be only one listener");
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"][0].callback + "", "function () { firstHandlerCallCounter++; }", "First handler is not equal");
@@ -175,25 +175,24 @@ export function test_ActionBarItemBindingToEvent() {
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"].length, 1, "There should be only one listener");
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"][0].callback + "", "function () { secondHandlerCallCounter++; }", "Second handler is not equal");
}
};
helper.navigate(function () { return p; });
testAction([p]);
}
export function test_Setting_ActionItems_doesnt_thrown() {
let page: Page;
let label: Label;
let gotException = false;
var page: PageModule.Page;
var label: LabelModule.Label;
var gotException = false;
var pageFactory = function (): PageModule.Page {
page = new PageModule.Page();
var actionItem = new actionBarModule.ActionItem();
const pageFactory = function (): Page {
page = new Page();
const actionItem = new actionBarModule.ActionItem();
actionItem.text = "Item";
page.actionBar.actionItems.addItem(actionItem);
label = new LabelModule.Label();
label = new Label();
label.text = "Text";
page.content = label;
return page;
@@ -211,9 +210,9 @@ export function test_Setting_ActionItems_doesnt_thrown() {
export function test_Setting_ActionItemsWithNumberAsText_doesnt_thrown() {
var gotException = false;
let gotException = false;
var moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
const moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
try {
helper.navigateToModule(moduleName + "/ActionBar_NumberAsText");
@@ -226,19 +225,19 @@ export function test_Setting_ActionItemsWithNumberAsText_doesnt_thrown() {
}
export function test_CanDefineEverythingAsContentBetweenTheTwoTags() {
var moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
helper.navigateToModuleAndRunTest(moduleName + "/ActionBar_BetweenTags", undefined, (page: PageModule.Page) => {
const moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
helper.navigateToModuleAndRunTest(moduleName + "/ActionBar_BetweenTags", undefined, (page: Page) => {
TKUnit.assertNotNull(page.actionBar.navigationButton);
TKUnit.assertEqual(page.actionBar.navigationButton.text, "nb");
TKUnit.assertNull(page.actionBar.title);
TKUnit.assertNotNull(page.actionBar.titleView);
TKUnit.assertTrue(page.actionBar.titleView instanceof LabelModule.Label);
TKUnit.assertEqual((<LabelModule.Label>page.actionBar.titleView).text, "tv");
TKUnit.assertTrue(page.actionBar.titleView instanceof Label);
TKUnit.assertEqual((<Label>page.actionBar.titleView).text, "tv");
TKUnit.assertNotNull(page.actionBar.actionItems);
var items = page.actionBar.actionItems.getItems();
const items = page.actionBar.actionItems.getItems();
TKUnit.assertEqual(items.length, 3);
TKUnit.assertEqual(items[0].text, "i1");
@@ -248,19 +247,19 @@ export function test_CanDefineEverythingAsContentBetweenTheTwoTags() {
}
export function test_LoadedEventsOrder() {
var loadedEvents = new Array<string>();
var pageFactory = function (): PageModule.Page {
var page = new PageModule.Page();
page.on(viewModule.View.loadedEvent, () => {
const loadedEvents = new Array<string>();
const pageFactory = function (): Page {
const page = new Page();
page.on(View.loadedEvent, () => {
loadedEvents.push("page");
});
page.actionBar.on(viewModule.View.loadedEvent, () => {
page.actionBar.on(View.loadedEvent, () => {
loadedEvents.push("action-bar");
});
var content = new LabelModule.Label();
content.on(viewModule.View.loadedEvent, () => {
const content = new Label();
content.on(View.loadedEvent, () => {
loadedEvents.push("content");
});
page.content = content;
@@ -274,14 +273,14 @@ export function test_LoadedEventsOrder() {
}
export function test_LoadedEventsOrder_WithoutPageContent() {
var loadedEvents = new Array<string>();
var pageFactory = function (): PageModule.Page {
var page = new PageModule.Page();
page.on(viewModule.View.loadedEvent, () => {
const loadedEvents = new Array<string>();
const pageFactory = function (): Page {
const page = new Page();
page.on(View.loadedEvent, () => {
loadedEvents.push("page");
});
page.actionBar.on(viewModule.View.loadedEvent, () => {
page.actionBar.on(View.loadedEvent, () => {
loadedEvents.push("action-bar");
});
@@ -294,8 +293,8 @@ export function test_LoadedEventsOrder_WithoutPageContent() {
}
export function test_setId() {
var pageFactory = function (): PageModule.Page {
var page = new PageModule.Page();
const pageFactory = function (): Page {
const page = new Page();
page.actionBar.id = "myId";
return page;
@@ -310,11 +309,11 @@ export function test_setId() {
}
export function createPageAndNavigate() {
var page: PageModule.Page;
var pageFactory = function (): PageModule.Page {
page = new PageModule.Page();
let page: Page;
const pageFactory = function (): Page {
page = new Page();
var label = new LabelModule.Label();
const label = new Label();
label.text = "Text";
page.content = label;
return page;

View File

@@ -2,16 +2,17 @@
import * as TKUnit from "../../TKUnit";
import { ActionItem } from "ui/action-bar";
import { Visibility } from "ui/enums";
import { Button } from "ui/button";
global.moduleMerge(actionTestsCommon, exports);
export function test_actionItem_visibility() {
var actionItem = new ActionItem();
const actionItem = new ActionItem();
actionItem.text = "Test";
var page = actionTestsCommon.createPageAndNavigate();
const page = actionTestsCommon.createPageAndNavigate();
page.actionBar.actionItems.addItem(actionItem);
var toolbar = <android.support.v7.widget.Toolbar>(<any>page.actionBar)._toolbar;
var menu = toolbar.getMenu();
const toolbar = <android.support.v7.widget.Toolbar>(<any>page.actionBar)._toolbar;
const menu = toolbar.getMenu();
TKUnit.assertTrue(menu.hasVisibleItems(), "Visibility does not work");
actionItem.visibility = Visibility.collapse;
@@ -19,14 +20,56 @@ export function test_actionItem_visibility() {
}
export function test_navigationButton_visibility() {
var actionItem = new ActionItem();
const actionItem = new ActionItem();
actionItem.icon = "~/small-image.png";
var page = actionTestsCommon.createPageAndNavigate();
const page = actionTestsCommon.createPageAndNavigate();
page.actionBar.navigationButton = actionItem;
var toolbar = <android.support.v7.widget.Toolbar>(<any>page.actionBar)._toolbar;
const toolbar = <android.support.v7.widget.Toolbar>(<any>page.actionBar)._toolbar;
TKUnit.assertNotNull(toolbar.getNavigationIcon(), "Visibility does not work");
actionItem.visibility = Visibility.collapse;
TKUnit.assertNull(toolbar.getNavigationIcon(), "Visibility does not work");
}
}
export function test_set_actionView_to_attached_actionItem_propagates_context() {
const actionItem = new ActionItem();
const actionButton = new Button();
actionItem.actionView = actionButton;
const page = actionTestsCommon.createPageAndNavigate();
// sanity check
TKUnit.assertNotNull(page.content._context, "Page content context should not be null");
// assert null before add
TKUnit.assertNull(actionItem._context, "Action Item context should be null before added");
TKUnit.assertNull(actionButton._context, "Action button context should not null before added");
page.actionBar.actionItems.addItem(actionItem);
// assert not null after add
TKUnit.assertNotNull(actionItem._context, "Action Item context should not be null after add");
TKUnit.assertNotNull(actionButton._context, "Action button context should not be null after add");
}
export function test_add_actionItem_with_actionView_propagates_context() {
const actionItem = new ActionItem();
const page = actionTestsCommon.createPageAndNavigate();
// sanity check
TKUnit.assertNotNull(page.content._context, "Page content context should not be null");
// add actionItem to the actionBar
TKUnit.assertNull(actionItem._context, "Action Item context should be null before added");
page.actionBar.actionItems.addItem(actionItem);
TKUnit.assertNotNull(actionItem._context, "Action Item context should not be null after add");
const actionButton = new Button();
// add actionButton to the actionItem
TKUnit.assertNull(actionButton._context, "Action button context should be null before added");
actionItem.actionView = actionButton;
TKUnit.assertNotNull(actionButton._context, "Action button context should not be null after add");
}

View File

@@ -13,7 +13,8 @@
}
},
"dependencies": {
"tns-core-modules": "2.4.0"
"tns-core-modules": "2.4.0",
"tns-core-modules-widgets": "file:///Users/vakrilov/Work/Projects/tns-core-modules-widgets/dist/tns-core-modules-widgets-2.5.0.tgz"
},
"devDependencies": {
"tns-platform-declarations": "*",

View File

@@ -28,6 +28,7 @@ export class ActionBarBase extends View implements ActionBarDefinition {
set navigationButton(value: NavigationButton) {
if (this._navigationButton !== value) {
if (this._navigationButton) {
this._removeView(this._navigationButton);
this._navigationButton.actionBar = undefined;
}
@@ -35,6 +36,7 @@ export class ActionBarBase extends View implements ActionBarDefinition {
if (this._navigationButton) {
this._navigationButton.actionBar = this;
this._addView(this._navigationButton);
}
this.update();
@@ -76,12 +78,6 @@ export class ActionBarBase extends View implements ActionBarDefinition {
}
set page(value: Page) {
this._page = value;
// // TODO: Move this in _eachChildView of Page class.
// this.unbind("bindingContext");
// this.bind({
// sourceProperty: "bindingContext",
// targetProperty: "bindingContext"
// }, this._page);
}
get android(): AndroidActionBarSettings {
@@ -104,7 +100,7 @@ export class ActionBarBase extends View implements ActionBarDefinition {
this._actionItems = new ActionItems(this);
}
public static onTitleChanged
public static onTitleChanged;
public update() {
//
@@ -132,26 +128,11 @@ export class ActionBarBase extends View implements ActionBarDefinition {
}
}
// public _onBindingContextChanged(oldValue: any, newValue: any) {
// super._onBindingContextChanged(oldValue, newValue);
// if (this._navigationButton) {
// this._navigationButton.bindingContext = newValue;
// }
// this._actionItems.getItems().forEach((item, i, arr) => { item.bindingContext = newValue; });
// }
public eachChildView(callback: (child: View) => boolean) {
const titleView = this.titleView;
if (titleView) {
callback(titleView);
}
this.actionItems.getItems().forEach((actionItem) => {
if (actionItem.actionView) {
callback(actionItem.actionView);
}
});
}
public eachChild(callback: (child: ViewBase) => boolean) {
@@ -199,6 +180,9 @@ export class ActionItems implements ActionItemsDefinition {
this._items.push(item);
item.actionBar = this._actionBar;
this._actionBar._addView(item);
this.invalidate();
}
@@ -207,12 +191,14 @@ export class ActionItems implements ActionItemsDefinition {
throw new Error("Cannot remove empty item");
}
var itemIndex = this._items.indexOf(item);
const itemIndex = this._items.indexOf(item);
if (itemIndex < 0) {
throw new Error("Cannot find item to remove");
}
this._items.splice(itemIndex, 1);
this._actionBar._removeView(item);
item.actionBar = undefined;
this.invalidate();
}
@@ -222,7 +208,7 @@ export class ActionItems implements ActionItemsDefinition {
}
public getVisibleItems(): Array<ActionItemDefinition> {
var visibleItems = [];
const visibleItems = [];
this._items.forEach((item) => {
if (isVisible(item)) {
visibleItems.push(item);
@@ -279,14 +265,20 @@ export class ActionItemBase extends ViewBase implements ActionItemDefinition {
}
set actionView(value: View) {
if (this._actionView !== value) {
if (this._actionView && this._actionBar) {
this._actionBar._removeView(this._actionView);
if (this._actionView) {
this._actionView.style[horizontalAlignmentProperty.cssName] = unsetValue;
this._actionView.style[verticalAlignmentProperty.cssName] = unsetValue;
this._removeView(this._actionView);
}
this._actionView = value;
this._addActionViewToActionBar();
if (this._actionView) {
this._addView(this._actionView);
this._actionView.style[horizontalAlignmentProperty.cssName] = HorizontalAlignment.CENTER;
this._actionView.style[verticalAlignmentProperty.cssName] = VerticalAlignment.MIDDLE;
}
if (this._actionBar) {
this._actionBar.update();
}
@@ -299,11 +291,6 @@ export class ActionItemBase extends ViewBase implements ActionItemDefinition {
set actionBar(value: ActionBarDefinition) {
if (value !== this._actionBar) {
this._actionBar = value;
if (this._actionBar) {
// ActionBarBase overrides _eachChildView so bindingContext should work without any manual work.
// this.bindingContext = this._actionBar.bindingContext;
this._addActionViewToActionBar();
}
}
}
@@ -319,11 +306,9 @@ export class ActionItemBase extends ViewBase implements ActionItemDefinition {
this.actionView = value;
}
private _addActionViewToActionBar() {
if (this._actionView && !this._actionView._isAddedToNativeVisualTree && this._actionBar) {
this._actionView.style[horizontalAlignmentProperty.cssName] = HorizontalAlignment.CENTER;
this._actionView.style[verticalAlignmentProperty.cssName] = VerticalAlignment.MIDDLE;
this._actionBar._addView(this._actionView);
public eachChild(callback: (child: ViewBase) => boolean) {
if (this._actionView) {
callback(this._actionView);
}
}
}

View File

@@ -69,7 +69,7 @@ export class AndroidActionBarSettings implements AndroidActionBarSettingsDefinit
}
}
export class NavigationButton extends ActionItemBase {
export class NavigationButton extends ActionItem {
}

View File

@@ -1,6 +1,6 @@
import { ViewBase as ViewBaseDefinition } from "ui/core/view-base";
import { Observable, EventData } from "data/observable";
import { Property, InheritedProperty, Style, clearInheritedProperties, propagateInheritedProperties, resetCSSProperties } from "./properties";
import { Property, InheritedProperty, Style, clearInheritedProperties, propagateInheritedProperties, resetCSSProperties, applyNativeSetters } from "./properties";
import { Binding, BindingOptions, Bindable } from "ui/core/bindable";
import { isIOS, isAndroid } from "platform";
import { fromString as gestureFromString } from "ui/gestures";
@@ -109,6 +109,8 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
public className: string;
public _domId: number;
public _context: any;
public _isAddedToNativeVisualTree: any;
public _cssState: CssState;
constructor() {
@@ -154,12 +156,12 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
public onLoaded() {
this._isLoaded = true;
this._loadEachChildView();
this._loadEachChild();
this._applyStyleFromScope();
this._emit("loaded");
}
public _loadEachChildView() {
public _loadEachChild() {
this.eachChild((child) => {
child.onLoaded();
return true;
@@ -168,12 +170,12 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
public onUnloaded() {
this._setCssState(null);
this._unloadEachChildView();
this._unloadEachChild();
this._isLoaded = false;
this._emit("unloaded");
}
private _unloadEachChildView() {
private _unloadEachChild() {
this.eachChild((child) => {
if (child.isLoaded) {
child.onUnloaded();
@@ -405,6 +407,15 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
}
protected _addViewCore(view: ViewBase, atIndex?: number) {
if (this._context) {
view._onAttached(this._context);
}
if (!view._isAddedToNativeVisualTree) {
let nativeIndex = this._childIndexToNativeChildIndex(atIndex);
view._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(view, nativeIndex);
}
// TODO: Discuss this.
if (this._isLoaded) {
view.onLoaded();
@@ -420,6 +431,7 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
if (traceEnabled) {
traceWrite(`${this}._removeView(${view})`, traceCategories.ViewHierarchy);
}
if (view.parent !== this) {
throw new Error("View not added to this instance. View: " + view + " CurrentParent: " + view.parent + " ExpectedParent: " + this);
}
@@ -439,6 +451,102 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
}
// view.unsetInheritedProperties();
// Remove the view from the native visual scene first
this._removeViewFromNativeVisualTree(view);
if (view._context) {
view._onDetached();
}
}
public _onAttached(context: any) {
if (!context) {
throw new Error("Expected valid android.content.Context instance.");
}
if (traceEnabled) {
traceWrite(`${this}._onAttached(context)`, traceCategories.VisualTreeEvents);
}
if (this._context === context) {
return;
}
if (this._context) {
this._onDetached(true);
}
this._context = context;
this._onContextChanged();
if (traceEnabled) {
traceNotifyEvent(this, "_onAttached");
}
// Notify each child for the _onAttached event
this.eachChild((child) => {
child._onAttached(context);
if (!child._isAddedToNativeVisualTree) {
// since we have lazy loading of the android widgets, we need to add the native instances at this point.
child._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(child);
}
return true;
});
// copy all the locally cached values to the native android widget
applyNativeSetters(this);
}
public _onDetached(force?: boolean) {
if (traceEnabled) {
traceWrite(`${this}._onDetached(force)`, traceCategories.VisualTreeEvents);
}
// Detach children first
this.eachChild((child: ViewBase) => {
if (child._isAddedToNativeVisualTree) {
this._removeViewFromNativeVisualTree(child);
}
if (child._context) {
child._onDetached(force);
}
return true;
});
this._context = undefined;
if (traceEnabled) {
traceNotifyEvent(this, "_onDetached");
}
}
public _onContextChanged(): void {
//
}
_childIndexToNativeChildIndex(index?: number): number {
return index;
}
/**
* Method is intended to be overridden by inheritors and used as "protected".
*/
public _addViewToNativeVisualTree(view: ViewBase, atIndex?: number): boolean {
if (view._isAddedToNativeVisualTree) {
throw new Error("Child already added to the native visual tree.");
}
return true;
}
/**
* Method is intended to be overridden by inheritors and used as "protected"
*/
public _removeViewFromNativeVisualTree(view: ViewBase) {
view._isAddedToNativeVisualTree = false;
}
public _goToVisualState(state: string) {

View File

@@ -77,7 +77,6 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
private _isLayoutValid: boolean;
private _cssType: string;
public _isAddedToNativeVisualTree: boolean;
public _gestureObservers = {};
// public parent: ViewCommon;
@@ -754,31 +753,10 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
return { boundsChanged, sizeChanged };
}
// TODO: We need to implement some kind of build step that includes these members only when building for Android
//@android
public _context: android.content.Context;
public _onAttached(context: android.content.Context) {
//
}
public _onDetached(force?: boolean) {
//
}
public _createUI() {
//
}
public _onContextChanged() {
//
}
//@endandroid
// TODO: We need to implement some kind of build step that includes these members only when building for iOS
//@endios
public eachChild(callback: (child: ViewBase) => boolean): void {
this.eachChildView(<any>callback);
}
@@ -786,10 +764,6 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
public eachChildView(callback: (view: ViewDefinition) => boolean) {
//
}
_childIndexToNativeChildIndex(index?: number): number {
return index;
}
_getNativeViewsCount(): number {
return this._isAddedToNativeVisualTree ? 1 : 0;
@@ -807,33 +781,6 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
// IOS specific
}
/**
* Method is intended to be overridden by inheritors and used as "protected"
*/
public _addViewCore(view: ViewBase, atIndex?: number) {
if (view instanceof ViewCommon) {
if (!view._isAddedToNativeVisualTree) {
let nativeIndex = this._childIndexToNativeChildIndex(atIndex);
view._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(view, nativeIndex);
}
}
super._addViewCore(view, atIndex);
}
/**
* Method is intended to be overridden by inheritors and used as "protected"
*/
public _removeViewCore(view: ViewBase) {
if (view instanceof ViewCommon) {
// TODO: Change type from ViewCommon to ViewBase. Probably this
// method will need to go to ViewBase class.
// Remove the view from the native visual scene first
this._removeViewFromNativeVisualTree(view);
}
super._removeViewCore(view);
}
// public unsetInheritedProperties(): void {
// // this._setValue(ProxyObject.bindingContextProperty, undefined, ValueSource.Inherited);
// // this._eachSetProperty((property) => {
@@ -843,24 +790,6 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
// // return true;
// // });
// }
/**
* Method is intended to be overridden by inheritors and used as "protected".
*/
public _addViewToNativeVisualTree(view: ViewDefinition, atIndex?: number): boolean {
if (view._isAddedToNativeVisualTree) {
throw new Error("Child already added to the native visual tree.");
}
return true;
}
/**
* Method is intended to be overridden by inheritors and used as "protected"
*/
public _removeViewFromNativeVisualTree(view: ViewDefinition) {
view._isAddedToNativeVisualTree = false;
}
public _updateLayout() {
// needed for iOS.

View File

@@ -6,7 +6,7 @@ import {
marginRightProperty, marginBottomProperty, horizontalAlignmentProperty, verticalAlignmentProperty,
rotateProperty, scaleXProperty, scaleYProperty,
translateXProperty, translateYProperty, zIndexProperty, backgroundInternalProperty,
Background, GestureTypes, GestureEventData, applyNativeSetters,
Background, GestureTypes, GestureEventData,
traceEnabled, traceWrite, traceCategories, traceNotifyEvent, Visibility, HorizontalAlignment, VerticalAlignment
} from "./view-common";
@@ -105,83 +105,11 @@ export class View extends ViewCommon {
}
}
public _addViewCore(view: ViewCommon, atIndex?: number) {
if (this._context) {
if (view._onAttached) {
view._onAttached(this._context);
}
}
super._addViewCore(view, atIndex);
}
public _removeViewCore(view: ViewCommon) {
super._removeViewCore(view);
if (view._context) {
view._onDetached();
}
}
public _onAttached(context: android.content.Context) {
if (!context) {
throw new Error("Expected valid android.content.Context instance.");
}
if (traceEnabled) {
traceWrite(`${this}._onAttached(context)`, traceCategories.VisualTreeEvents);
}
if (this._context === context) {
return;
}
if (this._context) {
this._onDetached(true);
}
this._context = context;
this._onContextChanged();
if (traceEnabled) {
traceNotifyEvent(this, "_onAttached");
}
// Notify each child for the _onAttached event
this.eachChildView((child) => {
child._onAttached(context);
if (!child._isAddedToNativeVisualTree) {
// since we have lazy loading of the android widgets, we need to add the native instances at this point.
child._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(child);
}
return true;
});
// copy all the locally cached values to the native android widget
applyNativeSetters(this);
}
public _onDetached(force?: boolean) {
if (traceEnabled) {
traceWrite(`${this}._onDetached(force)`, traceCategories.VisualTreeEvents);
}
// Detach children first
this.eachChildView((child: View) => {
if (child._isAddedToNativeVisualTree) {
this._removeViewFromNativeVisualTree(child);
}
if (child._context) {
child._onDetached(force);
}
return true;
});
// Call super for recursive detach of all children.
super._onDetached(force);
this._clearAndroidReference();
this._context = undefined;
if (traceEnabled) {
traceNotifyEvent(this, "_onDetached");
}
}
// TODO: revise this method

View File

@@ -526,10 +526,6 @@ declare module "ui/core/view" {
public getActualSize(): Size;
// Lifecycle events
_context: any /* android.content.Context */;
_childIndexToNativeChildIndex(index?: number): number;
_getNativeViewsCount(): number;
_eachLayoutView(callback: (View) => void): void;
@@ -550,17 +546,6 @@ declare module "ui/core/view" {
_gestureObservers: any;
// _isInheritedChange(): boolean;
_isAddedToNativeVisualTree: boolean;
/**
* Performs the core logic of adding a child view to the native visual tree. Returns true if the view's native representation has been successfully added, false otherwise.
*/
_addViewToNativeVisualTree(view: ViewBase, atIndex?: number): boolean;
_removeViewFromNativeVisualTree(view: ViewBase): void;
_onAttached(context: any /* android.content.Context */): void;
_onContextChanged(): void;
_onDetached(force?: boolean): void;
_createUI(): void;
_updateLayout(): void;

View File

@@ -26,10 +26,6 @@ export class View extends ViewCommon {
public _removeViewCore(view: ViewCommon) {
super._removeViewCore(view);
// TODO: Detach from the context?
if (view._onDetached) {
view._onDetached();
}
this.requestLayout();
}
@@ -355,7 +351,7 @@ export class View extends ViewCommon {
case Visibility.COLLAPSE:
this.nativeView.hidden = true;
break;
default:
default:
throw new Error(`Invalid visibility value: ${value}. Valid values are: "${Visibility.VISIBLE}", "${Visibility.HIDDEN}", "${Visibility.COLLAPSE}".`);
}
}

View File

@@ -111,6 +111,20 @@ declare module "ui/core/view-base" {
_unregisterAnimation(animation: KeyframeAnimation);
_cancelAllAnimations();
_context: any /* android.content.Context */;
_onAttached(context: any /* android.content.Context */): void;
_onContextChanged(): void;
_onDetached(force?: boolean): void;
_isAddedToNativeVisualTree: boolean;
/**
* Performs the core logic of adding a child view to the native visual tree. Returns true if the view's native representation has been successfully added, false otherwise.
*/
_addViewToNativeVisualTree(view: ViewBase, atIndex?: number): boolean;
_removeViewFromNativeVisualTree(view: ViewBase): void;
_childIndexToNativeChildIndex(index?: number): number;
/**
* @protected
* @unstable