CodingEditer.js 16.9 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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
;(function(window){
    var isReplayPage = false;

    var CodingEditer = {
        records: [],                    // 存储录入的操作
        recording_status: false,        // 当前是否在录制 true: 正在录制,false: 没有录制
        // isWeb: 0,                    // 是否是前端 1:是,0:否
        triggertotal: 0,                // 已播放总数
        speed: 1,                       // 播放速度
        isPause: true,                  // 是否暂停 true:不暂停,flase:暂停
        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;
            console.log(treeLinkEditor)

            initProgramPage(treeLinkEditor);

        },
        /**
         * 获取各个文件中的代码
         * Created by Kira on 2017-02-08
         */
        getCodeMirrorValue: function(){
            var pauseValue = [];
            var treeLinkEditor = Dir.treeLinkEditor;
            for (var i = 0; i < treeLinkEditor.length; i++) {
                // Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
                pauseValue.push(treeLinkEditor[i].CodeMirrorRecord.getValue());
            }
            console.log(pauseValue);
            return pauseValue;
        },
        /**
         * 播放代码
         * Created by Kira on 2017-02-08
         */
        playCoding: function(){
            CodingEditer.resetCodeMirrorValue(1)
            var playbackrecord = CodingEditer.records;
            var starttime = 0;
            $('#recordertab a:eq(1)').tab('show');
            for (var i = 0; i < playbackrecord.length; i++) {
                if (playbackrecord[i].trigger === 1) {
                    console.log('已播放------------------')
                }else if (playbackrecord[i].trigger === 0) {
                    var timeoutcontroller = null;
                    var replayinterval = playbackrecord[i].intervaltime;

                    if (!CodingEditer.isPause) {
                        starttime = replayinterval - (CodingEditer.audioTime - playTotalTime);
                        CodingEditer.isPause = false;
                    }else{
                        if (i == 0) {
                            starttime = replayinterval;
                        }else{
                            starttime = parseInt(starttime) + parseInt(replayinterval);
                        }
                    }
                    timeoutcontroller = setTimeout(changeValueCallback(playbackrecord[i]), starttime/CodingEditer.speed);
                    CodingEditer.recordtimeoutcontrolls.push(timeoutcontroller);
                }
            }
        },
        /**
         * 记录动作
         * Created by Kira on 2017-02-08
         */
        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(){
            CodingEditer.resetCodeMirrorValue();
            CodingEditer.record_startime = new Date().getTime();    // 设置开始录制的时间
            var treeLinkEditor = Dir.treeLinkEditor;
            CodingEditer.recording_status = true;
            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);
        },
        resetCodeMirrorValue: function(num) {
            var _this = this;
            var _thisNum = num;
            var treeLinkEditor = Dir.treeLinkEditor;
            if (_thisNum == 0) {
                for (var i = 0; i < treeLinkEditor.length; i++) {
                    var _path = treeLinkEditor[i].filePath;
                    console.log(treeLinkEditor)
                    Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
                    treeLinkEditor[i].CodeMirrorRecord.setValue("");
                }
            } else if (_thisNum == 1) {
                for (var i = 0; i < treeLinkEditor.length; i++) {
                    if (treeLinkEditor[i].CodeMirrorReplay.getValue()) {
                        Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}, 'replay': true});
                        treeLinkEditor[i].CodeMirrorReplay.setValue("");
                    }
                }
            } else {
                console.log(treeLinkEditor)
                for (var i = 0; i < treeLinkEditor.length; i++) {
                    if(treeLinkEditor[i].CodeMirrorRecord){
                        Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}});
                        treeLinkEditor[i].CodeMirrorRecord.setValue('');
                    }else if(treeLinkEditor[i].CodeMirrorReplay.getValue()){
                        Dir.showEdite({'treeNode': {'name': treeLinkEditor[i].file}, 'replay': true});
                        treeLinkEditor[i].CodeMirrorReplay.setValue("");
                    }
                }
            }
            Dir.showEdite({'treeNode': {'name': treeLinkEditor[0].file}});
        }

    }



    // 初始化编程页面
    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: '',
                    mode: "",
                    lineNumbers: true,
                    smartIndent: false,
                    onChange: function(em, changeobj) {
                        console.log(Dir.getCurrentEditor(em.getWrapperElement().getAttribute('id')))
                        var _pauseValue = CodingEditer.getCodeMirrorValue();
                        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])
                        }
                    },
                    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(); // 播放开始时间

            CodingEditer.triggertotal += 1;

            if (CodingEditer.triggertotal === CodingEditer.records.length && !replay_type) {

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

                for (var i = 0; i < CodingEditer.records.length; i++) {
                    CodingEditer.records[i].trigger = 0;
                }
                // $('.play-code').attr('disabled', false);
                
                // if(document.URL.indexOf('index.htm') >= 0) {
                //     Hourglass.stopTimer();
                // }

                CodingEditer.triggertotal = 0;
            }
            
        }
    }



    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)