mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-26 03:01:51 +08:00
definitions fixed
This commit is contained in:
2
ui/page/package.json
Normal file
2
ui/page/package.json
Normal file
@ -0,0 +1,2 @@
|
||||
{ "name" : "page",
|
||||
"main" : "page.js" }
|
132
ui/page/page-common.ts
Normal file
132
ui/page/page-common.ts
Normal file
@ -0,0 +1,132 @@
|
||||
import contentView = require("ui/content-view");
|
||||
import view = require("ui/core/view");
|
||||
import dts = require("ui/page");
|
||||
import frame = require("ui/frame");
|
||||
import styleScope = require("ui/styling/style-scope");
|
||||
import fs = require("file-system");
|
||||
import fileSystemAccess = require("file-system/file-system-access");
|
||||
import trace = require("trace");
|
||||
|
||||
export module knownEvents {
|
||||
export var navigatedTo = "navigatedTo";
|
||||
}
|
||||
|
||||
export class Page extends contentView.ContentView implements dts.Page {
|
||||
private _navigationContext: any;
|
||||
|
||||
private _cssApplied: boolean;
|
||||
private _styleScope: styleScope.StyleScope = new styleScope.StyleScope();
|
||||
|
||||
constructor(options?: dts.Options) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
this._applyCss();
|
||||
super.onLoaded();
|
||||
}
|
||||
|
||||
get navigationContext(): any {
|
||||
return this._navigationContext;
|
||||
}
|
||||
|
||||
get css(): string {
|
||||
if (this._styleScope) {
|
||||
return this._styleScope.css;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
set css(value: string) {
|
||||
this._styleScope.css = value;
|
||||
this._refreshCss();
|
||||
}
|
||||
|
||||
private _refreshCss(): void {
|
||||
if (this._cssApplied) {
|
||||
this._resetCssValues();
|
||||
}
|
||||
|
||||
this._cssApplied = false;
|
||||
if (this.isLoaded) {
|
||||
this._applyCss();
|
||||
}
|
||||
}
|
||||
|
||||
public addCss(cssString: string): void {
|
||||
this._styleScope.addCss(cssString);
|
||||
this._refreshCss();
|
||||
}
|
||||
|
||||
public addCssFile(cssFileName: string) {
|
||||
var cssString;
|
||||
var realCssFileName = fs.path.join(fs.knownFolders.currentApp().path, cssFileName);
|
||||
if (fs.File.exists(realCssFileName)) {
|
||||
new fileSystemAccess.FileSystemAccess().readText(realCssFileName, r => { cssString = r; });
|
||||
this.addCss(cssString);
|
||||
}
|
||||
}
|
||||
|
||||
get frame(): frame.Frame {
|
||||
return <frame.Frame>this.parent;
|
||||
}
|
||||
|
||||
public onNavigatingTo(context: any) {
|
||||
this._navigationContext = context;
|
||||
}
|
||||
|
||||
public onNavigatedTo(context: any) {
|
||||
this._navigationContext = context;
|
||||
this.notify({
|
||||
eventName: knownEvents.navigatedTo,
|
||||
object: this,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
|
||||
public onNavigatingFrom() {
|
||||
//
|
||||
}
|
||||
|
||||
public onNavigatedFrom(isBackNavigation: boolean) {
|
||||
// TODO: Should we clear navigation context here or somewhere else
|
||||
this._navigationContext = undefined;
|
||||
}
|
||||
|
||||
public _getStyleScope(): styleScope.StyleScope {
|
||||
return this._styleScope;
|
||||
}
|
||||
|
||||
private _applyCss() {
|
||||
if (this._cssApplied) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this._styleScope.ensureSelectors();
|
||||
|
||||
var scope = this._styleScope;
|
||||
var checkSelectors = (view: view.View): boolean => {
|
||||
scope.applySelectors(view);
|
||||
return true;
|
||||
}
|
||||
|
||||
checkSelectors(this);
|
||||
view.eachDescendant(this, checkSelectors);
|
||||
|
||||
this._cssApplied = true;
|
||||
} catch (e) {
|
||||
trace.write("Css styling failed: " + e, trace.categories.Style);
|
||||
}
|
||||
}
|
||||
|
||||
private _resetCssValues() {
|
||||
var resetCssValuesFunc = (view: view.View): boolean => {
|
||||
view.style._resetCssValues();
|
||||
return true;
|
||||
}
|
||||
|
||||
resetCssValuesFunc(this);
|
||||
view.eachDescendant(this, resetCssValuesFunc);
|
||||
|
||||
}
|
||||
}
|
30
ui/page/page.android.ts
Normal file
30
ui/page/page.android.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import pageCommon = require("ui/page/page-common");
|
||||
import definition = require("ui/page");
|
||||
import trace = require("trace");
|
||||
|
||||
module.exports.knownEvents = pageCommon.knownEvents;
|
||||
|
||||
export class Page extends pageCommon.Page {
|
||||
private _isBackNavigation = false;
|
||||
|
||||
constructor(options?: definition.Options) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
public _onDetached(force?: boolean) {
|
||||
var skipDetached = !force && this.frame.android.cachePagesOnNavigate && !this._isBackNavigation;
|
||||
|
||||
if (skipDetached) {
|
||||
// Do not detach the context and android reference.
|
||||
trace.write("Caching Page " + this._domId, trace.categories.NativeLifecycle);
|
||||
}
|
||||
else {
|
||||
super._onDetached();
|
||||
}
|
||||
}
|
||||
|
||||
public onNavigatedFrom(isBackNavigation: boolean) {
|
||||
this._isBackNavigation = isBackNavigation;
|
||||
super.onNavigatedFrom(isBackNavigation);
|
||||
}
|
||||
}
|
121
ui/page/page.d.ts
vendored
Normal file
121
ui/page/page.d.ts
vendored
Normal file
@ -0,0 +1,121 @@
|
||||
/**
|
||||
* Contains the Page class, which represents a logical unit for navigation inside a Frame.
|
||||
*/
|
||||
declare module "ui/page" {
|
||||
import observable = require("data/observable");
|
||||
import view = require("ui/core/view");
|
||||
import contentView = require("ui/content-view");
|
||||
import frame = require("ui/frame");
|
||||
//@private
|
||||
import styleScope = require("ui/styling/style-scope");
|
||||
//@endprivate
|
||||
|
||||
/**
|
||||
* Defines the data for the Page.navigatedTo event.
|
||||
*/
|
||||
export interface NavigatedData extends observable.EventData {
|
||||
/**
|
||||
* The navigation context (optional, may be undefined) passed to the Page.onNavigatedTo method.
|
||||
*/
|
||||
context: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates the known event names for the page module.
|
||||
*/
|
||||
export module knownEvents {
|
||||
/**
|
||||
* The event raised when the Page.onNavigatedTo method is called.
|
||||
*/
|
||||
export var navigatedTo: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a logical unit for navigation (inside Frame).
|
||||
*/
|
||||
export class Page extends contentView.ContentView {
|
||||
|
||||
constructor(options?: Options)
|
||||
|
||||
/**
|
||||
* A valid css string which will be applied for all nested UI components (based on css rules).
|
||||
*/
|
||||
css: string;
|
||||
|
||||
/**
|
||||
* Adds a new values to current css.
|
||||
* @param cssString - A valid css which will be added to current css.
|
||||
*/
|
||||
addCss(cssString: string): void;
|
||||
|
||||
/**
|
||||
* Adds the content of the file to the current css.
|
||||
* @param cssFileName - A valid file name (from the application root) which contains a valid css.
|
||||
*/
|
||||
addCssFile(cssFileName: string): void;
|
||||
|
||||
/**
|
||||
* A property that is used to pass a data from another page (while navigate to).
|
||||
*/
|
||||
navigationContext: any;
|
||||
|
||||
/**
|
||||
* Gets the Frame object controlling this instance.
|
||||
*/
|
||||
frame: frame.Frame;
|
||||
|
||||
/**
|
||||
* A method called before navigating to the page.
|
||||
* @param context - The data passed to the page through the NavigationEntry.context property.
|
||||
*/
|
||||
onNavigatingTo(context: any): void;
|
||||
|
||||
/**
|
||||
* A method called after navigated to the page.
|
||||
* @param context - The data passed to the page through the NavigationEntry.context property.
|
||||
*/
|
||||
onNavigatedTo(context: any): void;
|
||||
|
||||
/**
|
||||
* A method called before navigating from the page.
|
||||
*/
|
||||
onNavigatingFrom(): void;
|
||||
|
||||
/**
|
||||
* A method called after navigated from the page.
|
||||
* @param isBackNavigation - True if the Page is being navigated from using the Frame.goBack() method, false otherwise.
|
||||
*/
|
||||
onNavigatedFrom(isBackNavigation: boolean): void;
|
||||
|
||||
//@private
|
||||
_getStyleScope(): styleScope.StyleScope
|
||||
//@endprivate
|
||||
|
||||
on(event: string, callback: (data: observable.EventData) => void);
|
||||
|
||||
/**
|
||||
* Raised when navigation to the page is finished.
|
||||
*/
|
||||
on(event: "navigatedTo", callback: (args: NavigatedData) => void);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a set with most common option used to create a page instance.
|
||||
*/
|
||||
export interface Options extends view.Options {
|
||||
/**
|
||||
* Gets or sets the page module.
|
||||
*/
|
||||
module?: any;
|
||||
|
||||
/**
|
||||
* Gets or sets the page module file name.
|
||||
*/
|
||||
filename?: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the page module exports.
|
||||
*/
|
||||
exports?: any;
|
||||
}
|
||||
}
|
78
ui/page/page.ios.ts
Normal file
78
ui/page/page.ios.ts
Normal file
@ -0,0 +1,78 @@
|
||||
import pageCommon = require("ui/page/page-common");
|
||||
import definition = require("ui/page");
|
||||
import viewModule = require("ui/core/view");
|
||||
import trace = require("trace");
|
||||
|
||||
module.exports.knownEvents = pageCommon.knownEvents;
|
||||
|
||||
class UIViewControllerImpl extends UIViewController {
|
||||
static new(): UIViewControllerImpl {
|
||||
return <UIViewControllerImpl>super.new();
|
||||
}
|
||||
|
||||
private _owner: Page;
|
||||
|
||||
public initWithOwner(owner: Page): UIViewControllerImpl {
|
||||
this._owner = owner;
|
||||
this.automaticallyAdjustsScrollViewInsets = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
public viewDidLoad() {
|
||||
this.view.autoresizesSubviews = false;
|
||||
this.view.autoresizingMask = UIViewAutoresizing.UIViewAutoresizingNone;
|
||||
}
|
||||
|
||||
public viewDidLayoutSubviews() {
|
||||
trace.write(this._owner + " viewDidLayoutSubviews, isLoaded = " + this._owner.isLoaded, trace.categories.ViewHierarchy);
|
||||
this._owner._updateLayout();
|
||||
}
|
||||
}
|
||||
|
||||
/* tslint:enable */
|
||||
export class Page extends pageCommon.Page {
|
||||
private _ios: UIViewController;
|
||||
|
||||
constructor(options?: definition.Options) {
|
||||
super(options);
|
||||
this._ios = UIViewControllerImpl.new().initWithOwner(this);
|
||||
}
|
||||
|
||||
public _onContentChanged(oldView: viewModule.View, newView: viewModule.View) {
|
||||
super._onContentChanged(oldView, newView);
|
||||
this._removeNativeView(oldView);
|
||||
this._addNativeView(newView);
|
||||
}
|
||||
|
||||
private _addNativeView(view: viewModule.View) {
|
||||
if (view) {
|
||||
trace.write("Native: Adding " + view + " to " + this, trace.categories.ViewHierarchy);
|
||||
if (view.ios instanceof UIView) {
|
||||
this._ios.view.addSubview(view.ios);
|
||||
} else if (view.ios instanceof UIViewController) {
|
||||
this._ios.addChildViewController(view.ios);
|
||||
this._ios.view.addSubview(view.ios.view);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _removeNativeView(view: viewModule.View) {
|
||||
if (view) {
|
||||
trace.write("Native: Removing " + view + " from " + this, trace.categories.ViewHierarchy);
|
||||
if (view.ios instanceof UIView) {
|
||||
(<UIView>view.ios).removeFromSuperview();
|
||||
} else if (view.ios instanceof UIViewController) {
|
||||
(<UIViewController>view.ios).removeFromParentViewController();
|
||||
(<UIViewController>view.ios).view.removeFromSuperview();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get ios(): UIViewController {
|
||||
return this._ios;
|
||||
}
|
||||
|
||||
get _nativeView(): any {
|
||||
return this.ios.view;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user