修复了打开第一个标签后左侧文档树不展开的BUG
解决方法:
由于jstree在未指定li的id时,id生是由规律的,第一个生成的li为j1_1,第二个为j1_2,以此类推
jstree是通过深度优先搜索遍历的,在遍历的时候又会提供当前节点的relative_path
因此在遍历时记录了节点对应的jstree_id,然后返回
“
This commit is contained in:
wrmswindmill 2018-08-15 09:53:44 +08:00
parent 83651aa687
commit aa4c7bdea4
5 changed files with 98 additions and 83 deletions

View File

@ -625,7 +625,6 @@ class AddVoteView(View):
object.voteup = object.voteup -1 object.voteup = object.voteup -1
object.save() object.save()
exist_records.delete() exist_records.delete()
print(-vote_before)
return HttpResponse(json.dumps({"status": "success", "info": "cancel", "value": -(vote_before), "msg": "cancel success"}), return HttpResponse(json.dumps({"status": "success", "info": "cancel", "value": -(vote_before), "msg": "cancel success"}),
content_type='application/json') content_type='application/json')
vote = Vote() vote = Vote()
@ -640,7 +639,6 @@ class AddVoteView(View):
else: else:
object.voteup = object.voteup + 1 object.voteup = object.voteup + 1
object.save() object.save()
print(vote_value)
return HttpResponse(json.dumps({"status": "success", "msg": "vote success", "value": vote_value}), content_type='application/json') return HttpResponse(json.dumps({"status": "success", "msg": "vote success", "value": vote_value}), content_type='application/json')
else: else:
return HttpResponse('{"status":"fail","msg":"参数传递错误"}', content_type='application/json') return HttpResponse('{"status":"fail","msg":"参数传递错误"}', content_type='application/json')
@ -1016,7 +1014,6 @@ class RightView(View):
return html_str return html_str
def post(self, request): def post(self, request):
print(1111)
# 先判断他是文件还是文件夹 # 先判断他是文件还是文件夹
project_id = request.POST.get('project_id', '') project_id = request.POST.get('project_id', '')
file_path = request.POST.get('path', '') file_path = request.POST.get('path', '')

View File

@ -150,9 +150,9 @@ class ProjectInfoView(View):
class ProjectSourceView(View): class ProjectSourceView(View):
def get(self, request, name, path): def get(self, request, name, path):
project = Project.objects.filter(name=name).first() project = Project.objects.filter(name=name).first()
# print(request.user) project_tree_obj = get_project_tree.ProjectTree()
project_tree = get_project_tree.getHtml(settings.SOURCEPATH+project.path) project_tree = project_tree_obj.getHtml(settings.SOURCEPATH+project.path)
print(path) first_tabs_jstree_id = project_tree_obj.path_id_map[path]
return render(request, 'projects/source.html', locals()) return render(request, 'projects/source.html', locals())
#文件列表页 #文件列表页

View File

