11import { extract_info } from "./extract.js" ;
22
3+ /**
4+ * Fetches json_document and displays their contents in a table.
5+ * @param {Path } json_document - document do fetch data from.
6+ */
37export function displayJSON ( json_document ) {
4-
58 fetch ( json_document )
69 . then ( response => response . json ( ) )
710 . then ( data => {
11+ //Get data snippet from url
812 const params = new URLSearchParams ( location . search ) ;
913 if ( params . has ( "id" ) ) {
1014 const id = params . get ( "id" )
1115 data = get_data_snippet ( data , "@id" , id ) ;
1216
17+ //If your seeing a data snippet, create button to go back
1318 const back = document . createElement ( "button" ) ;
1419 back . innerText = "Back to Overview" ;
1520 back . onclick = ( ) => { window . location = window . location . href . split ( '?' ) [ 0 ] ; }
1621 document . body . appendChild ( back ) ;
1722 }
1823
24+ // Apply and fill in the template for Metadata
1925 const keys = Object . keys ( data ) ;
2026
2127 const metadateTemp = document . querySelector ( "#metadate" ) ;
@@ -27,60 +33,58 @@ export function displayJSON(json_document){
2733 const header = document . querySelector ( "#header-policies" ) ;
2834 header . style . display = "none" ;
2935
36+
3037 keys . forEach ( element => {
38+ // Get a something with Name as p Header
3139 if ( element . toLowerCase ( ) . includes ( "name" ) ) {
32- console . log ( element ) ;
3340 document . getElementById ( "project-name" ) . innerHTML = element . charAt ( 0 ) . toUpperCase ( ) + element . slice ( 1 ) + ' <b> ' + data [ element ] [ 0 ] + '</b>' ;
3441 }
42+ // Apply and fill in the template for Policies
3543 if ( element == "policies" ) {
3644 header . style . display = "block" ;
37- console . log ( "policies" ) ;
3845 data [ element ] . forEach ( pol => {
39- console . log ( pol ) ;
4046 const policy = document . importNode ( policyTemp . content , true ) ;
41- console . log ( policy ) ;
4247 const tbodyPol = policy . querySelector ( "tbody" ) ,
43- polname = policy . querySelector ( "#policy-name" ) ;
48+ polname = policy . querySelector ( "#policy-name" ) ,
49+ pconforms = policy . querySelector ( "#policy-conforms" ) ;
4450 const policyId = Object . keys ( pol ) [ 0 ] ;
4551 polname . textContent = `${ policyId } ` ;
4652 policyDiv . appendChild ( policy ) ;
4753 const policyReportTemp = document . querySelector ( "#policy-report" ) ;
4854 const polKeys = Object . keys ( pol [ policyId ] ) ;
49- console . log ( polKeys ) ;
5055 polKeys . forEach ( report => {
51- console . log ( report ) ;
5256 const prow = document . importNode ( policyReportTemp . content , true ) ;
5357
5458 const pkey = prow . querySelector ( "#pkey" ) ,
5559 pvalue = prow . querySelector ( "#pvalue" ) ;
56- //pconforms = prow.querySelector("#policy-conforms");
60+
5761 if ( report == "conforms" ) {
58- console . log ( "conforms" ) ;
62+ pconforms . innerText = `${ policyId } ${ ( pol [ policyId ] [ report ] ) ? 'is' : 'is not' } conform.` ;
63+ pconforms . style . color = ( pol [ policyId ] [ report ] ) ? 'black' : 'red' ;
5964 }
60- console . log ( report , pol [ policyId ] [ report ] ) ;
6165 pkey . textContent = `${ report } ` ;
6266 extract_info ( pvalue , pol [ policyId ] [ report ] ) ;
63- //pvalue.textContent = `${pol[policyId][report]}`;
67+
6468 tbodyPol . appendChild ( prow ) ;
6569 } )
6670 } )
6771
6872 return ;
69- }
73+ }
74+
75+ // Apply and fill in the template for Metadata
7076 const row = document . importNode ( metadateTemp . content , true ) ;
71- console . log ( row ) ;
7277 const mkey = row . querySelector ( "#key" ) ,
7378 mvalue = row . querySelector ( "#value" ) ,
7479 mtag = row . querySelector ( "#tag" ) ;
7580
7681 mkey . textContent = `${ element } ` ;
7782 extract_info ( mvalue , data [ element ] , mtag ) ;
78- /*
79- mvalue.textContent = `${data[element][0]}`;
80- mtag.textContent = `${data[element][1]["local_path"]}`;*/
83+
8184 tbody . appendChild ( row ) ;
8285 } )
8386 } )
87+ //Extend Checkbox for metadata source
8488 const checkbox = document . querySelector ( "#extended" ) ;
8589checkbox . addEventListener ( 'change' , ( event ) => {
8690 if ( checkbox . checked ) {
@@ -93,7 +97,16 @@ checkbox.addEventListener('change', (event)=>{
9397
9498
9599}
100+
101+ /**
102+ * Function to get a smaller object from the orginal object.
103+ * searches through data till skey and svalue match and return the data from this point.
104+ * @param {Dictonary } data - Object(dict) to search from
105+ * @param {string } skey - search key for key-value pair to get data from
106+ * @param { } svalue - search value for key-value pair to get data from
107+ */
96108function get_data_snippet ( data , skey , svalue ) {
109+
97110 const stack = [ data ] ;
98111 while ( stack ?. length > 0 ) {
99112 const obj = stack . pop ( ) ;
0 commit comments