This commit is contained in:
2019-06-30 21:18:31 +08:00
commit db843337ce
31 changed files with 4888 additions and 0 deletions

329
js/page/edit.js Normal file
View File

@ -0,0 +1,329 @@
$(function () {
var jsCode = `'use strict';
var phpEngine = uniter.createEngine('PHP');
phpEngine.expose({
pass: function () {
pass();
return '测试通过';
},
}, 'r');
phpEngine.getStdout().on('data', function (data) {
print(data);
});
phpEngine.getStderr().on('data', function (data) {
print(data);
});
phpEngine.execute(phpCode).fail(function (error) {
// print(error.toString());
});
`;
var testCode = `<?php
$pass = 1;
$m =
if ($m != ) {
$pass = 0;
echo "测试用例:<br>";
echo "输出结果:" . $m . "<br>";
}
if ($pass) {
echo $r->pass();
} else {
echo "<br>测试不通过";
}`;
require(["vs/editor/editor.main"], function () {
window.editor = monaco.editor.create(document.getElementById('phpContainer'), {
value: [
'<?php',
'\tfunction sum($a, $b) {',
'\t\treturn $a + $b;',
'\t}',
'',
'// --------- 测试区域',
''
].join('\n'),
language: 'php'
});
window.jsEditor = monaco.editor.create(document.getElementById('jsContainer'), {
value: jsCode,
language: 'javascript'
});
window.testEditor = monaco.editor.create(document.getElementById('testContainer'), {
value: testCode,
language: 'php'
});
function execMain() {
var javascriptCode = window.jsEditor.getValue(),
phpCode = window.editor.getValue(),
resultIframe = document.getElementById('result'),
resultDocument = resultIframe.contentWindow.document,
resultBody;
function clear() {
resultBody.innerHTML = '';
}
function print(html) {
resultBody.insertAdjacentHTML('beforeEnd', html);
}
function pass() {
console.log('pass')
}
function printText(text) {
resultBody.appendChild(document.createTextNode(text));
}
// Ensure the document has a body for IE9
resultDocument.write('<body></body>');
resultDocument.close();
resultBody = resultDocument.body;
clear();
try {
/*jshint evil: true */
new Function('phpCode, print, resultBody, pass', javascriptCode)(phpCode, print, resultBody, pass);
} catch (error) {
printText('<JavaScript error> ' + error.toString());
}
}
function runUnit() {
var javascriptCode = window.jsEditor.getValue(),
phpCode = window.editor.getValue(),
testCode = window.testEditor.getValue(),
resultIframe = document.getElementById('result'),
resultDocument = resultIframe.contentWindow.document,
resultBody;
if (phpCode.indexOf('// --------- 测试区域') !== -1) {
phpCode = phpCode.substring(0, phpCode.lastIndexOf('// --------- 测试区域')) + testCode.replace('<?php', '');
} else {
phpCode = phpCode + testCode.replace('<?php', '');
}
function clear() {
resultBody.innerHTML = '';
}
function print(html) {
resultBody.insertAdjacentHTML('beforeEnd', html);
}
function pass() {
console.log('测试通过')
}
function printText(text) {
resultBody.appendChild(document.createTextNode(text));
}
// Ensure the document has a body for IE9
resultDocument.write('<body></body>');
resultDocument.close();
resultBody = resultDocument.body;
clear();
try {
/*jshint evil: true */
new Function('phpCode, print, resultBody, pass', javascriptCode)(phpCode, print, resultBody, pass);
} catch (error) {
printText('<JavaScript error> ' + error.toString());
}
}
$('#runMain').click(function () {
execMain()
});
$('#runUnit').click(function () {
runUnit()
});
var contentHeight = window.document.body.clientHeight - 50;
$('#phpContainer').css('height', contentHeight - 70);
$('#jsContainer').css('height', contentHeight - 70);
$('#testContainer').css('height', contentHeight - 70);
$('#article').css('height', contentHeight + 30);
window.editor.layout();
window.jsEditor.layout();
window.testEditor.layout();
window.onresize = function () {
window.editor.layout();
window.jsEditor.layout();
window.testEditor.layout();
};
var describe = new SimpleMDE({ element: $("#describe")[0] });
var answer = new SimpleMDE({ element: $("#answer")[0] });
var testCase = new SimpleMDE({ element: $("#testCase")[0] });
var expectedResult = new SimpleMDE({ element: $("#expectedResult")[0] });
$('#submitForm').click(function () {
// 李永升你应该适配一下大于1时直接加百分号小于1时乘以100再加百分号
var passPercent = $('#passPercent').val();
if (passPercent > 1) {
passPercent = passPercent / 100
}
$('#passPercent').val(passPercent);
var postData = $('#edit-form').serializeArray();
var javascriptCode = window.jsEditor.getValue(),
phpUnitCode = window.testEditor.getValue(),
phpCode = window.editor.getValue();
postData.push({
name: 'javascriptCode',
value: javascriptCode
});
postData.push({
name: 'phpUnitCode',
value: phpUnitCode
});
postData.push({
name: 'phpCode',
value: phpCode
});
postData.push({
name: 'describe',
value: describe.value()
});
postData.push({
name: 'answer',
value: answer.value()
});
postData.push({
name: 'testCase',
value: testCase.value()
});
postData.push({
name: 'expectedResult',
value: expectedResult.value()
});
if (GetQueryString('id')) {
$.ajax({
type: "POST",
url: "/api.php?action=editProblemset&id=" + GetQueryString('id'),
dataType: "json",
cache: !1,
timeout: 6e4,
data: postData,
success: function (r) {
if (r.status === 1) {
window.location.href = '/index.php'
} else {
alert('很抱歉,保存失败,请稍后刷新重试。')
}
},
error: function () {
alert('很抱歉,出错了,请稍后刷新重试!')
}
})
} else {
$.ajax({
type: "POST",
url: "/api.php?action=addProblemset",
dataType: "json",
cache: !1,
timeout: 6e4,
data: postData,
success: function (r) {
if (r.status === 1) {
window.location.href = '/index.php'
} else {
alert('很抱歉,新增题目失败,请稍后刷新重试。')
}
},
error: function () {
alert('很抱歉,出错了,请稍后刷新重试!')
}
})
}
});
if (GetQueryString('id')) {
$.ajax({
type: "GET",
url: "/api.php?action=getProblemset&id=" + GetQueryString('id'),
dataType: "json",
cache: !1,
timeout: 6e4,
success: function (r) {
if (r.status === 1) {
$('#type').val(r.result.type);
$('#name').val(r.result.name);
$('#isAnswer').val(r.result.isAnswer);
$('#language').val(r.result.language);
$('#difficulty').val(r.result.difficulty);
$('#isCredit').val(r.result.isCredit);
$('#passPercent').val(r.result.passPercent);
$('#tag').val(r.result.tag);
$('#credit').val(r.result.credit);
$('#mark').val(r.result.mark);
describe.value(r.result.describe);
answer.value(r.result.answer);
testCase.value(r.result.testCase);
expectedResult.value(r.result.expectedResult);
window.editor.setValue(r.result.phpCode);
window.testEditor.setValue(r.result.phpUnitCode);
window.jsEditor.setValue(r.result.javascriptCode);
$('.tabs').tabslet({
controls: {
prev: '.prevTab',
next: '.nextTab'
}
});
} else {
alert('很抱歉,获取数据失败,请稍后刷新重试。')
}
$('#loading').hide();
},
error: function () {
alert('很抱歉,出错了,请稍后刷新重试!');
$('#loading').hide();
}
})
} else {
$('#loading').hide();
$('.tabs').tabslet({
controls: {
prev: '.prevTab',
next: '.nextTab'
}
});
}
});
});

