Files
NativeScript/ui/builder/template-builder.ts
2015-04-24 14:44:12 +03:00

57 lines
1.9 KiB
TypeScript

import definition = require("ui/builder/template-builder");
var KNOWNTEMPLATES = "knownTemplates";
export class TemplateBuilder {
private _items: Array<string>;
private _templateProperty: definition.TemplateProperty;
constructor(templateProperty: definition.TemplateProperty) {
this._items = new Array<string>();
this._templateProperty = templateProperty;
}
public get elementName(): string {
return this._templateProperty.elementName;
}
public addStartElement(prefix: string, namespace: string, elementName: string, attributes: Object) {
this._items.push("<" +
getElementNameWithPrefix(prefix, elementName) +
(namespace ? " " + getNamespace(prefix, namespace) : "") +
(attributes ? " " + getAttributesAsString(attributes) : "") +
">");
}
public addEndElement(prefix: string, elementName: string) {
this._items.push("</" + getElementNameWithPrefix(prefix, elementName) + ">");
}
public build() {
if (this._templateProperty.name in this._templateProperty.parent.component) {
this._templateProperty.parent.component[this._templateProperty.name] = this._items.join("");
}
}
}
export function isKnownTemplate(name: string, exports: any): boolean {
return KNOWNTEMPLATES in exports && exports[KNOWNTEMPLATES] && name in exports[KNOWNTEMPLATES];
}
function getAttributesAsString(attributes: Object): string {
var result = [];
for (var item in attributes) {
result.push(item + '="' + attributes[item] + '"');
}
return result.join(" ");
}
function getElementNameWithPrefix(prefix: string, elementName: string): string {
return (prefix ? prefix + ":" : "") + elementName;
}
function getNamespace(prefix: string, namespace: string): string {
return 'xmlns:' + prefix + '="' + namespace + '"';
}