<!DOCTYPE html>
<html lang="en">
{#
<!--
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
-->
#}
<head>
    <meta charset="utf-8">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>ScanCode scan results for: {{ scanned_path }}</title>
    <link href="{{ assets_dir }}/style.min.css" rel="stylesheet"/>
    <link href="{{ assets_dir }}/bootstrap.min.css" rel="stylesheet"/>
    <link href="{{ assets_dir }}/jquery.dataTables.min.css" rel="stylesheet"/>
    <link href="{{ assets_dir }}/main.css" rel="stylesheet"/>
    <link href="{{ assets_dir }}/simple-sidebar.css" rel="stylesheet"/>

    <style type="text/css">
       #tree {
            max-height: 70vh;
       }
    </style>

</head>
<body>
  <div class="navbar navbar-inverse navbar-static-top">
    <div class="container-fluid">
      <div class="navbar-header">
          <span class="navbar-brand icon-primary"><a href="https://github.com/nexB/scancode-toolkit/" target="_blank"><strong>ScanCode version {{ version }}</strong></a></span>
      </div>
        <ul class="nav navbar-nav navbar-right">
          <li><a href="{{ assets_dir }}/help.html" target="_blank">Help</a></li>
          <li><a href="http://www.nexb.com" target="_blank">Made by nexB</a></li>
       </ul>
    </div>
  </div>

  <div class="container-fluid">
    <div class="row">
      <div class="row" id = "scan-result-header">
        <div class="col-md-4">
          <strong>scan results for:</strong>
        </div>
        <div class = "col-md-8" id = "file-path">
          <p>{{ scanned_path }}</p>
        </div>
      </div>
      <div class="col-md-4" id="leftCol">
        <div id="tree" style="overflow-x:scroll; overflow-y:auto;"></div>
      </div>

      <div id="tabbar" class="col-md-8">
        <ul class="nav nav-tabs">
            <li id="tab4" class="active"><a data-toggle="tab" href="#chart">License Summary</a></li>
            <li id="tab5"><a data-toggle="tab" href="#">Copyright Summary</a></li>
            <li id="tab1"><a data-toggle="tab" href="#">Clues</a></li>
            <li id="tab2"><a data-toggle="tab" href="#">File Details</a></li>
            <li id="tab3"><a data-toggle="tab" href="#">Packages</a></li>
        </ul>
        <div id="summary">
            <p class="total-files"></p>
            <svg class="chart"></svg>
        </div>
        <div id="details">
            <table id="data_table" class="data_table display table-wrap" cellspacing="0" width="100%"></table>
        </div>
      </div>
    </div>
  </div>
  <footer class="footer">
      <div class="container-fluid">
          <p class="text-muted">Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
          No content created from ScanCode should be considered or used as legal advice. Consult an Attorney for any legal advice.
          ScanCode is a free software code scanning tool from nexB Inc. and others.
          Visit <a href="http://www.nexb.com/" target="_blank">http://www.nexb.com</a> and <a href="https://github.com/nexB/scancode-toolkit/" target="_blank">https://github.com/nexB/scancode-toolkit/</a> for support and download.
          </p>
      </div>
  </footer>
  <script src="{{ assets_dir }}/jquery.min.js"></script>
  <script src="{{ assets_dir }}/bootstrap.min.js"></script>
  <script src="{{ assets_dir }}/jstree.min.js"></script>
  <script src="{{ assets_dir }}/jquery.dataTables.min.js"></script>
  <script src="{{ assets_dir }}/d3.min.js"  charset="utf-8"></script>
  <script src="{{ assets_dir }}/chart.js"></script>
  <script src="{{ assets_dir }}/scancode_jstree.js"></script>
  <script src="{{ assets_dir }}/scancode_datatable.js"></script>
  <script type="text/javascript" src="{{ assets_dir }}/data.js"></script>
  <script type="text/javascript">
      var dataArray = data;
      var currNodeData = null;
      var chartOptions = {
          name: "License Summary",
          margin: 30,
          barHeight: 25,
          xAxisName: "License Count",
          yAxisName: "License Name"
      };

      // Prepare data for tree and table
      $( document ).ready(function() {
          // Setup JSTree
          var scancodeJSTree = new ScancodeJSTree('#tree', dataArray)
              .onNodeSelected(function(e, nodeData) {
                  // Redraw data table (forces custom filter to rerun)
                  // TODO: Figure out the tab and only update that table instead of all
                  table = $('#data_table').DataTable();
                  table.draw();
                  currNodeData = nodeData;
                  resetChartSelect();
              });

          // Setup DataTable
          var scancodeDataTable = new ScancodeDataTable('#data_table', dataArray)
              .addFilter(function( settings, data, dataIndex ) {
                  // Get selected ID in tree
                  var id = $('#tree').jstree('get_selected')[0];
                  // keep if column 0 starts with ID (i.e., indexOf(id) == 0)
                  return id == null || data[0].indexOf(id + '/') == 0 || data[0] == id;
              });
          $('#button').click(function(){ $('#data_table').toggleClass('ellipsis'); })

          // Setup BarChart
          var chartData = formatLicenseChartData(data);
          var barChart = new BarChart(chartData, chartOptions, '.chart');
          $(window).on('resize', barChart.draw);

          // Setup Tabs
          $( "#tab1" ).click(function() {
            $('#details').show();
            $('#summary').hide();
            scancodeDataTable.showLicenseAndCopyright();
          });
          $( "#tab2" ).click(function() {
            $('#details').show();
            $('#summary').hide();
            scancodeDataTable.showFileInfo();
          });
          $( "#tab3" ).click(function() {
            $('#details').show();
            $('#summary').hide();
            scancodeDataTable.showPkgInfo();
          });
          $("#tab4").click(function() {
            $('#details').hide();
            $('#summary').show();
            resetChart(formatLicenseChartData(filteredData(currNodeData)));
          });
          $("#tab5").click(function() {
            $('#details').hide();
            $('#summary').show();
            resetChart(formatCopyrightChartData(filteredData(currNodeData)));
          });
          $("#details").hide();

          function resetChartSelect() {
              if ($('#tab4').hasClass('active')) {
                  resetChart(formatLicenseChartData(filteredData(currNodeData)));
              } else if($('#tab5').hasClass('active')){
                  resetChart(formatCopyrightChartData(filteredData(currNodeData)));
              }
          }

          function resetChart(chartData) {
              barChart.remove();
              barChart = new BarChart(chartData, chartOptions, '.chart');
          }
      });

      // filter the data
      function filteredData(nodeData){
          if (nodeData == null) {
              return data;
          }
          return $.map(data, function(item){
              var pattern = '^' + nodeData.selected[0];
              pattern += nodeData.node.children.length > 0 ? '/' : '';
              if (item.path.match(pattern)){
                  return item;
              } else {
                  return;
              }
          });
      }

      function formatLicenseChartData(data){
        var NO_LICENSES = [{short_name: 'No License Found'}];
        // Get license short name and if no license set it to No License Found
        var shortNames = $.map(data, function(item){
            var licenses;
            if (!('licenses' in item)) {
                return;
            } else if (item.licenses.length > 0) {
                licenses = item.licenses;
            } else {
                licenses = NO_LICENSES;
            }
            return $.map(licenses, function(license) {
                return license.short_name;
            });
        });
        // Get the total number of files
        var totalFiles = data.length;
        $( "p.total-files" ).text( "Total Files Scanned: " + totalFiles );
        return formatChartData(shortNames)
      }

      function formatCopyrightChartData(data){
        var NO_COPYRIGHT = [{holders: ['No Copyright Found']}];
        // Get copyright holder and if none set it to No Copyright Found
        var holderNames = $.map(data, function(item){
            var holders;
            if (!('holders' in item)) {
                return;
            } else if (item.holders.length > 0) {
                holders = item.holders;
            } else {
                holders = NO_COPYRIGHT;
            }
            return $.map(holders, function(holder) {
                return holder.value;
            });
        });
        // Get the total number of files
        var totalFiles = data.length;
        $( "p.total-files" ).text( "Total Files Scanned: " + totalFiles );
        return formatChartData(holderNames)
      }

      function formatChartData(names) {
        // Sum the total number of times the name appears
        var count = {};
        $.each(names, function(i, name){
            count[name] = count[name] + 1 || 1;
        });

        // Transform license count into array of objects with license name & count
        var chartData = $.map(count, function(val, key) {
            return {
                name: key.substr(0, 80),
                val: val
            };
        });

        // Sorts the data highest value to lowest value
        chartData.sort(function(a,b){
            if (a.val == b.val) {
                return a.name.localeCompare(b.name);
            } else {
                return a.val > b.val ? -1 : 1;
            }
        });
        return chartData;
    }
  </script>
</body>
</html>