304
js/page/run.js Normal file
View File

@ -0,0 +1,304 @@
$(function () {
var converter = new showdown.Converter();
var phpUnitCode = "";
var javascriptCode = "";
var theType = "";
var no = "";
var isLoading = false;
require(["vs/editor/editor.main"], function () {
window.editor = monaco.editor.create(document.getElementById('container'), {
value: [
'<?php',
'\techo "Hello world!";'
].join('\n'),
language: 'php'
});
function run (phpCode, oldCode) {
var resultIframe = document.getElementById('result');
var resultDocument = resultIframe.contentWindow.document;
var resultBody = null;
var clear = function () {
resultBody.innerHTML = '';
};
var print = function (html) {
// outExeTime();
resultBody.insertAdjacentHTML('beforeEnd', html);
};
var pass = function () {
console.log('测试通过');
$.ajax({
type: "POST",
url: "/api.php?action=addRecord",
dataType: "json",
cache: !1,
timeout: 6e4,
data: {
user_id: window.localStorage.id,
problemset_id: GetQueryString('id'),
type: theType,
code: oldCode,
is_pass: '1'
},
success: function (r) {
if (r.status === 1) {
console.log('测试通过的结果保存成功');
if (confirm("测试通过,进入下一题?")) {
$.ajax({
type: "GET",
url: "/api.php?action=getNextNo&no=" + no + "&type=" + theType,
dataType: "json",
cache: !1,
timeout: 6e4,
success: function (r) {
if (r.status === 1) {
if (r.id > 0) {
window.location.href = '/page/run.php?id=' + r.id;
} else if (r.id == 0){
alert('恭喜你!已经做完最后一题!');
window.location.href = '/index.php';
} else {
window.location.href = '/index.php';
}
} else {
alert('很抱歉,获取下一题数据失败,请重试。')
}
},
error: function () {
alert('很抱歉,出错了,请重试!');
}
})
}
} else {
alert('很抱歉,保存数据失败,请重试。')
}
},
error: function () {
alert('很抱歉,出错了,请重试!');
}
})
}
var printText = function (text) {
resultBody.appendChild(document.createTextNode(text));
};
// Ensure the document has a body for IE9
resultDocument.write('<body></body>');
resultDocument.close();
resultBody = resultDocument.body;
clear();
try {
/*jshint evil: true */
new Function('phpCode, print, resultBody, pass', javascriptCode)(phpCode, print, resultBody, pass);
} catch (error) {
printText('<JavaScript error> ' + error.toString());
}
}
function exec(test) {
// var start = (new Date()).getTime()
var phpCode = window.editor.getValue();
var oldCode = phpCode;
if (!test) {
if (isLoading) return;
isLoading = true;
if (phpCode.indexOf('// --------- 测试区域') !== -1) {
phpCode = phpCode.substring(0, phpCode.lastIndexOf('// --------- 测试区域')) + phpUnitCode.replace('<?php', '');
} else {
phpCode = phpCode + phpUnitCode.replace('<?php', '');
}
// 每次提交都记录数据, 需要成功返回后再执行,确保数据一致性
$.ajax({
type: "POST",
url: "/api.php?action=addRecord",
dataType: "json",
cache: !1,
timeout: 6e4,
data: {
user_id: window.localStorage.id,
problemset_id: GetQueryString('id'),
type: theType,
code: oldCode,
is_pass: '0'
},
success: function () {
run(phpCode, oldCode);
isLoading = false;
},
error: function () {
alert('很抱歉,出错了,请重试!');
isLoading = false;
}
})
} else {
run(phpCode, oldCode)
}
}
$('#testRun').click(function () {
$('.tabs').trigger('show', '#resultView');
exec(true);
});
$('#run').click(function () {
$('.tabs').trigger('show', '#resultView');
exec(false);
});
var contentHeight = window.document.body.clientHeight - 50;
$('#container').css('height', contentHeight - 70);
var splitter = $('#content').height(contentHeight).split({
orientation: 'vertical',
limit: 10,
position: '40%', // if there is no percentage it interpret it as pixels
onDrag: function (event) {
console.log(splitter.position());
window.editor.layout();
}
});
if (!localStorage.auto) {
localStorage.auto = '1';
}
window.auto = localStorage.auto;
window.editor.onDidChangeModelContent(function () {
if (window.auto === '1') {
exec(true)
}
});
window.editor.layout();
if (window.auto === '1') {
$("#auto").prop("checked", 'true');
exec(true);
} else {
$("#auto").prop("checked", 'false')
}
$("#auto").click(function () {
if ($(this).prop("checked")) {
window.auto = '1';
localStorage.auto = '1'
} else {
window.auto = '2';
localStorage.auto = '2'
}
});
if (GetQueryString('id')) {
$.ajax({
type: "GET",
url: "/api.php?action=getProblemset&id=" + GetQueryString('id'),
dataType: "json",
cache: !1,
timeout: 6e4,
success: function (r) {
if (r.status === 1) {
var typeStr = '挑战赛';
if (r.result.type === '1') {
typeStr = '闯关模式'
}
var difficultyStr = '困难';
if (r.result.difficulty === '1') {
difficultyStr = '简单'
} else if (r.result.difficulty === '2') {
difficultyStr = '中等'
}
$(document).attr('title', r.result.name + ' - ' + typeStr);
$('#describeView .cont').html('<h3 style="font-weight: 600">' + r.result.name + '(' + difficultyStr + ')' + ' - ' + typeStr + '</h3>' + converter.makeHtml(r.result.describe));
$('#answerView .cont').html(converter.makeHtml(r.result.answer));
if (r.result.isAnswer !== '1') {
$('#answerViewButton').hide();
}
window.editor.setValue(r.result.phpCode);
phpUnitCode = r.result.phpUnitCode;
javascriptCode = r.result.javascriptCode;
var testCase = r.result.testCase;
var expectedResult = r.result.expectedResult;
$('.testCaseContent').html(converter.makeHtml(testCase));
$('.expectedResultContent').html(converter.makeHtml(expectedResult));
$('#rightPane pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
theType = r.result.type;
no = r.result.no;
$.ajax({
type: "POST",
url: "/api.php?action=getRecord",
dataType: "json",
cache: !1,
timeout: 6e4,
data: {
user_id: window.localStorage.id,
problemset_id: GetQueryString('id'),
},
success: function (r) {
if (r.status === 1) {
window.editor.setValue(r.code);
}
$('.tabs').tabslet({
controls: {
prev: '.prevTab',
next: '.nextTab'
}
});
$('#loading').hide();
},
error: function () {
alert('很抱歉,出错了,请稍后刷新重试!');
$('#loading').hide();
}
});
} else {
alert('很抱歉,获取数据失败,请稍后刷新重试。')
}
$('#loading').hide();
},
error: function () {
alert('很抱歉,出错了,请稍后刷新重试!');
$('#loading').hide();
}
})
} else {
$('#loading').hide();
$('.tabs').tabslet({
controls: {
prev: '.prevTab',
next: '.nextTab'
}
});
}
});
});

153
js/page/test.js Normal file
View File

@ -0,0 +1,153 @@
$(function () {
var saveTestCode = function (code) {
$.ajax({
type: "POST",
url: "/api.php?action=saveTestCode",
dataType: "json",
cache: !1,
timeout: 6e4,
data: {
user_id: window.localStorage.id,
code: code
}
})
};
require(["vs/editor/editor.main"], function () {
$.ajax({
type: "POST",
url: "/api.php?action=getTestCode",
dataType: "json",
cache: !1,
timeout: 6e4,
data: {
user_id: window.localStorage.id,
},
success: function (r) {
if (r.status === 1) {
window.editor = monaco.editor.create(document.getElementById('container'), {
value: r.code,
language: 'php'
});
function exec() {
// var start = (new Date()).getTime()
var javascriptCode = `'use strict';
var phpEngine = uniter.createEngine('PHP');
phpEngine.getStdout().on('data', function (data) {
print(data);
});
phpEngine.getStderr().on('data', function (data) {
print(data);
});
phpEngine.execute(phpCode).fail(function (error) {
// print(error.toString());
});
`,
phpCode = window.editor.getValue(),
resultIframe = document.getElementById('result'),
resultDocument = resultIframe.contentWindow.document,
resultBody;
saveTestCode(phpCode);
function clear() {
resultBody.innerHTML = '';
}
function print(html) {
// outExeTime();
resultBody.insertAdjacentHTML('beforeEnd', html);
}
function printText(text) {
resultBody.appendChild(document.createTextNode(text));
}
// function outExeTime () {
// // var execTimeString = getNowDate() + '执行结果(' + ((new Date()).getTime() - start) + 'ms)';
// // var headerHtml = '<span style="color: #888;font-weight: 300;font-size: 10px;">' + execTimeString + ':</span><br>'
// // $('#out').html(headerHtml)
// }
// Ensure the document has a body for IE9
resultDocument.write('<body></body>');
resultDocument.close();
resultBody = resultDocument.body;
clear();
try {
/*jshint evil: true */
new Function('phpCode, print, resultBody', javascriptCode)(phpCode, print, resultBody);
} catch (error) {
printText('<JavaScript error> ' + error.toString());
}
}
$('#run').click(function () {
exec()
});
var contentHeight = window.document.body.clientHeight - 50;
$('#container').css('height', contentHeight - 70);
var splitter = $('#content').height(contentHeight).split({
orientation: 'vertical',
limit: 10,
position: '40%', // if there is no percentage it interpret it as pixels
onDrag: function (event) {
console.log(splitter.position());
window.editor.layout();
}
});
if (!localStorage.auto) {
localStorage.auto = '1';
}
window.auto = localStorage.auto;
window.editor.onDidChangeModelContent(function () {
if (window.auto === '1') {
exec()
}
});
window.editor.layout();
if (window.auto === '1') {
$("#auto").prop("checked", 'true')
exec()
} else {
$("#auto").prop("checked", 'false')
}
$("#auto").click(function () {
if ($(this).prop("checked")) {
window.auto = '1';
localStorage.auto = '1'
} else {
window.auto = '2';
localStorage.auto = '2'
}
});
$('#loading').hide();
} else {
alert('很抱歉,获取已保存的代码失败,请稍后刷新重试。')
}
},
error: function () {
alert('很抱歉,出错了,请稍后刷新重试!')
}
});
});
});

3
js/page/user.js Normal file
View File

@ -0,0 +1,3 @@
$(function () {
$('#loading').hide();
});

3
js/page/userinfo.js Normal file
View File

@ -0,0 +1,3 @@
$(function () {
$('#loading').hide();
});