utils.js
6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* 通用方法
* by Kira 2017-5-19
*/
$(document).on('click', '#full-screen', function(event) {
event.preventDefault();
launchFullScreen(document.body);
$('#back-screen').show();
$('#full-screen').hide();
});
$(document).on('click', '#back-screen', function(event) {
exitFullscreen();
$('#full-screen').show();
$('#back-screen').hide();
})
// 找到支持的方法, 使用需要全屏的 element 调用
function launchFullScreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
function exitFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozExitFullScreen) {
document.mozExitFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
// 往编辑器中写入代码
function processChangeObject(playbackcm, obj) {
for (var i = 0; i < obj.text.length; i++) {
/* 设置鼠标行标示 */
if (playbackcm.getTextArea().id === "HTMLplayer") {
if (htmlhlLine !== null) {
playbackcm.clearMarker(htmlhlLine);
}
htmlhlLine = playbackcm.setMarker(obj.from.line + i, '<span class="text-primary glyphicon glyphicon-pencil"></span>');
} else if (playbackcm.getTextArea().id === "CSSplayer") {
if (csshlLine !== null) {
playbackcm.clearMarker(csshlLine);
}
csshlLine = playbackcm.setMarker(obj.from.line + i, '<span class="text-primary glyphicon glyphicon-pencil"></span>');
} else if (playbackcm.getTextArea().id === "JSplayer") {
if (jshlLine !== null) {
playbackcm.clearMarker(jshlLine);
}
jshlLine = playbackcm.setMarker(obj.from.line + i, '<span class="text-primary glyphicon glyphicon-pencil"></span>');
}
/* 设置鼠标输入指针 */
playbackcm.setCursor({ line: obj.from.line + i, ch: obj.from.ch });
//以下代码处理其他
if (obj.text.length === 3 && obj.text[1] !== '' && i === 1) {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 });
} else if (obj.text.length === 3 && obj.text[2] !== '' && i === 2) {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 });
//以下代码处理文字块选择并回车的问题
} else if (obj.text.length === 2 && obj.text[i] === '' && i === 1) {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 });
//以下处理块选择并且粘贴多行问题
} else {
if (i == 0) {
playbackcm.setSelection({ line: obj.from.line + i, ch: obj.from.ch }, { line: obj.to.line + i, ch: obj.to.ch });
} else {
playbackcm.setSelection({ line: obj.from.line + i, ch: 0 }, { line: obj.from.line + i, ch: 0 });
}
}
if (i !== obj.text.length - 1) {
playbackcm.replaceSelection(obj.text[i] + '\n');
} else {
playbackcm.replaceSelection(obj.text[i]);
}
}
if (obj.next) {
processChangeObject(playbackcm, obj.next);
}
}
// 编译结果
function compileResult(data){
if(data.result){
$('#cmpinfo').html('<p class="text-warning">' + data.cmpinfo.replace(/\n/g, "<br />") + "</p>");
$('#stderr').html('<p class="text-danger">' + data.stderr.replace(/\n/g, "<br />") + "</p>");
$('#output').html('<p class="text-success">' + data.output.replace(/\n/g, "<br />") + "</p>");
if(data.cmpinfo.length){
$('#cmprun-tabs a[href="#cmpinfo"]').tab('show');
}else if(data.stderr.length){
$('#cmprun-tabs a[href="#stderr"]').tab('show');
}else if(data.output.length){
$('#cmprun-tabs a[href="#output"]').tab('show');
}
if(!data.cmpinfo.length){
$('#cmpinfo').prepend('<p class="alert alert-success"><i class="glyphicon glyphicon-ok-sign"></i> 编译已成功</p>');
}
if(!data.stderr.length){
$('#stderr').prepend('<p class="alert alert-success"><i class="glyphicon glyphicon-ok-sign"></i> 无输出错误</p>');
}
if(!data.output.length){
$('#output').prepend('<p class="alert alert-warning"><i class="glyphicon glyphicon-exclamation-sign"></i> 代码编译错误或者运行相关功能受限制</p>');
}
}else{
$('#cmpinfo').html('<p class="alert alert-danger"><i class="glyphicon glyphicon-exclamation-sign"></i> 云端编译超时,请稍后重新尝试</p>');
$('#cmprun-tabs a[href="#cmpinfo"]').tab('show');
}
}
// 学生编译结果
function studCompileResult(data){
if(data.result){
$('#studCmpinfo').html('<p class="text-warning">' + data.cmpinfo.replace(/\n/g, "<br />") + "</p>");
$('#studStderr').html('<p class="text-danger">' + data.stderr.replace(/\n/g, "<br />") + "</p>");
$('#studOutput').html('<p class="text-success">' + data.output.replace(/\n/g, "<br />") + "</p>");
if(data.cmpinfo.length){
$('#cmprun-tabs a[href="#studCmpinfo"]').tab('show');
}else if(data.stderr.length){
$('#cmprun-tabs a[href="#studStderr"]').tab('show');
}else if(data.output.length){
$('#cmprun-tabs a[href="#studOutput"]').tab('show');
}
if(!data.cmpinfo.length){
$('#studCmpinfo').prepend('<p class="alert alert-success"><i class="glyphicon glyphicon-ok-sign"></i> 编译已成功</p>');
}
if(!data.stderr.length){
$('#studStderr').prepend('<p class="alert alert-success"><i class="glyphicon glyphicon-ok-sign"></i> 无输出错误</p>');
}
if(!data.output.length){
$('#studOutput').prepend('<p class="alert alert-warning"><i class="glyphicon glyphicon-exclamation-sign"></i> 代码编译错误或者运行相关功能受限制</p>');
}
}else{
$('#studCmpinfo').html('<p class="alert alert-danger"><i class="glyphicon glyphicon-exclamation-sign"></i> 云端编译超时,请稍后重新尝试</p>');
$('#cmprun-tabs a[href="#studCmpinfo"]').tab('show');
}
}