|
| 1 | +/* |
| 2 | +
|
| 3 | +Online Python Tutor |
| 4 | +https://github.com/pgbovine/OnlinePythonTutor/ |
| 5 | +
|
| 6 | +Copyright (C) Philip J. Guo (philip@pgbovine.net) |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | +copy of this software and associated documentation files (the |
| 10 | +"Software"), to deal in the Software without restriction, including |
| 11 | +without limitation the rights to use, copy, modify, merge, publish, |
| 12 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | +permit persons to whom the Software is furnished to do so, subject to |
| 14 | +the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included |
| 17 | +in all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | +
|
| 27 | +*/ |
| 28 | + |
| 29 | +// Pre-reqs: |
| 30 | +// - jquery-1.8.2.min.js |
| 31 | +// - pytutor.js |
| 32 | +// - opt-frontend-common.js |
| 33 | +// - ace/src-min-noconflict/ace.js |
| 34 | +// should all be imported BEFORE this file |
| 35 | + |
| 36 | + |
| 37 | +var testcasesPaneHtml = '\ |
| 38 | +<table id="testCasesTable">\ |
| 39 | + <thead>\ |
| 40 | + <tr>\ |
| 41 | + <td style="width: 310px">Tests</td>\ |
| 42 | + <td><button id="runAllTestsButton" type="button">Run All</button></td>\ |
| 43 | + <td>Results</td>\ |
| 44 | + <td></td>\ |
| 45 | + <td></td>\ |
| 46 | + </tr>\ |
| 47 | + </thead>\ |
| 48 | + <tbody>\ |
| 49 | + </tbody>\ |
| 50 | +</table>\ |
| 51 | +\ |
| 52 | +<a href="#" id="addNewTestCase">Add new test</a>\ |
| 53 | +' |
| 54 | + |
| 55 | +var curTestcaseId = 1; |
| 56 | + |
| 57 | +function initTestcasesPane(parentDivId) { |
| 58 | + $(parentDivId).html(testcasesPaneHtml); |
| 59 | + |
| 60 | + $("#addNewTestCase").click(function() { |
| 61 | + addTestcase(curTestcaseId); |
| 62 | + curTestcaseId++; |
| 63 | + return false; // to prevent link from being followed |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +function addTestcase(id) { |
| 68 | + var newTr = $('<tr/>').attr('id', 'testCaseRow_' + id); |
| 69 | + $("#testCasesTable tbody").append(newTr); |
| 70 | + var editorTd = $('<td/>'); |
| 71 | + var runBtnTd = $('<td/>'); |
| 72 | + var outputTd = $('<td/>'); |
| 73 | + var visualizeTd = $('<td/>'); |
| 74 | + var deleteTd = $('<td/>'); |
| 75 | + |
| 76 | + editorTd.append('<div id="testCaseEditor_' + id + '" class="testCaseEditor">'); |
| 77 | + runBtnTd.append('<button id="runTestCase_' + id + '" type="button">Run</button>'); |
| 78 | + outputTd.attr('id', 'outputTd_' + id); |
| 79 | + visualizeTd.append('<button id="vizTestCase_' + id + '" type="button">Visualize</button>'); |
| 80 | + deleteTd.append('<a id="delTestCase_' + id + '" href="javascript:void(0);">Delete test</a></td>'); |
| 81 | + |
| 82 | + newTr.append(editorTd); |
| 83 | + newTr.append(runBtnTd); |
| 84 | + newTr.append(outputTd); |
| 85 | + newTr.append(visualizeTd); |
| 86 | + newTr.append(deleteTd); |
| 87 | + |
| 88 | + $('#delTestCase_' + id).click(function() { |
| 89 | + var res = confirm("Press OK to delete this test."); |
| 90 | + if (res) { |
| 91 | + $('#testCaseRow_' + id).remove(); |
| 92 | + } |
| 93 | + return false; // to prevent link from being followed |
| 94 | + }); |
| 95 | + |
| 96 | + // initialize testCaseEditor with Ace: |
| 97 | + var te = ace.edit('testCaseEditor_' + id); |
| 98 | + // set the size and value ASAP to get alignment working well ... |
| 99 | + te.setOptions({minLines: 2, maxLines: 4}); // keep this SMALL |
| 100 | + te.setHighlightActiveLine(false); |
| 101 | + te.setShowPrintMargin(false); |
| 102 | + te.setBehavioursEnabled(false); |
| 103 | + te.setFontSize(10); |
| 104 | + //te.setReadOnly(true); |
| 105 | + |
| 106 | + var s = te.getSession(); |
| 107 | + s.setTabSize(2); |
| 108 | + s.setUseSoftTabs(true); |
| 109 | + // disable extraneous indicators: |
| 110 | + s.setFoldStyle('manual'); // no code folding indicators |
| 111 | + // don't do real-time syntax checks: |
| 112 | + // https://github.com/ajaxorg/ace/wiki/Syntax-validation |
| 113 | + s.setOption("useWorker", false); |
| 114 | + |
| 115 | + // TODO: change syntax highlighting mode if the user changes languages: |
| 116 | + var lang = $('#pythonVersionSelector').val(); |
| 117 | + var mod = 'python'; |
| 118 | + if (lang === 'java') { |
| 119 | + mod = 'java'; |
| 120 | + } else if (lang === 'js') { |
| 121 | + mod = 'javascript'; |
| 122 | + } else if (lang === 'ts') { |
| 123 | + mod = 'typescript'; |
| 124 | + } else if (lang === 'ruby') { |
| 125 | + mod = 'ruby'; |
| 126 | + } |
| 127 | + s.setMode("ace/mode/" + mod); |
| 128 | +} |
| 129 | + |
| 130 | +// returns a list of strings, each of which is a test case |
| 131 | +function getAllTestcases() { |
| 132 | + return $.map($("#testCasesTable .testCaseEditor"), function(e) { |
| 133 | + var editor = ace.edit($(e).attr('id')); |
| 134 | + return editor.getValue(); |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +// see getAppState to see where it calls out to this function: |
| 139 | +function appStateAugmenter(appState) { |
| 140 | + var tc = getAllTestcases(); |
| 141 | + if (tc.length > 0) { |
| 142 | + appState['testCases'] = tc; |
| 143 | + } |
| 144 | +} |
0 commit comments