Merge pull request #2049 from akagr/feature/add-attributes-2048

Expose configurable attributes property when loading components
This commit is contained in:
Vladimir Enchev
2016-05-11 10:10:38 +03:00
10 changed files with 38 additions and 15 deletions

View File

@ -133,6 +133,26 @@ export function test_loadWithOptionsFromTNSPath() {
TKUnit.assert(v instanceof Label, "Expected result: Label; Actual result: " + v + ";"); TKUnit.assert(v instanceof Label, "Expected result: Label; Actual result: " + v + ";");
}; };
export function test_loadWithAttributes() {
var lText = "Nativescript rocks";
var lWrap = true;
var lColor = "#FF0000"; // red
var v = builder.load({
path: "tns_modules/ui/label",
name: "Label",
attributes: {
text: lText,
textWrap: lWrap,
style: "color: " + lColor + ";"
}
});
TKUnit.assertEqual(v["text"], lText, "Expected result: true; Actual result: " + v + ";");
TKUnit.assertEqual(v["textWrap"], true, "Expected result: true; Actual result: " + v + ";");
helper.assertViewColor(v, lColor);
};
export function test_parse_ShouldNotCrashWithoutExports() { export function test_parse_ShouldNotCrashWithoutExports() {
var file = fs.File.fromPath(fs.path.join(__dirname, "mainPage.xml")); var file = fs.File.fromPath(fs.path.join(__dirname, "mainPage.xml"));
var text = file.readTextSync(); var text = file.readTextSync();
@ -879,4 +899,4 @@ export function test_hasSourceCodeLocations() {
var label = page.getViewById("label"); var label = page.getViewById("label");
var labelSource = Source.get(label); var labelSource = Source.get(label);
TKUnit.assertEqual(labelSource.toString(), "file:///app/" + basePath + "examples/test-page.xml:3:5"); TKUnit.assertEqual(labelSource.toString(), "file:///app/" + basePath + "examples/test-page.xml:3:5");
} }

View File

@ -209,7 +209,7 @@ module.exports = {
}, },
shell: { shell: {
collectAndroidLog: { collectAndroidLog: {
command: "./expect.exp " + "'adb logcat' " + localCfg.outFile, command: "./expect.exp " + "'adb logcat *:D' " + localCfg.outFile,
options: { options: {
execOptions: { execOptions: {
maxBuffer: Infinity maxBuffer: Infinity

View File

@ -9,6 +9,7 @@
export interface LoadOptions { export interface LoadOptions {
path: string; path: string;
name: string; name: string;
attributes?: any;
exports?: any; exports?: any;
page?: page.Page; page?: page.Page;
} }

View File

@ -115,7 +115,7 @@ export function load(pathOrOptions: string | LoadOptions, context?: any): View {
if (!context) { if (!context) {
if (!isString(pathOrOptions)) { if (!isString(pathOrOptions)) {
let options = <LoadOptions>pathOrOptions; let options = <LoadOptions>pathOrOptions;
componentModule = loadCustomComponent(options.path, options.name, undefined, options.exports, options.page); componentModule = loadCustomComponent(options.path, options.name, options.attributes, options.exports, options.page);
} else { } else {
let path = <string>pathOrOptions; let path = <string>pathOrOptions;
componentModule = loadInternal(path); componentModule = loadInternal(path);

View File

@ -2,7 +2,7 @@ declare module "ui/builder/component-builder" {
import view = require("ui/core/view"); import view = require("ui/core/view");
export function getComponentModule(elementName: string, namespace: string, attributes: Object, exports: Object): ComponentModule; export function getComponentModule(elementName: string, namespace: string, attributes: Object, exports: Object): ComponentModule;
export function setPropertyValue(instance: view.View, instanceModuleExports: Object, pageExports: Object, propertyName: string, propertyValue: string) : void; export function setPropertyValue(instance: view.View, instanceModuleExports: Object, pageExports: Object, propertyName: string, propertyValue: any) : void;
export interface ComponentModule { export interface ComponentModule {
component: view.View; component: view.View;

View File

@ -154,7 +154,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
return componentModule; return componentModule;
} }
export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: string) { export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: any) {
// Note: instanceModule can be null if we are loading custom compnenet with no code-behind. // Note: instanceModule can be null if we are loading custom compnenet with no code-behind.
if (isBinding(propertyValue) && instance.bind) { if (isBinding(propertyValue) && instance.bind) {
@ -183,7 +183,7 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
if (!attrHandled && (<any>instance)._applyXmlAttribute) { if (!attrHandled && (<any>instance)._applyXmlAttribute) {
attrHandled = (<any>instance)._applyXmlAttribute(propertyName, propertyValue); attrHandled = (<any>instance)._applyXmlAttribute(propertyName, propertyValue);
} }
if (!attrHandled) { if (!attrHandled) {
instance[propertyName] = convertString(propertyValue); instance[propertyName] = convertString(propertyValue);
} }
} }
@ -193,7 +193,7 @@ function getBindingExpressionFromAttribute(value: string): string {
return value.replace("{{", "").replace("}}", "").trim(); return value.replace("{{", "").replace("}}", "").trim();
} }
function isBinding(value: string): boolean { function isBinding(value: any): boolean {
var isBinding; var isBinding;
if (isString(value)) { if (isString(value)) {

View File

@ -1,7 +1,7 @@
declare module "ui/builder/special-properties" { declare module "ui/builder/special-properties" {
import view = require("ui/core/view"); import view = require("ui/core/view");
export type PropertySetter = (instance: view.View, propertyValue: string) => void; export type PropertySetter = (instance: view.View, propertyValue: any) => void;
export function registerSpecialProperty(name: string, setter: PropertySetter): void; export function registerSpecialProperty(name: string, setter: PropertySetter): void;
export function getSpecialPropertySetter(name: string): PropertySetter; export function getSpecialPropertySetter(name: string): PropertySetter;
} }

View File

@ -1,6 +1,6 @@
import view = require("ui/core/view"); import view = require("ui/core/view");
export type PropertySetter = (instance: view.View, propertyValue: string) => void; export type PropertySetter = (instance: view.View, propertyValue: any) => void;
var specialProperties: Map<string, PropertySetter> = new Map<string, PropertySetter>(); var specialProperties: Map<string, PropertySetter> = new Map<string, PropertySetter>();

View File

@ -34,10 +34,12 @@ export function escapeRegexSymbols(source: string): string {
return source.replace(escapeRegex, "\\$&"); return source.replace(escapeRegex, "\\$&");
} }
export function convertString(value: string): any { export function convertString(value: any): any {
var result; var result;
if (value.trim() === "") { if (!types.isString(value)) {
result = value;
} else if (value.trim() === "") {
result = value; result = value;
} else { } else {
// Try to convert value to number. // Try to convert value to number.
@ -50,7 +52,7 @@ export function convertString(value: string): any {
result = value; result = value;
} }
} }
return result; return result;
} }
@ -131,4 +133,4 @@ export function isDataURI(uri: string): boolean {
var firstSegment = uri.trim().split(',')[0]; var firstSegment = uri.trim().split(',')[0];
return firstSegment && firstSegment.indexOf("data:") === 0 && firstSegment.indexOf('base64') >= 0; return firstSegment && firstSegment.indexOf("data:") === 0 && firstSegment.indexOf('base64') >= 0;
} }

2
utils/utils.d.ts vendored
View File

@ -240,5 +240,5 @@
* Converts string value to number or boolean. * Converts string value to number or boolean.
* @param value The original value. * @param value The original value.
*/ */
export function convertString(value: string): any export function convertString(value: any): any
} }