DWV display in tabs example

Simplest DWV DICOM image display example with 2 views in tabs

by Fahd Murtaza

HTML

<script src="https://github.com/ivmartel/dwv/releases/download/v0.30.4/dwv-0.30.4.min.js"></script>
<div class="tab">
  <button class="tablinks" onclick="openTab(event, 'DWV1')">DWV App 1</button>
  <button class="tablinks" onclick="openTab(event, 'DWV2')">DWV App 2</button>
</div>

<!-- Tab content -->
<div id="DWV1" class="tabcontent">
  <div id="dwv">
    <div id="layerGroup0"></div>
  </div>
</div>

<div id="DWV2" class="tabcontent">
  <div id="dwv2">
    <div id="layerGroup1"></div>
  </div>
</div>

CSS

.tab {
   overflow: hidden;
   border: 1px solid #ccc;
   background-color: #f1f1f1;
 }

 /* Style the buttons inside the tab */
 .tab button {
   background-color: inherit;
   float: left;
   border: none;
   outline: none;
   cursor: pointer;
   padding: 14px 16px;
   transition: 0.3s;
 }

 /* Change background color of buttons on hover */
 .tab button:hover {
   background-color: #ddd;
 }

 /* Create an active/current tablink class */
 .tab button.active {
   background-color: #ccc;
 }

 /* Style the tab content */
 .tabcontent {
   display: none;
   padding: 6px 12px;
   border: 1px solid #ccc;
   border-top: none;
 }

JavaScript

// Declaration of app variables
var app = null;
var app2 = null;

function initApp1() {
  app = new dwv.App();
  app.init({
    containerDivId: "dwv",
    tools: ["Scroll", "ZoomAndPan", "WindowLevel"],
    dataViewConfigs: {
      '*': [{
        divId: 'layerGroup0'
      }]
    }
  });
  app.loadURLs(['https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm']);
}

function initApp2() {
  app2 = new dwv.App();
  app2.init({
    containerDivId: "dwv2",
    tools: ["Scroll", "ZoomAndPan", "WindowLevel"],
    dataViewConfigs: {
      '*': [{
        divId: 'layerGroup1'
      }]
    }
  });
  app2.loadURLs(['https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323707.dcm']);
}

function openTab(evt, tabName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(tabName).style.display = "block";
  evt.currentTarget.className += " active";

  // Check which tab is now active and initialize or reload the appropriate DWV app
  if (tabName === 'DWV1' && !app) {
    initApp1();
  } else if (tabName === 'DWV2' && !app2) {
    initApp2();
  }
}

// Setup a default tab to open (if necessary)
document.addEventListener("DOMContentLoaded", function() {
  document.getElementsByClassName("tablinks")[0].click();
});