| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e7a7165 commit ee6b7eb
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + <Page xmlns="http://schemas.nativescript.org/tns.xsd" | ||
| 2 | + xmlns:tc="xml-declaration/template-builder-tests/template-view"> | ||
| 3 | + <tc:TemplateView id="template-view"> | ||
| 4 | + <tc:TemplateView.template> | ||
| 5 | + <Button text="Click!" /> | ||
| 6 | + </tc:TemplateView.template> | ||
| 7 | + </tc:TemplateView> | ||
| 8 | + </Page> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + import { View, Template } from "ui/core/view" | ||
| 2 | + import { PropertyChangeData, Property, PropertyMetadataSettings } from "ui/core/dependency-observable" | ||
| 3 | + import * as proxy from "ui/core/proxy" | ||
| 4 | + import { LayoutBase } from "ui/layouts/layout-base" | ||
| 5 | + import { parse } from "ui/builder" | ||
| 6 | + | ||
| 7 | + export module knownTemplates { | ||
| 8 | + export var template = "template"; | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + export class TemplateView extends LayoutBase { | ||
| 12 | + public static templateProperty = new Property( | ||
| 13 | + "template", | ||
| 14 | + "TemplateView", | ||
| 15 | + new proxy.PropertyMetadata( | ||
| 16 | + undefined, | ||
| 17 | + PropertyMetadataSettings.AffectsLayout, | ||
| 18 | + null | ||
| 19 | + ) | ||
| 20 | + ); | ||
| 21 | + | ||
| 22 | + get template(): string | Template { | ||
| 23 | + return this._getValue(TemplateView.templateProperty); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + set template(value: string | Template) { | ||
| 27 | + this._setValue(TemplateView.templateProperty, value); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public parseTemplate() { | ||
| 31 | + this.addChild(parse(this.template)); | ||
| 32 | + } | ||
| 33 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,8 @@ import stackLayoutModule = require("ui/layouts/stack-layout"); | |||
| 14 | 14 | import {Label} from "ui/label"; | |
| 15 | 15 | import {Page} from "ui/page"; | |
| 16 | 16 | import {Button} from "ui/button"; | |
| 17 | + import {View} from "ui/core/view"; | ||
| 18 | + import {TemplateView} from "./template-builder-tests/template-view"; | ||
| 17 | 19 | import myCustomControlWithoutXml = require("./mymodule/MyControl"); | |
| 18 | 20 | import listViewModule = require("ui/list-view"); | |
| 19 | 21 | import helper = require("../ui/helper"); | |
@@ -818,4 +820,19 @@ export function test_searchbar_donotcrash_whentext_isspace() { | |||
| 818 | 820 | var sb = <searchBarModule.SearchBar>p.content; | |
| 819 | 821 | ||
| 820 | 822 | TKUnit.assertEqual(sb.text, " "); | |
| 821 | - }; | ||
| 823 | + }; | ||
| 824 | + | ||
| 825 | + export function test_parse_template_property() { | ||
| 826 | + var page = <Page>builder.load(fs.path.join(__dirname, "template-builder-tests/simple-template-test.xml")); | ||
| 827 | + TKUnit.assert(page, "Expected root page."); | ||
| 828 | + var templateView = <TemplateView>page.getViewById("template-view"); | ||
| 829 | + TKUnit.assert(templateView, "Expected TemplateView."); | ||
| 830 | + TKUnit.assert(templateView.template, "Expected the template of the TemplateView to be defined"); | ||
| 831 | + | ||
| 832 | + TKUnit.assertEqual(templateView.getChildrenCount(), 0, "Expected TemplateView initially to have no children."); | ||
| 833 | + templateView.parseTemplate(); | ||
| 834 | + TKUnit.assertEqual(templateView.getChildrenCount(), 1, "Expected TemplateView initially to have 1 child."); | ||
| 835 | + var button = <Button>templateView.getChildAt(0); | ||
| 836 | + TKUnit.assert(button, "Expected the TemplateView's template to create a button child."); | ||
| 837 | + TKUnit.assertEqual(button.text, "Click!", "Expected child Button to have text 'Click!'"); | ||
| 838 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -320,6 +320,7 @@ | |||
| 320 | 320 | "apps/tests/xml-declaration/mymodule/MyControl.ts", | |
| 321 | 321 | "apps/tests/xml-declaration/mymodulewithxml/MyControl.ts", | |
| 322 | 322 | "apps/tests/xml-declaration/xml-declaration-tests.ts", | |
| 323 | + "apps/tests/xml-declaration/template-builder-tests/template-view.ts", | ||
| 323 | 324 | "apps/tests/xml-parser-tests/xml-parser-tests.ts", | |
| 324 | 325 | "apps/transforms/app.ts", | |
| 325 | 326 | "apps/transforms/main-page.ts", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | 5 | export function load(fileName: string, exports?: any): view.View; | |
| 6 | 6 | export function load(options: LoadOptions): view.View; | |
| 7 | - export function parse(value: string, exports?: any): view.View; | ||
| 7 | + export function parse(value: string | view.Template, exports?: any): view.View; | ||
| 8 | 8 | ||
| 9 | 9 | export interface LoadOptions { | |
| 10 | 10 | path: string; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,20 +21,24 @@ function isCurentPlatform(value: string): boolean { | |||
| 21 | 21 | return value && value.toLowerCase() === platform.device.os.toLowerCase(); | |
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | - export function parse(value: string, context: any): view.View { | ||
| 25 | - var viewToReturn: view.View; | ||
| 26 | - | ||
| 27 | - if (context instanceof view.View) { | ||
| 28 | - context = getExports(context); | ||
| 29 | - } | ||
| 30 | - | ||
| 31 | - var componentModule = parseInternal(value, context); | ||
| 32 | - | ||
| 33 | - if (componentModule) { | ||
| 34 | - viewToReturn = componentModule.component; | ||
| 24 | + export function parse(value: string | view.Template, context: any): view.View { | ||
| 25 | + if (types.isString(value)) { | ||
| 26 | + var viewToReturn: view.View; | ||
| 27 | + | ||
| 28 | + if (context instanceof view.View) { | ||
| 29 | + context = getExports(context); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + var componentModule = parseInternal(<string>value, context); | ||
| 33 | + | ||
| 34 | + if (componentModule) { | ||
| 35 | + viewToReturn = componentModule.component; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + return viewToReturn; | ||
| 39 | + } else if (types.isFunction(value)) { | ||
| 40 | + return (<view.Template>value)(); | ||
| 35 | 41 | } | |
| 36 | - | ||
| 37 | - return viewToReturn; | ||
| 38 | 42 | } | |
| 39 | 43 | ||
| 40 | 44 | function parseInternal(value: string, context: any): componentBuilder.ComponentModule { | |
@@ -104,6 +108,7 @@ function parseInternal(value: string, context: any): componentBuilder.ComponentM | |||
| 104 | 108 | ||
| 105 | 109 | if (templateBuilderDef.isKnownTemplate(name, parent.exports)) { | |
| 106 | 110 | templateBuilder = new templateBuilderDef.TemplateBuilder({ | |
| 111 | + context: parent ? getExports(parent.component) : null, // Passing 'context' won't work if you set "codeFile" on the page | ||
| 107 | 112 | parent: parent, | |
| 108 | 113 | name: name, | |
| 109 | 114 | elementName: args.elementName, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | //@private | |
| 2 | 2 | declare module "ui/builder/template-builder" { | |
| 3 | 3 | import xml = require("xml"); | |
| 4 | + import page = require("ui/page"); | ||
| 4 | 5 | import componentBuilder = require("ui/builder/component-builder"); | |
| 5 | 6 | ||
| 6 | 7 | class TemplateBuilder { | |
@@ -18,6 +19,7 @@ declare module "ui/builder/template-builder" { | |||
| 18 | 19 | export function isKnownTemplate(name: string, exports: any): boolean; | |
| 19 | 20 | ||
| 20 | 21 | interface TemplateProperty { | |
| 22 | + context?: any; | ||
| 21 | 23 | parent: componentBuilder.ComponentModule; | |
| 22 | 24 | name: string; | |
| 23 | 25 | elementName: string; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,19 @@ | |||
| 1 | 1 | import definition = require("ui/builder/template-builder"); | |
| 2 | + import builder = require("ui/builder"); | ||
| 3 | + import view = require("ui/core/view"); | ||
| 4 | + import page = require("ui/page"); | ||
| 2 | 5 | import xml = require("xml"); | |
| 3 | 6 | ||
| 4 | 7 | var KNOWNTEMPLATES = "knownTemplates"; | |
| 5 | 8 | ||
| 6 | 9 | export class TemplateBuilder { | |
| 10 | + private _context: any; | ||
| 7 | 11 | private _items: Array<string>; | |
| 8 | 12 | private _templateProperty: definition.TemplateProperty; | |
| 9 | 13 | private _nestingLevel: number; | |
| 10 | 14 | ||
| 11 | 15 | constructor(templateProperty: definition.TemplateProperty) { | |
| 16 | + this._context = templateProperty.context; | ||
| 12 | 17 | this._items = new Array<string>(); | |
| 13 | 18 | this._templateProperty = templateProperty; | |
| 14 | 19 | this._nestingLevel = 0; | |
@@ -56,7 +61,10 @@ export class TemplateBuilder { | |||
| 56 | 61 | ||
| 57 | 62 | private build() { | |
| 58 | 63 | if (this._templateProperty.name in this._templateProperty.parent.component) { | |
| 59 | - this._templateProperty.parent.component[this._templateProperty.name] = this._items.join(""); | ||
| 64 | + var xml = this._items.join(""); | ||
| 65 | + var context = this._context; | ||
| 66 | + var template: view.Template = () => builder.parse(xml, context); | ||
| 67 | + this._templateProperty.parent.component[this._templateProperty.name] = template; | ||
| 60 | 68 | } | |
| 61 | 69 | } | |
| 62 | 70 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -511,6 +511,18 @@ declare module "ui/core/view" { | |||
| 511 | 511 | */ | |
| 512 | 512 | export class CustomLayoutView extends View { | |
| 513 | 513 | } | |
| 514 | + | ||
| 515 | + /** | ||
| 516 | + * Defines an interface for a View factory function. | ||
| 517 | + * Commonly used to specify the visualization of data objects. | ||
| 518 | + */ | ||
| 519 | + interface Template { | ||
| 520 | + /** | ||
| 521 | + * Call signature of the factory function. | ||
| 522 | + * Returns a new View instance. | ||
| 523 | + */ | ||
| 524 | + (): View; | ||
| 525 | + } | ||
| 514 | 526 | ||
| 515 | 527 | /** | |
| 516 | 528 | * Defines an interface for adding arrays declared in xml. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ import builder = require("ui/builder"); | |||
| 8 | 8 | import label = require("ui/label"); | |
| 9 | 9 | import color = require("color"); | |
| 10 | 10 | import weakEvents = require("ui/core/weak-event-listener"); | |
| 11 | + import types = require("utils/types"); | ||
| 11 | 12 | ||
| 12 | 13 | var ITEMS = "items"; | |
| 13 | 14 | var ITEMTEMPLATE = "itemTemplate"; | |
@@ -91,10 +92,10 @@ export class ListView extends view.View implements definition.ListView { | |||
| 91 | 92 | this._setValue(ListView.itemsProperty, value); | |
| 92 | 93 | } | |
| 93 | 94 | ||
| 94 | - get itemTemplate(): string { | ||
| 95 | + get itemTemplate(): string | view.Template { | ||
| 95 | 96 | return this._getValue(ListView.itemTemplateProperty); | |
| 96 | 97 | } | |
| 97 | - set itemTemplate(value: string) { | ||
| 98 | + set itemTemplate(value: string | view.Template) { | ||
| 98 | 99 | this._setValue(ListView.itemTemplateProperty, value); | |
| 99 | 100 | } | |
| 100 | 101 | ||
@@ -132,7 +133,7 @@ export class ListView extends view.View implements definition.ListView { | |||
| 132 | 133 | var v; | |
| 133 | 134 | ||
| 134 | 135 | if (this.itemTemplate && this.items) { | |
| 135 | - v = builder.parse(this.itemTemplate, this); | ||
| 136 | + v = builder.parse(this.itemTemplate); | ||
| 136 | 137 | } | |
| 137 | 138 | ||
| 138 | 139 | return v; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments