index.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <style type="text/css">
  2. textarea {
  3. width: 600px;
  4. height: 200px;
  5. }
  6. select {
  7. display: block;
  8. }
  9. .error {
  10. color: red;
  11. }
  12. </style>
  13. <form method="POST" id="calculate" action="cyclomatic_calculator.php">
  14. <select name="language">
  15. <option value="php">PHP</option>
  16. <option value="c">C/C++</option>
  17. <option value="js">javascript</option>
  18. </select>
  19. <textarea name="code" placeholder="if(true) { echo 'hello'; }"></textarea>
  20. <p>Disclaimer: The code evaluated is only intended to be within a single function scope; it will not work on
  21. objects, and it will not recurse through sub-functions. This tool is intended to provide an estimate only, and is not
  22. guaranteed to be accurate. This tool does not check for valid or functioning code. You use this tool at your own risk.</p>
  23. <input type="submit" />
  24. </form>
  25. <div id="result"></div>
  26. <script type="text/javascript">
  27. var calculate = function(event) {
  28. event.preventDefault();
  29. var r = new XMLHttpRequest();
  30. r.open("POST", "cyclomatic_calculator.php", true);
  31. r.onreadystatechange = function() {
  32. var result = document.getElementById("result");
  33. if (r.readyState != 4 || r.status != 200)
  34. return;
  35. var data = JSON.parse(r.responseText);
  36. if (data.error) {
  37. console.error(data.error);
  38. result.innerHTML = '<span class="error">' + data.error
  39. + '</span>';
  40. } else {
  41. result.innerHTML = "Estimated cyclomatic complexity: "
  42. + data.value;
  43. }
  44. };
  45. r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  46. var params = "language="
  47. + document.getElementsByName("language")[0].value + "&code="
  48. + document.getElementsByName("code")[0].value;
  49. r.send(params);
  50. result.innerHTML = "";
  51. return false;
  52. };
  53. var object = document.getElementById("calculate");
  54. object.addEventListener("submit", calculate);
  55. </script>