2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-25 03:44:52 +00:00

fhemweb.js: Add function to the dropdown (Forum #129858)

git-svn-id: https://svn.fhem.de/fhem/trunk@26600 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2022-10-27 11:35:56 +00:00
parent 69ac334900
commit 1480921267
2 changed files with 134 additions and 76 deletions

View File

@ -88,7 +88,7 @@ sub OldReadingsNum($$$;$);
sub OldReadingsTimestamp($$$); sub OldReadingsTimestamp($$$);
sub OldReadingsVal($$$); sub OldReadingsVal($$$);
sub OpenLogfile($); sub OpenLogfile($);
sub PrintHash($$); sub PrintHash($$;$);
sub ReadingsAge($$$); sub ReadingsAge($$$);
sub ReadingsNum($$$;$); sub ReadingsNum($$$;$);
sub ReadingsTimestamp($$$); sub ReadingsTimestamp($$$);
@ -2564,37 +2564,40 @@ CommandSetReading($$)
############# #############
sub sub
PrintHash($$) PrintHash($$;$)
{ {
my ($h, $lev) = @_; my ($h, $lev, $prefix) = @_;
my $si = AttrVal("global", "showInternalValues", 0); my $si = AttrVal("global", "showInternalValues", 0);
return "" if($h->{".visited"}); return "" if($h->{".visited"});
$h->{".visited"} = 1; $h->{".visited"} = 1;
$prefix="" if(!$prefix);
my ($str,$sstr) = ("",""); my ($str,$sstr) = ("","");
foreach my $c (sort keys %{$h}) { foreach my $c (sort keys %{$h}) {
next if(!$si && $c =~ m/^\./ || $c eq ".visited"); next if(!$si && $c =~ m/^\./ || $c eq ".visited");
if(ref($h->{$c})) { if(ref($h->{$c})) {
if(ref($h->{$c}) eq "HASH") { if(ref($h->{$c}) eq "HASH") {
if(defined($h->{$c}{TIME}) && defined($h->{$c}{VAL})) { if(defined($h->{$c}{TIME}) && defined($h->{$c}{VAL})) {
$str .= sprintf("%*s %-19s %-15s %s\n", $str .= sprintf("$prefix%*s %-19s %-15s %s\n",
$lev," ", $h->{$c}{TIME},$c,$h->{$c}{VAL}); $lev," ", $h->{$c}{TIME},$c,$h->{$c}{VAL});
} elsif($c eq "IODev" || $c eq "HASH") { } elsif($c eq "IODev" || $c eq "HASH") {
$str .= sprintf("%*s %-10s %s\n", $lev," ",$c, $h->{$c}{NAME}); $str .= sprintf("$prefix%*s %-10s %s\n", $lev," ",$c, $h->{$c}{NAME});
} else { } else {
$sstr .= sprintf("%*s %s:\n", $lev, " ", $c); $sstr .= sprintf("$prefix%*s %s:\n", $lev, " ", $c);
$sstr .= PrintHash($h->{$c}, $lev+2); $sstr .= PrintHash($h->{$c}, $lev+2, $prefix);
} }
} elsif(ref($h->{$c}) eq "ARRAY") { } elsif(ref($h->{$c}) eq "ARRAY") {
$sstr .= sprintf("%*s %s:\n", $lev, " ", $c); $sstr .= sprintf("$prefix%*s %s:\n", $lev, " ", $c);
foreach my $v (@{$h->{$c}}) { foreach my $v (@{$h->{$c}}) {
$sstr .= sprintf("%*s %s\n", $lev+2, " ", defined($v) ? $v:"undef"); $sstr .= sprintf("$prefix%*s %s\n",
$lev+2, " ", defined($v) ? $v:"undef");
} }
} }
} else { } else {
my $v = $h->{$c}; my $v = $h->{$c};
$str .= sprintf("%*s %-10s %s\n", $lev," ",$c, defined($v) ? $v : ""); $str .= sprintf("$prefix%*s %-10s %s\n",
$lev," ",$c, defined($v) ? $v : "");
} }
} }
delete $h->{".visited"}; delete $h->{".visited"};
@ -2607,22 +2610,25 @@ CommandList($$)
{ {
my ($cl, $param) = @_; my ($cl, $param) = @_;
my $str = ""; my $str = "";
my %opt;
my $optRegexp = '-r|-R|-i';
$param = cmd_parseOpts($param, $optRegexp, \%opt);
if($param =~ m/^-r *(.*)$/i) { if($opt{r} || $opt{R}) {
my @list; my @list;
my $arg = $1; if($opt{R}) {
if($param =~ m/^-R/) { return "-R needs a valid device as argument" if(!$param);
return "-R needs a valid device as argument" if(!$arg); push @list, $param;
push @list, $arg; push @list, getPawList($param);
push @list, getPawList($arg);
} else { } else {
@list = devspec2array($arg ? $arg : ".*", $cl); @list = devspec2array($param ? $param : ".*", $cl);
} }
foreach my $d (@list) { foreach my $d (@list) {
return "No device named $d found" if(!defined($defs{$d})); return "No device named $d found" if(!defined($defs{$d}));
$str .= "\n" if($str); $str .= "\n" if($str);
my @a = GetDefAndAttr($d); my @a = GetDefAndAttr($d);
$str .= join("\n", @a)."\n" if(@a); $str .= join("\n", @a)."\n" if(@a);
$str .= PrintHash($defs{$d}, 2, "#") if($opt{i});
} }
foreach my $d (sort @list) { foreach my $d (sort @list) {
$str .= "\n" if($str); $str .= "\n" if($str);

View File

@ -132,7 +132,6 @@ FW_jqueryReadyFn()
FW_root = r; FW_root = r;
FW_replaceWidgets($("html")); FW_replaceWidgets($("html"));
FW_confirmDelete();
// Fix the td count by setting colspan on the last column // Fix the td count by setting colspan on the last column
$("table.block.wide").each(function(){ // table $("table.block.wide").each(function(){ // table
@ -233,26 +232,6 @@ FW_jqueryReadyFn()
}); });
}); });
$("div.devSpecHelp a").each(function(){ // Help on detail window
var dev = FW_getLink(this).split("#").pop();
$(this).unbind("click");
$(this).attr("href", "#"); // Desktop: show underlined Text
$(this).removeAttr("onclick");
$(this).click(function(evt){
if($("#devSpecHelp").length) {
$("#devSpecHelp").remove();
return;
}
FW_getHelp(dev, function(data){
$("#content").append('<div id="devSpecHelp"></div>');
$("#devSpecHelp").html(data);
var off = $("#devSpecHelp").position().top-20;
$('body, html').animate({scrollTop:off}, 500);
});
});
});
$("table.attributes tr div.dname") // Click on attribute fills input value $("table.attributes tr div.dname") // Click on attribute fills input value
.each(function(){ .each(function(){
$(this) $(this)
@ -315,7 +294,7 @@ FW_jqueryReadyFn()
FW_smallScreenCommands(); FW_smallScreenCommands();
FW_inlineModify(); FW_inlineModify();
FW_rawDef(); FW_detLink();
FW_treeMenu(); FW_treeMenu();
$("body").attr("data-os", FW_os); $("body").attr("data-os", FW_os);
@ -464,48 +443,68 @@ FW_filterIcons()
} }
} }
function function
FW_confirmDelete() FW_deleteDevice(dev)
{ {
var b = $("body"); var cmd = addcsrf(FW_root+"?cmd=delete "+dev);
var cd = $(b).attr("data-confirmDelete");
if(!cd || cd == 0) var cd = $("body").attr("data-confirmDelete");
if(!cd || cd == 0) {
location.href = cmd;
return; return;
var wn = $(b).attr("data-webName"); }
$("div#content").find("a").each(function(){
var href = $(this).attr("href");
if(!href)
return;
var ma = $(this).attr("href").match(/.*cmd[^=]*=(delete[^&]*).*$/);
if(!ma || ma.length != 2)
return;
$(this).attr("href", "#");
$(this).unbind("click");
$(this).click(function(e){
e.preventDefault();
var div = $("<div id='FW_okDialog'>"); var div = $("<div>");
$(div).html("Do you really want to "+ma[1]+"?<br><br>"+ $(div).html("Do you really want to delete "+dev+"?<br><br>"+
"<input type='checkbox' name='noconf'> Skip this dialog in the future"); "<input type='checkbox' name='noconf'> Skip this dialog in the future");
$("body").append(div); $("body").append(div);
function $(div).dialog({
doClose() dialogClass:"no-close", modal:true, width:"auto", closeOnEscape:true,
{ maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
if($(div).find("input:checked").length) buttons: [
FW_cmd(FW_root+"?cmd=attr "+wn+" confirmDelete 0&XHR=1"); {text:"Yes", click:function(){ doClose(); location.href = cmd; }},
$(this).dialog("close"); $(div).remove(); {text:"No", click:doClose} ],
} close: doClose
$(div).dialog({
dialogClass:"no-close", modal:true, width:"auto", closeOnEscape:true,
maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
buttons: [
{text:"Yes", click:function(){ location.href = ma[0]; doClose(); }},
{text:"No", click:function(){ doClose(); }}]
});
});
}); });
function
doClose()
{
var wn = $("body").attr("data-webName");
if($(div).find("input:checked").length)
FW_cmd(FW_root+"?cmd=attr "+wn+" confirmDelete 0&XHR=1");
$(this).dialog("close"); $(div).remove();
}
}
function
FW_renameDevice(dev)
{
var div = $("<div>");
$(div).html('Rename '+dev+' to:<br><br><input type="text" size="30">');
$("body").append(div);
$(div).dialog({
dialogClass:"no-close", modal:true, width:"auto", closeOnEscape:true,
maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
buttons: [
{text:"Rename", click:function(){
var nn = $(div).find("input").val();
if(!nn.match(/^[a-z0-9._]*$/i))
return FW_okDialog("Illegal characters in the new name");
location.href = addcsrf(FW_root+"?cmd=rename "+dev+" "+nn);
}},
{text:"Cancel", click:doClose} ],
close: doClose
});
function
doClose()
{
$(this).dialog("close"); $(div).remove();
}
} }
// Show the webCmd list in a dialog if: smallScreen & hiddenroom=detail & room // Show the webCmd list in a dialog if: smallScreen & hiddenroom=detail & room
@ -902,8 +901,9 @@ FW_inlineModify() // Do not generate a new HTML page upon pressing modify
}); });
} }
// Fill the "detLink" line with life
function function
FW_rawDef() FW_detLink()
{ {
$("div.rawDef a").each(function(){ // Help on detail window $("div.rawDef a").each(function(){ // Help on detail window
var dev = FW_getLink(this).split(" ").pop().split("&")[0]; var dev = FW_getLink(this).split(" ").pop().split("&")[0];
@ -976,6 +976,58 @@ FW_rawDef()
}); });
}); });
$("#detLink .devSpecHelp a").each(function(){ // Help on detail window
var dev = FW_getLink(this).split("#").pop();
$(this).unbind("click");
$(this).attr("href", "#"); // Desktop: show underlined Text
$(this).removeAttr("onclick");
$(this).click(function(evt){
if($("#devSpecHelp").length) {
$("#devSpecHelp").remove();
return;
}
FW_getHelp(dev, function(data){
$("#content").append('<div id="devSpecHelp"></div>');
$("#devSpecHelp").html(data);
var off = $("#devSpecHelp").position().top-20;
$('body, html').animate({scrollTop:off}, 500);
});
});
});
$("#detLink select#moreCmds").change(function(){
var cmd = $(this).find("option:selected").attr("data-cmd");
if(!cmd)
return;
var m = cmd.match(/^([^ ]+) (.*)$/);
if(!m)
return;
if(m[1] == "forumCopy") {
if(!navigator.clipboard)
return FW_okDialog("Sorry, not supported by this browser");
FW_cmd(FW_root+"?cmd=list -r -i "+m[2]+"&XHR=1", function(data) {
navigator.clipboard.writeText('[code]'+data+'[/code]').then(
function(){
FW_okDialog('"forum ready" definition copied to the clipboard.') },
function(err) { FW_okDialog('Could not copy text: ', err)}
)
})
} else if(m[1] == "rename") {
FW_renameDevice(m[2]);
} else if(m[1] == "delete") {
FW_deleteDevice(m[2]);
} else {
location.href = addcsrf(FW_root+"?cmd="+cmd);
}
});
} }
function function