wide/static/js/wide.js

150 lines
4.0 KiB
JavaScript
Raw Normal View History

2014-08-22 10:06:47 +04:00
var outputWS = new WebSocket(config.channel.output + '/output/ws');
2014-08-18 17:45:43 +04:00
outputWS.onopen = function() {
console.log('[output onopen] connected');
};
2014-08-22 11:37:26 +04:00
2014-08-18 17:45:43 +04:00
outputWS.onmessage = function(e) {
console.log('[output onmessage]' + e.data);
var data = JSON.parse(e.data);
2014-08-29 13:24:08 +04:00
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-08-29 13:24:08 +04:00
} else if ('build' === data.cmd) {
2014-09-03 13:23:42 +04:00
$('#output').text(data.output);
2014-08-29 13:24:08 +04:00
if (0 !== data.output.length) { // 说明编译有错误输出
return;
}
2014-09-05 09:22:25 +04:00
} else if ('go get' === 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 = {
"executable": data.executable
};
$.ajax({
type: 'POST',
url: '/run',
data: JSON.stringify(request),
dataType: "json",
beforeSend: function(data) {
2014-09-03 13:23:42 +04:00
$('#output').text('');
2014-08-29 13:24:08 +04:00
},
success: function(data) {
}
});
}
2014-08-22 11:37:26 +04:00
}
2014-08-18 17:45:43 +04:00
};
outputWS.onclose = function(e) {
console.log('[output onclose] disconnected (' + e.code + ')');
delete outputWS;
};
outputWS.onerror = function(e) {
console.log('[output onerror] ' + e);
};
var wide = {
2014-08-29 13:24:08 +04:00
curNode: undefined,
curEditor: undefined,
2014-09-05 10:33:43 +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-08-18 17:45:43 +04:00
init: function() {
2014-09-02 14:09:01 +04:00
this._initLayout();
2014-08-18 17:45:43 +04:00
$("body").bind("mousedown", function(event) {
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();
}
});
},
save: function() {
var request = {
2014-08-19 13:07:35 +04:00
"file": wide.curNode.path,
"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",
success: function(data) {
console.log(data);
}
});
},
run: function() {
var request = {
2014-08-19 13:07:35 +04:00
"file": wide.curNode.path,
"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",
beforeSend: function(data) {
2014-09-03 13:23:42 +04:00
$('#output').text('');
2014-08-18 17:45:43 +04:00
},
2014-09-05 10:33:43 +04:00
success: function(data) {
2014-09-05 08:18:50 +04:00
}
});
},
2014-09-05 10:33:43 +04:00
goget: function() {
2014-09-05 08:18:50 +04:00
var request = {
"file": wide.curNode.path
};
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",
beforeSend: function(data) {
$('#output').text('');
},
success: function(data) {
2014-08-18 17:45:43 +04:00
}
});
},
fmt: function() {
var request = {
2014-08-19 13:07:35 +04:00
"file": wide.curNode.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-06 20:13:25 +04:00
// TODO: HTML/XML/JSON 格式化处理
2014-08-18 17:45:43 +04:00
$.ajax({
type: 'POST',
2014-09-06 20:13:25 +04:00
url: '/go/fmt',
2014-08-18 17:45:43 +04:00
data: JSON.stringify(request),
dataType: "json",
success: function(data) {
if (data.succ) {
2014-08-19 13:07:35 +04:00
wide.curEditor.setValue(data.code);
2014-08-18 17:45:43 +04:00
}
}
});
}
};
$(document).ready(function() {
wide.init();
tree.init();
});