2
0
ncannasse 11 жил өмнө
parent
commit
7e4798bc7d
3 өөрчлөгдсөн 160 нэмэгдсэн , 35 устгасан
  1. 35 13
      h2d/comp/Component.hx
  2. 103 13
      h2d/comp/JQuery.hx
  3. 22 9
      h2d/comp/Parser.hx

+ 35 - 13
h2d/comp/Component.hx

@@ -25,10 +25,29 @@ class Component extends Sprite {
 		this.name = name;
 		classes = [];
 		components = [];
+		if( parentComponent == null )
+			while( parent != null ) {
+				var p = Std.instance(parent, Component);
+				if( p != null ) {
+					parentComponent = p;
+					p.components.push(this);
+					break;
+				}
+				parent = parent.parent;
+			}
 		bg = new h2d.css.Fill(this);
 		needRebuild = true;
 	}
 	
+	function getComponentsRec(s : Sprite, ret : Array<Component>) {
+		var c = Std.instance(s, Component);
+		if( c == null ) {
+			for( s in s )
+				getComponentsRec(s, ret);
+		} else
+			ret.push(c);
+	}
+	
 	public function getParent() {
 		if( allocated )
 			return parentComponent;
@@ -44,8 +63,6 @@ class Component extends Sprite {
 	public function getElementById(id:String) {
 		if( this.id == id )
 			return this;
-		if( !allocated )
-			return getElementByIdRec(this, id);
 		for( c in components ) {
 			var c = c.getElementById(id);
 			if( c != null )
@@ -54,23 +71,20 @@ class Component extends Sprite {
 		return null;
 	}
 	
-	function getElementByIdRec( s : h2d.Sprite, id : String ) : Component {
-		var c = Std.instance(s, Component);
-		if( c != null && c.id == id )
-			return c;
-		for( s in s.childs ) {
-			var c = getElementByIdRec(s, id);
-			if( c != null ) return c;
-		}
-		return null;
-	}
-	
 	function set_needRebuild(v) {
 		needRebuild = v;
 		if( v && parentComponent != null && !parentComponent.needRebuild )
 			parentComponent.needRebuild = true;
 		return v;
 	}
+	
+	override function onDelete() {
+		if( parentComponent != null ) {
+			parentComponent.components.remove(this);
+			parentComponent = null;
+		}
+		super.onDelete();
+	}
 		
 	override function onAlloc() {
 		// lookup our parent component
@@ -106,6 +120,14 @@ class Component extends Sprite {
 		needRebuild = true;
 		return this;
 	}
+	
+	public function getStyle( willWrite ) {
+		if( customStyle == null )
+			customStyle = new h2d.css.Style();
+		if( willWrite )
+			needRebuild = true;
+		return customStyle;
+	}
 
 	public function addStyle(s) {
 		if( customStyle == null )

+ 103 - 13
h2d/comp/JQuery.hx

@@ -1,6 +1,8 @@
 package h2d.comp;
 import h2d.css.Defs;
 
+private typedef Query = Array<CssClass>;
+
 @:access(h2d.comp.Component)
 @:keep
 class JQuery {
@@ -19,6 +21,87 @@ class JQuery {
 		for( s in select ) s.toggleClass(cl,flag);
 		return this;
 	}
+	
+	public function find( q : Dynamic ) {
+		if( Std.is(q, Component) )
+			return new JQuery(root, Lambda.has(select, q) ? null : q);
+		if( Std.is(q, String) ) {
+			var q = parseQuery(q);
+			var out = [];
+			for( s in select )
+				lookupRec(s, q, out);
+			return new JQuery(root, out);
+		}
+		throw "Invalid JQuery " + q;
+		return null;
+	}
+	
+	public function filter( q : Dynamic ) {
+		if( Std.is(q, Component) )
+			return new JQuery(root, Lambda.has(select, q) ? null : q);
+		if( Std.is(q, String) ) {
+			var q = parseQuery(q);
+			return new JQuery(root, [for( s in select ) if( matchQuery(q, s) ) s]);
+		}
+		if( Std.is(q, JQuery) ) {
+			var q : JQuery = q;
+			return  new JQuery(root, [for( s in select ) if( Lambda.has(q.select, s) ) s]);
+		}
+		throw "Invalid JQuery " + q;
+		return null;
+	}
+
+	public function not( q : Dynamic ) {
+		if( Std.is(q, Component) )
+			return new JQuery(root, [for( s in select ) if( s != q ) s]);
+		if( Std.is(q, String) ) {
+			var q = parseQuery(q);
+			return new JQuery(root, [for( s in select ) if( !matchQuery(q, s) ) s]);
+		}
+		if( Std.is(q, JQuery) ) {
+			var q : JQuery = q;
+			return  new JQuery(root, [for( s in select ) if( !Lambda.has(q.select, s) ) s]);
+		}
+		throw "Invalid JQuery " + q;
+		return null;
+	}
+	
+	public function click( f : JQuery -> Void ) {
+		for( c in select ) {
+			var int = Std.instance(c, Interactive);
+			if( int == null ) throw c + " is not interactive";
+			int.onClick = function() f(new JQuery(root,c));
+		}
+		return this;
+	}
+	
+	public function show() {
+		for( s in select )
+			s.getStyle(true).display = true;
+		return this;
+	}
+
+	public function hide() {
+		for( s in select )
+			s.getStyle(true).display = false;
+		return this;
+	}
+	
+	public function toggle() {
+		for( s in select ) {
+			var s = s.getStyle(true);
+			s.display = !s.display;
+		}
+		return this;
+	}
+	
+	public function iterator() {
+		var it = select.iterator();
+		return {
+			hasNext : it.hasNext,
+			next : function() return new JQuery(root, it.next()),
+		};
+	}
 
 	function _get_val() : Dynamic {
 		var c = select[0];
@@ -105,28 +188,35 @@ class JQuery {
 			var a : Array<Dynamic> = query;
 			for( v in a ) if( !Std.is(v, Component) ) throw "Invalid JQuery "+query;
 			set = a;
-		} else if( Std.is(query, String) ) {
-			set = lookupSet(query);
-		} else
+		} else if( Std.is(query, String) )
+			set = lookup(root, query);
+		else
 			throw "Invalid JQuery " + query;
 		return set;
 	}
 	
-	function lookupSet( query : String ) {
-		var classes = new h2d.css.Parser().parseClasses(query);
+	function lookup( root : Component, query : String ) {
 		var set = [];
-		lookupRec(root, classes, set);
+		lookupRec(root, parseQuery(query), set);
 		return set;
 	}
 	
-	function lookupRec(comp:Component, classes:Array<CssClass>, set : Array<Component> ) {
-		for( c in classes )
-			if( h2d.css.Engine.ruleMatch(c, comp) ) {
-				set.push(comp);
-				break;
-			}
+	function parseQuery(q) : Query {
+		return new h2d.css.Parser().parseClasses(q);
+	}
+	
+	function matchQuery(q:Query, comp:Component) {
+		for( r in q )
+			if( h2d.css.Engine.ruleMatch(r, comp) )
+				return true;
+		return false;
+	}
+	
+	function lookupRec(comp:Component, q, set : Array<Component> ) {
+		if( matchQuery(q, comp) )
+			set.push(comp);
 		for( s in comp.components )
-			lookupRec(s, classes, set);
+			lookupRec(s, q, set);
 	}
 	
 }

+ 22 - 9
h2d/comp/Parser.hx

@@ -15,12 +15,18 @@ private class CustomInterp extends hscript.Interp {
 
 class Parser {
 	
-	var api : {};
 	var comps : Map<String, haxe.xml.Fast -> Component -> Component>;
+	var interp : hscript.Interp;
+	var root : Component;
 	
-	public function new(?api) {
-		this.api = api;
+	public function new(?api:{}) {
 		comps = new Map();
+		interp = new CustomInterp();
+		interp.variables.set("$", function(rq) return new h2d.comp.JQuery(root, rq));
+		interp.variables.set("api", api);
+		if( api != null )
+			for( f in Reflect.fields(api) )
+				interp.variables.set(f, Reflect.field(api, f));
 	}
 	
 	public function build( x : haxe.xml.Fast, ?parent : Component ) {
@@ -31,6 +37,9 @@ class Parser {
 		case "style":
 			parent.addCss(x.innerData);
 			return null;
+		case "script":
+			makeScript(null, x.innerData)();
+			return null;
 		case "div", "box":
 			c = new Box(parent);
 		case "button":
@@ -75,6 +84,7 @@ class Parser {
 			else
 				throw "Unknown node " + n;
 		}
+		if( root == null ) root = c;
 		for( n in x.x.attributes() ) {
 			var v = x.x.get(n);
 			switch( n.toLowerCase() ) {
@@ -209,6 +219,10 @@ class Parser {
 				var int = Std.instance(c, Interactive);
 				if( int != null )
 					int.onClick = makeScript(c, v);
+			case "onrclick":
+				var int = Std.instance(c, Interactive);
+				if( int != null )
+					int.onRightClick = makeScript(c, v);
 			case "disabled":
 				if( v != "false" )
 					c.addClass(":disabled");
@@ -235,17 +249,16 @@ class Parser {
 		} catch( e : hscript.Expr.Error ) {
 			throw "Invalid Script line " + p.line + " (" + e+ ")";
 		}
-		var i = new CustomInterp();
-		i.variables.set("api", api);
-		i.variables.set("this", c);
-		i.variables.set("$", function(rq) return new h2d.comp.JQuery(c,rq));
-		return function() try i.execute(e) catch( e : String ) throw "Error while running script " + script + " (" + e + ")" catch( e : hscript.Expr.Error ) throw "Error while running script " + script + " (" + e + ")" ;
+		return function() {
+			interp.variables.set("this", c);
+			try interp.execute(e) catch( e : String ) throw "Error while running script " + script + " (" + e + ")" catch( e : hscript.Expr.Error ) throw "Error while running script " + script + " (" + e + ")" ;
+		};
 		#else
 		return function() throw "Please compile with -lib hscript to get script access";
 		#end
 	}
 	
-	public static function fromHtml( html : String, ?api : {} ) : Component {
+	public static function fromHtml( html : String, ?api ) : Component {
 		function lookupBody(x:Xml) {
 			if( x.nodeType == Xml.Element && x.nodeName.toLowerCase() == "body" )
 				return x;