teacher.js
6.13 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
$(function(){
var teacherId = document.getElementById('teacher');
var socket = io('https://live-api.gaoxiaobang.com');
// var socket = io('http://localhost:5000');
// 初始化codemirror页面
var teacher = CodeMirror.fromTextArea(teacherId, {
value: '输入HTML代码',
mode: 'text/x-java',
lineNumbers: true,
smartIndent: false
});
// 记录每次操作的变化
CodeMirror.on(teacher,"change", function(instance, changeObj) {
// console.log(instance);
// console.log(changeObj);
var changeObjStr = JSON.stringify(changeObj);
//
socket.emit('teacher.programming', changeObjStr);
socket.emit('programming.content', teacher.getValue());
// 构建操作对象
buildOperate('teacher', changeObj);
console.log(IDELive.operations);
});
// 链接时执行初始化代码
// socket.on('programming.content', function(msg) {
// teacher.setValue(msg);
// });
var IDELive = {
operations: [] // 存储录制时的动作对象
}
// 选中 input 事件
$('#runtimeArgus').focus(function(){
console.log('------getValue')
socket.emit('select.menu');
})
// 输入事件
$('#runtimeArgus').bind('input propertychange', function() {
console.log($('#runtimeArgus').val());
socket.emit('stdin.value', $('#runtimeArgus').val());
});
// $("#runtimeArgus").change(function(){
// });
var LiveTeacher = {
init: function(){
LiveTeacher.events();
},
events: function(){
var _this = this;
$('#liveStart').on('click', function(){ liveStartFun(); });
$('#liveEnd').on('click', function(){ liveEndFun(); });
$('#liveConfirm').on('click', function(){ liveConfirmFun(); });
$('#liveCancel').on('click', function(){ liveCancelFun(); });
$('#liveSave').on('click', function(){ });
$('#liveOut').on('click', function(){ liveOutFun(); });
$('#CompileBtn').on('click', function(){ liveCompileFun(); });
},
liveStartFun: function(){},
liveEndFun: function(){},
liveConfirmFun: function(){},
liveCancelFun: function(){},
liveSaveFun: function(){},
liveOutFun: function(){},
liveCompileFun: function(){}
}
// 禁止关闭浏览器
window.onbeforeunload = function(){
alert('-0000');
console.log('-----======')
return ('你确定要结束教学吗?请确认保存内容');
}
LiveTeacher.init();
$('#cmprun-tabs').on('click', 'li', function(){
console.log($(this).index());
socket.emit('check.tab', $(this).index());
})
// 开始录制
function liveStartFun(){
// $('.tip-wrap').show();
var seconds = 2;
$('.countdown').show();
$('#liveStart').hide();
$('#liveEnd').show();
socket.emit('start.live');
// 倒计时
var countdown = setInterval(function() {
$(".countdown h1").html(seconds);
seconds = seconds - 1;
if (seconds == -1) {
$(".countdown").hide();
clearInterval(countdown);
socket.emit('sync.programming');
}
}, 1000);
}
// 确认开始录制
function liveConfirmFun(){
$('.tip-wrap').hide();
IDELive.record_startime = new Date().getTime()
console.log(IDELive.record_startime);
}
// 结束录制
function liveEndFun(){
// $('#liveStart').hide();
$('#liveEnd').hide();
$('#liveSave').removeAttr('disabled')
}
// 取消开始录制
function liveCancelFun(){
$('.tip-wrap').hide();
}
// 编译
function liveCompileFun(){
console.log('编译' + teacher.getValue());
socket.emit('programming.compile');
var el = document.querySelector('#CompileBtn')
var l = Ladda.create(el);
l.start();
var input = $('#input').val() || '';
var code = teacher.getValue();
var userId = 826920;
var compileData = {
code: code,
langid: 10,
input: input,
userId: userId
}
$.ajax({
type: "post",
url: gxb_api + "/submit/submitCode/api",
data: JSON.stringify(compileData),
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
l.remove();
compileResult(data);
},
error: function(){
l.stop();
alert('错误');
}
});
}
// 结束录制
function liveOutFun(){
// 课程结束时清楚服务端记录的数据
socket.emit('programming.clear', '');
teacher.setValue('');
console.log('programming.clear')
socket.on('programming.clear', function(msg) {
console.log('programming.clear ---- ');
content = '';
io.emit('programming.content', msg);
});
// window.onbeforeunload = function(){
// return ('你确定要结束教学吗?')
// }
// window.close()
$(window).unbind('beforeunload', function(){
return '确认退出? 将不保存你的代码';
});
window.close();
}
/**
* 组合录入代码
* by Kira 2017-5-17
*/
function buildOperate(editor, changeobj){
var intervaltime,
currenttime = new Date().getTime();
console.log(IDELive.record_startime);
var intervaltime = currenttime - IDELive.record_startime;
var operateObj = {
intervaltime: intervaltime,
editor: editor,
changeobj: changeobj,
currenttime: currenttime,
trigger: 0
};
IDELive.operations.push( operateObj); // 添加每次输入对象
IDELive.record_startime = currenttime; //设定起始时间
}
})