CodingEditer.js 13.9 KB
;(function(window){
    var isReplayPage = false;

    var CodingEditer = {
        records: [],
        recording_status: 1,    //当前是否在录制
        // isWeb: 0,               // 是否是前端 1:是,0:否
        triggertotal: 0,
        speed: 1,
        recordtimeoutcontrolls: [],
        init: function(){
            console.log('CodingEditer');
        },
        webResult: function(html, css, js){
            $('#ifrcontainer').html($('<iframe />', { 'id': 'resultiframe', 'frameBorder': '0', 'style': 'width:100%', 'z-index': '-1000' }));
            
            console.log(js);

            var iframe = $('#resultiframe')[0], doc;

            if (iframe.contentDocument) {
                doc = iframe.contentDocument;
            } else if (iframe.contentWindow) {
                doc = iframe.contentWindow.document;
            } else {
                doc = iframe.document;
            }

            /* 判断用户输入HTML片段还是完成的HTML文档 */
            // var completedflag = 0,
            //     lowhtml = html.toLowerCase();
            // if (lowhtml.toLowerCase().indexOf('</body>') >= 0 && lowhtml.toLowerCase().indexOf('<body>') >= 0) {
            //     completedflag = 1;
            // }

            var _css = "<style>" + css + "</style>";
            var _script = "<script>" + js + "</script>";

            var result = '<!DOCTYPE HTML><html><head>' + _css + '</head><body>' + html + '</body></html>';

            result = result + _script;

            doc.open();
            doc.writeln(result);
            doc.close();

        },
        initCodeMirror: function(langid){
            console.log('---------------------------')

            var treeLinkEditor = Dir.treeLinkEditor;

            CodingEditer.record_startime = new Date().getTime();    // 设置开始录制的时间

            initProgramPage(treeLinkEditor);

            this.initRecodesValue();

        },
        /**
         * 获取各个文件中的代码
         * Created by Kira on 2017-02-08
         */
        getCodeMirrorValue: function(){
            var pauseValue = [];
            var treeLinkEditor = Dir.treeLinkEditor;
            console.log(treeLinkEditor)
            for (var i = 0; i < treeLinkEditor.length; i++) {
                console.log(treeLinkEditor[i].CodeMirrorRecord.getValue())
                // .showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
                pauseValue.push(treeLinkEditor[i].CodeMirrorRecord.getValue());
            }

            console.log(pauseValue);
            return pauseValue;
        },
        playCoding: function(){
            var playbackrecord = CodingEditer.records;

            var starttime = 0;

            $('#recordertab a:eq(1)').tab('show');


            for (var i = 0; i < playbackrecord.length; i++) {
                var timeoutcontroller = null;
                var replayinterval = playbackrecord[i].intervaltime;
                
                if (i == 0) {
                    starttime = replayinterval;
                }else{
                    starttime = parseInt(starttime) + parseInt(replayinterval);
                }
                timeoutcontroller = setTimeout(changeValueCallback(playbackrecord[i]), starttime/CodingEditer.speed);
                CodingEditer.recordtimeoutcontrolls.push(timeoutcontroller);
            }
        },
        addRecord: function(treeNode, check_tree_time){

            console.log(check_tree_time)
            console.log(CodingEditer.record_startime)

            var recordobj = {
                intervaltime: check_tree_time - CodingEditer.record_startime,
                editor: 'tab',
                changeobj: {
                    from: {
                        ch: 0,
                        line: 0,
                    },
                    text: '',
                    to: {
                        ch: 0,
                        line: 0,
                    }
                },
                treefile: treeNode,
                trigger: 0,
                playTime: 0
            };

            CodingEditer.records.push(recordobj);
            console.log(CodingEditer.records);
            CodingEditer.record_startime = check_tree_time;
        },
        initRecodesValue: function(){
            var treeLinkEditor = Dir.treeLinkEditor;
            for (var i = treeLinkEditor.length-1; i >= 0; i--) {
                console.log(treeLinkEditor[i].CodeMirrorRecord.getValue());
                var recordobj = {
                    intervaltime: 0,
                    editor: treeLinkEditor[i].name,
                    changeobj: {
                        from: {
                            ch: 0,
                            line: 0,
                        },
                        text: [treeLinkEditor[i].CodeMirrorRecord.getValue()],
                        to: {
                            ch: 0,
                            line: 0,
                        }
                    },
                    trigger: 0,
                    playTime: 0
                };
                CodingEditer.records.push(recordobj);
            }
            console.log(CodingEditer.records);
        },

    }



    // 初始化编程页面
    function initProgramPage(treeLinkEditor, langid){
        console.log(treeLinkEditor);
        // 生成 CodeMirror
        for (var i = 0; i < treeLinkEditor.length; i++) {
            // 判断是否是播放页面,如果是则不显示录制区相关dom
            if(!isReplayPage){
                treeLinkEditor[i].CodeMirrorRecord = CodeMirror.fromTextArea($('#' + treeLinkEditor[i].record)[0], {
                    value: 'heoolo',
                    mode: "text/x-java",
                    lineNumbers: true,
                    smartIndent: false,
                    onChange: function(em, changeobj) {
                        console.log(Dir.getCurrentEditor(em.getWrapperElement().getAttribute('id')))
                        var _pauseValue = CodingEditer.getCodeMirrorValue();
                        console.log(_pauseValue)
                        if (CodingEditer.recording_status) {
                            buildRecords(Dir.getCurrentEditor(em.getWrapperElement().getAttribute('id')), changeobj);
                        }
                        if (CodingEditer.isWeb) {
                            console.log("++++++++++")
                            CodingEditer.webResult(_pauseValue[0], _pauseValue[1], _pauseValue[2])
                        }
                        
                        console.log(_pauseValue);

                    },
                    onFocus: function(em) {
                    },
                    onCursorActivity: function(em) {
                    }
                });
                treeLinkEditor[i].CodeMirrorRecordId = treeLinkEditor[i].record + 'CodeMirror';
                $('#' + treeLinkEditor[i].record).next().attr('id', treeLinkEditor[i].CodeMirrorRecordId).hide();
            }

            treeLinkEditor[i].CodeMirrorReplay = CodeMirror.fromTextArea($('#' + treeLinkEditor[i].replay)[0], {
                value: '',
                mode: "text/x-java",
                lineNumbers: true,
                smartIndent: false,
                onChange: function(em, changeobj) {
                },
                onFocus: function(em) {
                },
                onCursorActivity: function(em) {
                }
            });
            treeLinkEditor[i].CodeMirrorReplayId = treeLinkEditor[i].replay + 'CodeMirror';
            $('#' + treeLinkEditor[i].replay).next().attr('id', treeLinkEditor[i].CodeMirrorReplayId).hide();
        }

        console.log(treeLinkEditor);

        // 默认显示第一个文件夹的第一个文件
        Dir.showEdite();
    }

    /**
     * 组合录入代码 
     * by Kira 2017-3-9
     */ 
    function buildRecords(editor, changeobj){
        console.log(editor);

        var intervaltime,
            currenttime = new Date().getTime();

        // 根据不同的身份设置录制代码的时间间隔,当学生端输入的时候,重置间隔为100毫秒
        // if (_role == 0 && CodingEditer.record_startime) {
        //     intervaltime = currenttime - CodingEditer.record_startime;
        // }else if (_role == 1){
        //     intervaltime = 100;
        // }else{
        //     intervaltime = 0;
        // }

        intervaltime = currenttime - CodingEditer.record_startime;


        var recordobj = {
            intervaltime: intervaltime,
            editor: editor,
            changeobj: changeobj,
            trigger: 0,
            playTime: 0,
            currenttime: currenttime
        };

        CodingEditer.records.push(recordobj);       // 添加每次输入对象

        CodingEditer.record_startime = currenttime; //设定起始时间

        console.log(CodingEditer.records);

    }


    function changeValueCallback(record, replay_type, offset) {
        var obj = record.changeobj,
            editor = record.editor,
            treeLinkEditor = Dir.treeLinkEditor;
        console.log(treeLinkEditor);
        return function() {
            // 播放
            for (var i = 0; i < treeLinkEditor.length; i++) {
                if(editor == treeLinkEditor[i].name){ // 播放代码
                    processChangeObject(treeLinkEditor[i].CodeMirrorReplay, obj);
                }else if(record.treefile){ // 切换tree目录
                    Dir.showEdite({'treeNode': record.treefile, 'replay': true});
                }
            }

            record.trigger = 1; //标记已运行
            record.playTime = new Date().getTime(); // 播放开始时间
            
        }
    }



    function processChangeObject(playbackcm, obj) {
        console.log(playbackcm);
        // 菜单选中
        Dir.showEdite({'treeNode': {'name': getEditor(playbackcm, 'name')},'replay': true});

        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);
        }

        if (CodingEditer.autorun) {
            CodingEditer.generateResult(CodingEditer.htmlplayereditor.getValue(),
                CodingEditer.cssplayereditor.getValue(),
                CodingEditer.jsplayereditor.getValue()
            );
        }
    }


    function getEditor(obj, key){
        var currentObj,
            treeLinkEditor = Dir.treeLinkEditor;
        for (var i = 0; i < treeLinkEditor.length; i++) {
            if(treeLinkEditor[i].CodeMirrorReplay === obj){
                currentObj = treeLinkEditor[i];
            }
        }
        
        console.log(currentObj);
        switch (key) {
            case 'name':
                console.log('-------------------getEditor-------------------');
                return currentObj.name;
                break;
            case 'file':
                return currentObj.file;
                break;
            case 'filePath':
                return currentObj.file;
                break;
            case 'record':
                return currentObj.record;
                break;
            case 'replay':
                return currentObj.replay;
                break;
            case 'CodeMirrorRecord':
                return currentObj.CodeMirrorRecord;
                break;
            case 'CodeMirrorReplay':
                return currentObj.CodeMirrorReplay;
                break;
            case 'CodeMirrorRecordId':
                return currentObj.CodeMirrorRecordId;
                break;
            case 'CodeMirrorReplayId':
                return currentObj.CodeMirrorReplayId;
                break;
            default:
                break;
        }
    }

    window.CodingEditer = CodingEditer;

})(window)