PowerWeb.psm1


#webclasses

class WebTable{
    
    [array]$html 
    [string]$javasort = 'function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable");
  switching = true;
  //Set the sorting direction to ascending:
  dir = "asc";
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
    //start by saying: no switching is done:
    switching = false;
    rows = table.rows;
    /*Loop through all table rows (except the
    first, which contains table headers):*/
    for (i = 1; i < (rows.length - 1); i++) {
      //start by saying there should be no switching:
      shouldSwitch = false;
      /*Get the two elements you want to compare,
      one from current row and one from the next:*/
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /*check if the two rows should switch place,
      based on the direction, asc or desc:*/
      if (dir == "asc") {
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      } else if (dir == "desc") {
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /*If a switch has been marked, make the switch
      and mark that a switch has been done:*/
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      //Each time a switch is done, increase this count by 1:
      switchcount ++;
    } else {
      /*If no switching has been done AND the direction is "asc",
      set the direction to "desc" and run the while loop again.*/
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}'




    [string]$javasearch = '
 
function myFunction() {
 
  // Declare variables
  var input = document.getElementById("myInput");
  var filter = input.value.toUpperCase();
  var table = document.getElementById("myTable");
  var trs = table.tBodies[0].getElementsByTagName("tr");
 
  // Loop through first tbody rows
  for (var i = 1; i < trs.length; i++) {
 
    // define the row cells
    var tds = trs[i].getElementsByTagName("td");
 
    // hide the row
    trs[i].style.display = "none";
 
    // loop through row cells
    for (var i2 = 0; i2 < tds.length; i2++) {
 
      // if match
      if (tds[i2].innerHTML.toUpperCase().indexOf(filter) > -1) {
 
        // show the row
        trs[i].style.display = "";
 
        // skip to the next row
        continue;
 
      }
    }
  }
 
}'

    [string]$searchbar = '<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a search">'
    [string]$css
    [string]$textcolor
    [string]$backgroundcolor      
    [string]$caption
    
    
    }

Class Hbar {
    

    [array]
    $html
    
    [string]
    $textcolor
    
    [string]
    $backgroundcolor

    }

Class HTML {
        
        $title

        $refresh 
        #'<meta http-equiv="refresh" content="120">'

        
        
        }


#navbar

function Get-Hbar {
    
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, 
        ValueFromPipeline,
        ValueFromPipelineByPropertyName)]
        [array]
        $Headers,

        [string[]]
        $name,
        
        [string[]]
        $textcolor = "white",

        [string[]]
        $backgroundcolor = "green"


    )
  
  

       $list = @()
       $li = @()
        $Headers|ForEach-Object{
           
           $list += '<li><a href="' + $_ + '-link">' + $_ + "</a></li>" 
           
            }

        $list = [System.Collections.ArrayList]$list
            $list.Insert(0,"<ul>")
            $list.insert((($headers.count)+1),"</ul>")
      
      $list = if ($name -eq $null) {$list}
      elseif ($name -ne $null){
      $list|ForEach-Object{
                $index = $list[1]
                if($_ -match $index) {$_ -replace "a ", ('a class="' + $name +'" ') }
                else{$_}
                
                }
                }
    
   $hbar = New-Object Hbar
   $hbar.html = $list
   $hbar.textcolor = $textcolor
   $hbar.backgroundcolor = $backgroundcolor
   $hbar
      }

function Add-Weblinks {


[CmdletBinding()]
    param(
        [Parameter(Mandatory, 
        ValueFromPipeline,
        ValueFromPipelineByPropertyName)]
        [string[]]
        $navbar,


        

        [ValidateNotNull()]
        $links

)

    begin{
        
     $link =   $navbar.html|ForEach-Object{
    
        if($_ -match "href" -and ($i -lt $links.Count)){
       $_ -replace ($_.Split().split('>')|select-string "href"), ('href=' +'"'+ ($links[$i++]) +'"')
        
        }

        else {
            
            $_
            
            }
        $hbar = new-object hbar
        $hbar.html = $link
        $hbar.textcolor = $_.textcolor
        $hbar.backgroundcolor = $_.backgroundcolor
        $hbar 
        
        
        }
        
        
        
        }#begin

    process{
    
    
   $link =  $_.html|ForEach-Object{
    
        if($_ -match "href" -and ($i -lt $links.Count)){
       $_ -replace ($_.Split().split('>')|select-string "href"), ('href=' +'"'+ ($links[$i++]) +'"')
        
        }

        else {
            
            $_
            
            }

            
            }#foreach
    
    $hbar = new-object hbar
    $hbar.html = $link
    $hbar.textcolor = $_.textcolor
    $hbar.backgroundcolor = $_.backgroundcolor
    $hbar 
    }#process
    end{  
        
        }


    }

