12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <style type="text/css">
- textarea {
- width: 600px;
- height: 200px;
- }
- select {
- display: block;
- }
- .error {
- color: red;
- }
- </style>
- <form method="POST" id="calculate" action="cyclomatic_calculator.php">
- <select name="language">
- <option value="php">PHP</option>
- <option value="c">C/C++</option>
- <option value="js">javascript</option>
- </select>
- <textarea name="code" placeholder="if(true) { echo 'hello'; }"></textarea>
- <p>Disclaimer: The code evaluated is only intended to be within a single function scope; it will not work on
- objects, and it will not recurse through sub-functions. This tool is intended to provide an estimate only, and is not
- guaranteed to be accurate. This tool does not check for valid or functioning code. You use this tool at your own risk.</p>
- <input type="submit" />
- </form>
- <div id="result"></div>
- <script type="text/javascript">
- var calculate = function(event) {
- event.preventDefault();
- var r = new XMLHttpRequest();
- r.open("POST", "cyclomatic_calculator.php", true);
- r.onreadystatechange = function() {
- var result = document.getElementById("result");
- if (r.readyState != 4 || r.status != 200)
- return;
- var data = JSON.parse(r.responseText);
- if (data.error) {
- console.error(data.error);
- result.innerHTML = '<span class="error">' + data.error
- + '</span>';
- } else {
- result.innerHTML = "Estimated cyclomatic complexity: "
- + data.value;
- }
- };
- r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- var params = "language="
- + document.getElementsByName("language")[0].value + "&code="
- + document.getElementsByName("code")[0].value;
- r.send(params);
- result.innerHTML = "";
- return false;
- };
- var object = document.getElementById("calculate");
- object.addEventListener("submit", calculate);
- </script>
|