HTML-Obj2HTML
view release on metacpan or search on metacpan
lib/HTML/Obj2HTML/Plugin/SemanticUIForms.pm view on Meta::CPAN
delete($obj->{options});
delete($obj->{hiddenoptions});
}
if ($obj->{optionsql} && ref $obj->{optionsql} eq "ARRAY") {
my @contents = ();
if (defined $obj->{inclblank}) { push(@contents, option => { value => "", _ => $obj->{inclblank} }); }
if (!$obj->{valuefield}) { $obj->{valuefield} = "id"; }
if (!$obj->{textfield}) { $obj->{textfield} = "name"; }
for (my $r = $db->for(@{$obj->{optionsql}}); $r->more; $r->next) {
my $opt = { value => $r->{$obj->{valuefield}}, _ => $r->{$obj->{textfield}} };
if (!$obj->{multiple}) {
if (defined $obj->{value} && "$obj->{value}" eq "$r->{$obj->{valuefield}}") { $opt->{selected} = 1; }
} else {
if (defined $obj->{selectedfield} && $r->{$obj->{selectedfield}}) {
$opt->{selected} = 1;
} elsif (defined $obj->{value} && ($obj->{value} & $r->{$obj->{valuefield}})) {
$opt->{selected} = 1;
}
if (defined $obj->{values}) {
if (grep({$_ eq $r->{$obj->{valuefield}}} @{$obj->{values}})) { $opt->{selected} = 1; }
}
}
if ($readonly) {
if ($opt->{selected}) {
push(@contents, div => $opt->{_});
}
} else {
push(@contents, option => $opt);
}
}
$obj->{_} = \@contents;
delete($obj->{values});
delete($obj->{optionsql});
delete($obj->{valuefield});
delete($obj->{textfield});
}
if (!$obj->{_}) { $obj->{_} = []; }
delete($obj->{value});
delete($obj->{inclblank});
if ($readonly) {
return HTML::Obj2HTML::gen(commonfield($obj, $obj->{_}));
} else {
return HTML::Obj2HTML::gen(commonfield($obj, [ select => $obj ]));
}
}
});
HTML::Obj2HTML::register_extension("datefield", {
tag => "",
before => sub {
my $obj = shift;
if (ref $obj ne "HASH") { return ""; }
my $readonly = HTML::Obj2HTML::get_opt("readonly") || $obj->{readonly};
delete($obj->{readonly});
if ($readonly) {
return HTML::Obj2HTML::gen(commonfield($obj, [ span => $obj->{value} ] ));
} else {
return HTML::Obj2HTML::gen(commonfield($obj, [
div => { class => "ui calendar ".$obj->{class}, _ => [
div => { class => "ui input left icon", _ => [
i => { class => "calendar icon", _ => [] },
input => { type => "text", name => $obj->{name}, placeholder => $obj->{placeholder}, value => $obj->{value} }
]}
]}
]));
}
}
});
# Low level inputs - no "field" div
HTML::Obj2HTML::register_extension("checkbox", {
tag => "",
before => sub {
my $obj = shift;
if (ref $obj ne "HASH") { return ""; }
my $readonly = HTML::Obj2HTML::get_opt("readonly") || $obj->{readonly};
delete($obj->{readonly});
# TO-FIX: This should be multi param as multiple checkboxes might have the same name resulting in multiple values
if (!defined $obj->{value}) { $obj->{value} = 1; }
if (defined $obj->{name}) {
my $cgi = HTML::Obj2HTML::get_opt("CGI-params");
if (defined $cgi) {
my $val = $cgi->param($obj->{name});
if (defined $val && "$val" eq $obj->{value}) { $obj->{checked} = 1; } else { delete($obj->{checked}); }
}
}
if ($readonly) {
return HTML::Obj2HTML::gen([ div => [
if => { cond => $obj->{checked}, true => [ icon => 'green check' ], false => [ icon => 'red close' ]},
_ => " ".$obj->{label}
]]);
} else {
my $label = $obj->{label}; delete($obj->{label});
if (!$label && $obj->{checkboxlabel}) { $label = $obj->{checkboxlabel}; delete($obj->{checkboxlabel}); }
$obj->{if} = { cond => $obj->{checked}, true => { checked => 1 } }; delete($obj->{checked});
$obj->{type} = "checkbox";
return HTML::Obj2HTML::gen([
div => { class => 'ui checkbox', _ => [
input => $obj, label => $label
] }
]);
}
}
});
HTML::Obj2HTML::register_extension("radio", {
tag => "",
before => sub {
my $obj = shift;
if (ref $obj ne "HASH") { return ""; }
my $readonly = HTML::Obj2HTML::get_opt("readonly") || $obj->{readonly};
delete($obj->{readonly});
lib/HTML/Obj2HTML/Plugin/SemanticUIForms.pm view on Meta::CPAN
if ($readonly) {
return HTML::Obj2HTML::gen([ div => [
if => { cond => $obj->{checked}, true => [ icon => 'check' ] },
_ => " ".$obj->{label}
]]);
} else {
my $label = $obj->{label}; delete($obj->{label});
if (!$label && $obj->{radiolabel}) { $label = $obj->{radiolabel}; delete($obj->{radiolabel}); }
$obj->{if} = { cond => $obj->{checked}, true => { checked => 1 } }; delete($obj->{checked});
$obj->{type} = "radio";
return HTML::Obj2HTML::gen([
div => { class => 'ui radio checkbox', _ => [
input => $obj, label => $label
] }
]);
}
}
});
HTML::Obj2HTML::register_extension("labeledinput", {
tag => "",
before => sub {
my $obj = shift;
if (ref $obj ne "HASH") { return ""; }
my $readonly = HTML::Obj2HTML::get_opt("readonly") || $obj->{readonly};
delete($obj->{readonly});
if (defined $obj->{name}) {
my $cgi = HTML::Obj2HTML::get_opt("CGI-params");
if (defined $cgi) {
my $val = $cgi->param($obj->{name});
if (defined $val) { $obj->{value} = $val; }
}
}
if ($readonly) {
return [ div => $obj->{value}." ".$obj->{label} ];
} else {
my $label = $obj->{label}; delete($obj->{label});
# The has we were passed actually belongs to a child element, we need to copy and clear.
my $inputobj = {};
for (keys %$obj) { $inputobj->{$_} = $obj->{$_}; delete $obj->{$_}; }
return [ div => { class => "ui right labeled input", _ => [
input => $inputobj,
div => { class => "ui basic label", _ => $label }
]}];
}
}
});
HTML::Obj2HTML::register_extension("dateinput", {
tag => "",
before => sub {
my $obj = shift;
if (ref $obj ne "HASH") { return ""; }
return HTML::Obj2HTML::gen([
div => { class => "ui calendar dateonly", _ => [
div => { class => "ui input left icon", _ => [
i => { class => "calendar icon", _ => [] },
labeledinput => $obj
]}
]}
]);
}
});
HTML::Obj2HTML::register_extension("hiddeninput", {
tag => "input",
attr => { type => "hidden" }
});
HTML::Obj2HTML::register_extension("submit", {
tag => "",
before => sub {
my $obj = shift;
if (ref $obj ne "HASH") { $obj = { _ => $obj }; }
my $readonly = HTML::Obj2HTML::get_opt("readonly") || $obj->{readonly};
delete($obj->{readonly});
if ($readonly) {
return [];
}
if (!ref $obj) { $obj = { value => $obj }; } else { $obj->{value} = $obj->{label}; delete($obj->{label}); }
if (defined $obj->{class}) { $obj->{class} .= " ui button"; } else { $obj->{class}.="ui positive button"; }
$obj->{type} = "submit";
return [ input => $obj ];
},
});
HTML::Obj2HTML::register_extension("cancel", {
tag => "",
before => sub {
my $obj = shift;
if (ref $obj ne "HASH") { $obj = { _ => $obj }; }
my $readonly = HTML::Obj2HTML::get_opt("readonly") || $obj->{readonly};
delete($obj->{readonly});
if ($readonly) {
return [];
}
if (!ref $obj) { $obj = { value => $obj }; } else { $obj->{value} = $obj->{label}; delete($obj->{label}); }
if ($obj->{class}) { $obj->{class} .= " "; }
$obj->{class}.="ui negative button";
return [ a => $obj ];
},
});
HTML::Obj2HTML::register_extension("helplabel", {
tag => "label",
before => sub {
my $o = shift;
if (ref $o ne "HASH") { $o = { helptext => $o }; }
$o->{_} = [ _ => $o->{label} ];
( run in 0.941 second using v1.01-cache-2.11-cpan-524268b4103 )