wide/static/js/session.js

101 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-09-23 17:03:44 +04:00
var session = {
init: function () {
this._initWS();
// 定时30 秒)保存会话内容.
setInterval(function () {
var request = newWideRequest(),
filse = [],
fileTree = [],
2014-10-17 10:47:29 +04:00
currentId = editors.getCurrentId(),
currentFile = currentId ? editors.getCurrentPath() : "";
2014-09-23 17:03:44 +04:00
editors.tabs.obj._$tabs.find("div").each(function () {
var $it = $(this);
2014-10-21 18:48:33 +04:00
if ($it.find("span:eq(0)").attr("title") !== config.label.start_page) {
2014-10-17 10:47:29 +04:00
filse.push($it.find("span:eq(0)").attr("title"));
2014-09-23 17:03:44 +04:00
}
});
2014-09-23 18:29:53 +04:00
2014-09-23 17:03:44 +04:00
fileTree = tree.getOpenPaths();
request.currentFile = currentFile; // 当前编辑器
request.fileTree = fileTree; // 文件树展开状态
request.files = filse; // 编辑器打开状态
$.ajax({
type: 'POST',
url: '/session/save',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
}
});
2014-09-24 10:36:34 +04:00
}, 30000);
2014-09-23 17:03:44 +04:00
},
2014-09-23 18:29:53 +04:00
restore: function () {
2014-09-24 07:58:09 +04:00
var fileTree = config.latestSessionContent.FileTree,
files = config.latestSessionContent.Files,
currentFile = config.latestSessionContent.CurrentFile,
2014-09-23 18:29:53 +04:00
id = "",
nodesToOpen = [];
var nodes = tree.fileTree.transformToArray(tree.fileTree.getNodes());
for (var i = 0, ii = nodes.length; i < ii; i++) {
// expand tree
for (var j = 0, jj = fileTree.length; j < jj; j++) {
if (nodes[i].path === fileTree[j]) {
tree.fileTree.expandNode(nodes[i], true, false, false);
break;
}
}
// open editors
for (var k = 0, kk = files.length; k < kk; k++) {
if (nodes[i].path === files[k]) {
nodesToOpen.push(nodes[i]);
break;
}
}
if (nodes[i].path === currentFile) {
id = nodes[i].tId;
}
}
// 处理编辑器打开顺序
for (var m = 0, mm = files.length; m < mm; m++) {
for (var n = 0, nn = nodesToOpen.length; n < nn; n++) {
if (nodesToOpen[n].path === files[m]) {
tree.openFile(nodesToOpen[n]);
2014-09-23 18:29:53 +04:00
break;
}
}
}
editors.tabs.setCurrent(id);
},
2014-09-23 17:03:44 +04:00
_initWS: function () {
// 用于保持会话,如果该通道断开,则服务器端会销毁会话状态,回收相关资源.
2014-10-23 09:07:48 +04:00
var sessionWS = new ReconnectingWebSocket(config.channel.session + '/session/ws?sid=' + config.wideSessionId);
2014-09-23 17:03:44 +04:00
sessionWS.onopen = function () {
console.log('[session onopen] connected');
};
sessionWS.onmessage = function (e) {
console.log('[session onmessage]' + e.data);
var data = JSON.parse(e.data);
};
sessionWS.onclose = function (e) {
console.log('[session onclose] disconnected (' + e.code + ')');
delete sessionWS;
};
sessionWS.onerror = function (e) {
console.log('[session onerror] ' + JSON.parse(e));
};
}
};