mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
file resolver added + lint errors fixed
This commit is contained in:
@ -8,6 +8,8 @@ import templateBuilderDef = require("ui/builder/template-builder");
|
|||||||
import platform = require("platform");
|
import platform = require("platform");
|
||||||
import definition = require("ui/builder");
|
import definition = require("ui/builder");
|
||||||
import page = require("ui/page");
|
import page = require("ui/page");
|
||||||
|
import fileResolverModule = require("file-system/file-name-resolver");
|
||||||
|
import trace = require("trace");
|
||||||
|
|
||||||
var KNOWNCOLLECTIONS = "knownCollections";
|
var KNOWNCOLLECTIONS = "knownCollections";
|
||||||
|
|
||||||
@ -185,22 +187,25 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr
|
|||||||
var result: componentBuilder.ComponentModule;
|
var result: componentBuilder.ComponentModule;
|
||||||
componentPath = componentPath.replace("~/", "");
|
componentPath = componentPath.replace("~/", "");
|
||||||
|
|
||||||
var fileName = componentPath;
|
var fullComponentPathFilePathWithoutExt = componentPath;
|
||||||
|
|
||||||
if (!fs.File.exists(fileName)) {
|
if (!fs.File.exists(componentPath)) {
|
||||||
fileName = fs.path.join(fs.knownFolders.currentApp().path, componentPath, componentName) + ".xml";
|
fullComponentPathFilePathWithoutExt = fs.path.join(fs.knownFolders.currentApp().path, componentPath, componentName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs.File.exists(fileName)) {
|
var xmlFilePath = resolveFilePath(fullComponentPathFilePathWithoutExt, "xml");
|
||||||
|
|
||||||
|
if (xmlFilePath) {
|
||||||
// Custom components with XML
|
// Custom components with XML
|
||||||
var jsPath = fileName.replace(".xml", ".js");
|
var jsFilePath = resolveFilePath(fullComponentPathFilePathWithoutExt, "js");
|
||||||
|
|
||||||
var subExports;
|
var subExports;
|
||||||
if (fs.File.exists(jsPath)) {
|
if (jsFilePath) {
|
||||||
// Custom components with XML and code
|
// Custom components with XML and code
|
||||||
subExports = require(jsPath.replace(".js", ""))
|
subExports = require(jsFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
result = loadInternal(fileName, subExports);
|
result = loadInternal(xmlFilePath, subExports);
|
||||||
|
|
||||||
// Attributes will be transfered to the custom component
|
// Attributes will be transfered to the custom component
|
||||||
if (types.isDefined(result) && types.isDefined(result.component) && types.isDefined(attributes)) {
|
if (types.isDefined(result) && types.isDefined(result.component) && types.isDefined(attributes)) {
|
||||||
@ -215,32 +220,45 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add component CSS file if exists.
|
// Add component CSS file if exists.
|
||||||
var cssFileName = fileName.replace(".xml", ".css");
|
var cssFilePath = resolveFilePath(fullComponentPathFilePathWithoutExt, "css");
|
||||||
if (fs.File.exists(cssFileName) && parentPage) {
|
if (cssFilePath) {
|
||||||
if (parentPage) {
|
if (parentPage) {
|
||||||
parentPage.addCssFile(cssFileName);
|
parentPage.addCssFile(cssFilePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("CSS file found but no page specified. Please specify page in the options!");
|
trace.write("CSS file found but no page specified. Please specify page in the options!", trace.categories.Error, trace.messageType.error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fileNameResolver: fileResolverModule.FileNameResolver;
|
||||||
|
function resolveFilePath(path, ext): string {
|
||||||
|
if (!fileNameResolver) {
|
||||||
|
fileNameResolver = new fileResolverModule.FileNameResolver({
|
||||||
|
width: platform.screen.mainScreen.widthDIPs,
|
||||||
|
height: platform.screen.mainScreen.heightDIPs,
|
||||||
|
os: platform.device.os,
|
||||||
|
deviceType: platform.device.deviceType
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return fileNameResolver.resolveFileName(path, ext);
|
||||||
|
}
|
||||||
|
|
||||||
export function load(pathOrOptions: string | definition.LoadOptions, context?: any): view.View {
|
export function load(pathOrOptions: string | definition.LoadOptions, context?: any): view.View {
|
||||||
var viewToReturn: view.View;
|
var viewToReturn: view.View;
|
||||||
var componentModule: componentBuilder.ComponentModule;
|
var componentModule: componentBuilder.ComponentModule;
|
||||||
|
|
||||||
if (!context) {
|
if (!context) {
|
||||||
if (!types.isString(pathOrOptions)) {
|
if (!types.isString(pathOrOptions)) {
|
||||||
var options = <definition.LoadOptions>pathOrOptions;
|
let options = <definition.LoadOptions>pathOrOptions;
|
||||||
componentModule = loadCustomComponent(options.path, options.name, undefined, options.exports, options.page);
|
componentModule = loadCustomComponent(options.path, options.name, undefined, options.exports, options.page);
|
||||||
} else {
|
} else {
|
||||||
var path = <string>pathOrOptions;
|
let path = <string>pathOrOptions;
|
||||||
componentModule = loadInternal(path);
|
componentModule = loadInternal(path);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var path = <string>pathOrOptions;
|
let path = <string>pathOrOptions;
|
||||||
componentModule = loadInternal(path, context);
|
componentModule = loadInternal(path, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user