View previous topic :: View next topic |
Author |
Message |
simhau
Joined: 06 Feb 2007 Posts: 55
|
Posted: Mon Apr 07, 2008 10:20 am Post subject: behavior::select-buddy |
|
|
I ported the select-buddy behavior. This is done pretty much line by line, and I don't know c++, d or htmlayout, sooo... Check for errors
Code: |
/*
BEHAVIOR: select-buddy
goal: Auxiliary buttons for various <select multiple> actions.
actions: "select-all" - select all options
"clear-all" - deselect all options
for pair of <select>s:
"move-all" - move all options from source to destination
"move-selected" - move selected options from source to destination
"revoke-all" - move all options from destination to source
"revoke-selected" - move all options from destination to source
TYPICAL USE CASE:
<div style="behavior:select-buddy">
<widget type="select" multiple="checks" role="source">
<option>...
</widget>
<input type="button" action="move-all">move all</input>
<input type="button" action="move-selected">move selected</button>
<widget type="select" multiple="checks" role="destination">
<option>...
</widget>
</div>
SAMPLE:
*/
module htmlayout.behaviors.select_buddy;
import htmlayout.events;
import htmlayout.capi;
import htmlayout.element;
import Integer = tango.text.convert.Integer;
static this()
{
new HBehaviorSelectBuddy;
}
/// Selects all options in multiselect
void selectAllOptions(HElement el)
{
HElement[] selected = el.select("option");
foreach( sel; selected )
sel.state(STATE_CHECKED, 0, false);
el.update();
}
/// Clear checked states in multiselect <select>.
/// This simply resets :checked state for all checked <option>'s
void clearAllOptions(HElement el)
{
HElement[] selected = el.select("option:checked,[role='option']:checked");
foreach( sel; selected )
sel.state(0, STATE_CHECKED, false);
el.update();
}
class HBehaviorSelectBuddy : HBehavior
{
this()
{
super( "select-buddy", HANDLE_BEHAVIOR_EVENT );
}
bool onBehaviorEvent(HBehaviorEvent evt)
{
if( evt.cmd == BUTTON_CLICK )
{
auto target = new HElement(evt.heTarget);
auto action = target.attribute("action");
switch( action )
{
case "select-all":
return doSelectAll(evt.element);
case "clear-all":
return doClearAll(evt.element);
case "move-all":
return doMove(evt.element, true, false);
case "move-selected":
return doMove(evt.element, true, true);
case "revoke-all":
return doMove(evt.element, false, false);
case "revoke-selected":
return doMove(evt.element, false, true);
default:
return false;
}
}
return false;
}
bool doSelectAll(HElement el)
{
HElement[] select = el.select("[role='source']");
if( select.length )
{
selectAllOptions(el);
return true;
}
else
{
return false;
}
}
bool doClearAll(HElement el)
{
HElement[] select = el.select("[role='source']");
if( select.length )
{
clearAllOptions(el);
return true;
}
else
{
return false;
}
}
bool doMove(HElement el, bool srcToDst, bool selectedOnly)
{
HElement src = el.select("[role='source']")[0];
HElement dst = el.select("[role='destination']")[0];
if( !srcToDst ) // Swap 'em
{
HElement tmp = src;
src = dst;
dst = tmp;
}
HElement[] options = src.select(
selectedOnly?"option:checked":"option");
if( srcToDst )
{
foreach_reverse( sel; options )
{
sel.attribute("-srcindex", Integer.toString(sel.index));
sel.insert(dst, 0);
}
}
else
{
foreach( sel; options )
{
auto idx = Integer.toInt(sel.attribute("-srcindex"));
sel.insert(dst, idx);
}
}
src.update();
dst.update();
return true;
}
}
|
|
|
Back to top |
|
|
bobef
Joined: 05 Jun 2005 Posts: 269
|
Posted: Wed Apr 09, 2008 3:16 am Post subject: |
|
|
Looks nice and clean. Pretty good for someone who doesn't know c++ or d or htmlayout I haven't tried it but you probably did, so if it is working I could include it in the distribution (with your permission). |
|
Back to top |
|
|
simhau
Joined: 06 Feb 2007 Posts: 55
|
Posted: Wed Apr 09, 2008 6:55 am Post subject: |
|
|
Yes, it is tested and works. Feel free to add and change as you please. I'm porting a couple of other behaviors too. How would you like them? Ticket, forum or mail? |
|
Back to top |
|
|
bobef
Joined: 05 Jun 2005 Posts: 269
|
Posted: Wed Apr 09, 2008 12:46 pm Post subject: |
|
|
Tickets support attachments, but send it the way you prefer, I don't mind. My email is bobef /at/ abv /dot/ bg . Thanks! |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|