Commit 282b3318850e1d661d0cc443deaa4ed5d9911e1a

Authored by Keystion
1 parent ce1fcae2

合并gbreplayer.js代码到gb.js

Showing 1 changed file with 257 additions and 50 deletions
src/js/gb.js
@@ -1495,7 +1495,7 @@ $(function() { @@ -1495,7 +1495,7 @@ $(function() {
1495 /* 解决Chrome浏览器非激活标签页下错乱问题 */ 1495 /* 解决Chrome浏览器非激活标签页下错乱问题 */
1496 (function() { 1496 (function() {
1497 var hidden = "hidden"; 1497 var hidden = "hidden";
1498 - 1498 +
1499 // Standards: 1499 // Standards:
1500 if (hidden in document) 1500 if (hidden in document)
1501 document.addEventListener("visibilitychange", onchange); 1501 document.addEventListener("visibilitychange", onchange);
@@ -1534,9 +1534,8 @@ $(function() { @@ -1534,9 +1534,8 @@ $(function() {
1534 /* Bugfix: Mobile Firefox has a bug here, when loading page it shows blur */ 1534 /* Bugfix: Mobile Firefox has a bug here, when loading page it shows blur */
1535 1535
1536 if (document.body.className === 'hidden') { 1536 if (document.body.className === 'hidden') {
1537 - if ($('#play').data('status') == 1) {  
1538 - $('#play').trigger('click');  
1539 - } 1537 + // player.pauseCallback();
  1538 + // pauseCode()
1540 } 1539 }
1541 } 1540 }
1542 // set the initial state 1541 // set the initial state
@@ -1679,7 +1678,7 @@ $(function() { @@ -1679,7 +1678,7 @@ $(function() {
1679 var pausetatus = $('#pause').data('status'); 1678 var pausetatus = $('#pause').data('status');
1680 1679
1681 if (_role == 0 && pausetatus == 1) { 1680 if (_role == 0 && pausetatus == 1) {
1682 - intervaltime = (currenttime - GBCodePlayBack.record_continue_time) + (GBCodePlayBack.record_pause_time - GBCodePlayBack.record_startime); 1681 + intervaltime = (currenttime - GBCodePlayBack.record_continue_time);
1683 $('#pause').data('status', '0'); 1682 $('#pause').data('status', '0');
1684 } 1683 }
1685 1684
@@ -1689,7 +1688,8 @@ $(function() { @@ -1689,7 +1688,8 @@ $(function() {
1689 editor: editor, 1688 editor: editor,
1690 changeobj: changeobj, 1689 changeobj: changeobj,
1691 trigger: 0, 1690 trigger: 0,
1692 - playTime: 0 1691 + playTime: 0,
  1692 + currenttime: currenttime
1693 }; 1693 };
1694 console.log('GBCodePlayBack.onChange'); 1694 console.log('GBCodePlayBack.onChange');
1695 console.log(GBCodePlayBack.records); 1695 console.log(GBCodePlayBack.records);
@@ -1717,6 +1717,7 @@ $(function() { @@ -1717,6 +1717,7 @@ $(function() {
1717 1717
1718 record.trigger = 1; //标记已运行 1718 record.trigger = 1; //标记已运行
1719 record.playTime = new Date().getTime(); // 播放开始时间 1719 record.playTime = new Date().getTime(); // 播放开始时间
  1720 + GBCodePlayBack.codeTimes = 1; //
1720 1721
1721 var resultData = { 1722 var resultData = {
1722 output: record.output || '', 1723 output: record.output || '',
@@ -1870,11 +1871,22 @@ $(function() { @@ -1870,11 +1871,22 @@ $(function() {
1870 1871
1871 // 开始录制 1872 // 开始录制
1872 $('#start-recording').click(function() { 1873 $('#start-recording').click(function() {
  1874 + $('#recordertab a:eq(0)').tab('show');
1873 // 编译状态 1875 // 编译状态
1874 var data = $(this).attr('data'); 1876 var data = $(this).attr('data');
1875 var seconds = 0; 1877 var seconds = 0;
1876 $("#countdown").show(); 1878 $("#countdown").show();
  1879 +
  1880 + var resultData = {
  1881 + output: '',
  1882 + stderr: '',
  1883 + cmpinfo: '',
  1884 + result: 1
  1885 + };
  1886 + runResult()
  1887 +
1877 if (data == 1) { 1888 if (data == 1) {
  1889 + GBCodePlayBack.isRcording = 1;
1878 $("#countdown").find('img').attr('src', 'img/ready-go.gif'); 1890 $("#countdown").find('img').attr('src', 'img/ready-go.gif');
1879 $("#countdown").find('img').load(function() { 1891 $("#countdown").find('img').load(function() {
1880 var countdown = setInterval(function() { 1892 var countdown = setInterval(function() {
@@ -1882,10 +1894,13 @@ $(function() { @@ -1882,10 +1894,13 @@ $(function() {
1882 if (seconds == 2) { 1894 if (seconds == 2) {
1883 $("#countdown").hide(); 1895 $("#countdown").hide();
1884 startReacoder(); 1896 startReacoder();
  1897 + compileResult(resultData);
1885 } 1898 }
1886 }, 1000); 1899 }, 1000);
1887 }) 1900 })
1888 } else { 1901 } else {
  1902 + GBCodePlayBack.isRcording = 0;
  1903 + GBCodePlayBack.htmleditor.setOption('readOnly', true);
1889 $("#countdown").hide(); 1904 $("#countdown").hide();
1890 $("#re-recoding").show(); 1905 $("#re-recoding").show();
1891 $("#start-recording").hide(); 1906 $("#start-recording").hide();
@@ -1895,6 +1910,10 @@ $(function() { @@ -1895,6 +1910,10 @@ $(function() {
1895 }) 1910 })
1896 1911
1897 $('#re-recoding').click(function() { 1912 $('#re-recoding').click(function() {
  1913 + $('#audio')[0].pause();
  1914 + Hourglass.stopTimer();
  1915 + playEnd()
  1916 + $('.audio-play').attr('disabled', false);
1898 $('.re-tips').show(); 1917 $('.re-tips').show();
1899 }) 1918 })
1900 1919
@@ -1907,6 +1926,154 @@ $(function() { @@ -1907,6 +1926,154 @@ $(function() {
1907 $(".re-tips").hide(); 1926 $(".re-tips").hide();
1908 }) 1927 })
1909 1928
  1929 + // 切换自动/手动运行代码
  1930 + $('#autorun').click(function(){
  1931 + var $this = $(this),
  1932 + autorun = $this.data('autorun');
  1933 + autoRun(autorun)
  1934 + $this.data('autorun', (autorun) ? 0 : 1);
  1935 + GBCodePlayBack.autorun = !GBCodePlayBack.autorun;
  1936 + runResult()
  1937 + });
  1938 +
  1939 + $('#runnow').click(function(){
  1940 + runResult()
  1941 + });
  1942 +
  1943 + // 使用服务端编译语言
  1944 + $('#comprunbtngrp').unbind("click").on('click', '#comprun', function(event){
  1945 + event.preventDefault();
  1946 + var l = Ladda.create(this);
  1947 + $('#comprun').find('ladda-spinner').remove();
  1948 + l.start();
  1949 +
  1950 + if (!GBCodePlayBack.isRcording) {
  1951 + compileRun(l)
  1952 + }else{
  1953 + var runStatus = $("#comprun").data('run');
  1954 + if (runStatus == 1) {
  1955 + Hourglass.pauseTimer(); // 暂停计时器
  1956 + recorder.pause(); // 音频暂停
  1957 + }
  1958 + $("#comprun").data('run', 0);
  1959 + $('.compile-loading').show();
  1960 + // 暂停录制
  1961 + var pauseState = $('#pause').data('pause');
  1962 + if (pauseState == 1 && _role == 0) {
  1963 + GBCodePlayBack.record_pause_time = new Date().getTime(); // 暂停的时间
  1964 + var recordobj = {
  1965 + intervaltime: GBCodePlayBack.record_pause_time - GBCodePlayBack.record_startime,
  1966 + editor: 'html',
  1967 + changeobj: {
  1968 + from: {
  1969 + ch: 0,
  1970 + line: 0,
  1971 + },
  1972 + text: '',
  1973 + to: {
  1974 + ch: 0,
  1975 + line: 0,
  1976 + }
  1977 + },
  1978 + trigger: 0,
  1979 + playTime: 0
  1980 + };
  1981 + GBCodePlayBack.records.push(recordobj);
  1982 +
  1983 + $('#pause').data('status', '1');
  1984 + $('#pause').data('pause', '0');
  1985 + $('#pause').removeClass('hidden').addClass('show');
  1986 + $('#start-recording').hide();
  1987 +
  1988 + $('.pause-shodow').removeClass('hidden').addClass('show');
  1989 + }
  1990 + compileRun(l)
  1991 + }
  1992 + });
  1993 +
  1994 +
  1995 + // 保存编译结果
  1996 + $('#save_compile').unbind("click").click(function(){
  1997 + var length = GBCodePlayBack.records.length - 1;
  1998 + recodingConf()
  1999 +
  2000 + GBCodePlayBack.records[length].compile = GBCodePlayBack.compileResultData;
  2001 +
  2002 + GBCodePlayBack.records[length].output = GBCodePlayBack.compileResultData.output;
  2003 + GBCodePlayBack.records[length].stderr = GBCodePlayBack.compileResultData.stderr;
  2004 + GBCodePlayBack.records[length].cmpinfo = GBCodePlayBack.compileResultData.cmpinfo;
  2005 + GBCodePlayBack.records[length].result = GBCodePlayBack.compileResultData.result;
  2006 +
  2007 + contrlPause();
  2008 + })
  2009 +
  2010 + // 不保存编译结果
  2011 + $('#cancel_compile').unbind('click').click(function(){
  2012 + recodingConf()
  2013 + contrlPause()
  2014 + })
  2015 +
  2016 + // 继续录制配置
  2017 + function recodingConf(){
  2018 + Hourglass.pauseTimer();
  2019 + $("#comprun").data('run', 1);
  2020 + GBCodePlayBack.record_startime = new Date().getTime();
  2021 + GBCodePlayBack.record_continue_time = new Date().getTime();
  2022 + }
  2023 +
  2024 + // 切换自动/手动运行代码
  2025 + function autoRun(autorun){
  2026 + if(autorun===1){
  2027 + $('#runnow').prop('disabled',false).html('手动运行').attr('data-original-title', '手动运行');
  2028 + }else{
  2029 + $('#runnow').prop('disabled',true).html('自动运行').attr('data-original-title', '自动运行');
  2030 + }
  2031 + }
  2032 +
  2033 + // 编译结果
  2034 + function runResult(){
  2035 + if(typeof GBCodePlayBack.htmleditor !== 'undefined'){
  2036 + GBCodePlayBack.generateResult(GBCodePlayBack.htmleditor.getValue());
  2037 + return false;
  2038 + }
  2039 + if(typeof GBCodePlayBack.htmlplayereditor !== 'undefined'){
  2040 + GBCodePlayBack.generateResult(GBCodePlayBack.htmlplayereditor.getValue());
  2041 + }
  2042 + }
  2043 +
  2044 + // 云编译
  2045 + function compileRun(l){
  2046 + var code = (typeof GBCodePlayBack.htmleditor !=='undefined' && GBCodePlayBack.htmleditor.getValue())
  2047 + || (typeof GBCodePlayBack.htmlplayereditor !=='undefined' && GBCodePlayBack.htmlplayereditor.getValue());
  2048 + var langid = $('#htmlbutton').data("languageid");
  2049 + var input = $('#input').val() || '';
  2050 + var compileData = {
  2051 + code: code,
  2052 + langid: langid,
  2053 + input: input
  2054 + }
  2055 + $.ajax({
  2056 + type: "post",
  2057 + url: gxb_api + "/submit/submitCode/api",
  2058 + data: JSON.stringify(compileData),
  2059 + dataType: "json",
  2060 + contentType: "application/json",
  2061 + success: function(data) {
  2062 + compileResult(data)
  2063 + l.stop();
  2064 +
  2065 + $('.compile-loading').hide();
  2066 + $('.compile-reslut').show();
  2067 + GBCodePlayBack.compileResultData = data;
  2068 +
  2069 + },
  2070 + error: function(){
  2071 + l.stop();
  2072 + alert('错误');
  2073 + }
  2074 + });
  2075 + }
  2076 +
1910 // 初始化页面 2077 // 初始化页面
1911 function initRecoding() { 2078 function initRecoding() {
1912 console.log('initRecoding') 2079 console.log('initRecoding')
@@ -2115,7 +2282,7 @@ $(function() { @@ -2115,7 +2282,7 @@ $(function() {
2115 // $('#comprunbtngrp').html('<button id="runnow" type="button" data-style="expand-right" class="btn" title="运行代码">&nbsp; 运行</button>'); 2282 // $('#comprunbtngrp').html('<button id="runnow" type="button" data-style="expand-right" class="btn" title="运行代码">&nbsp; 运行</button>');
2116 } else { 2283 } else {
2117 $('#fontendrun').addClass('hidden'); 2284 $('#fontendrun').addClass('hidden');
2118 - $('#comprunbtngrp').html('<button id="comprun" data-toggle="tooltip" data-placement="bottom" type="button" data-style="expand-right" class="btn btn-default btn-sm" title="编译并运行代码">&nbsp;编译&运行</button>'); 2285 + $('#comprunbtngrp').html('<button id="comprun" data-toggle="tooltip" data-placement="bottom" type="button" data-run="1" data-style="expand-right" class="btn comprun" title="编译并运行代码">&nbsp;编译&运行</button>');
2119 2286
2120 $('#ifrcontainer').addClass('hidden'); 2287 $('#ifrcontainer').addClass('hidden');
2121 $('#cmprun-output').removeClass('hidden'); 2288 $('#cmprun-output').removeClass('hidden');
@@ -2130,57 +2297,58 @@ $(function() { @@ -2130,57 +2297,58 @@ $(function() {
2130 } 2297 }
2131 2298
2132 // 播发代码 2299 // 播发代码
2133 - function playCoder() {  
2134 -  
2135 - $('#recordertab a:eq(1)').tab('show');  
2136 -  
2137 - var playstatus = $('#play').data('status');  
2138 - if (!playstatus) {  
2139 - var playbackrecord = GBCodePlayBack.records;  
2140 - var starttime = playbackrecord[0].intervaltime;  
2141 -  
2142 - for (var i = 0; i < playbackrecord.length; i++) {  
2143 - if (playbackrecord[i].trigger === 0) {  
2144 - var timeoutcontroller = null;  
2145 - var replayinterval = playbackrecord[i].intervaltime,  
2146 - replay_enhancement_interval = GBCodePlayBack.replay_enhancement_value; 2300 + function playCode(){
  2301 + if (GBCodePlayBack.firstPlay == 0) {
  2302 + $('#readonly').trigger('click');
  2303 + GBCodePlayBack.htmlplayereditor.setValue(GBCodePlayBack.pauseValue)
  2304 + }else{
  2305 + GBCodePlayBack.htmlplayereditor.setValue('')
  2306 + var resultData = {
  2307 + output: '',
  2308 + stderr: '',
  2309 + cmpinfo: '',
  2310 + result: 1
  2311 + };
  2312 + compileResult(resultData);
  2313 + }
2147 2314
2148 - if (i === 0) { 2315 + var playbackrecord = GBCodePlayBack.records;
  2316 + var starttime = 0;
  2317 + var playTotalTime = 0;
  2318 +
  2319 + for (var i = 0; i < playbackrecord.length; i++) {
  2320 + if (playbackrecord[i].trigger === 1) {
  2321 + playTotalTime += parseInt(playbackrecord[i].intervaltime);
  2322 + }else if (playbackrecord[i].trigger === 0) {
  2323 + var timeoutcontroller = null;
  2324 + var replayinterval = playbackrecord[i].intervaltime;
  2325 +
  2326 + if (!GBCodePlayBack.isPause) {
  2327 + starttime = replayinterval - (GBCodePlayBack.audioTime - playTotalTime);
  2328 + GBCodePlayBack.isPause = 1
  2329 + }else{
  2330 + if (i == 0) {
2149 starttime = replayinterval; 2331 starttime = replayinterval;
2150 - if (GBCodePlayBack.pauseTime) {  
2151 - if (tag === 1) {  
2152 - GBCodePlayBack.stopIntervalTime = playbackrecord[0].intervaltime - (GBCodePlayBack.pauseTime - GBCodePlayBack.playRecordStart);  
2153 - starttime = GBCodePlayBack.stopIntervalTime;  
2154 - tag++;  
2155 - } else {  
2156 - GBCodePlayBack.stopIntervalTime = GBCodePlayBack.stopIntervalTime - (GBCodePlayBack.pauseTime - GBCodePlayBack.playRecordStart);  
2157 - starttime = GBCodePlayBack.stopIntervalTime;  
2158 - }  
2159 - }  
2160 - } else { 2332 + }else{
2161 starttime = parseInt(starttime) + parseInt(replayinterval); 2333 starttime = parseInt(starttime) + parseInt(replayinterval);
2162 -  
2163 - if (GBCodePlayBack.pauseTime && GBCodePlayBack.pauseTime - playbackrecord[i - 1].playTime > 0) {  
2164 - GBCodePlayBack.stopIntervalTime = GBCodePlayBack.pauseTime - playbackrecord[i - 1].playTime;  
2165 - starttime = GBCodePlayBack.stopIntervalTime + parseInt(replayinterval);  
2166 - }  
2167 } 2334 }
2168 - timeoutcontroller = setTimeout(GBCodePlayBack.changeValueCallback(playbackrecord[i]), starttime);  
2169 - GBCodePlayBack.recordtimeoutcontrolls.push(timeoutcontroller);  
2170 } 2335 }
  2336 + timeoutcontroller = setTimeout(GBCodePlayBack.changeValueCallback(playbackrecord[i]), starttime/GBCodePlayBack.speed);
  2337 + GBCodePlayBack.recordtimeoutcontrolls.push(timeoutcontroller);
2171 } 2338 }
2172 - // 记录播放的时间  
2173 - GBCodePlayBack.playRecordStart = new Date().getTime();  
2174 - } else {  
2175 - var recordtimeoutcontrolls = GBCodePlayBack.recordtimeoutcontrolls; 2339 + }
  2340 + }
2176 2341
2177 - for (var i = 0; i < recordtimeoutcontrolls.length; i++) {  
2178 - clearTimeout(recordtimeoutcontrolls[i]);  
2179 - }  
2180 - GBCodePlayBack.recordtimeoutcontrolls = [];  
2181 - GBCodePlayBack.recordtimeouts = []; 2342 + // 暂停播放代码
  2343 + function pauseCode(){
  2344 + var recordtimeoutcontrolls = GBCodePlayBack.recordtimeoutcontrolls;
  2345 + GBCodePlayBack.isPause = 0;
  2346 +
  2347 + for (var i = 0; i < recordtimeoutcontrolls.length; i++) {
  2348 + clearTimeout(recordtimeoutcontrolls[i]);
2182 } 2349 }
2183 - $('#play').data('status', !playstatus); 2350 + GBCodePlayBack.recordtimeoutcontrolls = [];
  2351 + GBCodePlayBack.recordtimeouts = [];
2184 } 2352 }
2185 2353
2186 function processChangeObject(playbackcm, obj) { 2354 function processChangeObject(playbackcm, obj) {
@@ -2277,6 +2445,45 @@ $(function() { @@ -2277,6 +2445,45 @@ $(function() {
2277 break; 2445 break;
2278 } 2446 }
2279 } 2447 }
  2448 + function selectLang(langid){
  2449 + switch (langid) {
  2450 + case 1:
  2451 + setHtmlButton(langid, "C");
  2452 + break;
  2453 + case 4:
  2454 + setHtmlButton(langid, "Python");
  2455 + break;
  2456 + case 10:
  2457 + setHtmlButton(langid, "JAVA");
  2458 + break;
  2459 + case 11:
  2460 + setHtmlButton(langid, "C");
  2461 + break;
  2462 + case 29:
  2463 + setHtmlButton(langid, "PHP");
  2464 + break;
  2465 + case 35:
  2466 + setHtmlButton(langid, "HTML/CSS/JS");
  2467 + break;
  2468 + case 40:
  2469 + setHtmlButton(langid, "SQL");
  2470 + break;
  2471 + case 42:
  2472 + setHtmlButton(langid, "C");
  2473 + break;
  2474 + case 43:
  2475 + setHtmlButton(langid, "Objective-C");
  2476 + break;
  2477 + case 82:
  2478 + setHtmlButton(langid, "C")++;
  2479 + break;
  2480 + case 117:
  2481 + setHtmlButton(langid, "R");
  2482 + break;
  2483 + default:
  2484 + break;
  2485 + }
  2486 + }
2280 2487
2281 // 缓存languageid title 2488 // 缓存languageid title
2282 function setHtmlButton(languageid, title) { 2489 function setHtmlButton(languageid, title) {