ToolBox.js
15.7 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
;(function(window){
var template = '<div class="record-ctrl">'+
'<div class="audio-view" style="display: none;">'+
'<audio id="audio"></audio>'+
'</div>'+
'<div class="ctrl-wrap">'+
'<div class="tool-item" id="start-recording" data="1"><span class="start-recording-tips">点此开始录制</span><img src="" alt="" /></div>'+
'<div class="tool-item" id="stop-recording" data="1" data-toggle="tooltip" data-placement="top" title="结束录制"><img src="" alt="" /></div>'+
'<div class="tool-item" id="re-recoding" data="1" data-toggle="tooltip" data-placement="top" title="重新录制"><img src="" alt="" /></div>'+
'</div>'+
'<div class="before">'+
'<div class="tool-item" id="audio-play" data="1" data-toggle="tooltip" data-placement="top" title="播放预览"><img src="" alt="" /></div>'+
'<div class="recording-time" id="timer" value="">00:00:00</div>'+
// '<div class="visual"></div>'+
'</div>'+
// '<div class="after">'+
// '<div class="tool-item" id="audio-play" data="1" data-toggle="tooltip" data-placement="top" title="播放预览"><img src="" alt="" /></div>'+
// '<div class="recording-time" id="timer" value="">00:00:00</div>'+
// '<div class="visual"></div>'+
// '</div>'+
'<div class="save-wrap">'+
'<input type="button" id="upload" class="upload-teach" disabled="disabled" value="保存">'+
'<input type="button" id="savecode" class="upload-stud" style="display: none;" disabled="disabled" value="保存da">'+
'<input type="button" id="cancel" value="关闭">'+
'</div>'+
'<div class="score-wrap" style="display: none;">预估得分:<span class="score-text">0</span>,点击保存按钮分数才会生效</div>'+
'</div>';
function ToolBox(){
this.userId = getQuery('languageid');
this.languageid = getQuery('languageid');
this.template = template;
this.init.apply(this, arguments);
}
var _source = getQuery('source');
ToolBox.prototype = {
constructor: ToolBox,
options: {
el: "#toolBox", // 控制条插入位置id
role: null, // 角色
userId: '',
languageId: '',
startCallback: null, // 开始录制代码
stopCallback: null, // 结束录制
restartCallback: null, // 重新录制代码
playCallback: null, // 播放代码
uploadCallback: null, // 上传音频
saveCallback: null, // 上传代码
cancelCallback: null, // 关闭
compileCallback: null // 编译
},
init: function(options){
var _this = this;
_this.options = $.extend(this.options, options);
var toolBox = this.options.el;
$(toolBox).html(template)
$('#prepare-recording img').attr('src', '../img/ideicon/new-recording.svg');
$('#start-recording img').attr('src', '../img/ideicon/new-recording.svg');
$('#stop-recording img').attr('src', '../img/recording.svg');
$('#re-recoding img').attr('src', '../img/ideicon/new-recording.svg');
$('#audio-play img').attr('src', '../img/play.svg');
// $('#prepare-recording').show();
// $('#prepare-recording').tooltip('show');
// $('#prepare-recording').on('click', function() {
// $(this).hide();
// $('#start-recording').show();
// });
$('#start-recording').show();
$('#start-recording').tooltip({ html : true });
$('#start-recording').tooltip('show');
_this.events();
},
events: function(){
var _this = this;
$('#start-recording').on('click', function(){ _this.startRecording(); })
$('#stop-recording').on('click', function(){ _this.stopRecording(); })
$('#re-recoding').on('click', function(){ _this.restartRecord(); })
$('#audio-play').on('click', function(){ _this.playCode(); })
$('#upload').on('click', function(){ _this.uploadAudio(); })
$('#savecode').on('click', function(){ _this.saveCode(); })
$('#cancel').on('click', function(){ _this.cancel(); })
$('#re-sure').on('click', function(){ _this.reSure(); })
$('#re-cancel').on('click', function(){ _this.reCancel(); })
$('.play-code').on('click', function(){ _this.playerPage(); })
window.onbeforeunload = function() {
return '尚未保存,关闭将丢失本次操作内容,确定关闭?'
};
},
startRecording: function(callback){
console.log(this.options.userId);
$('#start-recording').hide()
$('#stop-recording').show()
var _this = this;
_this.readOnly(0)
setTimeout(function(){
Dir.treeObj.setting.edit.showRenameBtn = false;
Dir.treeObj.setting.edit.enable = false;
$('.button.edit').hide();
},0)
if (typeof _this.options.startCallback == 'function') {
console.log('startCallback');
_this.options.startCallback();
}
CodingEditer.isRcording = true;
},
stopRecording: function(){
console.log('停止录制');
$('#stop-recording').hide()
$('#re-recoding').show()
$('#audio-play').show()
Hourglass.stopTimer()
var _this = this;
_this.readOnly(1)
// 老师端录制停止录音
// if (_this.options.role == 0) {
_this.stopAudio(); //学生和老师一样,都进行录音
// }
// 回调
if (typeof _this.options.stopCallback == 'function') {
console.log('stopCallback');
_this.options.stopCallback();
}
CodingEditer.isRcording = false;
$('#upload').removeClass('upload-teach').addClass('recording-finish').removeAttr('disabled').prop("disabled", false);
},
restartRecord: function(){
$('.re-tips').show()
if (typeof _this.options.restartCallback == 'function') {
_this.options.restartCallback();
}
// var _this = this;
// $('#recordertab a:first').tab('show');
// // CodingEditer.records = [];
// _this.readOnly(0)
// _this.startRecording()
},
reSure: function(){
$('.re-tips').hide()
$('#re-recoding').hide()
$('#stop-recording').show()
$('#audio-play').hide()
$('#upload').removeClass('recording-finish').addClass('upload-teach').attr("disabled", true);
$('#recordertab a:eq(0)').tab('show');
$('.score-wrap').hide();
CodingEditer.resetCodeMirrorValue(0);
var recordtimeoutcontrolls = CodingEditer.recordtimeoutcontrolls;
for (var i = 0; i < recordtimeoutcontrolls.length; i++) {
clearTimeout(recordtimeoutcontrolls[i]);
}
CodingEditer.recordtimeoutcontrolls = [];
CodingEditer.recordtimeouts = [];
$('#audio')[0].pause();
Hourglass.stopTimer();
this.startRecording()
},
reCancel: function(){
$('.re-tips').hide()
},
initRecording: function(){
var _this = this;
// if (_this.options.role == 0) {
HZRecorder.get(function(rec) {
recorder = rec;
recorder.start();
CodingEditer.initRecodesValue()
});
// }else{
// CodingEditer.initRecodesValue()
// }
},
stopAudio: function(){
recorder.stop();
recorder.getBlobData();
},
playCode: function(){
console.log('播放代码=-------------------------');
if (CodingEditer.flag) {
$('#audio')[0].play();
Hourglass.init()
CodingEditer.playCoding()
$('#audio')[0].addEventListener('ended', function () {
console.log('over');
if (CodingEditer.isRecordPage) {
$('#recordertab a:first').tab('show');
}
Hourglass.stopTimer();
}, false);
}else{
console.log('播放代码=-------------------------');
return false;
}
},
uploadAudio: function(){
console.log('uploadAudio');
var _this = this;
$('.upload-tip').show();
var role = getQuery('role');
// if (role == 0) {
recorder.upload(); //学生和老师都进行音频保存
// }else{
// _this.saveCode();
// }
},
saveCode: function(){
var _this = this;
var pickup = 0 //是否要同步录音 0:同步录音,1异步录音
var saveformat = $('#saveformat').find('.active').data('saveformat') || 5;
var programmingFileList = Dir.getTreeData();
var keepTimer = Math.round($('#audio')[0].duration);
var postData = {
"mainPath": Dir.mainPath,
"mainArgus": "lixxx",
"programmingFileList": programmingFileList,
"runtimeArgus": $('#runtimeArgus').val(),
"pickup": pickup,
"userId": _this.options.userId,
"replaytitle": "课程名",
"languageid": _this.options.languageId,
"replaydesc": "回放说明",
"image": "placeholder.png",
"deleteFlag": 1,
"viewcount": "0",
"saveformat": saveformat,
"audioUrl": $('.audio-url').val(),
"programmingSource": 1,
"totaltime": keepTimer
}
var temp_record = JSON.parse(JSON.stringify(CodingEditer.records))
var codeRecordLists = [];
var _role = _this.options.role;
var roleUrl;
for (var i = 0; i < temp_record.length; i++) {
var temp_record_item = temp_record[i];
if(temp_record_item){
temp_record_item.changeobj=JSON.stringify(temp_record_item.changeobj);
if(temp_record_item.treefile){
temp_record_item.treefile=JSON.stringify(temp_record_item.treefile);
}
}
codeRecordLists.push(temp_record_item);
}
if (_role == 0) {
postData.codeRecordList = codeRecordLists;
roleUrl = gxb_api + "/programming/codeReplay/api"
}else if (_role == 1) {
postData.studentRecordList = codeRecordLists;
roleUrl = gxb_api + "/programming/student/studentReplay/api"
}
if (_this.options.autoScore) {
postData.html = CodingEditer.getCodeMirrorValue()[0];
}
console.log(postData);
console.log(roleUrl);
// return false;
$.ajax({
type: "post",
url: roleUrl,
data: JSON.stringify(postData),
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
$('.upload-tip').hide();
$(window).unbind('beforeunload');
var _data = {
"programId": data.codeReplayId || data.studentReplayId,
"programLanguageName": data.languageName,
"programLanguageId": data.languageid,
"programmingSource": data.programmingSource,
"programAudioLength": keepTimer || 0
}
console.log(_data);
$('.upload-tip').hide();
$('#cancel').prop("disabled", false);
window.opener.postMessage(_data, _source);
window.onbeforeunload = null;
window.close();
},
error: function() {
$('.upload-tip').hide();
console.log(CodingEditer.records)
alert('代码保存失败重新保存')
}
});
},
playerPage: function(){
$('.play-code').hide();
// $('.pause-play-code').show();
CodingEditer.playCoding();
CodingEditer.editable(0)
},
cancel: function(){
console.log('cancel');
window.close();
},
/*
* 修改代码状态
*/
readOnly: function(status){
// status: 1 禁止修改,0 允许修改
var treeLinkEditor = Dir.treeLinkEditor;
console.log(status + '##########################')
if (status == 0 || status == 1) {
for (var i = 0; i < treeLinkEditor.length; i++) {
treeLinkEditor[i].CodeMirrorRecord.setOption('readOnly', status);
treeLinkEditor[i].CodeMirrorReplay.setOption('readOnly', status);
}
}else{
console.log('status undefind');
}
}
};
// 编译结果
function compileResult(data, value) {
var runtimeArgus = value || '';
console.info('compileResult')
if (value != '') {
$('#cmprun-tabs a[href="#stdin"]').tab('show');
$('#stdin input').val(runtimeArgus);
}else{
return;
}
if (data.status == 200) {
$('#cmpinfo').html('<p class="text-warning">' + data.compilerInfo.replace(/\n/g, "<br />") + "</p>");
$('#stderr').html('<p class="text-danger">' + data.error.replace(/\n/g, "<br />") + "</p>");
$('#output').html('<p class="text-success">' + data.content.replace(/\n/g, "<br />") + "</p>");
if(data.compilerInfo.length){
$('#cmprun-tabs a[href="#cmpinfo"]').tab('show');
}else if(data.error.length){
$('#cmprun-tabs a[href="#stderr"]').tab('show');
}else if(data.content.length){
$('#cmprun-tabs a[href="#output"]').tab('show');
}
if(!data.compilerInfo.length){
$('#cmpinfo').prepend('<p class="alert alert-success"><i class="glyphicon glyphicon-ok-sign"></i> 编译已成功</p>');
}
}else if(data.status == 500){
$('#cmprun-tabs a[href="#stderr"]').tab('show');
$('#stderr').html('<p class="text-danger">' + data.error.replace(/\n/g, "<br />") + "</p>");
}else{
$('#cmpinfo').html('<p class="alert alert-danger"><i class="glyphicon glyphicon-exclamation-sign"></i> 请求失败 </p>');
$('#cmprun-tabs a[href="#cmpinfo"]').tab('show');
}
}
window.ToolBox = ToolBox;
})(window)