8000 Merge pull request #2049 from akagr/feature/add-attributes-2048 · hbcodeXCI/NativeScript@87c9828 · GitHub
[go: up one dir, main page]

Skip to content

Commit 87c9828

Browse files
committed
Merge pull request NativeScript#2049 from akagr/feature/add-attributes-2048
Expose configurable attributes property when loading components
2 parents 3607bc6 + 1e2a991 commit 87c9828

File tree

10 files changed

+38
-15
lines changed

10 files changed

+38
-15
lines changed

apps/tests/xml-declaration/xml-declaration-tests.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ export function test_loadWithOptionsFromTNSPath() {
133133
TKUnit.assert(v instanceof Label, "Expected result: Label; Actual result: " + v + ";");
134134
};
135135

136+
export function test_loadWithAttributes() {
137+
var lText = "Nativescript rocks";
138+
var lWrap = true;
139+
var lColor = "#FF0000"; // red
140+
141+
var v = builder.load({
142+
path: "tns_modules/ui/label",
143+
name: "Label",
144+
attributes: {
145+
text: lText,
146+
textWrap: lWrap,
147+
style: "color: " + lColor + ";"
148+
}
149+
});
150+
151+
TKUnit.assertEqual(v["text"], lText, "Expected result: true; Actual result: " + v + ";");
152+
TKUnit.assertEqual(v["textWrap"], true, "Expected result: true; Actual result: " + v + ";");
153+
helper.assertViewColor(v, lColor);
154+
};
155+
136156
export function test_parse_ShouldNotCrashWithoutExports() {
137157
var file = fs.File.fromPath(fs.path.join(__dirname, "mainPage.xml"));
138158
var text = file.readTextSync();
@@ -879,4 +899,4 @@ export function test_hasSourceCodeLocations() {
879899
var label = page.getViewById("label");
880900
var labelSource = Source.get(label);
881901
TKUnit.assertEqual(labelSource.toString(), "file:///app/" + basePath + "examples/test-page.xml:3:5");
882-
}
902+
}

build/run-testsapp.grunt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ module.exports = {
209209
},
210210
shell: {
211211
collectAndroidLog: {
212-
command: "./expect.exp " + "'adb logcat' " + localCfg.outFile,
212+
command: "./expect.exp " + "'adb logcat *:D' " + localCfg.outFile,
213213
options: {
214214
execOptions: {
215215
maxBuffer: Infinity

ui/builder/builder.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
export interface LoadOptions {
1010
path: string;
1111
name: string;
12+
attributes?: any;
1213
exports?: any;
1314
page?: page.Page;
1415
}

ui/builder/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function load(pathOrOptions: string | LoadOptions, context?: any): View {
115115
if (!context) {
116116
if (!isString(pathOrOptions)) 57A7 {
117117
let options = <LoadOptions>pathOrOptions;
118-
componentModule = loadCustomComponent(options.path, options.name, undefined, options.exports, options.page);
118+
componentModule = loadCustomComponent(options.path, options.name, options.attributes, options.exports, options.page);
119119
} else {
120120
let path = <string>pathOrOptions;
121121
componentModule = loadInternal(path);

ui/builder/component-builder.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ declare module "ui/builder/component-builder" {
22
import view = require("ui/core/view");
33

44
export function getComponentModule(elementName: string, namespace: string, attributes: Object, exports: Object): ComponentModule;
5-
export function setPropertyValue(instance: view.View, instanceModuleExports: Object, pageExports: Object, propertyName: string, propertyValue: string) : void;
5+
export function setPropertyValue(instance: view.View, instanceModuleExports: Object, pageExports: Object, propertyName: string, propertyValue: any) : void;
66

77
export interface ComponentModule {
88
component: view.View;

ui/builder/component-builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
154154
return componentModule;
155155
}
156156

157-
export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: string) {
157+
export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: any) {
158158
// Note: instanceModule can be null if we are loading custom compnenet with no code-behind.
159159

160160
if (isBinding(propertyValue) && instance.bind) {
@@ -183,7 +183,7 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
183183
if (!attrHandled && (<any>instance)._applyXmlAttribute) {
184184
attrHandled = (<any>instance)._applyXmlAttribute(propertyName, propertyValue);
185185
}
186-
if (!attrHandled) {
186+
if (!attrHandled) {
187187
instance[propertyName] = convertString(propertyValue);
188188
}
189189
}
@@ -193,7 +193,7 @@ function getBindingExpressionFromAttribute(value: string): string {
193193
return value.replace("{{", "").replace("}}", "").trim();
194194
}
195195

196-
function isBinding(value: string): boolean {
196+
function isBinding(value: any): boolean {
197197
var isBinding;
198198

199199
if (isString(value)) {

ui/builder/special-properties.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
declare module "ui/builder/special-properties" {
22
import view = require("ui/core/view");
33

4-
export type PropertySetter = (instance: view.View, propertyValue: string) => void;
4+
export type PropertySetter = (instance: view.View, propertyValue: any) => void;
55
export function registerSpecialProperty(name: string, setter: PropertySetter): void;
66
export function getSpecialPropertySetter(name: string): PropertySetter;
77
}

ui/builder/special-properties.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import view = require("ui/core/view");
22

3-
export type PropertySetter = (instance: view.View, propertyValue: string) => void;
3+
export type PropertySetter = (instance: view.View, propertyValue: any) => void;
44

55
var specialProperties: Map<string, PropertySetter> = new Map<string, PropertySetter>();
66

utils/utils-common.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ export function escapeRegexSymbols(source: string): string {
3434
return source.replace(escapeRegex, "\\$&");
3535
}
3636

37-
export function convertString(value: string): any {
37+
export function convertString(value: any): any {
3838
var result;
39-
40-
if (value.trim() === "") {
39+
40+
if (!types.isString(value)) {
41+
result = value;
42+
} else if (value.trim() === "") {
4143
result = value;
4244
} else {
4345
// Try to convert value to number.
@@ -50,7 +52,7 @@ export function convertString(value: string): any {
5052
result = value;
5153
}
5254
}
53-
55+
5456
return result;
5557
}
5658

@@ -131,4 +133,4 @@ export function isDataURI(uri: string): boolean {
131133
var firstSegment = uri.trim().split(',')[0];
132134

133135
return firstSegment && firstSegment.indexOf("data:") === 0 && firstSegment.indexOf('base64') >= 0;
134-
}
136+
}

utils/utils.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,5 @@
240240
* Converts string value to number or boolean.
241241
* @param value The original value.
242242
*/
243-
export function convertString(value: string): any
243+
export function convertString(value: any): any
244244
}

0 commit comments

Comments
 (0)
0