/* * Copyright (c) 2014-2019, b3log.org & hacpai.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * @file wide.js * * @author Liyuan Li * @author Liang Ding * @version 1.0.0.1, Dec 8, 2015 */ var wide = { curNode: undefined, curEditor: undefined, curProcessId: undefined, // curent running process id (pid) refreshOutline: function () { if (!wide.curEditor || (wide.curEditor && wide.curEditor.doc.getMode().name !== "go")) { $("#outline").html(''); return false; } var request = newWideRequest(); request.code = wide.curEditor.getValue(); $.ajax({ type: 'POST', async: false, url: config.context + '/outline', data: JSON.stringify(request), dataType: "json", success: function (result) { if (!result.succ) { return; } var data = result.data; var outlineHTML = '
' + data.output + ''); } else { bottomGroup.fillOutput(content.replace(/<\/pre>$/g, data.output + '')); } wide.curProcessId = data.pid; break; case 'run-done': bottomGroup.fillOutput($('.bottom-window-group .output > div').html().replace(/<\/pre>$/g, data.output + '')); wide.curProcessId = undefined; $("#buildRun").removeClass("ico-stop") .addClass("ico-buildrun").attr("title", config.label.build_n_run); break; case 'start-build': case 'start-test': case 'start-vet': case 'start-install': case 'start-get': bottomGroup.fillOutput(data.output); break; case 'go test': case 'go vet': case 'go install': case 'go get': bottomGroup.fillOutput($('.bottom-window-group .output > div').html() + data.output); break; case 'git clone': bottomGroup.fillOutput($('.bottom-window-group .output > div').html() + data.output); tree.fileTree.reAsyncChildNodes(wide.curNode, "refresh", false); break; case 'build': case 'cross-build': bottomGroup.fillOutput($('.bottom-window-group .output > div').html() + data.output); if (data.lints) { // has build error var files = {}; for (var i = 0; i < data.lints.length; i++) { var lint = data.lints[i]; goLintFound.push({from: CodeMirror.Pos(lint.lineNo, 0), to: CodeMirror.Pos(lint.lineNo, 0), message: lint.msg, severity: lint.severity}); files[lint.file] = lint.file; } $("#buildRun").removeClass("ico-stop") .addClass("ico-buildrun").attr("title", config.label.build_n_run); // trigger gutter lint for (var path in files) { var editor = editors.getEditorByPath(path); CodeMirror.signal(editor, "change", editor); } } else { if ('cross-build' === data.cmd) { var request = newWideRequest(), path = null; request.path = data.executable; request.name = data.name; $.ajax({ async: false, type: 'POST', url: config.context + '/file/zip/new', data: JSON.stringify(request), dataType: "json", success: function (result) { if (!result.succ) { $("#dialogAlert").dialog("open", result.msg); return false; } path = result.data; } }); if (path) { window.open(config.context + '/file/zip?path=' + path + ".zip"); } } } break; } }; outputWS.onclose = function (e) { console.log('[output onclose] disconnected (' + e.code + ')'); }; outputWS.onerror = function (e) { console.log('[output onerror]'); }; }, _initFooter: function () { $(".footer .cursor").dblclick(function () { $("#dialogGoLinePrompt").dialog("open"); }); }, init: function () { this._initFooter(); this._initWS(); // 点击隐藏弹出层 $("body").bind("mouseup", function (event) { // MAC 右键文件树失效 if (event.which === 3) { return false; } $(".frame").hide(); if (!($(event.target).closest(".frame").length === 1 || event.target.className === "frame")) { $(".menu > ul > li").unbind().removeClass("selected"); menu.subMenu(); } }); // 刷新提示 window.onbeforeunload = function () { if (editors.data.length > 0) { return config.label.confirm_save; } }; // 禁止鼠标右键菜单 document.oncontextmenu = function () { return false; }; this._initDialog(); }, _save: function (path, editor) { if (!path) { return false; } var request = newWideRequest(); request.file = path; request.code = editor.getValue(); $.ajax({ type: 'POST', url: config.context + '/file/save', data: JSON.stringify(request), dataType: "json", success: function (result) { // reset the save state editor.doc.markClean(); $(".edit-panel .tabs > div").each(function () { var $span = $(this).find("span:eq(0)"); if ($span.attr("title") === path) { $span.removeClass("changed"); } }); } }); }, saveFile: function () { var path = editors.getCurrentPath(); if (!path) { return false; } var editor = wide.curEditor; if (editor.doc.isClean()) { // no modification return false; } if ("text/x-go" === editor.getOption("mode")) { wide.gofmt(path, wide.curEditor); // go fmt will save // build the file at once var request = newWideRequest(); request.file = path; request.code = editor.getValue(); request.nextCmd = ""; // build only, no following operation $.ajax({ type: 'POST', url: config.context + '/build', data: JSON.stringify(request), dataType: "json", beforeSend: function () { bottomGroup.resetOutput(); }, success: function (result) { } }); // refresh outline wide.refreshOutline(); return; } wide._save(path, wide.curEditor); }, stop: function () { if ($("#buildRun").hasClass("ico-buildrun")) { menu.run(); return false; } if (!wide.curProcessId) { return false; } var request = newWideRequest(); request.pid = wide.curProcessId; $.ajax({ type: 'POST', url: config.context + '/stop', data: JSON.stringify(request), dataType: "json", success: function (result) { $("#buildRun").removeClass("ico-stop") .addClass("ico-buildrun").attr("title", config.label.build_n_run); } }); }, gofmt: function (path, editor) { var cursor = editor.getCursor(); var scrollInfo = editor.getScrollInfo(); var request = newWideRequest(); request.file = path; request.code = editor.getValue(); request.cursorLine = cursor.line; request.cursorCh = cursor.ch; $.ajax({ async: false, // sync type: 'POST', url: config.context + '/go/fmt', data: JSON.stringify(request), dataType: "json", success: function (result) { if (result.succ) { editor.setValue(result.data.code); editor.setCursor(cursor); editor.scrollTo(null, scrollInfo.top); wide._save(path, editor); } } }); }, fmt: function (path, editor) { var mode = editor.getOption("mode"); var cursor = editor.getCursor(); var scrollInfo = editor.getScrollInfo(); var request = newWideRequest(); request.file = path; request.code = editor.getValue(); request.cursorLine = cursor.line; request.cursorCh = cursor.ch; var formatted = null; switch (mode) { case "text/x-go": $.ajax({ async: false, // sync type: 'POST', url: config.context + '/go/fmt', data: JSON.stringify(request), dataType: "json", success: function (result) { if (result.succ) { formatted = result.data.code; } } }); break; case "text/html": formatted = html_beautify(editor.getValue()); break; case "text/javascript": case "application/json": formatted = js_beautify(editor.getValue()); break; case "text/css": formatted = css_beautify(editor.getValue()); break; default : break; } if (formatted) { editor.setValue(formatted); editor.setCursor(cursor); editor.scrollTo(null, scrollInfo.top); wide._save(path, editor); } }, getClassBySuffix: function (suffix) { var iconSkin = "ico-ztree-other "; switch (suffix) { case "html": case "htm": iconSkin = "ico-ztree-html "; break; case "go": iconSkin = "ico-ztree-go "; break; case "css": iconSkin = "ico-ztree-css "; break; case "txt": iconSkin = "ico-ztree-text "; break; case "sql": iconSkin = "ico-ztree-sql "; break; case "properties": iconSkin = "ico-ztree-pro "; break; case "md": iconSkin = "ico-ztree-md "; break; case "js", "json": iconSkin = "ico-ztree-js "; break; case "xml": iconSkin = "ico-ztree-xml "; break; case "jpg": case "jpeg": case "bmp": case "gif": case "png": case "svg": case "ico": iconSkin = "ico-ztree-img "; break; } return iconSkin; } }; $(document).ready(function () { wide.init(); tree.init(); menu.init(); hotkeys.init(); session.init(); notification.init(); editors.init(); windows.init(); bottomGroup.init(); });