mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactoring circular imports
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
import { debug, ScopeError, SourceError, Source } from "utils/debug";
|
||||
// Definitions.
|
||||
import { LoadOptions } from "ui/builder";
|
||||
import { View, ViewBase, Template, KeyedTemplate } from "ui/core/view";
|
||||
|
||||
// Types.
|
||||
import { debug, ScopeError, SourceError, Source } from "utils/debug";
|
||||
import * as xml from "xml";
|
||||
import { View, Template, KeyedTemplate } from "ui/core/view";
|
||||
import { File, path, knownFolders } from "file-system";
|
||||
import { isString, isFunction, isDefined } from "utils/types";
|
||||
import { isString, isDefined } from "utils/types";
|
||||
import { ComponentModule, setPropertyValue, getComponentModule } from "ui/builder/component-builder";
|
||||
import { platformNames, device } from "platform";
|
||||
import { LoadOptions } from "ui/builder";
|
||||
import { Page } from "ui/page";
|
||||
import { resolveFileName } from "file-system/file-name-resolver";
|
||||
import * as traceModule from "trace";
|
||||
|
||||
const ios = platformNames.ios.toLowerCase();
|
||||
const android = platformNames.android.toLowerCase();
|
||||
|
||||
const defaultNameSpaceMatcher = /tns\.xsd$/i;
|
||||
|
||||
var trace: typeof traceModule;
|
||||
@@ -20,22 +25,15 @@ function ensureTrace() {
|
||||
}
|
||||
|
||||
export function parse(value: string | Template, context: any): View {
|
||||
if (isString(value)) {
|
||||
var viewToReturn: View;
|
||||
|
||||
if (context instanceof View) {
|
||||
context = getExports(context);
|
||||
}
|
||||
|
||||
var componentModule = parseInternal(<string>value, context);
|
||||
|
||||
if (componentModule) {
|
||||
viewToReturn = componentModule.component;
|
||||
}
|
||||
|
||||
return viewToReturn;
|
||||
} else if (isFunction(value)) {
|
||||
if (typeof value === "function") {
|
||||
return (<Template>value)();
|
||||
} else {
|
||||
const exports = context ? getExports(context) : undefined;
|
||||
const componentModule = parseInternal(value, exports);
|
||||
if (componentModule) {
|
||||
return componentModule.component;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +60,7 @@ function parseInternal(value: string, context: any, uri?: string, moduleNamePath
|
||||
return ui.rootComponentModule;
|
||||
}
|
||||
|
||||
function loadCustomComponent(componentPath: string, componentName?: string, attributes?: Object, context?: Object, parentPage?: Page): ComponentModule {
|
||||
function loadCustomComponent(componentPath: string, componentName?: string, attributes?: Object, context?: Object, parentPage?: View): ComponentModule {
|
||||
if (!parentPage && context) {
|
||||
// Read the parent page that was passed down below
|
||||
// https://github.com/NativeScript/NativeScript/issues/1639
|
||||
@@ -119,8 +117,8 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr
|
||||
// Add component CSS file if exists.
|
||||
var cssFilePath = resolveFileName(fullComponentPathFilePathWithoutExt, "css");
|
||||
if (cssFilePath) {
|
||||
if (parentPage) {
|
||||
parentPage.addCssFile(cssFilePath);
|
||||
if (parentPage && typeof (<any>parentPage).addCssFile === "function") {
|
||||
(<any>parentPage).addCssFile(cssFilePath);
|
||||
} else {
|
||||
ensureTrace();
|
||||
|
||||
@@ -155,7 +153,7 @@ export function load(pathOrOptions: string | LoadOptions, context?: any): View {
|
||||
return viewToReturn;
|
||||
}
|
||||
|
||||
export function loadPage(moduleNamePath: string, fileName: string, context?: any): Page {
|
||||
export function loadPage(moduleNamePath: string, fileName: string, context?: any): View {
|
||||
var componentModule: ComponentModule;
|
||||
|
||||
// Check if the XML file exists.
|
||||
@@ -173,7 +171,7 @@ export function loadPage(moduleNamePath: string, fileName: string, context?: any
|
||||
(<any>componentModule.component).exports = context;
|
||||
}
|
||||
|
||||
return (<Page>componentModule.component);
|
||||
return componentModule.component;
|
||||
}
|
||||
|
||||
function loadInternal(fileName: string, context?: any): ComponentModule {
|
||||
@@ -197,14 +195,20 @@ function loadInternal(fileName: string, context?: any): ComponentModule {
|
||||
return componentModule;
|
||||
}
|
||||
|
||||
function getExports(instance: View): any {
|
||||
var parent = instance.parent;
|
||||
function getExports(instance: ViewBase): any {
|
||||
const isView = !!instance._domId;
|
||||
if (!isView) {
|
||||
return (<any>instance).exports || instance;
|
||||
}
|
||||
|
||||
while (parent && (<any>parent).exports === undefined) {
|
||||
let exportObject = (<any>instance).exports;
|
||||
let parent = instance.parent;
|
||||
while (exportObject === undefined && parent) {
|
||||
exportObject = (<any>parent).exports;
|
||||
parent = parent.parent;
|
||||
}
|
||||
|
||||
return parent ? (<any>parent).exports : undefined;
|
||||
return exportObject;
|
||||
}
|
||||
|
||||
namespace xml2ui {
|
||||
@@ -316,8 +320,12 @@ namespace xml2ui {
|
||||
}
|
||||
|
||||
private static isPlatform(value: string): boolean {
|
||||
return value && (value.toLowerCase() === platformNames.android.toLowerCase()
|
||||
|| value.toLowerCase() === platformNames.ios.toLowerCase());
|
||||
if (value) {
|
||||
const toLower = value.toLowerCase();
|
||||
return toLower === android || toLower === ios;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static isCurentPlatform(value: string): boolean {
|
||||
@@ -514,7 +522,7 @@ namespace xml2ui {
|
||||
|
||||
private context: any;
|
||||
|
||||
private currentPage: Page;
|
||||
private currentRootView: View;
|
||||
private parents = new Array<ComponentModule>();
|
||||
private complexProperties = new Array<ComponentParser.ComplexProperty>();
|
||||
|
||||
@@ -575,7 +583,7 @@ namespace xml2ui {
|
||||
|
||||
if (args.prefix && args.namespace) {
|
||||
// Custom components
|
||||
componentModule = loadCustomComponent(args.namespace, args.elementName, args.attributes, this.context, this.currentPage);
|
||||
componentModule = loadCustomComponent(args.namespace, args.elementName, args.attributes, this.context, this.currentRootView);
|
||||
} else {
|
||||
// Default components
|
||||
let namespace = args.namespace;
|
||||
@@ -599,11 +607,11 @@ namespace xml2ui {
|
||||
// Set root component.
|
||||
this.rootComponentModule = componentModule;
|
||||
|
||||
if (this.rootComponentModule && this.rootComponentModule.component instanceof Page) {
|
||||
this.currentPage = <Page>this.rootComponentModule.component;
|
||||
if (this.rootComponentModule) {
|
||||
this.currentRootView = this.rootComponentModule.component;
|
||||
|
||||
if ((<any>this.currentPage).exports) {
|
||||
this.context = (<any>this.currentPage).exports;
|
||||
if ((<any>this.currentRootView).exports) {
|
||||
this.context = (<any>this.currentRootView).exports;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { isString, isDefined, isFunction } from "utils/types";
|
||||
import { Page } from "ui/page";
|
||||
import { View, isEventOrGesture } from "ui/core/view";
|
||||
// Deifinitions.
|
||||
import { ComponentModule } from "ui/builder/component-builder";
|
||||
import { View } from "ui/core/view";
|
||||
|
||||
// Types.
|
||||
import { isEventOrGesture } from "ui/core/bindable";
|
||||
import { File, path, knownFolders } from "file-system";
|
||||
import { getBindingOptions, bindingConstants } from "./binding-builder";
|
||||
import { resolveFileName } from "file-system/file-name-resolver";
|
||||
import * as debugModule from "utils/debug";
|
||||
import * as platformModule from "platform";
|
||||
import * as platform from "platform";
|
||||
|
||||
const UI_PATH = "ui/";
|
||||
const MODULES = {
|
||||
@@ -22,13 +24,6 @@ const CODEFILE = "codeFile";
|
||||
const CSSFILE = "cssFile";
|
||||
const IMPORT = "import";
|
||||
|
||||
let platform: typeof platformModule;
|
||||
function ensurePlatform() {
|
||||
if (!platform) {
|
||||
platform = require("platform");
|
||||
}
|
||||
}
|
||||
|
||||
export function getComponentModule(elementName: string, namespace: string, attributes: Object, exports: Object, moduleNamePath?: string): ComponentModule {
|
||||
var instance: View;
|
||||
var instanceModule: Object;
|
||||
@@ -88,7 +83,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
(<any>instance).exports = exports;
|
||||
}
|
||||
|
||||
if (instance instanceof Page) {
|
||||
// if (instance instanceof Page) {
|
||||
if (attributes[CODEFILE]) {
|
||||
let codeFilePath = attributes[CODEFILE].trim();
|
||||
if (codeFilePath.indexOf("~/") === 0) {
|
||||
@@ -104,26 +99,26 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
}
|
||||
}
|
||||
|
||||
if (attributes[CSSFILE]) {
|
||||
if (attributes[CSSFILE] && typeof (<any>instance).addCssFile === "function") {
|
||||
let cssFilePath = attributes[CSSFILE].trim();
|
||||
if (cssFilePath.indexOf("~/") === 0) {
|
||||
cssFilePath = path.join(knownFolders.currentApp().path, cssFilePath.replace("~/", ""));
|
||||
}
|
||||
if (File.exists(cssFilePath)) {
|
||||
instance.addCssFile(cssFilePath);
|
||||
(<any>instance).addCssFile(cssFilePath);
|
||||
cssApplied = true;
|
||||
} else {
|
||||
throw new Error(`Css file with path "${cssFilePath}" cannot be found!`);
|
||||
}
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
if (instance instanceof Page) {
|
||||
if (typeof (<any>instance).addCssFile === "function") {//instance instanceof Page) {
|
||||
if (moduleNamePath && !cssApplied) {
|
||||
let cssFilePath = resolveFileName(moduleNamePath, "css");
|
||||
if (cssFilePath) {
|
||||
instance.addCssFile(cssFilePath);
|
||||
(<any>instance).addCssFile(cssFilePath);
|
||||
cssApplied = true;
|
||||
}
|
||||
}
|
||||
@@ -131,7 +126,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
if (!cssApplied) {
|
||||
// Called only to apply application css.
|
||||
// If we have page css (through file or cssAttribute) we have appCss applied.
|
||||
instance._refreshCss();
|
||||
(<any>instance)._refreshCss();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,8 +138,6 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
if (attr.indexOf(":") !== -1) {
|
||||
var platformName = attr.split(":")[0].trim();
|
||||
|
||||
ensurePlatform();
|
||||
|
||||
if (platformName.toLowerCase() === platform.device.os.toLowerCase()) {
|
||||
attr = attr.split(":")[1].trim();
|
||||
} else {
|
||||
@@ -158,12 +151,12 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
const subPropName = properties[properties.length - 1];
|
||||
|
||||
for (let i = 0; i < properties.length - 1; i++) {
|
||||
if (isDefined(subObj)) {
|
||||
if (subObj !== undefined && subObj !== null) {
|
||||
subObj = subObj[properties[i]];
|
||||
}
|
||||
}
|
||||
|
||||
if (isDefined(subObj)) {
|
||||
if (subObj !== undefined && subObj !== null) {
|
||||
setPropertyValue(subObj, instanceModule, exports, subPropName, attrValue);
|
||||
}
|
||||
} else {
|
||||
@@ -193,11 +186,11 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
|
||||
var handler = exports && exports[propertyValue];
|
||||
|
||||
// Check if the handler is function and add it to the instance for specified event name.
|
||||
if (isFunction(handler)) {
|
||||
if (typeof handler === "function") {
|
||||
instance.on(propertyName, handler);
|
||||
}
|
||||
}
|
||||
else if (isKnownFunction(propertyName, instance) && isFunction(exports && exports[propertyValue])) {
|
||||
else if (isKnownFunction(propertyName, instance) && exports && typeof exports[propertyValue] === "function") {
|
||||
instance[propertyName] = exports[propertyValue];
|
||||
}
|
||||
else {
|
||||
@@ -218,7 +211,7 @@ function getBindingExpressionFromAttribute(value: string): string {
|
||||
function isBinding(value: any): boolean {
|
||||
var isBinding;
|
||||
|
||||
if (isString(value)) {
|
||||
if (typeof value === "string") {
|
||||
var str = value.trim();
|
||||
isBinding = str.indexOf("{{") === 0 && str.lastIndexOf("}}") === str.length - 2;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user