GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — NativeScript/ui/builder/builder.ts at master · alvsgithub/NativeScript Expand file tree/ Copy pathbuilder.tsMore file actions 236 lines (192 loc) · 9.29 KB / Copy pathbuilder.tsFile metadata and controls236 lines (192 loc) · 9.29 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 import view = require("ui/core/view"); import fs = require("file-system"); import xml = require("xml"); import file_access_module = require("file-system/file-system-access"); import types = require("utils/types"); import componentBuilder = require("ui/builder/component-builder"); import templateBuilderDef = require("ui/builder/template-builder"); var KNOWNCOLLECTIONS = "knownCollections"; export function parse(value: string, exports: any): view.View { var viewToReturn: view.View; var componentModule = parseInternal(value, exports); if (componentModule) { viewToReturn = componentModule.component; } return viewToReturn; } function parseInternal(value: string, exports: any): componentBuilder.ComponentModule { var rootComponentModule: componentBuilder.ComponentModule; // Temporary collection used for parent scope. var parents = new Array<componentBuilder.ComponentModule>(); var complexProperties = new Array<ComplexProperty>(); var templateBuilder: templateBuilderDef.TemplateBuilder; // Parse the XML. var xmlParser = new xml.XmlParser((args: xml.ParserEvent) => { if (templateBuilder) { if (args.eventType === xml.ParserEventType.StartElement) { templateBuilder.addStartElement(args.prefix, args.namespace, args.elementName, args.attributes); } else if (args.eventType === xml.ParserEventType.EndElement) { if (templateBuilder.elementName !== args.elementName) { templateBuilder.addEndElement(args.prefix, args.elementName); } else { templateBuilder.build(); templateBuilder = undefined; } } } // Get the current parent. var parent = parents[parents.length - 1]; var complexProperty = complexProperties[complexProperties.length - 1]; // Create component instance from every element declaration. if (args.eventType === xml.ParserEventType.StartElement) { if (isComplexProperty(args.elementName)) { var name = getComplexProperty(args.elementName); complexProperties.push({ parent: parent, name: name, items: [], }); if (templateBuilderDef.isKnownTemplate(name, parent.exports)) { templateBuilder = new templateBuilderDef.TemplateBuilder({ parent: parent, name: name, elementName: args.elementName, templateItems: [] }); } } else { var componentModule: componentBuilder.ComponentModule; if (args.prefix) { // 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); } } } else { // Default components componentModule = componentBuilder.getComponentModule(args.elementName, ns, args.attributes, exports); } if (componentModule) { if (parent) { if (componentModule.component instanceof view.View) { if (complexProperty) { // Add to complex property to component. addToComplexProperty(parent, complexProperty, componentModule) } else if ((<any>parent.component)._addChildFromBuilder) { // Add component to visual tree (<any>parent.component)._addChildFromBuilder(args.elementName, componentModule.component); } } else if (complexProperty) { // Add component to complex property of parent component. addToComplexProperty(parent, complexProperty, componentModule); } } else if (parents.length === 0) { // Set root component. rootComponentModule = componentModule; } // Add the component instance to the parents scope collection. parents.push(componentModule); } } } else if (args.eventType === xml.ParserEventType.EndElement) { if (isComplexProperty(args.elementName)) { if (complexProperty) { if (parent && (<any>parent.component)._addArrayFromBuilder) { // If parent is AddArrayFromBuilder call the interface method to populate the array property. (<any>parent.component)._addArrayFromBuilder(complexProperty.name, complexProperty.items); complexProperty.items = []; } } // Remove the last complexProperty from the complexProperties collection (move to the previous complexProperty scope). complexProperties.pop(); } else { // Remove the last parent from the parents collection (move to the previous parent scope). parents.pop(); } } }, (e) => { throw new Error("XML parse error: " + e.message); }, true); if (types.isString(value)) { value = value.replace(/xmlns=("|')http:\/\/www.nativescript.org\/tns.xsd\1/, ""); xmlParser.parse(value); } return rootComponentModule; } export function load(fileName: string, exports: any): view.View { var viewToReturn: view.View; var componentModule = loadInternal(fileName, exports); if (componentModule) { viewToReturn = componentModule.component; } return viewToReturn; } function loadInternal(fileName: string, exports: any): componentBuilder.ComponentModule { var componentModule: componentBuilder.ComponentModule; // Check if the XML file exists. if (fileName && fs.File.exists(fileName)) { var fileAccess = new file_access_module.FileSystemAccess(); // Read the XML file. fileAccess.readText(fileName, result => { componentModule = parseInternal(result, exports); }, (e) => { throw new Error("Error loading file " + fileName + " :" + e.message); }); } if (componentModule && componentModule.component) { // Save exports to root component (will be used for templates). (<any>componentModule.component).exports = exports; } return componentModule; } function isComplexProperty(name: string): boolean { return types.isString(name) && name.indexOf(".") !== -1; } function getComplexProperty(fullName: string): string { var name: string; if (types.isString(fullName)) { var names = fullName.split("."); name = names[names.length - 1]; } return name; } function isKnownCollection(name: string, exports: any): boolean { return KNOWNCOLLECTIONS in exports && exports[KNOWNCOLLECTIONS] && name in exports[KNOWNCOLLECTIONS]; } function addToComplexProperty(parent: componentBuilder.ComponentModule, complexProperty, elementModule: componentBuilder.ComponentModule) { // If property name is known collection we populate array with elements. var parentComponent = <any>parent.component; if (isKnownCollection(complexProperty.name, parent.exports)) { complexProperty.items.push(elementModule.component); } else if (parentComponent._addChildFromBuilder) { parentComponent._addChildFromBuilder("", elementModule.component); } else { // Or simply assign the value; parentComponent[complexProperty.name] = elementModule.component; } } interface ComplexProperty { parent: componentBuilder.ComponentModule; name: string; items?: Array<any>; } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |