Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.36 KB | None | 0 0
  1.         var meta = null;
  2.         if (Context.getLocalClass().get().meta.has(":xml") == true) {
  3.             meta = Context.getLocalClass().get().meta.extract(":xml");
  4.         } else if (Context.getLocalClass().get().meta.has("xml") == true) {
  5.             meta = Context.getLocalClass().get().meta.extract("xml");
  6.         }
  7.         if (meta != null) {
  8.             var m = null;
  9.             for (t in meta) {
  10.                 if (t.name == "xml" || t.name == ":xml") {
  11.                     m = t;
  12.                     break;
  13.                 }
  14.             }
  15.            
  16.             var ctor = MacroHelpers.getConstructor(fields);
  17.             if (MacroHelpers.hasSuperClass(Context.getLocalType(), "haxe.ui.core.Component") == false) {
  18.                 Context.error("Must have a superclass of haxe.ui.core.Component", Context.currentPos());
  19.             }
  20.  
  21.             if (ctor == null) Context.error("A class building component must have a constructor", Context.currentPos());
  22.            
  23.             var xml = ExprTools.toString(m.params[0]);
  24.             xml = StringTools.trim(xml.substring(1, xml.length - 1));
  25.             xml = StringTools.replace(xml, "\\n", "");
  26.             xml = StringTools.replace(xml, "\\r", "");
  27.             xml = StringTools.replace(xml, "\\\"", "\"");
  28.             xml = StringTools.replace(xml, "\\'", "'");
  29.  
  30.             ModuleMacros.populateClassMap();
  31.            
  32.             var namedComponents:Map<String, String> = new Map<String, String>();
  33.             var expr = ComponentMacros.buildComponentFromString([], xml, namedComponents, null);
  34.             switch (expr.expr) {
  35.                 case EBlock(el): el.push(Context.parseInlineString("addComponent(c0)", pos));
  36.                 case _:
  37.             }
  38.            
  39.             var currentCreateChildrenFn = MacroHelpers.getFunction(fields, "createChildren");
  40.             if (currentCreateChildrenFn != null) {
  41.                 MacroHelpers.appendLine(currentCreateChildrenFn, expr);
  42.             } else {
  43.                 var code:String = "";
  44.                 code += "function() {\n";
  45.                 code += "super.createChildren();\n";
  46.                 code += ExprTools.toString(expr);
  47.                 code += "}";
  48.                
  49.                 var access:Array<Access> = [APrivate, AOverride];
  50.                 MacroHelpers.addFunction("createChildren", Context.parseInlineString(code, pos), access, fields, pos);
  51.             }
  52.            
  53.             var n = 1;
  54.             for (id in namedComponents.keys()) {
  55.                 var safeId:String = StringUtil.capitalizeHyphens(id);
  56.                 var cls:String = namedComponents.get(id);
  57.                 var classArray:Array<String> = cls.split(".");
  58.                 var className = classArray.pop();
  59.                 var ttype = TPath( { pack : classArray, name : className, params : [], sub : null } );
  60.                 fields.push( { name : safeId, doc : null, meta : [], access : [APublic], kind : FVar(ttype, null), pos : pos } );
  61.  
  62.                 var e:Expr = Context.parseInlineString('this.${safeId} = findComponent("${id}", ${cls}, true)', Context.currentPos());
  63.                 ctor.expr = switch(ctor.expr.expr) {
  64.                     case EBlock(el): macro $b{MacroHelpers.insertExpr(el, n, e)};
  65.                     case _: macro $b { MacroHelpers.insertExpr([ctor.expr], n, e) }
  66.                 }
  67.             }
  68.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement