socialforge/public/javascripts/homework.js

221 lines
6.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$(function(){
//提交作业
var bt=baidu.template;
bt.LEFT_DELIMITER='<!';
bt.RIGHT_DELIMITER='!>';
var tested = false;
var valid_form = function() {
var src = $('#program-src').val();
var title = $('#program-title').val();
if (!src) {
alert('请输入正确的代码');
return false;
}
if (!title) {
alert('请输入标题');
return false;
}
return true;
}
var test_program = function(cb){
var homework_id = $('#test-program-btn').attr('data-homework-id');
var student_work_id = $('#test-program-btn').attr('data-student-work-id');
var src = $('#program-src').val();
var title = $('#program-title').val();
var is_test = $('input[name=is_test]').val();
if(!valid_form()){
return;
}
$.post(
'/student_work/program_test',
{homework: homework_id, student_work_id: student_work_id, src: src, title: title, is_test: is_test},
function(data,status){
tested = true;
console.log(data);
if(data.index <=0){
data.index = $('.ProResultTop').length+1;
}
if (typeof cb == 'function') {cb(data); return;}
var html=bt('t:result-list',data);
$('.ProResult').prepend(html);
if (data.status==0 && is_test != 'true') {
var r=confirm("答题正确,是否立刻提交?");
if (r) {
$(".HomeWorkCon form").submit();
}
}
}
);
};
$('#test-program-btn').on('click', test_program);
$('#commit-program-work-btn').on('click', function(){
if(!valid_form()){
return;
}
if($('.ProResult .ProResultTop').length<=0){
var r=confirm("测试后才能提交,是否立刻测试?");
if (r) {
test_program();
}
return;
}
if (!tested) {
test_program(function(data){
if (data.status!=0) {
var r=confirm("测试不通过,是否强制提交?");
if (!r) {
return;
}
};
$(".HomeWorkCon form").submit();
});
return;
}
$(".HomeWorkCon form").submit();
});
$('form.edit_student_work').on('keydown', '#program-src', function(){
tested = false;
});
//发布作业
$('#program-src').focus(function(){
$(this).css('height', '100px');
});
var datepickerOptions={dateFormat:'yy-mm-dd',firstDay:0,showWeek:true,showOtherMonths:true,selectOtherMonths:true};
$('input.date-input').datepicker(datepickerOptions);
$('a.pic_date').on('click', function(){
$(this).parent().prev().first().focus();
})
$("#BluePopupBox").dialog({
modal: true,
autoOpen: false,
dialogClass: 'BluePopupBox',
minWidth: 753
});
$('a.ProBtn').on('click', function(){
$("#BluePopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.CloseBtn").on('click', function(){
$("#BluePopupBox" ).dialog("close");
});
$('#textarea_input_test').focus();
});
var saveProgramAnswers = function() {
var test_numbers = 0;
var valid = true;
var input = null;
var output = null;
var input_groups = [];
$.each($('#BluePopupBox textarea.InputBox'), function(i, val){
if ($(val).val().length<=0) {
$(val)[0].focus();
valid =false;
return false;
}
if (test_numbers %2==0) {
input = $(val).val();
} else {
output = $(val).val();
input_groups.push({input: input, output: output});
}
test_numbers += 1;
});
var language = '';
if($('select.language_type').val() == 1){
language = 'C';
}else if($('select.language_type').val() == 2){
language = 'C++';
}else if($('select.language_type').val() == 3){
language = 'Python';
}
if (valid) {
$("input[name=homework_type]").val(2);
$('span.program_detail_info').text('('+language+''+test_numbers/2+'组测试)');
//保存js值
var data = {
language_type: $('select.language_type').val(),
input_groups: input_groups
};
//构建到form中
$('.program-input').remove();
var html=bt('t:program-input-list',data);
$("input[name=homework_type]").after(html);
}
return valid;
}
$("#BluePopupBox a.BlueCirBtn").on('click', function(){
if(saveProgramAnswers()){
if($( "#BluePopupBox" ).dialog( "isOpen" )){
$("#BluePopupBox").dialog( "close" );
}
}
});
$("#BluePopupBox").on('click', 'a.icon_add', function(){
var html = bt('t:test-answer-list', null);
$(this).parent('.mt10').after(html);
});
$("#BluePopupBox").on('click', 'a.icon_remove', function(){
$(this).parent('.mt10').remove();
});
//代码编辑器
if (typeof CodeMirror != "undefined") {
var program_name = "text/x-csrc";
var language = $('#data-language').attr('data-language');
if (language == 1) {
program_name = 'text/x-csrc';
} else if(language==2){
program_name = 'text/x-c++src';
}else if(language==3){
program_name = 'text/x-cython';
}
var editor = CodeMirror.fromTextArea(document.getElementById("program-src"), {
mode: {name: program_name,
version: 2,
singleLineStringErrors: false},
lineNumbers: true,
indentUnit: 2,
matchBrackets: true
});
editor.on('change',function(cMirror){
// get value right from instance
$('#program-src').val(cMirror.getValue());
});
}
});