diff --git a/static/src/js/app/admin/problem/add_problem.js b/static/src/js/app/admin/problem/add_problem.js index 09862d35..8213ad58 100644 --- a/static/src/js/app/admin/problem/add_problem.js +++ b/static/src/js/app/admin/problem/add_problem.js @@ -1,5 +1,5 @@ -require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "validation"], - function ($, avalon, editor, uploader, bs_alert) { +require(["jquery", "avalon", "editor", "uploader", "bs_alert", "csrf", "tagEditor", "validation", "jqueryUI"], + function ($, avalon, editor, uploader, bs_alert, csrfHeader) { avalon.vmodels.add_problem = null; $("#add-problem-form") .formValidation({ @@ -17,17 +17,10 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val } } }, - description:{ - validators: { - notEmpty: { - message: "请输入描述" - } - } - }, cpu: { validators: { notEmpty: { - message: "请输入cpu时间" + message: "请输入时间限制" }, integer: { message: "请输入一个合法的数字" @@ -43,52 +36,121 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val memory: { validators: { notEmpty: { - message: "请输入内存" + message: "请输入内存限制" }, integer: { message: "请输入一个合法的数字" } } + }, + difficulty: { + validators: { + notEmpty: { + message: "请输入难度" + }, + integer: { + message: "难度用一个整数表示" + } + } + }, + source: { + validators: { + notEmpty: { + message: "请输入题目来源" + } + } } } }) .on("success.form.fv", function (e) { e.preventDefault(); + if (vm.test_case_id == '') + { + bs_alert("你还没有上传测试数据!"); + return; + } + if (vm.description == '') + { + bs_alert("题目描述不能为空!"); + return; + } + if (vm.hint == '') + { + bs_alert("提示不能为空!"); + return; + } var ajaxData = { title: vm.title, description: vm.description, - cpu: vm.cpu, - memory: vm.memory, - samples: [] + time_limit: vm.cpu, + memory_limit: vm.memory, + samples: [], + test_case_id: vm.test_case_id, + hint: vm.hint, + source: vm.source, + tags: [], + difficulty: vm.difficulty }; - - for (var i = 0; i < vm.samples.length; i++) { - ajaxData.samples.push({input: vm.samples[i].input, output: vm.samples[i].output}); + if (vm.samples.length == 0) + { + 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); + $.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 testCaseUploader = uploader("#testCaseFile", "/api/admin/test_case_upload/",function(file, respond){ + var testCaseUploader = uploader("#testCaseFile", "/api/admin/test_case_upload/", function (file, respond) { if (respond.code) bs_alert(respond.data); - else{ + else { vm.test_case_id = respond.data.test_case_id; vm.uploadSuccess = true; vm.testCaseList = []; - for (var i = 0; i < respond.data.file_list.input.length; i++) - { - vm.testCaseList.push({input: respond.data.file_list.input[i], output: respond.data.file_list.output[i]}); + for (var i = 0; i < respond.data.file_list.input.length; i++) { + vm.testCaseList.push({ + input: respond.data.file_list.input[i], + output: respond.data.file_list.output[i] + }); } } }); var hinteditor = editor("#hint"); - $("#tags").tagEditor(); + var tagList = [], completeList = []; var vm = avalon.define({ $id: "add_problem", title: "", description: "", - cpu: 0, - memory: 0, + cpu: 1000, + memory: 256, samples: [], hint: "", visible: false, @@ -99,14 +161,6 @@ require(["jquery", "avalon", "editor", "uploader", "bs_alert", "tagEditor", "val testCaseList: [], uploadSuccess: false, source: "", - checkTag: function () { - alert("11"); - if (event.keyCode == 13) - { - alert("You press the enter key!"); - return false; - } - }, add_sample: function () { 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(); }); \ No newline at end of file diff --git a/template/admin/problem/add_problem.html b/template/admin/problem/add_problem.html index 1c78a9ed..d039302b 100644 --- a/template/admin/problem/add_problem.html +++ b/template/admin/problem/add_problem.html @@ -20,12 +20,12 @@
-
+
-
+
@@ -42,7 +42,7 @@

- +