2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-13 17:26:34 +00:00

AttrTemplate.pm: gui change, radio option, additional syntax (Forum #99195)

git-svn-id: https://svn.fhem.de/fhem/trunk@21316 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2020-02-29 20:24:41 +00:00
parent bb9e992fe0
commit d8b5cf61af
2 changed files with 75 additions and 16 deletions

View File

@ -2,6 +2,32 @@
# $Id$
package main;
# Syntax of the AttrTemplate file:
# - empty lines are ignored
# - lines starting with # are comments (see also desc:)
# - lines starting with the following keywords are special, all others are
# regular FHEM commands
# - name:<name> name of the template, marks the end of the previous template.
# - filter:<devspec2array-expression>, describing the list of devices this
# template is applicable for. Well be executed when the set function is
# executed.
# - prereq:<cond>, where cond is a perl expression, or devspec2array returning
# exactly one device. Evaluated at initialization(!).
# - par:<name>:<comment>:<perl>. if there is an additional argument in the set,
# name in alle commands will be replaced with it. Else <perl> will be
# excuted: if returns a value, name in the commands will be replaced with it,
# else an error message/dialog will request the user to enter a value for
# name.
# For each name starting with RADIO_, a radio select button is offered. Such
# parameters must defined last.
# - desc: additional text for the "set attrTemplate help ?". If missing, the
# last comment before name: will be used for this purpose.
# - farewell:<text> to be shown after the commands are executed.
# - order:<val> sort the templates for help purposes.
# - option:<perl> if perl code return false, skip all commands until next
# option (or name:)
my %templates;
my $initialized;
my %cachedUsage;
@ -187,14 +213,29 @@ AttrTemplate_Set($$@)
my $h = $templates{$entry};
return "Unknown template_entry_name $entry" if(!$h);
my (%repl, @mComm, @mList, $missing);
my (@na, %repl, @mComm, @mList, $missing);
for(my $i1=0; $i1<@a; $i1++) { # parse parname=value form the command line
my $fnd;
for my $k (@{$h->{pars}}) {
my ($parname, $comment, $perl_code) = split(";",$k,3);
if($a[$i1] =~ m/$parname=(.*)/) {
$repl{$parname} = $1;
return "Empty parameters are not allowed" if($1 eq "");
$fnd=1;
last;
}
}
push(@na,$a[$i1]) if(!$fnd);
}
@a = @na;
for my $k (@{$h->{pars}}) {
my ($parname, $comment, $perl_code) = split(";",$k,3);
if(@a) {
next if(defined($repl{$parname}));
if(@a) { # old-style, without prefix
$repl{$parname} = $a[0];
push(@mList, $parname);
push(@mComm, "$parname: with the $comment");
shift(@a);
next;
}
@ -210,8 +251,8 @@ AttrTemplate_Set($$@)
}
}
push(@mList, $parname);
push(@mComm, "$parname: with the $comment");
push(@mList, "$parname=...");
push(@mComm, "$parname= with $comment");
$missing = 1;
}
@ -219,19 +260,36 @@ AttrTemplate_Set($$@)
if($hash->{CL} && $hash->{CL}{TYPE} eq "FHEMWEB") {
return
"<html>".
"<input size='60' type='text' spellcheck='false' ".
"value='set $name attrTemplate $entry @mList'>".
"<br><br>Replace<br>".join("<br>",@mComm).
"<input type='hidden' value='set $name attrTemplate $entry'>".
"<p>Specify the unknown parameters for $entry:</p>".
"<table class='block wide'><tr>".
join("</tr><tr>", map {
my @t=split("= with ",$_,2);
"<td>$t[1]</td><td>" .($t[0] =~ m/^RADIO_/ ?
"<input type='radio' name='s' value='$t[0]'>":
"<input type='text' name='$t[0]' size='20'></td>")
} @mComm)."</tr></table>".
'<script>
setTimeout(function(){
// TODO: fix multiple dialog calls
$("#FW_okDialog input[type=radio]").first().prop("checked",true);
$("#FW_okDialog").parent().find("button").css("display","block");
$("#FW_okDialog").parent().find(".ui-dialog-buttonpane button")
.unbind("click").click(function(){
var val = encodeURIComponent($("#FW_okDialog input").val());
FW_cmd(FW_root+"?cmd="+val+"&XHR=1", function(resp){
if(resp)
return FW_okDialog("<pre>"+resp+"</pre>");
var cmd;
$("#FW_okDialog input").each(function(){
var t=$(this).attr("type");
if(t=="hidden") cmd = $(this).val();
if(t=="text") cmd +=" "+$(this).attr("name")+"="+$(this).val();
if(t=="radio") cmd +=" "+$(this).val()+"="+
($(this).prop("checked") ? 1:0);
});
FW_cmd(FW_root+"?cmd="+encodeURIComponent(cmd)+"&XHR=1",
function(resp){
if(resp) {
if(!resp.match(/^<html>[\s\S]*<\/html>/))
resp = "<pre>"+resp+"</pre>";
return FW_okDialog(resp);
}
location.reload()
});
$("#FW_okDialog").remove();
@ -240,8 +298,8 @@ AttrTemplate_Set($$@)
</html>';
} else {
return "Usage: set $name attrTemplate $entry @mList\nReplace\n".
join("\n", @mComm);
return "Usage: set $name attrTemplate $entry @mList\n".
"Replace the ... after\n".join("\n", @mComm);
}
}

View File

@ -579,6 +579,7 @@ FW_errmsg(txt, timeout)
function
FW_okDialog(txt, parent, removeFn)
{
$("#FW_okDialog").remove();
var div = $("<div id='FW_okDialog'>");
$(div).html(txt);
$("body").append(div);