AttrTemplate.pm: add order and "online" help (Forum #94495)

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@20206 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2019-09-20 10:48:29 +00:00
parent a24650ef4a
commit 52a2f4e656
2 changed files with 52 additions and 3 deletions

View File

@ -89,6 +89,7 @@ use vars qw(%FW_visibleDeviceHash);
use vars qw(@FW_httpheader); # HTTP header, line by line
use vars qw(%FW_httpheader); # HTTP header, as hash
use vars qw($FW_userAgent); # user agent string
use vars qw($FW_addJs); # Only for helper like AttrTemplate
$FW_formmethod = "post";
@ -1064,6 +1065,7 @@ FW_answerCall($)
my $n = $_; $n =~ s+.*/++; $n =~ s/.js$//; $n =~ s/fhem_//; $n .= "Param";
FW_pO sprintf($jsTemplate, AttrVal($FW_wname, $n, ""), "$FW_ME/$_");
} @jsList;
FW_pO $FW_addJs if($FW_addJs);
########################
# FW Extensions

View File

@ -5,6 +5,7 @@ package main;
my %templates;
my $initialized;
my %cachedUsage;
use vars qw($FW_addJs); # Only for helper like AttrTemplate
sub
AttrTemplate_Initialize()
@ -68,6 +69,9 @@ AttrTemplate_Initialize()
} elsif($line =~ m/^farewell:(.*)/) {
$templates{$name}{farewell} = $1;
} elsif($line =~ m/^order:(.*)/) {
$templates{$name}{order} = $1;
} else {
push(@{$templates{$name}{cmds}}, $line);
@ -80,9 +84,52 @@ AttrTemplate_Initialize()
delete($templates{$name});
}
my $nr = (int keys %templates);
@templates = sort {
my $ao = $templates{$a}{order};
my $bo = $templates{$b}{order};
$ao = (defined($ao) ? $ao : $a);
$bo = (defined($bo) ? $bo : $b);
return $ao cmp $bo;
} keys %templates;
my $nr = @templates;
$initialized = 1;
Log 2, "AttrTemplates: got $nr entries" if($nr);
$FW_addJs = "" if(!defined($FW_addJs));
$FW_addJs .= << 'JSEND';
<script type="text/javascript">
$(document).ready(function() {
$("select.set").change(attrAct);
function
attrAct(){
if($("select.set").val() == "attrTemplate") {
$('<div id="attrTemplateHelp" class="makeTable help"></div>')
.insertBefore("div.makeTable.internals");
$("select.select_widget[informid$=attrTemplate]").change(function(){
var cmd = "{AttrTemplate_Help('"+$(this).val()+"')}";
FW_cmd(FW_root+"?cmd="+cmd+"&XHR=1", function(ret) {
$("div#attrTemplateHelp").html(ret);
});
});
} else {
$("div#attrTemplateHelp").remove();
}
}
attrAct();
});
</script>
JSEND
}
sub
AttrTemplate_Help($)
{
my ($n) = @_;
return "" if(!$templates{$n});
my $ret = "";
$ret = $templates{$n}{desc} if($templates{$n}{desc});
$ret .= "<br><pre>".join("\n",@{$templates{$n}{cmds}})."</pre>";
return $ret;
}
sub
@ -100,7 +147,7 @@ AttrTemplate_Set($$@)
if($cmd ne "attrTemplate") {
if(!$cachedUsage{$name}) {
my @list;
for my $k (sort keys %templates) {
for my $k (@templates) {
my $h = $templates{$k};
my $matches;
$matches = devspec2array($h->{filter}, undef, [$name]) if($h->{filter});
@ -121,7 +168,7 @@ AttrTemplate_Set($$@)
if($entry eq "?") {
my @hlp;
for my $k (sort keys %templates) {
for my $k (@templates) {
my $h = $templates{$k};
my $matches;
$matches = devspec2array($h->{filter}, undef, [$name]) if($h->{filter});