更新open_tab的代码框架,更容易明白和读懂

This commit is contained in:
wrmswindmill 2018-10-01 15:02:19 +08:00
parent 1484ec68fd
commit 2bccd9060e
2 changed files with 150 additions and 149 deletions

BIN
dump.rdb

Binary file not shown.

View File

@ -973,43 +973,68 @@ function url_deal(path,archor){
} }
} }
// 打开一个已经存在的标签页
// 注意这个还得将对应的路径更改了
function open_tab(path,archor) {
try{
// console.log(window.location.pathname); 端口号后面的路径
path_input=path
path = path_predeal(path)
$('#jstree').jstree("deselect_all", true); function deal_for_tab(project_id,normal_path,path_after){
$('#jstree').jstree("select_node", tree_nodes[path]); // 关闭其他标签页
setTimeout(function timer() {
var offsettop=$("#"+tree_nodes[path]).position().top;
$("#filelist-content").scrollTop(offsettop);
},500)
var tabcontent = document.getElementsByClassName("codereading"); var tabcontent = document.getElementsByClassName("codereading");
for (var i = 0; i < tabcontent.length; i++) { for (var i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none"; tabcontent[i].style.display = "none";
} }
var tab_items = document.getElementsByClassName("tab_item"); var tab_items = document.getElementsByClassName("tab_item");
for (var i = 0; i < tab_items.length; i++) { for (var i = 0; i < tab_items.length; i++) {
// debugger; // debugger;
tab_items[i].className = tab_items[i].className.replace(" active", ""); tab_items[i].className = tab_items[i].className.replace(" active", "");
} }
// 将当前标签页置为活跃
document.getElementById("code_" + path).style.display = "block"; document.getElementById("code_" + path_after).style.display = "block";
document.getElementById("tab_" + path).style.display = "block"; document.getElementById("tab_" + path_after).style.display = "block";
document.getElementById("tab_" + path).className += " active"; document.getElementById("tab_" + path_after).className += " active";
// 如果当前的内容未加载,则加载当前的内容
url_deal(path_input,archor) get_codereading_content(project_id,normal_path,path_after)
document.getElementsByClassName("filename")[0].innerHTML = path_input;
if((path in right_map)){
$("#right_panel").html(right_map[path]);
} }
function deal_for_right(project_id,normal_path,path_after){
if(path_after in right_map){
$("#right_panel").html(right_map[path_after]);
}else{
$.ajax({
cache: false,
type: "POST",
url: '/operations/right/',
data: { 'project_id': project_id, 'path': normal_path},
dataType: 'json',
async: true,
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function (data) {
if (data.status === 'success') {
right_map[path] = data.html_str;
$("#right_panel").html(right_map[path]);
}
else {
right_map[path] = ""
}
$("#right_panel").html(right_map[path_after]);
},error:function(jqXHR, textStatus, errorThrown){
if(jqXHR.readyState===0&&jqXHR.status===0){
alert('网络故障,提交失败!请联网后重新提交');
}
}
});
}
}
// 打开一个已经存在的标签页
// 注意这个还得将对应的路径更改了
function open_tab(normal_path,archor,project_id) {
try{
highlight_jstree_node(project_id,normal_path);
// console.log(window.location.pathname); 端口号后面的路径
var path_after = path_predeal(normal_path)
deal_for_tab(project_id,normal_path,path_after)
url_deal(normal_path,archor)
document.getElementsByClassName("filename")[0].innerHTML = normal_path;
deal_for_right(project_id,normal_path,path_after)
// if(!issue_map.has(path)){ // if(!issue_map.has(path)){
// window.setTimeout(function () { $("#hotest_issue").html(issue_map[path]) }, 3000); // window.setTimeout(function () { $("#hotest_issue").html(issue_map[path]) }, 3000);
// }else{ // }else{
@ -1143,10 +1168,66 @@ function get_path_link_and_archor(path){
return [path,null] return [path,null]
} }
function change_jstree_position(path_after){
$('#jstree').jstree("deselect_all", true);
$('#jstree').jstree("select_node", tree_nodes[path_after]);
setTimeout(function timer() {
var offsettop=$("#"+tree_nodes[path_after]).position().top;
$("#filelist-content").scrollTop(offsettop);
},500)
// var offsettop=$("#"+tree_nodes[path_after]).position().top;
// $("#filelist-content").scrollTop(offsettop);
}
function get_codereading_content(project_id,normal_path,path_after){
div_codereading = document.getElementById("code_"+path_after)
if(div_codereading.innerHTML==""){
$.ajax({
cache: false,
type: "POST",
url: '/operations/get_codereading_content/',
data: { 'project_id': project_id, 'path': normal_path },
dataType: 'json',
async: true,
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function (data) {
if (data.status === 'success') {
// 获取code-reading的内容并填充到对应的code-reading的Element
if(data.is_dir=="0"){
var content = generate_codereading_content(data.lines,data.self_annos,data.fileid,data.annos_count,data.issues_count,data.questions_count,data.issues,data.language)
div_codereading.innerHTML = content;
hljs.configure({useBR: false});
// hljs.highlightBlock(div_codereading)
$('#code_'+path_after+' pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
// .each(function(i, block) {
// hljs.highlightBlock(block);
// });
}else{
var content = data.html_str
div_codereading.innerHTML = content;
}
}
else {
div_codereading.innerHTML = "";
}
},error:function(jqXHR, textStatus, errorThrown){
if(jqXHR.readyState===0&&jqXHR.status===0){
alert('网络故障,提交失败!请联网后重新提交');
}
}
});
}
}
function add_tab(project_id,path,filename) { function add_tab(project_id,path,filename) {
// var tree_node = $("#jstree").jstree("get_selected"); // var tree_node = $("#jstree").jstree("get_selected");
var path_before = path
var returnValue = get_path_link_and_archor(path) var returnValue = get_path_link_and_archor(path)
// 正常应该得到的path为normal path // 正常应该得到的path为normal path
@ -1155,7 +1236,7 @@ function add_tab(project_id,path,filename) {
path = path_predeal(normal_path) path = path_predeal(normal_path)
if (tabSet.has(path)) { if (tabSet.has(path)) {
open_tab(normal_path,archor) open_tab(normal_path,archor,project_id)
} else { } else {
var tab_tag = document.getElementsByClassName("tab_head")[0]; var tab_tag = document.getElementsByClassName("tab_head")[0];
//添加tab //添加tab
@ -1178,7 +1259,7 @@ function add_tab(project_id,path,filename) {
tag_a1.textContent = filename tag_a1.textContent = filename
tag_a1.onclick = (function () { tag_a1.onclick = (function () {
return function () { return function () {
open_tab(normal_path,null); open_tab(normal_path,null,project_id);
} }
})(); })();
@ -1205,84 +1286,7 @@ function add_tab(project_id,path,filename) {
// div_codereading.style.overflowY="auto"; // div_codereading.style.overflowY="auto";
div_code.appendChild(div_codereading) div_code.appendChild(div_codereading)
$.ajax({ open_tab(normal_path,archor,project_id);
cache: false,
type: "POST",
url: '/operations/right/',
data: { 'project_id': project_id, 'path': normal_path},
dataType: 'json',
async: true,
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function (data) {
if (data.status === 'success') {
right_map[path] = data.html_str;
$("#right_panel").html(right_map[path]);
}
else {
right_map[path] = ""
}
},error:function(jqXHR, textStatus, errorThrown){
if(jqXHR.readyState===0&&jqXHR.status===0){
alert('网络故障,提交失败!请联网后重新提交');
}
}
});
$.ajax({
cache: false,
type: "POST",
url: '/operations/get_codereading_content/',
data: { 'project_id': project_id, 'path': normal_path },
dataType: 'json',
async: true,
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function (data) {
if (data.status === 'success') {
// 获取code-reading的内容并填充到对应的code-reading的Element
if(data.is_dir=="0"){
var content = generate_codereading_content(data.lines,data.self_annos,data.fileid,data.annos_count,data.issues_count,data.questions_count,data.issues,data.language)
div_codereading.innerHTML = content;
open_tab(normal_path,archor);
hljs.configure({useBR: false});
// hljs.highlightBlock(div_codereading)
$('#code_'+path+' pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
// .each(function(i, block) {
// hljs.highlightBlock(block);
// });
}else{
var content = data.html_str
div_codereading.innerHTML = content;
open_tab(normal_path,archor);
}
// 如果包含#
// tmp_index = path_before.indexOf("#");
// if(tmp_index != -1){
// var hash = path_before.substring(tmp_index)
// }
// console.log(hash)
// location.hash=hash
}
else {
div_codereading.innerHTML = "";
}
},error:function(jqXHR, textStatus, errorThrown){
if(jqXHR.readyState===0&&jqXHR.status===0){
alert('网络故障,提交失败!请联网后重新提交');
}
}
});
open_tab(normal_path,archor);
// 现在Hotest Question 模块先不用了 // 现在Hotest Question 模块先不用了
// // 填充hotest_question // // 填充hotest_question
@ -1306,19 +1310,20 @@ function add_tab(project_id,path,filename) {
// } // }
// }); // });
highlight_jstree_node(project_id,normal_path);
tabSet.add(path); tabSet.add(path);
} }
} }
function highlight_jstree_node(project_id,path_after){ function highlight_jstree_node(project_id,normal_path){
path = path_predeal(path_after) path_after = path_predeal(normal_path)
if(path_after in tree_nodes){
change_jstree_position(path_after)
}else{
$.ajax({ $.ajax({
cache: false, cache: false,
type: "POST", type: "POST",
url: '/operations/get_jstree_id/', url: '/operations/get_jstree_id/',
data: { 'project_id': project_id, 'path': path_after}, data: { 'project_id': project_id, 'path': normal_path},
dataType: 'json', dataType: 'json',
async: true, async: true,
beforeSend: function (xhr, settings) { beforeSend: function (xhr, settings) {
@ -1327,14 +1332,8 @@ function highlight_jstree_node(project_id,path_after){
success: function (data) { success: function (data) {
if (data.status === 'success') { if (data.status === 'success') {
var tree_node = data.jstree_id var tree_node = data.jstree_id
tree_nodes[path]=tree_node tree_nodes[path_after]=tree_node
$('#jstree').jstree("deselect_all", true); change_jstree_position(path_after)
$('#jstree').jstree("select_node", tree_nodes[path]);
// location.href="#"+tree_node
setTimeout(function timer() {
var offsettop=$("#"+tree_nodes[path]).position().top;
$("#filelist-content").scrollTop(offsettop);
},500)
} }
},error:function(jqXHR, textStatus, errorThrown){ },error:function(jqXHR, textStatus, errorThrown){
if(jqXHR.readyState===0&&jqXHR.status===0){ if(jqXHR.readyState===0&&jqXHR.status===0){
@ -1343,6 +1342,8 @@ function highlight_jstree_node(project_id,path_after){
} }
}); });
} }
}
// 这里也可以更改为item传入this // 这里也可以更改为item传入this
function close_tab(path) { function close_tab(path) {
// 将当前的当前Element的父节点isplay:none // 将当前的当前Element的父节点isplay:none
@ -1360,13 +1361,13 @@ function close_tab(path) {
// 如果该标签页有上一个标签,将此标签的上一个标签页打开 // 如果该标签页有上一个标签,将此标签的上一个标签页打开
if (previosuElement != undefined) { if (previosuElement != undefined) {
var path = previosuElement.id.substr(4); var path = previosuElement.id.substr(4);
open_tab(path,null) open_tab(path,null,project_id)
return; return;
} }
// 如果该标签页有下一个兄弟标签,将此标签的下一个标签页打开 // 如果该标签页有下一个兄弟标签,将此标签的下一个标签页打开
if (nextElement != undefined) { if (nextElement != undefined) {
var path = nextElement.id.substr(4); var path = nextElement.id.substr(4);
open_tab(path,null); open_tab(path,null,project_id);
return; return;
} }
} }
@ -2101,7 +2102,7 @@ function init_jstree(project_id,path,content){
$(".spinner").remove(); $(".spinner").remove();
$("#jstree").jstree("open_node", $("#j1_1")); $("#jstree").jstree("open_node", $("#j1_1"));
$("#jstree").css("visibility","visible"); $("#jstree").css("visibility","visible");
highlight_jstree_node(project_id,path) // highlight_jstree_node(project_id,path)
} }
$(function(){ $(function(){