From f999c54e0d06f511c9e54c93288d3b4c1ebd7c40 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Tue, 7 Jul 2015 11:31:55 +0300 Subject: [PATCH 1/4] better support for custom components loading --- .../xml-declaration/xml-declaration-tests.ts | 19 +++ ui/builder/builder.d.ts | 7 + ui/builder/builder.ts | 121 +++++++++++------- 3 files changed, 101 insertions(+), 46 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 73aa541c7..83d2ee89a 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -40,6 +40,25 @@ export function test_load_ShouldNotCrashWithoutExports() { TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";"); }; +export function test_loadWithOptionsNoXML() { + var v = builder.load({ + fileName: "~/xml-declaration/mymodule", + componentName: "MyControl", + exports: exports + }); + + TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";"); +}; + +export function test_loadWithOptionsWithXML() { + var v = builder.load({ + fileName: "~/xml-declaration/mymodulewithxml", + componentName: "MyControl", + exports: exports + }); + TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";"); +}; + export function test_parse_ShouldNotCrashWithoutExports() { var fileAccess = new fileSystemAccess.FileSystemAccess(); diff --git a/ui/builder/builder.d.ts b/ui/builder/builder.d.ts index 55b07657d..128ccaeed 100644 --- a/ui/builder/builder.d.ts +++ b/ui/builder/builder.d.ts @@ -3,5 +3,12 @@ declare module "ui/builder" { import view = require("ui/core/view"); export function load(fileName: string, exports?: any): view.View; + export function load(options: LoadOptions): view.View; export function parse(value: string, exports?: any): view.View; + + export interface LoadOptions { + fileName: string; + componentName: string; + exports: any; + } } diff --git a/ui/builder/builder.ts b/ui/builder/builder.ts index c0de226fd..a9532ed1f 100644 --- a/ui/builder/builder.ts +++ b/ui/builder/builder.ts @@ -6,6 +6,7 @@ import types = require("utils/types"); import componentBuilder = require("ui/builder/component-builder"); import templateBuilderDef = require("ui/builder/template-builder"); import platform = require("platform"); +import definition = require("ui/builder"); var KNOWNCOLLECTIONS = "knownCollections"; @@ -18,14 +19,14 @@ function isCurentPlatform(value: string): boolean { return value && value.toLowerCase() === platform.device.os.toLowerCase(); } -export function parse(value: string, exports: any): view.View { +export function parse(value: string, context: any): view.View { var viewToReturn: view.View; - if (exports instanceof view.View) { - exports = getExports(exports); + if (context instanceof view.View) { + context = getExports(context); } - var componentModule = parseInternal(value, exports); + var componentModule = parseInternal(value, context); if (componentModule) { viewToReturn = componentModule.component; @@ -34,7 +35,7 @@ export function parse(value: string, exports: any): view.View { return viewToReturn; } -function parseInternal(value: string, exports: any): componentBuilder.ComponentModule { +function parseInternal(value: string, context: any): componentBuilder.ComponentModule { var rootComponentModule: componentBuilder.ComponentModule; // Temporary collection used for parent scope. var parents = new Array(); @@ -112,40 +113,12 @@ function parseInternal(value: string, exports: any): componentBuilder.ComponentM var componentModule: componentBuilder.ComponentModule; - if (args.prefix) { + if (args.prefix && args.namespace) { // Custom components - - var ns = args.namespace; - - if (ns) { - var xmlPath = fs.path.join(fs.knownFolders.currentApp().path, ns, args.elementName) + ".xml"; - - if (fs.File.exists(xmlPath)) { - // Custom components with XML - var jsPath = xmlPath.replace(".xml", ".js"); - var subExports; - if (fs.File.exists(jsPath)) { - // Custom components with XML and code - subExports = require(jsPath.replace(".js", "")) - } - - componentModule = loadInternal(xmlPath, subExports); - - // Attributes will be transfered to the custom component - if (types.isDefined(componentModule) && types.isDefined(componentModule.component)) { - var attr: string; - for (attr in args.attributes) { - componentBuilder.setPropertyValue(componentModule.component, subExports, exports, attr, args.attributes[attr]); - } - } - } else { - // Custom components without XML - componentModule = componentBuilder.getComponentModule(args.elementName, ns, args.attributes, exports); - } - } + componentModule = loadCustomComponent(args.namespace, args.elementName, args.attributes, context); } else { // Default components - componentModule = componentBuilder.getComponentModule(args.elementName, ns, args.attributes, exports); + componentModule = componentBuilder.getComponentModule(args.elementName, args.namespace, args.attributes, context); } if (componentModule) { @@ -190,7 +163,7 @@ function parseInternal(value: string, exports: any): componentBuilder.ComponentM } } - },(e) => { + }, (e) => { throw new Error("XML parse error: " + e.message); }, true); @@ -202,9 +175,65 @@ function parseInternal(value: string, exports: any): componentBuilder.ComponentM return rootComponentModule; } -export function load(fileName: string, exports: any): view.View { +function loadCustomComponent(componentPath: string, componentName?: string, attributes?: Object, context?: Object): componentBuilder.ComponentModule { + var result: componentBuilder.ComponentModule; + componentPath = componentPath.replace("~/", ""); + + var fileName = componentPath; + console.log("BEFORE: " + componentPath) + + if (!fs.File.exists(fileName)) { + fileName = fs.path.join(fs.knownFolders.currentApp().path, componentPath, componentName) + ".xml"; + } + + console.log("AFTER: " + fileName) + + if (fs.File.exists(fileName)) { + + console.log("XML: " + fileName) + + // Custom components with XML + var jsPath = fileName.replace(".xml", ".js"); + var subExports; + if (fs.File.exists(jsPath)) { + console.log("JS: " + fileName) + + // Custom components with XML and code + subExports = require(jsPath.replace(".js", "")) + } + + result = loadInternal(fileName, subExports); + + // Attributes will be transfered to the custom component + if (types.isDefined(result) && types.isDefined(result.component) && types.isDefined(attributes)) { + var attr: string; + for (attr in attributes) { + componentBuilder.setPropertyValue(result.component, subExports, context, attr, attributes[attr]); + } + } + } else { + // Custom components without XML + console.log("NO XML: " + componentPath) + result = componentBuilder.getComponentModule(componentName, componentPath, attributes, context); + } + + return result; +} + +export function load(arg: any): view.View { var viewToReturn: view.View; - var componentModule = loadInternal(fileName, exports); + var componentModule: componentBuilder.ComponentModule; + + if (arguments.length === 1) { + if (!types.isString(arguments[0])) { + var options = arguments[0]; + componentModule = loadCustomComponent(options.fileName, options.componentName, undefined, options.exports); + } else { + componentModule = loadInternal(arguments[0]); + } + } else { + componentModule = loadInternal(arguments[0], arguments[1]); + } if (componentModule) { viewToReturn = componentModule.component; @@ -213,25 +242,25 @@ export function load(fileName: string, exports: any): view.View { return viewToReturn; } -function loadInternal(fileName: string, exports: any): componentBuilder.ComponentModule { +function loadInternal(fileName: string, context?: any): componentBuilder.ComponentModule { var componentModule: componentBuilder.ComponentModule; // Check if the XML file exists. - if (fileName && fs.File.exists(fileName)) { + if (fs.File.exists(fileName)) { var fileAccess = new file_access_module.FileSystemAccess(); // Read the XML file. fileAccess.readText(fileName, result => { - componentModule = parseInternal(result, exports); - },(e) => { + componentModule = parseInternal(result, context); + }, (e) => { throw new Error("Error loading file " + fileName + " :" + e.message); }); } if (componentModule && componentModule.component) { // Save exports to root component (will be used for templates). - (componentModule.component).exports = exports; + (componentModule.component).exports = context; } return componentModule; @@ -252,8 +281,8 @@ function getComplexProperty(fullName: string): string { return name; } -function isKnownCollection(name: string, exports: any): boolean { - return KNOWNCOLLECTIONS in exports && exports[KNOWNCOLLECTIONS] && name in exports[KNOWNCOLLECTIONS]; +function isKnownCollection(name: string, context: any): boolean { + return KNOWNCOLLECTIONS in context && context[KNOWNCOLLECTIONS] && name in context[KNOWNCOLLECTIONS]; } function addToComplexProperty(parent: componentBuilder.ComponentModule, complexProperty, elementModule: componentBuilder.ComponentModule) { From 3aa8c049299cf3fc9730ccc6a556c05a1d47abe1 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Tue, 7 Jul 2015 15:16:06 +0300 Subject: [PATCH 2/4] two more tests added --- .../xml-declaration/xml-declaration-tests.ts | 20 ++++++++++++++++++- ui/builder/builder.d.ts | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 83d2ee89a..027c331e0 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -59,6 +59,24 @@ export function test_loadWithOptionsWithXML() { TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";"); }; +export function test_loadWithOptionsFromTNS() { + var v = builder.load({ + fileName: "ui/label", + componentName: "Label" + }); + + TKUnit.assert(v instanceof labelModule.Label, "Expected result: Label; Actual result: " + v + ";"); +}; + +export function test_loadWithOptionsFromTNSPath() { + var v = builder.load({ + fileName: "tns_modules/ui/label", + componentName: "Label" + }); + + TKUnit.assert(v instanceof labelModule.Label, "Expected result: Label; Actual result: " + v + ";"); +}; + export function test_parse_ShouldNotCrashWithoutExports() { var fileAccess = new fileSystemAccess.FileSystemAccess(); @@ -178,7 +196,7 @@ export function test_parse_ThrowErrorWhenNestingPlatforms() { } catch (ex) { e = ex; } - + TKUnit.assert(e, "Expected result: Error; Actual result: " + e); }; diff --git a/ui/builder/builder.d.ts b/ui/builder/builder.d.ts index 128ccaeed..9e91c0b77 100644 --- a/ui/builder/builder.d.ts +++ b/ui/builder/builder.d.ts @@ -9,6 +9,6 @@ declare module "ui/builder" { export interface LoadOptions { fileName: string; componentName: string; - exports: any; + exports?: any; } } From 09d289131e28375be1be629e8e999600f3ce5302 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Tue, 7 Jul 2015 15:36:20 +0300 Subject: [PATCH 3/4] fileName change to path and componentName changed to name --- .../xml-declaration/xml-declaration-tests.ts | 16 ++++++++-------- ui/builder/builder.d.ts | 4 ++-- ui/builder/builder.ts | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 027c331e0..5fbc4de33 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -42,8 +42,8 @@ export function test_load_ShouldNotCrashWithoutExports() { export function test_loadWithOptionsNoXML() { var v = builder.load({ - fileName: "~/xml-declaration/mymodule", - componentName: "MyControl", + path: "~/xml-declaration/mymodule", + name: "MyControl", exports: exports }); @@ -52,8 +52,8 @@ export function test_loadWithOptionsNoXML() { export function test_loadWithOptionsWithXML() { var v = builder.load({ - fileName: "~/xml-declaration/mymodulewithxml", - componentName: "MyControl", + path: "~/xml-declaration/mymodulewithxml", + name: "MyControl", exports: exports }); TKUnit.assert(v instanceof view.View, "Expected result: View; Actual result: " + v + ";"); @@ -61,8 +61,8 @@ export function test_loadWithOptionsWithXML() { export function test_loadWithOptionsFromTNS() { var v = builder.load({ - fileName: "ui/label", - componentName: "Label" + path: "ui/label", + name: "Label" }); TKUnit.assert(v instanceof labelModule.Label, "Expected result: Label; Actual result: " + v + ";"); @@ -70,8 +70,8 @@ export function test_loadWithOptionsFromTNS() { export function test_loadWithOptionsFromTNSPath() { var v = builder.load({ - fileName: "tns_modules/ui/label", - componentName: "Label" + path: "tns_modules/ui/label", + name: "Label" }); TKUnit.assert(v instanceof labelModule.Label, "Expected result: Label; Actual result: " + v + ";"); diff --git a/ui/builder/builder.d.ts b/ui/builder/builder.d.ts index 9e91c0b77..9834658f5 100644 --- a/ui/builder/builder.d.ts +++ b/ui/builder/builder.d.ts @@ -7,8 +7,8 @@ declare module "ui/builder" { export function parse(value: string, exports?: any): view.View; export interface LoadOptions { - fileName: string; - componentName: string; + path: string; + name: string; exports?: any; } } diff --git a/ui/builder/builder.ts b/ui/builder/builder.ts index a9532ed1f..93d639d4f 100644 --- a/ui/builder/builder.ts +++ b/ui/builder/builder.ts @@ -227,7 +227,7 @@ export function load(arg: any): view.View { if (arguments.length === 1) { if (!types.isString(arguments[0])) { var options = arguments[0]; - componentModule = loadCustomComponent(options.fileName, options.componentName, undefined, options.exports); + componentModule = loadCustomComponent(options.path, options.name, undefined, options.exports); } else { componentModule = loadInternal(arguments[0]); } From f74d633fc6dfbd3d8d5db9911677ef6983f6dcb0 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Tue, 7 Jul 2015 16:43:34 +0300 Subject: [PATCH 4/4] console.log removed --- ui/builder/builder.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ui/builder/builder.ts b/ui/builder/builder.ts index 93d639d4f..1942552e7 100644 --- a/ui/builder/builder.ts +++ b/ui/builder/builder.ts @@ -180,24 +180,16 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr componentPath = componentPath.replace("~/", ""); var fileName = componentPath; - console.log("BEFORE: " + componentPath) if (!fs.File.exists(fileName)) { fileName = fs.path.join(fs.knownFolders.currentApp().path, componentPath, componentName) + ".xml"; } - console.log("AFTER: " + fileName) - if (fs.File.exists(fileName)) { - - console.log("XML: " + fileName) - // Custom components with XML var jsPath = fileName.replace(".xml", ".js"); var subExports; if (fs.File.exists(jsPath)) { - console.log("JS: " + fileName) - // Custom components with XML and code subExports = require(jsPath.replace(".js", "")) } @@ -213,7 +205,6 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr } } else { // Custom components without XML - console.log("NO XML: " + componentPath) result = componentBuilder.getComponentModule(componentName, componentPath, attributes, context); }