function Set-ActiveNavbar {
    param(
        [Parameter(
        ValueFromPipeline,
        ValueFromPipelineByPropertyName)]
        
        $navhtml,

        [string]
        $activename,

        
        $linenumber
        )

       
     
      
      $a = $navhtml.html -replace 'class="' + $activename + '"','' 
      
      $t = $navhtml.html[$linenumber] -replace "a ", ('a class="' + $activename +'" ')  
      $tee = $a -replace $a[$linenumber], $t
      $tc = $navhtml.textcolor
      $bg = $navhtml.backgroundcolor

   $navigate = [hbar]::new()
   $navigate.html = $tee
   $navigate.backgroundcolor = $bg
   $navigate.textcolor = $tc
   $navigate
    
    }




#get table html

function Get-Html{
    
    param(
        [Parameter(Mandatory)]
        [object[]]
        $data,

        [Parameter(Mandatory=$true)]
        [string[]]
        $TableName
 
       
)

function Get-HtmlTable{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, 
        ValueFromPipeline,
        ValueFromPipelineByPropertyName)]
        [string[]]
        $HTMLVariable,

        [Parameter(Mandatory=$true)]
        [string[]]
        $TableName,
 
        [ValidateSet("Table","List")]
        [string[]]
        $Type = "Table"


    )
       Begin{
        

     if ($Type -eq "table"){
        $a = $htmlvariable|select -Skip 5
        $a -replace "</body></html>",'' -replace "</table>",'' # -replace "<table>", "<table id= $TableName>"
        
        
        }#if
     elseif ($Type -eq "list"){
        write-host "List Under Construction"
        
        }#elseif
    else
        {
        
       write-host "Your lack of faith is disturbing"
        
        }
        
        
      }#Begin
      
 Process{ 
    if ($Type -eq "table"){
        [array]$z += $_;
         
     }#if
      
    elseif ($Type -eq "list"){
    write-host "List Under Construction"
        $[array]$l += $_;
        }#else if
        
    else
        {
        
    write-host "Your lack of faith is disturbing"
        
        } #else
    
        }#Process
    
    end{
     
     
     ($z|select -Skip 5) -replace "</body></html>",'' #-replace "</table>"," " -replace "<table>" , "<table id= $TableName>"
     
    
        }#End


     
   }

$TableName|ForEach-Object{

   New-Variable -name $_ -Scope Global -Force
    
    }


if($tablename.Length -eq 1){

$info = $data|convertto-html|Get-HtmlTable -TableName $tablename
Set-Variable -Name $TableName[0] -Scope Global -Value $info -Force
}

else
    {

  for($i = 0;$i -lt $tablenames.count;$i++){  
$info = $data[$i]|convertto-html|Get-HtmlTable -TableName $tablenames[$i]

Set-Variable -Name $TableName[$i] -Scope Global -Value $info -Force
   
    }}




}




function Webclass {
    
    param(
        
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        $table,
        
        [Parameter(Mandatory=$true)]
        [string]
        $id,

        [Switch]
        $Addsort,

        [Switch]
        $Addsearch,

        [string]
        $textcolor = "white",

        [string]
        $backgroundcolor = "blue",

        [string]
        $caption
        
        )

        #Replaced Table with Table ID
        $table = $table -replace "<table>", ('<table id="' +$id +'">') 
        
        #Add the On click for table sort with asterick identifier
        if($addsort){
          $table = $table -replace "<th>", ('<th onclick=' + '"sortTable(' + "*" +')">') 
            }
        
        #found the line with the asterick|convert line to string|acquire the indexes of each asterick|count the number of indexes|
        #replace the asterick with number in sequence at the string index|rejoin
       $table = $table|foreach-object{
    
    if($_ -match "\*"){
        $a = $_
        $b = $a.ToString()
        $occurs = ($_|select-string "\*" -AllMatches |ForEach-Object {$_.matches}).index
        $count = 0..($occurs.length -1)
        

        for($i=0;$i -lt $occurs.Length;$i++){
        $b = ($b.Remove($occurs[$i],1).insert($occurs[$i],$count[$i])).tostring() 
       
            }
            $b
        }
        else {
            
            $_
            }
    
    } #table foreach loop


   
    
    if($addsort -eq $true -and $addsearch -eq $false){
   
    $classyTable = New-Object Webtable
    $classytable.html = $table -replace "sortTable",("sort$id")
    $classyTable.javasort = $classyTable.javasort -replace 'myTable',"$id" -replace "sortTable",("sort$id")
    $classyTable.javasearch = $null
    $classyTable.searchbar = $null

   }# if addsort end
   elseif ($addsearch -eq $true -and $addsort -eq $false) {
   
   $classyTable = New-Object Webtable
    $classytable.html = $table
    $classyTable.javasearch = $classyTable.javasearch -replace "myInput",($id + "input") -replace "myFunction()",($id + "Function") -replace "myTable","$id" 
    $classyTable.searchbar =  $classyTable.searchbar -replace "myInput",($id + "input") -replace "myFunction()",($id + "Function")
    $classyTable.javasort = $null
     
    }
    elseif ($addsort -and $addsearch){
    
    $classyTable = New-Object Webtable
    $classytable.html = $table -replace "sortTable",("sort$id")
    $classyTable.javasort = $classyTable.javasort -replace 'myTable',"$id" -replace "sortTable",("sort$id")
    $classyTable.javasearch = $classyTable.javasearch -replace "myInput",($id + "input") -replace "myFunction()",($id + "Function") -replace "myTable","$id"
    $classyTable.searchbar =  $classyTable.searchbar -replace "myInput",($id + "input") -replace "myFunction()",($id + "Function")        
        
        
        }
    else{
    $classyTable = New-Object Webtable
    $classytable.html = $table  
    $classyTable.javasort = $null
    $classyTable.javasearch = $null
    $classyTable.searchbar = $null    
        }

    $classyTable.backgroundcolor = $backgroundcolor
    $classyTable.textcolor = $textcolor  
    $classyTable.caption = $caption
    
    
        
    if($caption){     
    $rep = $classyTable.html[1]
    $classytable.html = $classyTable.html -replace $rep,("<caption style='text-align:left'>$caption</caption>" + "`n$rep")       
    }                    
    
    $classyTable
        
          
            }



