Iterm.js 8.71 KB
;(function(window){
    var isCompile = false;
    var _languageid = getQuery('languageid');
    var _userId = getQuery('userId');

    var Iterm = {
        constructor: Iterm,
        compileStatus: true,
        options: {
            userId: _userId,
            languageId: _languageid,
            compileCallback: null    // 编译回调
        },
        init: function(options){
            var _this = this;
            _this.events();
        },
        events: function(){
            var _this = this;
            $('#CompileBtn').unbind('click').on('click', function(){ _this.compile(); })
            $('#save_compile').on('click', function(){ _this.saveCompile(); })
            $('#cancel_compile').on('click', function(){ _this.cancelCompile(); })
            $('#full-screen').on('click', function(){ _this.fullScreen(); })
            $('#back-screen').on('click', function(){ _this.exitFullscreen(); })
            $(document).on('focus', '#runtimeArgus', function(){  })
        },
        compile: function(){
            if (_languageid == 35) {
                var _pauseValue = CodingEditer.getCodeMirrorValue();
                CodingEditer.webResult(_pauseValue[0], _pauseValue[1], _pauseValue[2])
            }else{
                var _this = this;
                var el = document.querySelector('#CompileBtn');
                var userId = _this.options.userId;
                // var languageId = _this.options.languageId;
                var languageId = _this.options.languageId || Dir.languageid;
                var role = getQuery('role');

                // 判断是否录制走不同的逻辑
                if (!CodingEditer.isRcording) {
                    compileMode(el, userId, languageId)
                }else{
                    
                    if (_this.compileStatus && !_this.isCompile) {
                        $('#CompileBtn').removeClass('compile-btn').addClass('static-btn');
                        Hourglass.pauseTimer();     // 暂停计时器
                        if (role == 0) {
                           recorder.pause();        // 音频暂停 
                        }

                        $('.compile-loading').show();
                        $('.pause-shodow').show()

                        // 编译时插入操作
                        var treeNode = null;
                        var check_tree_time = new Date().getTime();
                        CodingEditer.addRecord(treeNode, check_tree_time);

                        _this.compileStatus = false;
                        isCompile = true;

                        compileMode(el, userId, languageId)
                        $('.contrl-shodow').show();
                    }
                    console.log('******************************' + languageId)
                    
                }
            }
        },
        compileResult: function(data, value){
            if (!data) {
                var data = {
                    status: 200,
                    compilerInfo: '',
                    content: '',
                    error: '',
                    code: ''
                };
                console.log('------------- compileResult --------------')
            }

            compileResult(data, value)
        },
        saveCompile: function(){
            console.log(this.compileStatus)
            this.compileStatus = true;
            console.log(this.compileStatus)

            recodingConf()
            
            var length = CodingEditer.records.length - 1;

            CodingEditer.records[length].compile = CodingEditer.compileResultData;

            CodingEditer.records[length].content = CodingEditer.compileResultData.content;
            CodingEditer.records[length].error = CodingEditer.compileResultData.error;
            CodingEditer.records[length].compilerInfo = CodingEditer.compileResultData.compilerInfo;
            CodingEditer.records[length].code = CodingEditer.compileResultData.code;
        },
        cancelCompile: function(){
            console.log(this.compileStatus)
            this.compileStatus = true;
            console.log(this.compileStatus)
            recodingConf()
        },
        fullScreen: function(){
            launchFullScreen(document.body);
            $('#back-screen').show();
            $('#full-screen').hide();
        },
        exitFullscreen: function(){
            exitFullscreen();
            $('#full-screen').show();
            $('#back-screen').hide();
        }
    }

    Iterm.init();

    // 继续录制配置
    function recodingConf(){
        $('.pause-shodow').hide()
        $('.compile-reslut').hide();
        $('.contrl-shodow').hide();
        $('.contrl-shodow').hide();

        Hourglass.pauseTimer();

        var role = getQuery('role');
        if (role == 0) {
            recorder.pause();
        }
        CodingEditer.record_startime = new Date().getTime();

        $('#CompileBtn').addClass('compile-btn').removeClass('static-btn');
        isCompile = false;
    } 

    // 编译
    function compileMode(el, userId, languageId){
        var l = Ladda.create(el);
        l.start();
        // 获取标准输入值
        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);

                $('.compile-loading').hide();
                $('.compile-reslut').show();

                compileResult(data)
                l.remove();
                CodingEditer.compileResultData = data;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                l.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');
        }
    }

    // 找到支持的方法, 使用需要全屏的 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();
        }
    }

    window.Iterm = Iterm
})(window)