wide/static/js/wide.js

317 lines
9.3 KiB
JavaScript
Raw Normal View History

2014-08-22 10:06:47 +04:00
var outputWS = new WebSocket(config.channel.output + '/output/ws');
2014-09-13 20:07:03 +04:00
outputWS.onopen = function () {
2014-08-18 17:45:43 +04:00
console.log('[output onopen] connected');
};
2014-08-22 11:37:26 +04:00
2014-09-13 20:07:03 +04:00
outputWS.onmessage = function (e) {
2014-08-18 17:45:43 +04:00
console.log('[output onmessage]' + e.data);
var data = JSON.parse(e.data);
2014-08-29 13:24:08 +04:00
2014-09-09 11:33:12 +04:00
if (goLintFound) {
goLintFound = [];
}
2014-08-22 11:37:26 +04:00
if ('run' === data.cmd) {
2014-09-03 13:23:42 +04:00
$('#output').text($('#output').text() + data.output);
2014-09-09 13:01:22 +04:00
} else if ('build' === data.cmd || 'go install' === data.cmd) {
2014-09-03 13:23:42 +04:00
$('#output').text(data.output);
2014-08-29 13:24:08 +04:00
2014-09-09 12:33:48 +04:00
if (0 !== data.output.length) { // 说明编译有错误输出
2014-09-09 11:33:12 +04:00
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),
2014-09-09 12:33:48 +04:00
message: lint.msg, severity: lint.severity});
2014-09-09 11:33:12 +04:00
}
2014-08-29 13:24:08 +04:00
}
2014-09-09 17:53:53 +04:00
// 触发一次 gutter lint
CodeMirror.signal(wide.curEditor, "change", wide.curEditor);
2014-09-09 13:01:22 +04:00
} else if ('go get' === data.cmd || 'go install' === data.cmd) {
2014-09-05 10:33:43 +04:00
$('#output').text($('#output').text() + data.output);
}
2014-08-29 13:24:08 +04:00
if ('build' === data.cmd) {
if ('run' === data.nextCmd) {
var request = {
2014-09-12 05:32:20 +04:00
executable: data.executable
2014-08-29 13:24:08 +04:00
};
$.ajax({
type: 'POST',
url: '/run',
data: JSON.stringify(request),
dataType: "json",
2014-09-13 20:07:03 +04:00
beforeSend: function (data) {
2014-09-03 13:23:42 +04:00
$('#output').text('');
2014-08-29 13:24:08 +04:00
},
2014-09-13 20:07:03 +04:00
success: function (data) {
2014-08-29 13:24:08 +04:00
}
});
}
2014-08-22 11:37:26 +04:00
}
2014-08-18 17:45:43 +04:00
};
2014-09-13 20:07:03 +04:00
outputWS.onclose = function (e) {
2014-08-18 17:45:43 +04:00
console.log('[output onclose] disconnected (' + e.code + ')');
delete outputWS;
};
2014-09-13 20:07:03 +04:00
outputWS.onerror = function (e) {
2014-08-18 17:45:43 +04:00
console.log('[output onerror] ' + e);
};
var wide = {
2014-08-29 13:24:08 +04:00
curNode: undefined,
curEditor: undefined,
2014-09-13 20:07:03 +04:00
_initLayout: function () {
2014-09-02 18:57:30 +04:00
var mainH = $(window).height() - $(".menu").height() - $(".footer").height() - 2;
2014-09-02 14:09:01 +04:00
$(".content, .ztree").height(mainH);
2014-09-05 10:33:43 +04:00
2014-09-02 14:09:01 +04:00
$(".edit-panel").height(mainH - $(".output").height());
},
2014-09-13 20:07:03 +04:00
init: function () {
2014-09-02 14:09:01 +04:00
this._initLayout();
2014-08-18 17:45:43 +04:00
2014-09-13 20:07:03 +04:00
$("body").bind("mousedown", function (event) {
2014-08-18 17:45:43 +04:00
if (!(event.target.id === "dirRMenu" || $(event.target).closest("#dirRMenu").length > 0)) {
$("#dirRMenu").hide();
}
if (!(event.target.id === "fileRMenu" || $(event.target).closest("#fileRMenu").length > 0)) {
$("#fileRMenu").hide();
}
2014-09-10 14:08:35 +04:00
if (!($(event.target).closest(".frame").length > 0 || event.target.className === "frame")) {
$(".frame").hide();
$(".menu > ul > li > a, .menu > ul> li > span").unbind("mouseover");
menu.subMenu();
}
2014-08-18 17:45:43 +04:00
});
2014-09-13 20:07:03 +04:00
this._bindKey();
2014-08-18 17:45:43 +04:00
},
2014-09-13 20:07:03 +04:00
saveFile: function () {
2014-08-18 17:45:43 +04:00
var request = {
2014-09-12 13:50:46 +04:00
file: $(".edit-header .current span:eq(0)").attr("title"),
2014-09-12 05:32:20 +04:00
code: wide.curEditor.getValue()
2014-08-18 17:45:43 +04:00
};
$.ajax({
type: 'POST',
2014-08-22 06:09:48 +04:00
url: '/file/save',
2014-08-18 17:45:43 +04:00
data: JSON.stringify(request),
dataType: "json",
2014-09-13 20:07:03 +04:00
success: function (data) {
2014-08-18 17:45:43 +04:00
}
});
},
2014-09-13 20:07:03 +04:00
saveAllFiles: function () {
2014-09-14 16:37:48 +04:00
// TODO: save all files
2014-09-10 18:43:34 +04:00
},
2014-09-13 20:07:03 +04:00
closeFile: function () {
2014-09-14 16:37:48 +04:00
// TODO: close file
2014-09-10 18:43:34 +04:00
},
2014-09-13 20:07:03 +04:00
closeAllFiles: function () {
2014-09-14 16:37:48 +04:00
// TODO: close all files
2014-09-10 18:43:34 +04:00
},
2014-09-13 20:07:03 +04:00
exit: function () {
2014-09-10 18:43:34 +04:00
// TODO: exit
},
2014-09-13 20:07:03 +04:00
run: function () {
2014-08-18 17:45:43 +04:00
var request = {
2014-09-12 13:50:46 +04:00
file: $(".edit-header .current span:eq(0)").attr("title"),
2014-09-12 05:32:20 +04:00
code: wide.curEditor.getValue()
2014-08-18 17:45:43 +04:00
};
2014-08-29 13:24:08 +04:00
2014-08-18 17:45:43 +04:00
$.ajax({
type: 'POST',
2014-08-22 07:57:05 +04:00
url: '/build',
2014-08-18 17:45:43 +04:00
data: JSON.stringify(request),
dataType: "json",
2014-09-13 20:07:03 +04:00
beforeSend: function (data) {
2014-09-03 13:23:42 +04:00
$('#output').text('');
2014-08-18 17:45:43 +04:00
},
2014-09-13 20:07:03 +04:00
success: function (data) {
2014-09-05 08:18:50 +04:00
}
});
},
2014-09-13 20:07:03 +04:00
goget: function () {
2014-09-05 08:18:50 +04:00
var request = {
2014-09-12 13:50:46 +04:00
file: $(".edit-header .current span:eq(0)").attr("title")
2014-09-05 08:18:50 +04:00
};
2014-08-29 13:24:08 +04:00
2014-09-05 08:18:50 +04:00
$.ajax({
type: 'POST',
url: '/go/get',
data: JSON.stringify(request),
dataType: "json",
2014-09-13 20:07:03 +04:00
beforeSend: function (data) {
2014-09-09 13:01:22 +04:00
$('#output').text('');
},
2014-09-13 20:07:03 +04:00
success: function (data) {
2014-09-09 13:01:22 +04:00
}
});
},
2014-09-13 20:07:03 +04:00
goinstall: function () {
2014-09-09 13:01:22 +04:00
var request = {
2014-09-12 13:50:46 +04:00
file: $(".edit-header .current span:eq(0)").attr("title"),
2014-09-12 05:32:20 +04:00
code: wide.curEditor.getValue()
2014-09-09 13:01:22 +04:00
};
$.ajax({
type: 'POST',
url: '/go/install',
data: JSON.stringify(request),
dataType: "json",
2014-09-13 20:07:03 +04:00
beforeSend: function (data) {
2014-09-05 08:18:50 +04:00
$('#output').text('');
},
2014-09-13 20:07:03 +04:00
success: function (data) {
2014-08-18 17:45:43 +04:00
}
});
},
2014-09-13 20:07:03 +04:00
fmt: function () {
2014-09-12 13:50:46 +04:00
var path = $(".edit-header .current span:eq(0)").attr("title");
2014-09-07 13:31:57 +04:00
var mode = wide.curNode.mode;
2014-08-18 17:45:43 +04:00
var request = {
2014-09-12 05:32:20 +04:00
file: path,
code: wide.curEditor.getValue(),
cursorLine: wide.curEditor.getCursor().line,
cursorCh: wide.curEditor.getCursor().ch
2014-08-18 17:45:43 +04:00
};
2014-09-07 13:31:57 +04:00
switch (mode) {
case "text/x-go":
$.ajax({
type: 'POST',
url: '/go/fmt',
data: JSON.stringify(request),
dataType: "json",
2014-09-13 20:07:03 +04:00
success: function (data) {
2014-09-07 13:31:57 +04:00
if (data.succ) {
wide.curEditor.setValue(data.code);
}
}
});
break;
case "text/html":
$.ajax({
type: 'POST',
url: '/html/fmt',
data: JSON.stringify(request),
dataType: "json",
2014-09-13 20:07:03 +04:00
success: function (data) {
2014-09-07 13:31:57 +04:00
if (data.succ) {
wide.curEditor.setValue(data.code);
}
}
});
2014-09-07 14:13:55 +04:00
break;
case "application/json":
try {
// 在客户端浏览器中进行 JSON 格式化
var json = JSON.parse(wide.curEditor.getValue());
wide.curEditor.setValue(JSON.stringify(json, "", " "));
2014-09-09 11:33:12 +04:00
2014-09-07 14:13:55 +04:00
this.save();
} catch (e) {
delete e;
}
2014-09-07 13:31:57 +04:00
break;
default :
// TODO: XML/JSON 格式化处理
break;
}
2014-09-13 20:07:03 +04:00
},
_bindKey: function () {
$("#files").keydown(function (event) {
switch (event.which) {
case 13: // 回车
2014-09-14 16:37:48 +04:00
if (!wide.curNode) {
return false;
}
2014-09-13 20:07:03 +04:00
if (wide.curNode.iconSkin === "ico-ztree-dir ") { // 选中节点是目录
// 不做任何处理
return false;
}
// 模拟点击:打开文件
tree._onClick(wide.curNode);
2014-09-07 13:31:57 +04:00
2014-09-13 20:07:03 +04:00
break;
case 38: // 上
2014-09-14 16:37:48 +04:00
if (!wide.curNode) {
return false;
}
2014-09-13 20:07:03 +04:00
tree.fileTree.selectNode(wide.curNode.getPreNode());
wide.curNode = wide.curNode.getPreNode();
$("#files").focus();
break;
case 40: // 下
2014-09-14 16:37:48 +04:00
if (!wide.curNode) {
return false;
}
2014-09-13 20:07:03 +04:00
// TODO: 处理滚动条,递归获取下一个
tree.fileTree.selectNode(wide.curNode.getNextNode());
wide.curNode = wide.curNode.getNextNode();
$("#files").focus();
break;
}
});
$(document).keydown(function (event) {
2014-09-14 16:37:48 +04:00
if (event.ctrlKey && event.which === 49) { // Ctrl+1 焦点切换到文件树
2014-09-13 20:07:03 +04:00
// 有些元素需设置 tabindex 为 -1 时才可以 focus
$("#files").focus();
event.preventDefault();
2014-09-14 16:37:48 +04:00
return;
}
if (event.ctrlKey && event.which === 52) { // Ctrl+4 焦点切换到输出窗口
$("#output").focus();
event.preventDefault();
return;
}
if (event.ctrlKey && event.which === 83) { // Ctrl+S 保存当前编辑器文件
wide.saveFile();
event.preventDefault();
return;
2014-09-13 20:07:03 +04:00
}
2014-09-14 16:43:12 +04:00
if (event.altKey && event.shiftKey && event.which === 70) { // Alt+Shift+F 格式化当前编辑器文件
if (!wide.curNode) {
return false;
}
wide.fmt();
event.preventDefault();
return;
}
2014-09-14 16:56:16 +04:00
if (event.which === 117) { // F6 构建并运行
wide.run();
event.preventDefault();
return;
}
2014-09-13 20:07:03 +04:00
});
2014-08-18 17:45:43 +04:00
}
};
2014-09-13 20:07:03 +04:00
$(document).ready(function () {
2014-08-18 17:45:43 +04:00
wide.init();
tree.init();
2014-09-10 14:08:35 +04:00
menu.init();
2014-08-18 17:45:43 +04:00
});