function Write-WebPage{


[CmdletBinding()]
    param(
        [Parameter(Mandatory, 
        ValueFromPipeline)]

        [array]
        $CustomObj
        )
        
        
        #arrays
        $style = @()
        $script = @('<script>')   
         
        $qty = (($CustomObj|gm).Name|select -Skip 4).count #count field names
        [System.Collections.ArrayList]$body= @()
            $n = 0..$qty
            $n|foreach-object{
            $body.insert($_," ")
    
            }   
            $body += '<div style="padding:20px;margin-top:30px;height:1500px;">'
       
        
        
        #Definitions

        $css = @()

        $navbarcss = '
body {margin:0;}
 
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
}
 
li {
  float: left;
}
 
li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
 
li a:hover:not(.active) {
  background-color: #111;
}
 
.active {
  background-color: #4CAF50;
}
'


        $trailer = '</body>', '</html>'
        

        $header = '<!DOCTYPE html>',
                  '<head>', 
                  "<title> PAGETITLE </title>",
                  '<meta http-equiv="refresh" content="SECONDS">',
                  '<style>'
                  
        $chead_obod= '</style>',
                     '</head>',  
                     '<body>'

                     
        
        $csstable = '
        #customers {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}
 
#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}
 
#customers tr:nth-child(even){background-color: #f2f2f2;}
 
#customers tr:hover {background-color: #ddd;}
 
#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}
'



        $tablebytable = '
* {
  box-sizing: border-box;
}
 
.row {
  display: flex;
  margin-left:-5px;
  margin-right:-5px;
}
 
.column {
  flex: 50%;
  padding: 5px;
}
 
table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 1px solid #ddd;
}
 
th, td {
  text-align: left;
  padding: 16px;
}
 
