reverselayout.lzx
<library>
<include href="utils/layouts/layout.lzx"/>
<class name="reverselayout" extends="layout">
<attribute name="axis" value="y
" setter="this.setAxis( axis )" type="string"/>
<attribute name="sizeAxis" value="height
" type="string"/>
<attribute name="inset" value="0
"/>
<attribute name="end" type="expression" value="true
"/>
<attribute name="spacing" value="0
" setter="this.spacing = spacing; if( this.subviews.length ) this.update()"/>
<method name="setAxis" args="a">
this.axis = a;
this.sizeAxis = a == "x" ? "width" : "height"
</method>
<method name="init">
super.init();
this.updateDelegate.register( this.parent, "on" + this.sizeAxis );
</method>
<handler name="onend">
this.reset();
</handler>
<method name="addSubview" args="newsub">
this.updateDelegate.register( newsub, "on" + this.sizeAxis);
this.updateDelegate.register( newsub, "onvisible" );
super.addSubview( newsub );
// A new view means resetting everything
this.reset();
</method>
<method name="update" args="v=null">
if ( this.locked ) return;
var parentsize = this.parent[ this.sizeAxis ];
if (this.end) {
var total = this.inset;
var l = this.subviews.length;
for(var i=0; i < l; i++) {
var s = this.subviews[i];
if ( !s.visible ) continue;
// use computed bounds when necessary
if (s.usegetbounds){
s = s.getBounds();
}
total += s[ this.sizeAxis ] + spacing;
}
if (total < parentsize) {
total = parentsize;
}
var c = total - this.inset;
for(var i=0; i < l; i++) {
var s = this.subviews[i];
if ( !s.visible ) continue;
// use computed bounds when necessary
if (s.usegetbounds){
var bounds = s.getBounds();
} else {
var bounds = s;
}
c -= bounds[ this.sizeAxis ];
if (c) s.setAttribute( this.axis , c );
c -= this.spacing;
}
} else {
var l = this.subviews.length;
var c = this.inset;
for(var i=l-1; i >= 0; i--) {
var s = this.subviews[i];
if ( !s.visible ) continue;
if ( s[ this.axis ] != c ){
s.setAttribute( this.axis , c );
}
// use computed bounds when necessary
if (s.usegetbounds){
s = s.getBounds();
}
c += this.spacing + s[ this.sizeAxis ];
}
}
</method>
<doc>
<tag name="shortdesc"><text>A layout for arranging siblings vertically or horizontally in reverse order.</text></tag>
<text>
<para><classname>reverselayout</classname> is just like <sgmltag class="element" role="lz.simplelayout"><simplelayout></sgmltag>,
except that the order of the views it keeps is reversed (bottom to top, or right to left).</para>
<para><classname>reverselayout</classname> extends <sgmltag class="element" role="LzLayout"><layout></sgmltag>, and therefore it is responsible
for arranging all of the subviews for the view that it is attached to. The attributes of a view that <classname>reverselayout</classname>
effects are the <literal>x</literal> and <literal>y</literal> values.</para>
<example title="Example of reverselayout">
<canvas height="60" >
<include href="utils/layouts/reverselayout.lzx"/>
<view bgcolor="yellow">
<reverselayout axis="x" spacing="10"/>
<text text="one"/>
<text text="two"/>
<text text="three"/>
</view>
</canvas>
</example>
<example title="Example of reverselayout with end='false'">
<canvas height="60">
<include href="utils/layouts/reverselayout.lzx"/>
<view bgcolor="yellow">
<reverselayout axis="x" end="false" spacing="10"/>
<text text="one"/>
<text text="two"/>
<text text="three"/>
</view>
</canvas>
</example>
</text>
</doc>
</class>
</library>
Cross References
Includes
Classes