mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-05-04 22:19:38 +00:00
AttrTemplate.pm: gui change, radio option, additional syntax (Forum #99195)
git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@21316 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
59079b089f
commit
e2efe00246
@ -2,6 +2,32 @@
|
|||||||
# $Id$
|
# $Id$
|
||||||
package main;
|
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 %templates;
|
||||||
my $initialized;
|
my $initialized;
|
||||||
my %cachedUsage;
|
my %cachedUsage;
|
||||||
@ -187,14 +213,29 @@ AttrTemplate_Set($$@)
|
|||||||
my $h = $templates{$entry};
|
my $h = $templates{$entry};
|
||||||
return "Unknown template_entry_name $entry" if(!$h);
|
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}}) {
|
for my $k (@{$h->{pars}}) {
|
||||||
my ($parname, $comment, $perl_code) = split(";",$k,3);
|
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];
|
$repl{$parname} = $a[0];
|
||||||
push(@mList, $parname);
|
|
||||||
push(@mComm, "$parname: with the $comment");
|
|
||||||
shift(@a);
|
shift(@a);
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
@ -210,8 +251,8 @@ AttrTemplate_Set($$@)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
push(@mList, $parname);
|
push(@mList, "$parname=...");
|
||||||
push(@mComm, "$parname: with the $comment");
|
push(@mComm, "$parname= with $comment");
|
||||||
$missing = 1;
|
$missing = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,19 +260,36 @@ AttrTemplate_Set($$@)
|
|||||||
if($hash->{CL} && $hash->{CL}{TYPE} eq "FHEMWEB") {
|
if($hash->{CL} && $hash->{CL}{TYPE} eq "FHEMWEB") {
|
||||||
return
|
return
|
||||||
"<html>".
|
"<html>".
|
||||||
"<input size='60' type='text' spellcheck='false' ".
|
"<input type='hidden' value='set $name attrTemplate $entry'>".
|
||||||
"value='set $name attrTemplate $entry @mList'>".
|
"<p>Specify the unknown parameters for $entry:</p>".
|
||||||
"<br><br>Replace<br>".join("<br>",@mComm).
|
"<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>
|
'<script>
|
||||||
setTimeout(function(){
|
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("button").css("display","block");
|
||||||
$("#FW_okDialog").parent().find(".ui-dialog-buttonpane button")
|
$("#FW_okDialog").parent().find(".ui-dialog-buttonpane button")
|
||||||
.unbind("click").click(function(){
|
.unbind("click").click(function(){
|
||||||
var val = encodeURIComponent($("#FW_okDialog input").val());
|
var cmd;
|
||||||
FW_cmd(FW_root+"?cmd="+val+"&XHR=1", function(resp){
|
$("#FW_okDialog input").each(function(){
|
||||||
if(resp)
|
var t=$(this).attr("type");
|
||||||
return FW_okDialog("<pre>"+resp+"</pre>");
|
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()
|
location.reload()
|
||||||
});
|
});
|
||||||
$("#FW_okDialog").remove();
|
$("#FW_okDialog").remove();
|
||||||
@ -240,8 +298,8 @@ AttrTemplate_Set($$@)
|
|||||||
</html>';
|
</html>';
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return "Usage: set $name attrTemplate $entry @mList\nReplace\n".
|
return "Usage: set $name attrTemplate $entry @mList\n".
|
||||||
join("\n", @mComm);
|
"Replace the ... after\n".join("\n", @mComm);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -579,6 +579,7 @@ FW_errmsg(txt, timeout)
|
|||||||
function
|
function
|
||||||
FW_okDialog(txt, parent, removeFn)
|
FW_okDialog(txt, parent, removeFn)
|
||||||
{
|
{
|
||||||
|
$("#FW_okDialog").remove();
|
||||||
var div = $("<div id='FW_okDialog'>");
|
var div = $("<div id='FW_okDialog'>");
|
||||||
$(div).html(txt);
|
$(div).html(txt);
|
||||||
$("body").append(div);
|
$("body").append(div);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user