tr:nth-child(even) {
  background-color: #f2f2f2;
}
'

        
        #Get PSOBJECT Names in Order
        $psnames = $CustomObj
        $psnames = $psnames -split "@{" -split "=" -split ";" -split "}"
        $psnames = for ($i=0;$i -lt $psnames.Count;$i+=2) {$psnames[($i -1)]}
        $psnames = $psnames.trim()
        
         $psnames|foreach-object{
       
       #$classtype = (($CustomObj).$_).gettype().name
       $qty = (($CustomObj).$_).count
       if ($qty -eq 1){
        #HTML
            if ((($CustomObj).$_).gettype().name -match "HTML" -and (($CustomObj).$_).refresh -ne $null ){
                $header = $header -replace "PAGETITLE", (($CustomObj).$_).title -replace "SECONDS", (($CustomObj).$_).refresh
                
            }
           elseif ((($CustomObj).$_).gettype().name -match "HTML" -and $CustomObj.($psnames[$i]).refresh -eq $null ){
                $header = $header -replace "PAGETITLE", (($CustomObj).$_).title -replace '<meta http-equiv="refresh" content="SECONDS">',''
            
            }
            
        #HBAR
            elseif ((($CustomObj).$_).gettype().name -match "HBAR"){
            $getclassname = ((($CustomObj).$_).html|Where-Object{$_|select-string "class"}).split('="')[2]
             $hbar += (($CustomObj).$_).html
             $hbar += '<br><br>'
           
            
            
            $navbarcss = $navbarcss -replace '.active',('.' + $getclassname)   
            if((($CustomObj).$_).backgroundcolor -and (($CustomObj).$_).textcolor) {
                $navbarcss = $navbarcss -replace '#333',(($CustomObj).$_).backgroundcolor -replace 'white',(($CustomObj).$_).textcolor
                    }#if navbar
                elseif ((($CustomObj).$_).backgroundcolor){
                    $navbarcss = $navbarcss -replace '#333',(($CustomObj).$_).backgroundcolor
                    $css += $navbarcss
                    }#if navbar
                elseif((($CustomObj).$_).textcolor){
                     $navbarcss = $navbarcss -replace 'white',(($CustomObj).$_).textcolor
                     }#if navbar
                     
                     $css += $navbarcss
                     
                    }#hbar end
                
            #Single Table
            elseif((($CustomObj).$_).gettype().name -match "Webtable"){
            $gettablename =  ((($CustomObj).$_).html|Where-Object{$_|select-string "id"}).split('="')[2]
            
            if((($CustomObj).$_).searchbar){
                $body+= (($CustomObj).$_).searchbar
                
                }
                $body += '<div>','<br>'
                $body += (($CustomObj).$_).html
                $body += '</div>'
                $body += '<br><br>' 
                

                $newcsstable = $csstable -replace "customers",$gettablename
                if((($CustomObj).$_).backgroundcolor -and (($CustomObj).$_).textcolor){
               $newcsstable = $newcsstable -replace "#4CAF50", (($CustomObj).$_).backgroundcolor -replace 'white',(($CustomObj).$_).textcolor}
                elseif ((($CustomObj).$_).backgroundcolor){
                    $css += $newcsstable -replace "#4CAF50", (($CustomObj).$_).backgroundcolor
                    $css += $newcsstable
                    }
                elseif ((($CustomObj).$_).backgroundcolor){
                    $newcsstable = $newcsstable -replace 'white',(($CustomObj).$_).textcolor
                    $css += $newcsstable
                    }

                if((($CustomObj).$_).javasort -and (($CustomObj).$_).javasearch){
                $script += (($CustomObj).$_).javasort + (($CustomObj).$_).javasearch
                
                }####if
                elseif((($CustomObj).$_).javasort){
                   $script += (($CustomObj).$_).javasort
                        }

                elseif((($CustomObj).$_).javasearch){
                    $script += (($CustomObj).$_).javasearch
                    
                    }
                $css += $newcsstable
                } #single table end
               
                    
                  
                   

            }#if close

       elseif ($qty -gt 1)
        {
            
            $sidebyside = $classtype
            $qty = (($CustomObj).$_).count
            $num = 0..($qty -1)
            $body += '<div class="row">'

            foreach ($n in $num){
                # (($CustomObj).$_)[$n].html
                
                $body += '<div class="column">'
                $gettablename = (((($CustomObj).$_)[$n].html)|Where-Object{$_|select-string "id"}).split('="')[2]    
                $body+='<br><br>'
                if((($CustomObj).$_)[$n].searchbar){
                    
                    $body+= (($CustomObj).$_)[$n].searchbar}
                    $body+='<br><br>'
                    $body += (($CustomObj).$_)[$n].html
                    $body += '</div>',''
                     
               
                   
                    
                 $newcsstables = $csstable -replace "customers",$gettablename
                 if((($CustomObj).$_)[$n].backgroundcolor -and (($CustomObj).$_)[$n].textcolor){
                    $newcsstables = $newcsstables -replace "#4CAF50", (($CustomObj).$_)[$n].backgroundcolor -replace 'white',(($CustomObj).$_)[$n].textcolor
                    $css += $newcsstables
                    } 
                    
                 elseif ((($CustomObj).$_)[$n].backgroundcolor){
                    $newcsstables = $newcsstables -replace "#4CAF50",  (($CustomObj).$_)[$n].backgroundcolor
                    $css += $newcsstables
                    }
                 elseif ( (($CustomObj).$_)[$n].backgroundcolor){
                    $newcsstables = $newcsstables -replace 'white', (($CustomObj).$_)[$n].textcolor
                    $css += $newcsstables
                    }
                    
                if((($CustomObj).$_)[$n].javasort -and (($CustomObj).$_)[$n].javasearch){
                $script += (($CustomObj).$_)[$n].javasort + (($CustomObj).$_)[$n].javasearch
                
                }
                elseif((($CustomObj).$_)[$n].javasort){
                   $script += (($CustomObj).$_)[$n].javasort
                        }
                elseif((($CustomObj).$_)[$n].javasearch){
                    $script += (($CustomObj).$_)[$n].javasearch
                    
                    }
                 
                  $css+=$tablebytable
                  
            
                    
                   
                    
                    
                    
                    
                    
                    }#foreach loop end


        $body += '</div>',''
        
        } #else end
            
        } #foreach-object psnames end
      $script += '</script>'
      $header
      $css
      $chead_obod
      $hbar
      $body  
      $script
     


      }