@ -7,87 +7,97 @@ from projects.models import Project
# ondbclick的参数为:project_id,relative_path以及文件名 # ondbclick的参数为:project_id,relative_path以及文件名
# 其中relative_path是相对于项目根路径的 # 其中relative_path是相对于项目根路径的
project_name = "" class ProjectTree:
project_id = "" def __init__(self):
#######获取当前层的文件夹和文件 self.count = 1;
def getindex(path): self.path_id_map = {}
dirs=[]
files=[]
for file in os.listdir(path):
if os.path.isdir(os.path.join(path, file)):
dirs.append(file)
else:
files.append(file)
return dirs,files
#####生成节点树########## #######获取当前层的文件夹和文件
def genetree(path,parentdirs): def getindex(self,path):
dirs,files=getindex(path) dirs=[]
# 如果parentdir不为空那么htmlstr应该为"",否则会有问题,建议上次提交时的文件 files=[]
if parentdirs =="": for file in os.listdir(path):
isParentDirNull=True if os.path.isdir(os.path.join(path, file)):
else: dirs.append(file)
isParentDirNull=False else:
if isParentDirNull: files.append(file)
htmlstr = "<ul>" return dirs,files
else:
htmlstr = "" #####生成节点树##########
# 处理多级单文件夹目录 def genetree(self,path,parentdirs,project_name,project_id):
# parentdir用来保存那个多级单文件父目录 dirs,files=self.getindex(path)
if len(dirs)==1: # 如果parentdir不为空那么htmlstr应该为"",否则会有问题,建议上次提交时的文件
parentdirs += (dirs[0] + os.sep) if parentdirs =="":
current_dir = os.path.join(path, dirs[0]) isParentDirNull=True
htmlstr += genetree(current_dir,parentdirs) else:
else: isParentDirNull=False
for dir in dirs: if isParentDirNull:
htmlstr = "<ul>"
else:
htmlstr = ""
# 处理多级单文件夹目录
# parentdir用来保存那个多级单文件父目录
if len(dirs)==1:
parentdirs += (dirs[0] + os.sep)
current_dir = os.path.join(path, dirs[0])
htmlstr += self.genetree(current_dir,parentdirs,project_name,project_id)
else:
for dir in dirs:
# 合成ondbclick 字符串
index = path.find(project_name)
relative_path = path[index+len(project_name):]+"/"+dir
dirname = parentdirs + dir
# ondbclick_str = '<text ondbclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,dirname)
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,dirname)
ondbclick_str += (dirname +"</text>")
self.record_path_and_id(relative_path)
# 形成li字符串
htmlstr += "<li>"+ondbclick_str
dir = os.path.join(path, dir)
htmlstr += self.genetree(dir,"",project_name,project_id)
htmlstr += "</li>"
# 循环遍历完毕parentdirs应该设置为空
if parentdirs != "":
parentdirs = ""
for file in files:
# 合成ondbclick 字符串 # 合成ondbclick 字符串
index = path.find(project_name) index = path.find(project_name)
relative_path = path[index+len(project_name):]+"/"+dir relative_path = path[index+len(project_name):]+"/"+file
dirname = parentdirs + dir ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,file)
# ondbclick_str = '<text ondbclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,dirname) ondbclick_str += (file +"</text>")
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,dirname) # 合成li
ondbclick_str += (dirname +"</text>") htmlstr += "<li data-jstree='{\"icon\":\"fa fa-file-code-o color-blue\"}'>"+ ondbclick_str+"</li>"
self.record_path_and_id(relative_path)
# 形成li字符串
htmlstr += "<li>"+ondbclick_str if isParentDirNull:
htmlstr += "</ul>"
dir = os.path.join(path, dir) return htmlstr
htmlstr += genetree(dir,"")
htmlstr += "</li>"
# 循环遍历完毕parentdirs应该设置为空
if parentdirs != "":
parentdirs = ""
for file in files:
# 合成ondbclick 字符串
index = path.find(project_name)
relative_path = path[index+len(project_name):]+"/"+file
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,file)
ondbclick_str += (file +"</text>")
# 合成li
htmlstr += "<li data-jstree='{\"icon\":\"fa fa-file-code-o color-blue\"}'>"+ ondbclick_str+"</li>"
if isParentDirNull: def getHtml(self,path):
htmlstr += "</ul>" htmlstr = '<div id="jstree">'
return htmlstr try:
# 在这里把最开始的目录给加上
project_name = path.split("/")[-1]
project_id = str(Project.objects.get(name=project_name).pk)
relative_path = ""
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,project_name)
ondbclick_str += (project_name+"</text>")
htmlstr += "<ul><li>"+ ondbclick_str
self.record_path_and_id(relative_path)
htmlstr += self.genetree(path,"",project_name,project_id)
htmlstr += "</li></ul>"
except:
self.record_path_and_id(relative_path)
htmlstr += self.genetree(path,"",project_name,project_id)
finally:
htmlstr += "</div>"
return htmlstr
def getHtml(path): def record_path_and_id(self,path):
htmlstr = '<div id="jstree">' self.path_id_map[path] = "j1_"+str(self.count)
try: self.count = self.count + 1
# 在这里把最开始的目录给加上
global project_name,project_id
project_name = path.split("/")[-1]
project_id = str(Project.objects.get(name=project_name).pk)
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,"",project_name)
ondbclick_str += (project_name+"</text>")
htmlstr += "<ul><li>"+ ondbclick_str
htmlstr += genetree(path,"")
htmlstr += "</li></ul>"
except:
htmlstr += genetree(path,"")
finally:
htmlstr += "</div>"
return htmlstr

View File

@ -1299,7 +1299,13 @@ window.onload = function () {
// 展开当前标签对应的节点 // 展开当前标签对应的节点
// $('#jstree').jstree("deselect_all", true); // $('#jstree').jstree("deselect_all", true);
// $('#jstree').jstree("select_node", "j1_1355554"); // $('#jstree').jstree("select_node", "j1_1355554");
// 第一个标签页对应的id保存在first_tabs_jstree_id中
var first_tabs_jstree_id = $("#first_tabs_jstree_id").text()
path_after = path_predeal(data.path)
tree_nodes[path_after] = first_tabs_jstree_id
add_tab(data.project_id, data.path, data.filename) add_tab(data.project_id, data.path, data.filename)
tree_nodes[path_after] = first_tabs_jstree_id
//
} }
} }
}); });

View File

@ -139,6 +139,8 @@
</div> </div>
</div> </div>
<span id="projectName">{{ project.name }}</span> <span id="projectName">{{ project.name }}</span>
<span id="first_tabs_jstree_id">{{ first_tabs_jstree_id }}</span>
</body> </body>
</html> </html>