[前端] 完成了添加题目的页面[CI SKIP]

This commit is contained in:
esp 2015-08-10 21:05:55 +08:00
parent b1b996271f
commit 9f7206d114
2 changed files with 116 additions and 36 deletions

View File

@ -1,5 +1,5 @@
require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "validation"], require(["jquery", "avalon", "editor", "uploader", "bs_alert", "csrf", "tagEditor", "validation", "jqueryUI"],
function ($, avalon, editor, uploader, bs_alert) { function ($, avalon, editor, uploader, bs_alert, csrfHeader) {
avalon.vmodels.add_problem = null; avalon.vmodels.add_problem = null;
$("#add-problem-form") $("#add-problem-form")
.formValidation({ .formValidation({
@ -17,17 +17,10 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val
} }
} }
}, },
description:{
validators: {
notEmpty: {
message: "请输入描述"
}
}
},
cpu: { cpu: {
validators: { validators: {
notEmpty: { notEmpty: {
message: "请输入cpu时间" message: "请输入时间限制"
}, },
integer: { integer: {
message: "请输入一个合法的数字" message: "请输入一个合法的数字"
@ -43,29 +36,96 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val
memory: { memory: {
validators: { validators: {
notEmpty: { notEmpty: {
message: "请输入内存" message: "请输入内存限制"
}, },
integer: { integer: {
message: "请输入一个合法的数字" message: "请输入一个合法的数字"
} }
} }
},
difficulty: {
validators: {
notEmpty: {
message: "请输入难度"
},
integer: {
message: "难度用一个整数表示"
}
}
},
source: {
validators: {
notEmpty: {
message: "请输入题目来源"
}
}
} }
} }
}) })
.on("success.form.fv", function (e) { .on("success.form.fv", function (e) {
e.preventDefault(); e.preventDefault();
if (vm.test_case_id == '')
{
bs_alert("你还没有上传测试数据!");
return;
}
if (vm.description == '')
{
bs_alert("题目描述不能为空!");
return;
}
if (vm.hint == '')
{
bs_alert("提示不能为空!");
return;
}
var ajaxData = { var ajaxData = {
title: vm.title, title: vm.title,
description: vm.description, description: vm.description,
cpu: vm.cpu, time_limit: vm.cpu,
memory: vm.memory, memory_limit: vm.memory,
samples: [] samples: [],
test_case_id: vm.test_case_id,
hint: vm.hint,
source: vm.source,
tags: [],
difficulty: vm.difficulty
}; };
if (vm.samples.length == 0)
for (var i = 0; i < vm.samples.length; i++) { {
ajaxData.samples.push({input: vm.samples[i].input, output: vm.samples[i].output}); bs_alert("请至少添加一组样例!");
return;
}
var tags = $("#tags").tagEditor("getTags")[0].tags;
if (tags.length == 0)
{
bs_alert("请至少添加一个标签,这将有利于用户发现你的题目!");
return;
}
for (key in vm.samples.length) {
ajaxData.samples.push({input: vm.samples[key].input, output: vm.samples[key].output});
}
for (key in tags) {
ajaxData.tags.push(tags[key].tag);
} }
console.log(ajaxData); console.log(ajaxData);
$.ajax({
beforeSend: csrfHeader,
url: "/api/admin/problem/",
dataType: "json",
data:ajaxData,
method: "post",
success: function (data) {
if (!data.code) {
bs_alert("successful!");
console.log(data);
}
else {
bs_alert(data.data);
}
}
})
}); });
var problemDiscription = editor("#problemDescription"); var problemDiscription = editor("#problemDescription");
var testCaseUploader = uploader("#testCaseFile", "/api/admin/test_case_upload/", function (file, respond) { var testCaseUploader = uploader("#testCaseFile", "/api/admin/test_case_upload/", function (file, respond) {
@ -75,20 +135,22 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val
vm.test_case_id = respond.data.test_case_id; vm.test_case_id = respond.data.test_case_id;
vm.uploadSuccess = true; vm.uploadSuccess = true;
vm.testCaseList = []; vm.testCaseList = [];
for (var i = 0; i < respond.data.file_list.input.length; i++) for (var i = 0; i < respond.data.file_list.input.length; i++) {
{ vm.testCaseList.push({
vm.testCaseList.push({input: respond.data.file_list.input[i], output: respond.data.file_list.output[i]}); input: respond.data.file_list.input[i],
output: respond.data.file_list.output[i]
});
} }
} }
}); });
var hinteditor = editor("#hint"); var hinteditor = editor("#hint");
$("#tags").tagEditor(); var tagList = [], completeList = [];
var vm = avalon.define({ var vm = avalon.define({
$id: "add_problem", $id: "add_problem",
title: "", title: "",
description: "", description: "",
cpu: 0, cpu: 1000,
memory: 0, memory: 256,
samples: [], samples: [],
hint: "", hint: "",
visible: false, visible: false,
@ -99,14 +161,6 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val
testCaseList: [], testCaseList: [],
uploadSuccess: false, uploadSuccess: false,
source: "", source: "",
checkTag: function () {
alert("11");
if (event.keyCode == 13)
{
alert("You press the enter key!");
return false;
}
},
add_sample: function () { add_sample: function () {
vm.samples.push({input: "", output: "", "visible": true}); vm.samples.push({input: "", output: "", "visible": true});
}, },
@ -125,5 +179,31 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val
} }
}); });
$.ajax({
beforeSend: csrfHeader,
url: "/api/admin/tag/",
dataType: "json",
method: "get",
success: function (data) {
if (!data.code) {
tagList = data.data;
completeList = [];
for (key in tagList) {
completeList.push(tagList[key].name);
}
$("#tags").tagEditor({
autocomplete: {
delay: 0, // show suggestions immediately
position: {collision: 'flip'}, // automatic menu position up/down
source: completeList
}
});
}
else {
bs_alert(data.data);
}
}
});
avalon.scan(); avalon.scan();
}); });

View File

@ -20,12 +20,12 @@
</div> </div>
<div class="col-md-3"> <div class="col-md-3">
<div class="form-group"><label>cpu</label> <div class="form-group"><label>时间限制(ms)</label>
<input type="number" name="cpu" class="form-control" ms-duplex="cpu"> <input type="number" name="cpu" class="form-control" ms-duplex="cpu">
</div> </div>
</div> </div>
<div class="col-md-3"> <div class="col-md-3">
<div class="form-group"><label>内存</label> <div class="form-group"><label>内存限制(MB)</label>
<input type="number" name="memory" class="form-control" ms-duplex="memory"> <input type="number" name="memory" class="form-control" ms-duplex="memory">
</div> </div>
</div> </div>
@ -42,7 +42,7 @@
</div> </div>
<div id="tag" class="col-md-12"> <div id="tag" class="col-md-12">
<label>标签</label><br> <label>标签</label><br>
<input type="text" id="tags" onchange="console.log(a)"> <input type="text" id="tags">
</div> </div>
<div class="col-md-12"><br> <div class="col-md-12"><br>
<label>样例</label> <label>样例</label>