Subversion Repositories f9daq

Rev

Rev 266 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>Belle II Masterclass</title>
  6.   <link rel="stylesheet" type="text/css" href="masterclass.css">
  7.   <script src="js/blockly_compressed.js"></script>
  8.   <script src="js/blocks_compressed.js"></script>
  9.   <script src="js/javascript_compressed.js"></script>
  10.   <script src="js/en.js"></script>
  11.   <script src="js/belle2_def.js"></script>
  12.   <script src="js/belle2_gen.js"></script>  
  13.   <script src="js/workspace.js"></script>
  14.   <script src="js/FileSaver.min.js"></script>
  15.   <script src="js/Blob.js"></script>
  16.  
  17.  
  18. <!--  <script type="text/javascript" src="jsroot/scripts/JSRootCore.js?2d&onload=startGUI""></script>   -->
  19.  <script type="text/javascript" src="https://root.cern/js/latest/scripts/JSRootCore.js?2d&onload=startGUI"></script>
  20.   <script type = "text/javascript"      language = "javascript">
  21.                  
  22. var es;
  23. function addLog(message) {
  24.     var r = document.getElementById('results');
  25.    //r.innerHTML += message + '<br>';
  26.    r.insertAdjacentHTML('beforeend', message +'<br/>');
  27.   // r.scrollTop = r.scrollHeight;
  28. }
  29.  
  30. function showStat(message) {
  31.     var r = document.getElementById('sbar');
  32.     r.innerHTML = message ;
  33. }
  34.  
  35.  
  36.  
  37. function startGUI() {
  38.         // d3.select('html').style('height','100%');
  39.         // d3.select('body').style({'min-height':'100%', 'margin':'0px', "overflow" :"hidden"});
  40.         var r = document.getElementById('results');
  41.         r.style.display = 'none';
  42.         var r0 = document.getElementById('sbar');
  43.         r0.style.display = 'none';
  44.  
  45. }
  46.  
  47.  
  48. var cnt=0;
  49. function startTask() {
  50.        
  51.         var r = document.getElementById('results');
  52.         if ( r.style.display == 'none' ) switchTask();
  53.         r.innerHTML = '';
  54.  
  55.  
  56.         Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  57.         var code = Blockly.JavaScript.workspaceToCode(workspace);
  58.             code = code.replace(/\(|\)/gi, '');
  59.                
  60.         addLog(code + '<br>');
  61.        //addLog(JSON.stringify(code)+ '<br>');
  62.         es = new EventSource('runscript.php?code='  + code  );
  63.          
  64.        //a message is received
  65.         es.addEventListener('message',EventListener);      
  66.         es.addEventListener('error', function(e) {
  67.         addLog('Error occurred');
  68.         es.close();
  69.     });
  70. }
  71.  
  72. function EventListener(e) {
  73. var result = JSON.parse( e.data );
  74.  
  75.         if(e.lastEventId == 'CLOSE') {
  76.             addLog('Received CLOSE closing');
  77.             es.close();
  78.             var pBar = document.getElementById('progressor');
  79.             pBar.value = pBar.max; //max out the progress bar
  80.             var perc = document.getElementById('percentage');
  81.             perc.innerHTML   =  "100%";
  82.             perc.style.width = (Math.floor(pBar.clientWidth * (0.5)) + 15) + 'px';
  83.         }   else {
  84.             if(e.lastEventId == '0' ) {
  85.               addLog(result.message);
  86.             } else {
  87.               if(e.lastEventId == '2' ) {
  88.                 showStat(result.message);
  89.               } else {
  90.                 var jsonobj = JSROOT.parse(result.message);
  91.                 var sframe = 'rh'+ (cnt++);
  92.                 addLog('ROOT_JSON object '+ sframe );
  93.                 var r = document.getElementById('results');
  94.                 r.insertAdjacentHTML('beforeend', '<div id="' + sframe +'" style="width:60%; height:60%;"></div>');
  95.              
  96.                 var frame = document.getElementById(sframe);
  97.                 JSROOT.draw(frame, jsonobj, "hist");
  98.                 frame.scrollIntoView();
  99.               }
  100.             }
  101.             var pBar = document.getElementById('progressor');
  102.             pBar.value = result.progress;
  103.             var perc = document.getElementById('percentage');
  104.             perc.innerHTML   = result.progress  + "%";
  105.             perc.style.width = (Math.floor(pBar.clientWidth * (result.progress/100)) + 15) + 'px';
  106.         }
  107. }
  108.  
  109.  
  110.  
  111. function stopTask() {
  112.     es.close();
  113.     addLog('Interrupted');
  114. }
  115.  
  116.        
  117.  
  118.     function switchTask() {
  119.     var div  = document.getElementById('results');
  120.     var divs  = document.getElementById('sbar');
  121.     var div0 = document.getElementById('blocklyDiv');
  122.  
  123.     if (div.style.display !== 'none') {
  124.         div.style.display = 'none';
  125.         divs.style.display = 'none';
  126.         div0.style.display = 'block';
  127.     }
  128.     else {
  129.         div.style.display = 'block';
  130.         divs.style.display = 'block';
  131.         div0.style.display = 'none';
  132.     }
  133.  
  134.   }
  135.  
  136.  
  137. function readSingleFile(e) {
  138.  
  139.   var div0 = document.getElementById('blocklyDiv');
  140.   if (div0.style.display === 'none') switchTask();
  141.  
  142.   var file = e.target.files[0];
  143.   if (!file) {
  144.    return;
  145.   }
  146.   var reader = new FileReader();
  147.   reader.onload = function(e) {
  148.     var contents = e.target.result;
  149.     displayContents(contents);
  150.  
  151.   };
  152.   reader.readAsText(file);
  153.  
  154. }
  155.  
  156. function displayContents(contents) {
  157.  
  158. workspace.clear();
  159. var xml = Blockly.Xml.textToDom(contents);
  160. Blockly.Xml.domToWorkspace(xml, workspace);
  161.  
  162. }
  163.  
  164.  
  165. function loadDoc( url ) {
  166.   var xhttp = new XMLHttpRequest();
  167.   xhttp.onreadystatechange = function () {
  168.      if (xhttp.readyState == 4 && xhttp.status == 200) {
  169.         return xhttp.responseText;
  170.      }
  171.   }
  172.   xhttp.open("GET", url, false); // !!! should be in synchronous mode
  173.   xhttp.send();
  174.   return xhttp.onreadystatechange();
  175. }
  176.  
  177.  function showCode() {
  178.       // Generate JavaScript code and display it.
  179.       Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  180.       var code = Blockly.JavaScript.workspaceToCode(workspace);
  181.           //var code = Blockly.JSON.fromWorkspace( workspace );
  182.           //var code =  Blockly.Xml.domToPrettyText(workspace );
  183.       code = code.replace(/\(|\)/gi, '');
  184.       console.log(code);
  185.       console.log(code.length);  
  186.       alert(code);
  187.  
  188.   }
  189.  
  190.  
  191.   function saveBlockly(){
  192.     //  https://eligrey.com/blog/saving-generated-files-on-the-client-side/
  193.  
  194.     var xml = Blockly.Xml.workspaceToDom(workspace);
  195.     var txt = Blockly.Xml.domToText(xml);
  196.    
  197. //  var txt=Blockly.Xml.domToPrettyText(Blockly.Xml.workspaceToDom(Blockly.mainWorkspace)); // exports workspace as pretty xml
  198.   console.log(txt);
  199.   var blob = new Blob([txt], {type: "text/xml"});
  200.   saveAs(blob, "test.blab2");
  201.   }
  202.  
  203.   function loadBlockly(){
  204.     Blockly.Xml.domToWorkspace(Blockly.mainWorkspace,$("#hiBlocks")[0]); // loads xml from dom into workspace  
  205.   }
  206.  
  207.     function runCode() {
  208.       // Generate JavaScript code and run it.
  209.       window.LoopTrap = 1000;
  210.       Blockly.JavaScript.INFINITE_LOOP_TRAP =
  211.           'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
  212.       var code = Blockly.JavaScript.workspaceToCode(workspace);
  213.       Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  214.       try {
  215.         eval(code);
  216.       } catch (e) {
  217.         alert(e);
  218.       }
  219.     }
  220.    
  221.  
  222.  
  223.  
  224.  
  225.  
  226.   </script>
  227.                  
  228. </head>
  229. <body>
  230.   <h1>Belle II Masterclass: Define process &rarr;Analyse data &rarr;Visualise results &rarr;Save/load process locally</h1>
  231. <!--        <input type="button" onclick="showCode();"  class="mybutton" value="Show JavaScript" /> -->
  232.         <input type="button" onclick="startTask();"  class="mybutton" value="Run Analysis" />
  233.         <input type="button" onclick="stopTask();"  class="mybutton" value="Interrupt" />
  234.         <input type="button" onclick="switchTask();"  class="mybutton" value="Switch between Diagram and Results" />
  235.         <input type="button" onclick="saveBlockly();"  class="mybutton" value="Save Diagram" />
  236.         <form style="display:inline;"><label for="file-input" class="mybutton" style="">Load Diagram</label>
  237.           <input type="file" style="visibility:hidden;" id="file-input" onClick="this.form.reset()" data-buttonText="Load Diagram"/></form>
  238.         <br />
  239.        
  240.        
  241.  
  242.         <progress id='progressor' value="0" max='100' style="width:95%"></progress>  
  243.         <span id="percentage" style="text-align:right; display:block; margin-top:5px;">0</span>
  244.         <p id="sbar" ></div>     
  245.  
  246.  
  247.         <br />
  248.  
  249.  <div id="blocklyDiv" style="height:500px; width:95%"></div><br />
  250.  <div id="results" style="border:1px solid #000; padding:10px; width:95%; height:80% ; overflow:auto; background:#eee;"></div>
  251.   <br />
  252.  
  253.   <script>
  254.    
  255.     document.getElementById('file-input').addEventListener('change', readSingleFile, false);
  256.  
  257. //  var toolbox = document.getElementById("toolbox");
  258.   var toolbox = loadDoc("toolbox.xml");
  259.  
  260. var options = {
  261.         toolbox : toolbox,
  262.         collapse : true,
  263.         comments : true,
  264.         disable : true,
  265.         maxBlocks : Infinity,
  266.         trashcan : true,
  267.         horizontalLayout : false,
  268.         toolboxPosition : 'start',
  269.         css : true,
  270. /*      media : '../media/', */
  271.         rtl : false,
  272.         scrollbars : true,
  273.         sounds : true,
  274.         oneBasedIndex : true,
  275.         zoom : {
  276.                 controls : true,
  277.                 wheel : true,
  278.                 startScale : 1,
  279.                 maxcale : 3,
  280.                 minScale : 0.3
  281.   },
  282.   grid:
  283.   {spacing: 20,
  284.   length: 3,
  285.   colour: '#ccc',
  286.   snap: true}
  287. };
  288.  
  289. /* Inject your workspace */
  290. var workspace = Blockly.inject('blocklyDiv', options);
  291.  
  292. /* Load Workspace Blocks from XML to workspace. Remove all code below if no blocks to load */
  293.  
  294. /* TODO: Change workspace blocks XML ID if necessary. Can export workspace blocks XML from Workspace Factory. */
  295. //var workspaceBlocks = document.getElementById("workspaceBlocks");
  296. /* Load blocks to workspace. */
  297. //    Blockly.Xml.domToWorkspace(workspaceBlocks, workspace);
  298.  
  299. <?php
  300.  
  301.  if (isset($_GET["decay"])){
  302.    $decay =  $_GET["decay"];
  303.  } else  {
  304.    $decay = "data/sample.blab2";
  305.  }
  306. // echo "var workspaceBlocks = loadDoc( \"$decay\");"
  307. /* Load blocks to workspace. */
  308.  echo "displayContents( loadDoc( \"$decay\"));"
  309.  
  310. ?>
  311.  
  312. //displayContents(workspaceBlocks);
  313.    
  314.  
  315.  
  316. </script>
  317.  
  318. </body>
  319. </html>
  320.