formlayout.lzx
<library>
<class name="formlayout" extends="layout">
<attribute name="axis" value="y
" setter="this.setAxis( axis )" type="string"/>
<attribute name="inset" value="0
" setter="this.inset = inset; if( this.subviews.length ) this.update(); if (this['oninset']) this.oninset.sendEvent(this.inset)"/>
<attribute name="spacing" value="0
" setter="this.spacing = spacing; if( this.subviews.length ) this.update(); if (this['onspacing']) this.onspacing.sendEvent(this.spacing)"/>
<attribute name="align" type="string" value="left
" setter="this.align = align; if( this.subviews.length ) this.update()"/>
<attribute name="maxwidth" value="0
"/>
<attribute name="counter" value="0
"/>
<method name="setMaxwidth" args="width">
if ( this.maxwidth < width )
this.maxwidth=width;
</method>
<method name="setAxis" args="a">
this.axis = a;
this.sizeAxis = a == "x" ? "width" : "height"
</method>
<method name="addSubview" args="newsub">
this.updateDelegate.register( newsub, "on" + this.sizeAxis);
this.updateDelegate.register( newsub, "onvisible" );
//this is an optimization to avoid calling update in the simple case
//where a single subview was just added
if ( ! this.locked && this.subviews.length ){
var s= this.subviews[ this.subviews.length-1 ];
var p = s[ this.axis ] + s[ this.sizeAxis ] + this.spacing;
newsub.setAttribute( this.axis , p ) ;
}
this.counter += 1;
if( this.counter % 2 == 1 )
setMaxwidth(newsub["width"]);
super.addSubview( newsub );
</method>
<method name="update" args="v=null">
if ( this.locked ) return;
var l = this.subviews.length;
var c = this.inset;
var height = 0;
for(var i=0; i < l; i++) {
var s = this.subviews[i];
if ( !s.visible ) continue;
//edittext
if( i % 2 == 1 ){
var x = this.maxwidth + (this.spacing * 2);
if ( s[ "x" ] != x ){
s.setAttribute( "x" , x );
}
if ( s[ "y" ] != c ){
s.setAttribute( "y" , c);
}
if( height < s["height"] ) height = s["height"];
c += this.spacing + height;
//title
}else{
if( this.align == "right" )
var x = this.maxwidth - s[ "width" ] + this.spacing;
else
var x = this.spacing;
if ( s[ "x" ] != x ){
s.setAttribute( "x" , x );
}
if ( s[ "y" ] != c ){
s.setAttribute( "y" , c );
}
height=s[ "height" ];
}
}
</method>
</class>
</library>