更新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 deal_for_tab(project_id,normal_path,path_after){
// 关闭其他标签页
var tabcontent = document.getElementsByClassName("codereading");
for (var i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
var tab_items = document.getElementsByClassName("tab_item");
for (var i = 0; i < tab_items.length; i++) {
// debugger;
tab_items[i].className = tab_items[i].className.replace(" active", "");
}
// 将当前标签页置为活跃
document.getElementById("code_" + path_after).style.display = "block";
document.getElementById("tab_" + path_after).style.display = "block";
document.getElementById("tab_" + path_after).className += " active";
// 如果当前的内容未加载,则加载当前的内容
get_codereading_content(project_id,normal_path,path_after)
}
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(path,archor) { function open_tab(normal_path,archor,project_id) {
try{ try{
// console.log(window.location.pathname); 端口号后面的路径 highlight_jstree_node(project_id,normal_path);
path_input=path // console.log(window.location.pathname); 端口号后面的路径
path = path_predeal(path) var path_after = path_predeal(normal_path)
deal_for_tab(project_id,normal_path,path_after)
$('#jstree').jstree("deselect_all", true); url_deal(normal_path,archor)
$('#jstree').jstree("select_node", tree_nodes[path]); document.getElementsByClassName("filename")[0].innerHTML = normal_path;
setTimeout(function timer() { deal_for_right(project_id,normal_path,path_after)
var offsettop=$("#"+tree_nodes[path]).position().top;
$("#filelist-content").scrollTop(offsettop);
},500)
var tabcontent = document.getElementsByClassName("codereading");
for (var i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
var tab_items = document.getElementsByClassName("tab_item");
for (var i = 0; i < tab_items.length; i++) {
// debugger;
tab_items[i].className = tab_items[i].className.replace(" active", "");
}
document.getElementById("code_" + path).style.display = "block";
document.getElementById("tab_" + path).style.display = "block";
document.getElementById("tab_" + path).className += " active";
url_deal(path_input,archor)
document.getElementsByClassName("filename")[0].innerHTML = path_input;
if((path in right_map)){
$("#right_panel").html(right_map[path]);
}
// 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,42 +1310,39 @@ 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)
$.ajax({ if(path_after in tree_nodes){
cache: false, change_jstree_position(path_after)
type: "POST", }else{
url: '/operations/get_jstree_id/', $.ajax({
data: { 'project_id': project_id, 'path': path_after}, cache: false,
dataType: 'json', type: "POST",
async: true, url: '/operations/get_jstree_id/',
beforeSend: function (xhr, settings) { data: { 'project_id': project_id, 'path': normal_path},
xhr.setRequestHeader("X-CSRFToken", csrftoken); dataType: 'json',
}, async: true,
success: function (data) { beforeSend: function (xhr, settings) {
if (data.status === 'success') { xhr.setRequestHeader("X-CSRFToken", csrftoken);
var tree_node = data.jstree_id },
tree_nodes[path]=tree_node success: function (data) {
$('#jstree').jstree("deselect_all", true); if (data.status === 'success') {
$('#jstree').jstree("select_node", tree_nodes[path]); var tree_node = data.jstree_id
// location.href="#"+tree_node tree_nodes[path_after]=tree_node
setTimeout(function timer() { change_jstree_position(path_after)
var offsettop=$("#"+tree_nodes[path]).position().top; }
$("#filelist-content").scrollTop(offsettop); },error:function(jqXHR, textStatus, errorThrown){
},500) if(jqXHR.readyState===0&&jqXHR.status===0){
alert('网络故障,提交失败!请联网后重新提交');
}
} }
},error:function(jqXHR, textStatus, errorThrown){ });
if(jqXHR.readyState===0&&jqXHR.status===0){ }
alert('网络故障,提交失败!请联网后重新提交');
}
}
});
} }
// 这里也可以更改为item传入this // 这里也可以更改为item传入this
function close_tab(path) { function close_tab(path) {
@ -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(){