From 52aacaf8b7d884611663970b167f6a5ebd222003 Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Thu, 27 Oct 2022 11:35:56 +0000 Subject: [PATCH] fhemweb.js: Add function to the dropdown (Forum #129858) git-svn-id: https://svn.fhem.de/fhem/trunk@26600 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/fhem.pl | 40 +++++---- fhem/www/pgm2/fhemweb.js | 170 +++++++++++++++++++++++++-------------- 2 files changed, 134 insertions(+), 76 deletions(-) diff --git a/fhem/fhem.pl b/fhem/fhem.pl index 7da4b3832..7283673b1 100755 --- a/fhem/fhem.pl +++ b/fhem/fhem.pl @@ -88,7 +88,7 @@ sub OldReadingsNum($$$;$); sub OldReadingsTimestamp($$$); sub OldReadingsVal($$$); sub OpenLogfile($); -sub PrintHash($$); +sub PrintHash($$;$); sub ReadingsAge($$$); sub ReadingsNum($$$;$); sub ReadingsTimestamp($$$); @@ -2564,37 +2564,40 @@ CommandSetReading($$) ############# sub -PrintHash($$) +PrintHash($$;$) { - my ($h, $lev) = @_; + my ($h, $lev, $prefix) = @_; my $si = AttrVal("global", "showInternalValues", 0); return "" if($h->{".visited"}); $h->{".visited"} = 1; + $prefix="" if(!$prefix); my ($str,$sstr) = ("",""); foreach my $c (sort keys %{$h}) { next if(!$si && $c =~ m/^\./ || $c eq ".visited"); if(ref($h->{$c})) { if(ref($h->{$c}) eq "HASH") { 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}); } 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 { - $sstr .= sprintf("%*s %s:\n", $lev, " ", $c); - $sstr .= PrintHash($h->{$c}, $lev+2); + $sstr .= sprintf("$prefix%*s %s:\n", $lev, " ", $c); + $sstr .= PrintHash($h->{$c}, $lev+2, $prefix); } } 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}}) { - $sstr .= sprintf("%*s %s\n", $lev+2, " ", defined($v) ? $v:"undef"); + $sstr .= sprintf("$prefix%*s %s\n", + $lev+2, " ", defined($v) ? $v:"undef"); } } } else { 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"}; @@ -2607,22 +2610,25 @@ CommandList($$) { my ($cl, $param) = @_; 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 $arg = $1; - if($param =~ m/^-R/) { - return "-R needs a valid device as argument" if(!$arg); - push @list, $arg; - push @list, getPawList($arg); + if($opt{R}) { + return "-R needs a valid device as argument" if(!$param); + push @list, $param; + push @list, getPawList($param); } else { - @list = devspec2array($arg ? $arg : ".*", $cl); + @list = devspec2array($param ? $param : ".*", $cl); } foreach my $d (@list) { return "No device named $d found" if(!defined($defs{$d})); $str .= "\n" if($str); my @a = GetDefAndAttr($d); $str .= join("\n", @a)."\n" if(@a); + $str .= PrintHash($defs{$d}, 2, "#") if($opt{i}); } foreach my $d (sort @list) { $str .= "\n" if($str); diff --git a/fhem/www/pgm2/fhemweb.js b/fhem/www/pgm2/fhemweb.js index 638b26972..1c6cfd012 100644 --- a/fhem/www/pgm2/fhemweb.js +++ b/fhem/www/pgm2/fhemweb.js @@ -132,7 +132,6 @@ FW_jqueryReadyFn() FW_root = r; FW_replaceWidgets($("html")); - FW_confirmDelete(); // Fix the td count by setting colspan on the last column $("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('
'); - $("#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 .each(function(){ $(this) @@ -315,7 +294,7 @@ FW_jqueryReadyFn() FW_smallScreenCommands(); FW_inlineModify(); - FW_rawDef(); + FW_detLink(); FW_treeMenu(); $("body").attr("data-os", FW_os); @@ -464,48 +443,68 @@ FW_filterIcons() } } + function -FW_confirmDelete() +FW_deleteDevice(dev) { - var b = $("body"); - var cd = $(b).attr("data-confirmDelete"); - if(!cd || cd == 0) + var cmd = addcsrf(FW_root+"?cmd=delete "+dev); + + var cd = $("body").attr("data-confirmDelete"); + if(!cd || cd == 0) { + location.href = cmd; 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).html("Do you really want to "+ma[1]+"?

"+ - " Skip this dialog in the future"); - $("body").append(div); + var div = $("
"); + $(div).html("Do you really want to delete "+dev+"?

"+ + " Skip this dialog in the future"); + $("body").append(div); - function - doClose() - { - if($(div).find("input:checked").length) - FW_cmd(FW_root+"?cmd=attr "+wn+" confirmDelete 0&XHR=1"); - $(this).dialog("close"); $(div).remove(); - } - - $(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(); }}] - }); - }); + $(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(){ doClose(); location.href = cmd; }}, + {text:"No", click:doClose} ], + close: 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).html('Rename '+dev+' to:

'); + $("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 @@ -902,8 +901,9 @@ FW_inlineModify() // Do not generate a new HTML page upon pressing modify }); } +// Fill the "detLink" line with life function -FW_rawDef() +FW_detLink() { $("div.rawDef a").each(function(){ // Help on detail window 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('
'); + $("#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