ONO::Lib::UI::Radio

package ONO::Lib::UI::Radio;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

###############################################################################
# radio buttons
###############################################################################

sub onclick {

my (
$self,
$id,
$opts_ref,
$color,
) = @_;

#: Radio button onclick logic.
#: Can be used along with "buttons", but currently doesn't support lists
#: with different button colours yet.

my @RADIO = ('');
foreach my $opt (@$opts_ref) {
$opt =~ s~:(.*)$~~;
@RADIO = (@RADIO,qq~onojs_ui_radio_${id}_select('$opt','button_$color','bg_paper');~);
}
return @RADIO;

}

sub buttons {

my (
$self,
$id,
$opts_ref,
$selected,
$switches,
$JS,
) = @_;

#: Radio buttons.
#:
#: -B default color: blue
#: -c center
#: -d disable
#: -f select first is $select is empty
#: -D hard disable (almost invisible)
#: -G default color: green
#: -j add $JS to existing javascript on ALL buttons (you may add custom JS per button by using semicolons in @opts, which doesn't require -j)
#: -J use $JS instead of standard function - required for AJAX for example note that 'opt' will automatically be added to the function's arguments
#: -m mt2 - can be useful if vtop used in tables
#: -r align right
#: -R default color: red
#: -Y default color: yellow

my (@RADIO,$RESET);

my @size = (16,10,5);
my @color = ('white','fabric');
my @color_sel = ('green','paper');

if ($switches =~ /B/) {
$color_sel[0] = "blue";
}
if ($switches =~ /G/) {
$color_sel[0] = "green";
}
if ($switches =~ /R/) {
$color_sel[0] = "red";
}
if ($switches =~ /Y/) {
$color_sel[0] = "yellow";
}

foreach my $opt (@$opts_ref) {

# id:color:opts

my ($but_switch,$TRANS,$but_JS);

if ($opt =~ s/:(.*)$//) {
$but_switch = $1;
}
if ($opt =~ s/;(.*)$//) {
$but_JS = $1;
}

if ($but_switch =~ /B/) {
$color_sel[0] = "blue";
}
if ($but_switch =~ /G/) {
$color_sel[0] = "green";
}
if ($but_switch =~ /R/) {
$color_sel[0] = "red";
}
if ($but_switch =~ /Y/) {
$color_sel[0] = "yellow";
}

if ($switches =~ /f/ && !$selected) {
$selected = $opt;
}

my @color_use = @color;
if ($opt eq $selected) {
@color_use = @color_sel;
}

my $ONCLICK;
if ($switches =~ /d/i || $but_switch =~ /d/i) {
$TRANS = " trans50";
if ($switches =~ /D/ || $but_switch =~ /D/) {
$TRANS = " trans30";
}
} else {
$ONCLICK = "onojs_ui_radio_${id}_select('$opt','button_$color_sel[0]','bg_$color_sel[1]');$but_JS";
if ($switches =~ /j/) {
$ONCLICK .= $JS;
}
if ($switches =~ /J/) {
$ONCLICK = $JS;
$ONCLICK =~ s~\)~,'$opt'\)~;
}
$RESET .= qq~onojs_class('${id}_opt1_$opt','block$size[0] button_$color[0] p0 bo radius$size[1]');
onojs_class('${id}_opt2_$opt','block$size[2] bg_$color[1] bo radius$size[2] auto');
~;
}

my $RADIO = qq~<div id="${id}_opt1_$opt" class="block$size[0] button_$color_use[0] p0 bo radius$size[1]$TRANS" style="margin:0px" onclick="$ONCLICK">
<div id="${id}_opt2_$opt" class="block$size[2] bg_$color_use[1] bo radius$size[2] auto" style="width:6px;height:6px;margin-top:4px"></div>
</div>
~;

if ($switches =~ /c/) {
$RADIO = qq~<div class="inline auto">$RADIO</div>~;
}
if ($switches =~ /r/) {
$RADIO = qq~<div class="fr">$RADIO</div>~;
}
if ($switches =~ /m/) {
$RADIO = qq~<div class="mt2">$RADIO</div>~;
}

@RADIO = (@RADIO,$RADIO);

}

return (
qq~<input type="hidden" id="${id}_value_id" name="$id" value="$selected">
<script>
function onojs_ui_radio_${id}_reset() {$RESET}
function onojs_ui_radio_${id}_select(val,color1,color2) {
if (onojs_exists('${id}_value_id')) {
onojs_setvalue('${id}_value_id',val);
onojs_ui_radio_${id}_reset();
onojs_class('${id}_opt1_'+val,'block$size[0] p0 bo radius$size[1] '+color1);
onojs_class('${id}_opt2_'+val,'block$size[2] bo radius$size[2] auto '+color2);
}
}
</script>
~,
@RADIO,
);

}

###############################################################################
# end of script
###############################################################################

1;

__END__