";
}
ret += divHtml;
}
if (data.subFiles.length == 0) {
divHtml = "
"
+ "
当前目录为空"
+ "
";
ret += divHtml;
}
ret += "
";
return ret;
};
// Tab related handlers============
var onListFile = function (obj) {
var data = JSON.parse(obj.data);
data.name = "...";
data.changed = false;
// .replace(/.*\//g,"");
data.isPrivate = obj.isPrivate;
mainVue.openedFiles.push(data);
setTimeout(function () {
clickTab(data.path);
}, 100);
};
var onStaticVerify = function (obj) {
var data = {};
data.name = data.path = "/tmp/result_" + (new Date().getTime() % 1000)
+ ".txt";
obj = JSON.parse(obj.data);
obj.result = JSON.parse(obj.result);
data.val = JSON.stringify(obj, null, 4);
mainVue.openedFiles.push(data);
setTimeout(function () {
clickTab(data.path);
}, 100);
}
var clickProjectDir = function () {
};
var clickProjectFile = function (tar) {
var pingObj = {};
pingObj.action = "listFile";
pingObj.path = tar;
pingObj.isPrivate = $("#privateprojecttab").hasClass("active");
for (var i = 0; i < mainVue.openedFiles.length; i++) {
var file = mainVue.openedFiles[i];
if (file.path == tar) {
clickTab(tar);
return;
}
}
global.filewssocket.send(JSON.stringify(pingObj));
};
var formatScriptEditor = function () {
var from = global.scriptEditor.getCursor(true);
var to = global.scriptEditor.getCursor(false);
if (from == to) {
from = {
line: 0,
ch: 0
};
to = {
line: global.scriptEditor.lastLine() + 1,
ch: 1
};
}
global.scriptEditor.autoFormatRange(from, to);
}
var adjustFileName = function (title) {
if (mainVue.openedFiles.length < 2)
return;
var totalLen = 90;
var average = (totalLen - title.length - 6)
/ (mainVue.openedFiles.length - 1);
average -= 4;
for (var i = 0; i < mainVue.openedFiles.length; i++) {
var file = mainVue.openedFiles[i];
if (file.path == title)
file.name = file.path;
else {
if (file.path.length > average + 5)
file.name = file.path.substr(0, average) + " ...";
else
file.name = file.path;
}
if (file.changed) {
file.name = "* " + file.name;
}
}
};
var clickTab = function (title) {
if (title.getAttribute != undefined) {
title = title.getAttribute("path");
}
// console.log("clickTab:" + title);
if (global.currentFile != undefined && global.currentFile.val != undefined) {
var val = global.scriptEditor.getValue();
if (global.currentFile.val != val) {
global.currentFile.changed = true;
}
global.currentFile.val = val;
}
for (var i = 0; i < mainVue.openedFiles.length; i++) {
var file = mainVue.openedFiles[i];
if (file.path == title) {
if (file.val != undefined) {
global.scriptEditor.setOption("mode", "application/javascript");
if (file.path.endsWith(".css")) {
global.scriptEditor.setOption("mode", "text/css");
}
if (file.path.endsWith(".html")) {
global.scriptEditor.setOption("mode", "text/html");
}
if (file.path.endsWith(".js")) {
global.scriptEditor.setOption("mode",
"application/javascript");
}
global.scriptEditor.setValue(file.val);
} else {
global.scriptEditor.setValue("not text file!");
}
$("#myFileTab a").removeClass("active");
$("#myFileTab a")[i].classList.add("active");
global.currentFile = file;
adjustFileName(title);
return;
}
}
// it is Hello
if (title == "Hello") {
$("#myFileTab a")[0].classList.add("active");
global.scriptEditor.setValue("Hello Page");
adjustFileName(title);
}
};
var closeTab = function (ele) {
var path = ele.getAttribute("path");
var newList = [];
var pos = 0;
for (var i = 0; i < mainVue.openedFiles.length; i++) {
var file = mainVue.openedFiles[i];
if (file.path == global.currentFile.path) {
pos = i;
}
if (file.path == path) {
continue;
}
newList.push(file);
}
mainVue.openedFiles = newList;
if (pos < mainVue.openedFiles.length) {
clickTab(mainVue.openedFiles[pos].path);
} else {
pos--;
if (pos >= 0)
clickTab(mainVue.openedFiles[pos].path);
}
// console.log("closeTab:" + path);
};
// Tab related handlers============
var getProjectTreeHtml = function (data, clz, preFix) {
if (data == undefined || data.subFiles == undefined)
return "";
var ret = "
";
for (var i = 0; i < data.subFiles.length; i++) {
var obj = data.subFiles[i];
var clickfun = "";
if (obj.subFiles == undefined) {
clickfun = "onclick = 'clickProjectFile(\"" + preFix + obj.name
+ "\")'" + " class='projectFileDiv' ";
} else {
clickfun = "onclick ='clickProjectDir(\"" + preFix + obj.name
+ "\")'" + " class='projectFileDiv'";
}
ret += "
" + obj.name + "
";
if (obj.subFiles != undefined)
ret += getProjectTreeHtml(obj, "subFileTreeUl", preFix + obj.name
+ "/");
ret += "
"
}
return ret + "
";
};
// context menu callbacks=======================
var showRename = function (projectName) {
console.log("showRename");
var body = "
";
body += "
";
body += " 输入新名称";
body += "
";
$("#dialogBodyDiv").html(body);
showDialog("重命名项目", function () {
var pingObj = {};
var newVal = $("#dialogInput")[0].value;
if (newVal == undefined || newVal.length == 0 || newVal.indexOf("..") != -1 || newVal.indexOf("\\") != -1) {
setTimeout(function () {
myToast("重命名失败", "新文件名不符合规范");
}, 1000);
return;
}
pingObj.newFile = newVal;
pingObj.action = "renameFile";
pingObj.oldFile = projectName;
pingObj.isPrivate = $("#privateprojecttab").hasClass("active");
global.filewssocket.send(JSON.stringify(pingObj));
});
};
var showDelete = function (projectName) {
var body = "
";
body += " 请确认是否删除:" + projectName
+ "";
body += "
";
$("#dialogBodyDiv").html(body);
showDialog("删除项目", function () {
global.projectName = projectName;
deleteFile();
});
};
var showCreateFile = function (projectName, isDir) {
var body = "
";
body += "
";
body += " 创建文件" + (isDir ? "夹" : "")
+ "";
body += "
";
$("#dialogBodyDiv").html(body);
showDialog("创建新文件", function () {
var fileName = $("#newProjectNameInput")[0].value;
if (fileName == undefined || fileName.length == 0) {
setTimeout(function () {
myToast("新建文件失败", "请输入文件名");
}, 1000);
return;
}
// console.log("[sendCreateProject] projectName:" + projectName
// + " fileName:" + fileName);
var arg = {};
var privateTab = $("#privateprojecttab").hasClass("active");
if (privateTab)
arg.isPrivate = true;
else
arg.isPrivate = false;
arg.dir = projectName;
arg.action = "createFile";
arg.isFolder = isDir;
arg.name = fileName;
console.log(arg);
global.filewssocket.send(JSON.stringify(arg));
});
}
var deleteFile = function () {
var pingObj = {};
pingObj.file = global.projectName;
pingObj.action = "deleteFile";
pingObj.isPrivate = $("#privateprojecttab").hasClass("active");
global.filewssocket.send(JSON.stringify(pingObj));
};
var saveFile = function () {
var pingObj = {};
pingObj.isAppend = false;
pingObj.isPrivate = global.currentFile.isPrivate;
pingObj.path = global.currentFile.path;
pingObj.action = "saveFile";
pingObj.content = global.scriptEditor.getValue();
if (pingObj.path == "Hello") {
myToast("保存出错", "不能保存Hello");
return;
}
global.filewssocket.send(JSON.stringify(pingObj));
global.currentFile.changed = false;
adjustFileName(global.currentFile.path);
};
var sliceFile = function (file) {
let piece = 1024 * 50;
let totalSize = file.size; // 文件总大小
let start = 0; // 每次上传的开始字节
let end = start + piece; // 每次上传的结尾字节
let chunks = []
while (start < totalSize) {
// 根据长度截取每次需要上传的数据
// File对象继承自Blob对象,因此包含slice方法
let blob = file.slice(start, end);
chunks.push(blob)
start = end;
end = start + piece;
}
return chunks;
};
var uploadInternal = function () {
console.log("upload:" + global.uploadOrder + " total:" + global.chunks.length);
var isAppend = true;
if (global.uploadOrder == 0)
isAppend = false;
if (global.uploadOrder >= global.chunks.length) {
progressTextDiv.innerText = global.uploadFiles[global.fileOrder - 1].name + "上传已完成";
progressDiv.style.width = "100%";
console.log("setTimeout uploadSingleFile");
setTimeout(uploadSingleFile, 500);
return;
}
var num = global.uploadOrder * 100 / global.chunks.length;
progressDiv.style.width = num.toFixed(2) + "%";
var chunk = global.chunks[global.uploadOrder];
let fd = new FormData();
fd.append("file", chunk);
var isPrivate = $("#privateprojecttab").hasClass("active");
var arg = "path=" + global.lastClickProjectName;
arg += "&fileName=" + global.uploadFileName;
arg += "&isPrivate=" + isPrivate;
arg += "&order=" + global.uploadOrder;
arg += "&count=" + global.chunks.length;
arg += "&pubKey=" + global.sm2Key.publicKey;
console.log(arg);
var sign = sm2.doSignature(arg, global.sm2Key.privateKey, {hash: true, der: true});
console.log(sign)
arg += "&sign=" + sign;
const url = `${global.urlparam && global.urlparam.nodeAddr ?
(location.href.startsWith('https') ? 'https://' : 'http://') + global.urlparam.nodeAddr :
'.'}/Upload?${arg}`;
$.ajax({
url: url,
type: 'POST',
data: fd,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (data) {
uploadInternal();
},
error: function (data) {
console.log("[uploadFile] error!");
alert("Upload failed!");
}
});
global.uploadOrder++;
return;
var pingObj = {};
pingObj.isAppend = false;
pingObj.isPrivate = $("#privateprojecttab").hasClass("active");
pingObj.path = global.lastClickProjectName;
pingObj.fileName = global.uploadFileName;
pingObj.action = "uploadFile";
var text = btoa(new Uint8Array(chunk.arrayBuffer));
pingObj.content = text;
global.filewssocket.send(JSON.stringify(pingObj));
}
var uploadFile = function (obj) {
console.log("uploadFile : ");
global.uploadFiles = $("#uploadFileInput")[0].files;
global.fileOrder = 0;
var body = "
";
body += " ";
body += "
";
body += "
";
body += "
";
$("#dialogBodyDiv").html(body);
uploadSingleFile();
showDialog("正在上传文件", function () {
cancelUpload();
}, function () {
cancelUpload();
});
};
var uploadSingleFile = function () {
console.log("uploadSingleFile");
console.log(global.uploadFiles[global.fileOrder]);
var file = global.uploadFiles[global.fileOrder];
global.fileOrder++;
if (file == undefined) {
progressTextDiv.innerHTML = global.uploadFiles.length + "个文件全部上传完毕";
return;
} else {
progressTextDiv.innerHTML = "上传第("
+ (global.fileOrder) + "/" + global.uploadFiles.length + ")个文件:" + file.name;
}
global.uploadFile = file;
console.log(file);
global.chunks = sliceFile(file);
global.uploadOrder = 0;
global.uploadFileName = file.name;
uploadInternal();
};
var cancelUpload = function () {
global.uploadOrder = global.chunks.length + 1;
global.fileOrder = global.uploadFiles.length + 1;
}
var downloadContract = function (projectName) {
// console.log(window.location.href);
let url;
if (global.urlparam) {
if (location.href.startsWith("https")) {
url = "https://";
} else {
url = "http://";
}
url += `${global.urlparam.nodeAddr}/SCIDE/CMManager?`;
} else {
if (window.location.href.indexOf("/SCIDE") != -1)
url = window.location.href.replace("/OnlineIDE.html", "/CMManager?");
else
url = window.location.href.replace("/OnlineIDE.html",
"/SCIDE/CMManager?");
}
var arg = "action=downloadContract&projectName="
arg += projectName;
var isPrivate = $("#privateprojecttab").hasClass("active");
arg += "&isPrivate=" + isPrivate;
arg += "&pubKey=" + global.sm2Key.publicKey;
arg += "×tamp=" + new Date().getTime();
var sign = sm2.doSignature(arg, global.sm2Key.privateKey, {hash: true, der: true});
arg += "&sign=" + sign;
console.log(url + arg);
window.open(url + arg);
};
var staticVerifyNaive = function () {
var arg = {};
arg.action = "staticVerifyContract";
arg.contractid = "-";
arg.script = "-";
if (global.currentFile == undefined) {
myToast("提示", "请打开一个文件以静态分析该项目");
return;
}
var p = global.currentFile.path;
p = p.substr(0, p.indexOf("/", 1) + 1);
arg.path = p;
var privateTab = $("#privateprojecttab").hasClass("active");
if (privateTab)
arg.isPrivate = true;
else
arg.isPrivate = false;
global.wssocket.send(JSON.stringify(arg));
};
var compile = function () {
var arg = {};
arg.action = "compile";
if (global.projects[global.lastClickedProjectId] == undefined) {
myToast("提示", "请选择一个合约项目进行编译");
return;
}
arg.path = global.projects[global.lastClickedProjectId];
var privateTab = $("#privateprojecttab").hasClass("active");
if (privateTab)
arg.isPrivate = true;
else
arg.isPrivate = false;
global.wssocket.send(JSON.stringify(arg));
};
var registerDOI = function () {
var arg = {};
arg.action = "registerDOI";
if (global.projects[global.lastClickedProjectId] == undefined) {
myToast("提示", "请选择一个合约进行DOI注册");
return;
}
arg.path = global.projects[global.lastClickedProjectId];
var privateTab = $("#privateprojecttab").hasClass("active");
if (privateTab)
arg.isPrivate = true;
else
arg.isPrivate = false;
var body = "
";
body += " 如果已经注册过,再次注册将导致之前的注册被覆盖." + "";
body += "
";
$("#dialogBodyDiv").html(body);
showDialog("请确认是否注册" + arg.path + "的DOI", function () {
global.wssocket.send(JSON.stringify(arg));
});
};
var onCompile = function (obj) {
myToast("提示", global.projects[global.lastClickedProjectId] + "项目编译结果:\n" + obj.result);
};
var onRegisterDOI = function (obj) {
myToast("提示", global.projects[global.lastClickedProjectId] + "项目注册DOI结果:\n" + obj.result);
};