ToolBox.js 11.6 KB
;(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" title="开始录制"><img src="" alt="" /></div>'+
                        '<div class="tool-item" id="stop-recording" data="1" title="结束录制"><img src="" alt="" /></div>'+
                        '<div class="tool-item" id="re-recoding" data="1" title="开始录制"><img src="" alt="" /></div>'+
                        '<div class="tool-item" id="audio-play" data="1" title="开始录制"><img src="" alt="" /></div>'+
                     '</div>'+
                     '<div class="recording-time" id="timer" value="">00:00:00</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="保存">'+
                        '<input type="button" id="cancel" value="关闭">'+
                     '</div>'+
                   '</div>';

    function ToolBox(){
        this.template = template;
        this.init.apply(this, arguments);
    }

    ToolBox.prototype = {
        constructor: ToolBox,
        options: {
            el: "#toolBox",             // 控制条插入位置id
            role: 0,                    // 角色
            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)

            $('#start-recording img').attr('src', '../img/ideicon/record.svg');
            $('#stop-recording img').attr('src', '../img/ideicon/stop.svg');
            $('#re-recoding img').attr('src', '../img/ideicon/re-record.svg');
            $('#audio-play img').attr('src', '../img/ideicon/play.svg');

            _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(); })
            $('#compileBtn').on('click', function(){ _this.compileMode(); })
        },
        startRecording: function(callback){
            console.log('startRecord');
            var _this = this;

            if (typeof _this.options.startRecordingCallback == 'function') {
                console.log('startRecordingCallback');
                _this.options.startRecordingCallback();
            }

            HZRecorder.get(function(rec) {
                console.log(rec);
                recorder = rec;
                recorder.start();
                // CodingEditer.resetCodeMirrorValue(0);
                CodingEditer.initRecodesValue();

            });

        },
        stopRecording: function(){
            console.log('停止录制');
            var _this = this;
            // 录制完成后禁止修改
            var treeLinkEditor = Dir.treeLinkEditor
            for (var i = 0; i < treeLinkEditor.length; i++) {
                treeLinkEditor[i].CodeMirrorRecord.setOption('readOnly', true);
                treeLinkEditor[i].CodeMirrorReplay.setOption('readOnly', true);
            }

            if (typeof _this.options.stopCallback == 'function') {
                console.log('stopCallback');
                _this.options.stopCallback();
            }

            recorder.stop();
            recorder.getBlobData();

        },
        restartRecord: function(){
            console.log('重新录制代码');
            // CodingEditer.resetCodeMirrorValue(2);
        },
        playCode: function(){
            console.log('播放代码');
            $('#audio')[0].play();
            CodingEditer.playCoding()
        },
        uploadAudio: function(){
            console.log('uploadAudio');
        },
        saveCode: function(){
            console.log('saveCode');
        },
        cancel: function(){
            console.log('cancel');
        },
        compileMode: function(userId, languageid){
            // 获取标准输入值
            var runtimeArgus = $('#runtimeArgus').val() || '';
            var mainPath = Dir.mainPath;
            var _fileList = Dir.getTreeData();

            var compileData = {
                "userId": userId,
                "langId": languageid,
                "mainPath": mainPath,
                "mainArgus": "",
                "runtimeArgus": runtimeArgus,
                "rand": "123456",
                "fileList": _fileList || []
            }
            console.log(compileData);
            $.ajax({
                type: "POST",
                url: gxb_api + "/gxb-web/programmingMulti/codeRun/api",
                data: JSON.stringify(compileData),
                dataType: "json",
                contentType: "application/json",
                success: function(data) {
                    console.log(data);
                    compileResult(data)
                    // ladda.remove();
                    CodingEditer.compileResultData = data;
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    // ladda.remove();
                    alert('错误');
                    console.log(XMLHttpRequest);
                },
            });
        }


    };

    // ToolBox.prototype = {
    //     // constructor: ToolBox,
    //     options: {
    //         el: "#toolBox",
    //         role: 0
    //     },
    //     init: function(options){
    //         this.options = $.extend(this.options, options);
    //         var toolBox = this.options.el;
    //         console.log(toolBox)
    //         $(toolBox).html(template)
    //     },
    //     startRecord: function(){
    //         console.log('-----------')
    //         console.log(this.template)
    //     },
    //     stopRecord: function(){
    //         console.log('停止录制')
    //     },
    //     pauseRecord: function(){
    //         console.log('暂停录制')
    //     }
    // }

    // var ToolBox = {
    //     template: $('<div><h1>播放</h1></div>'),
    //     compileMode: function(userId, languageid){
    //         // 获取标准输入值
    //         var runtimeArgus = $('#runtimeArgus').val() || '';
    //         var mainPath = Dir.mainPath;
    //         var _fileList = Dir.getTreeData();

    //         var compileData = {
    //             "userId": userId,
    //             "langId": languageid,
    //             "mainPath": mainPath,
    //             "mainArgus": "",
    //             "runtimeArgus": runtimeArgus,
    //             "rand": "123456",
    //             "fileList": _fileList || []
    //         }

    //         console.log(compileData);

    //         $.ajax({
    //             type: "POST",
    //             url: gxb_api + "/gxb-web/programmingMulti/codeRun/api",
    //             data: JSON.stringify(compileData),
    //             dataType: "json",
    //             contentType: "application/json",
    //             success: function(data) {
    //                 console.log(data);

    //                 compileResult(data)
    //                 ladda.remove();

    //                 CodingEditer.compileResultData = data;
                    
    //             },
    //             error: function(XMLHttpRequest, textStatus, errorThrown) {
    //                 ladda.remove();
    //                 alert('错误');
    //                 console.log(XMLHttpRequest);
    //             },
    //         });
    //     }
    // }

    // 编译结果
    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)



// 云编译
// function compileRun(ladda){
//     // 获取标准输入值
//     var runtimeArgus = $('#runtimeArgus').val() || '';

//     fileList = ZTREE.getTreeData();
//     console.log(fileList);
//     if(!fileList){
//         alert('请输入代码');
//         ladda.remove();
//         return false;
//     }
//     var compileData = {
//         "userId": _userId,
//         "langId": _languageid,
//         "mainPath": mainPath,
//         "mainArgus": "",
//         "runtimeArgus": runtimeArgus,
//         "rand": "123456",
//         "fileList": fileList
//     }
//     console.log(compileData);
//     $.ajax({
//         type: "POST",
//         url: gxb_api + "/gxb-web/programmingMulti/codeRun/api",
//         data: JSON.stringify(compileData),
//         dataType: "json",
//         contentType: "application/json",
//         success: function(data) {
//             console.log(data);

//             compileResult(data)
//             ladda.remove();

//             $('.compile-loading').hide();
//             $('.compile-reslut').show();
//             GBCodePlayBack.compileResultData = data;
            
//         },
//         error: function(XMLHttpRequest, textStatus, errorThrown) {
//             ladda.remove();
//             alert('错误');
//             console.log(XMLHttpRequest);
//         },
//     });
// }