complete left project tree

This commit is contained in:
wrmswindmill 2018-07-18 15:53:29 +08:00
parent 54c70ee061
commit fa0d953aba
3 changed files with 179 additions and 172 deletions

View File

@ -1,86 +1,93 @@
#encoding:utf-8
import os
import sys
from projects.models import Project
url_head = "/projects"
# ondbclick的参数为:project_id,relative_path以及文件名
# 其中relative_path是相对于项目根路径的
project_name = ""
project_id = ""
# 将叶子节点的href替换成url
# 传入一个工程根目录路径返回一个project_tree对应的html代码
# 思路是基于递归的深度优先遍历
def getHtml(projectPath):
global project_name,project_id
project_name = projectPath[projectPath.rfind('/')+1:]
project_id = str(Project.objects.get(name=project_name).pk)
html_str = "<ul>"
files = os.listdir(projectPath)
for i in range(len(files)):
filePath = projectPath + os.path.sep + files[i]
if os.path.isdir(filePath):
tag_html = '<li class = "parent-item">\n'
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
project_id,files[i], files[i], files[i])
tag_html += getInnerHtml(projectPath, filePath)
tag_html += '</li>\n'
#######获取当前层的文件夹和文件
def getindex(path):
dirs=[]
files=[]
for file in os.listdir(path):
if os.path.isdir(os.path.join(path, file)):
dirs.append(file)
else:
# 叶子节点,替换成绝对路径
url = url_head+"/"+project_name+"/"+files[i]
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
project_id,files[i], files[i], files[i])
files.append(file)
return dirs,files
html_str += tag_html
html_str += "</ul>"
return html_str
#####生成节点树##########
def genetree(path,parentdirs):
dirs,files=getindex(path)
# 如果parentdir不为空那么htmlstr应该为"",否则会有问题,建议上次提交时的文件
if parentdirs =="":
isParentDirNull=True
else:
isParentDirNull=False
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 += genetree(current_dir,parentdirs)
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>")
# 合并文件夹(将下级只有一个文件夹的连续文件夹拼接)
# 例如假设net目录下只有ui目录ui目录下只有css目录css下有1.css2.css
# 那么就将net,ui,css合并成net/ui/css/
def getFiles(path):
files = os.listdir(path)
flag = False
while len(files) == 1 and os.path.isdir(path + os.path.sep + files[0]):
path = path + os.path.sep + files[0]
files = os.listdir(path)
flag = True
# 形成li字符串
htmlstr += "<li>"+ondbclick_str
return path, files, flag
def getInnerHtml(projectPath, path):
html_str = '<ul class = "item sub-item" >\n'
# files = os.listdir(path)
path_before = path
path, files, flag = getFiles(path)
# print(path)
for i in range(len(files)):
filePath = path + os.path.sep + files[i]
dir = os.path.join(path, dir)
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):]+"/"+files[i]
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\":\"glyphicon glyphicon-leaf\"}'>"+ ondbclick_str+"</li>"
if os.path.isdir(filePath):
tag_html = '<li>\n'
# 如果有多级层叠的情况
if flag:
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
project_id,relative_path, files[i], path.replace(path_before, '')+"/"+files[i])
else:
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
project_id,relative_path, files[i], files[i])
tag_html += getInnerHtml(projectPath, filePath)
tag_html += '</li>\n'
else:
# 替换成绝对路径
# index = path.find(project_name)
# url = url_head+"/"+path[index:]+"/"+files[i]
# tag_html = '<li><a href="%s"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
# url, files[i])
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","%s","%s")><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
project_id,relative_path, files[i], files[i])
html_str += tag_html
html_str += '</ul>\n'
return html_str
if isParentDirNull:
htmlstr += "</ul>"
return htmlstr
def getHtml(path):
htmlstr = '<div id="jstree">'
try:
# 在这里把最开始的目录给加上
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

@ -0,0 +1,86 @@
import os
from projects.models import Project
url_head = "/projects"
project_name = ""
project_id = ""
# 将叶子节点的href替换成url
# 传入一个工程根目录路径返回一个project_tree对应的html代码
# 思路是基于递归的深度优先遍历
def getHtml(projectPath):
global project_name,project_id
project_name = projectPath[projectPath.rfind('/')+1:]
project_id = str(Project.objects.get(name=project_name).pk)
html_str = "<ul>"
files = os.listdir(projectPath)
for i in range(len(files)):
filePath = projectPath + os.path.sep + files[i]
if os.path.isdir(filePath):
tag_html = '<li class = "parent-item">\n'
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
project_id,files[i], files[i], files[i])
tag_html += getInnerHtml(projectPath, filePath)
tag_html += '</li>\n'
else:
# 叶子节点,替换成绝对路径
url = url_head+"/"+project_name+"/"+files[i]
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
project_id,files[i], files[i], files[i])
html_str += tag_html
html_str += "</ul>"
return html_str
# 合并文件夹(将下级只有一个文件夹的连续文件夹拼接)
# 例如假设net目录下只有ui目录ui目录下只有css目录css下有1.css2.css
# 那么就将net,ui,css合并成net/ui/css/
def getFiles(path):
files = os.listdir(path)
flag = False
while len(files) == 1 and os.path.isdir(path + os.path.sep + files[0]):
path = path + os.path.sep + files[0]
files = os.listdir(path)
flag = True
return path, files, flag
def getInnerHtml(projectPath, path):
html_str = '<ul class = "item sub-item" >\n'
# files = os.listdir(path)
path_before = path
path, files, flag = getFiles(path)
# print(path)
for i in range(len(files)):
filePath = path + os.path.sep + files[i]
index = path.find(project_name)
relative_path = path[index+len(project_name):]+"/"+files[i]
if os.path.isdir(filePath):
tag_html = '<li>\n'
# 如果有多级层叠的情况
if flag:
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
project_id,relative_path, files[i], path.replace(path_before, '')+"/"+files[i])
else:
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
project_id,relative_path, files[i], files[i])
tag_html += getInnerHtml(projectPath, filePath)
tag_html += '</li>\n'
else:
# 替换成绝对路径
# index = path.find(project_name)
# url = url_head+"/"+path[index:]+"/"+files[i]
# tag_html = '<li><a href="%s"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
# url, files[i])
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","%s","%s")><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
project_id,relative_path, files[i], files[i])
html_str += tag_html
html_str += '</ul>\n'
return html_str

View File

@ -1,86 +0,0 @@
#encoding:utf-8
import os
import sys
from projects.models import Project
# ondbclick的参数为:project_id,relative_path以及文件名
# 其中relative_path是相对于项目根路径的
project_name = ""
project_id = ""
#######获取当前层的文件夹和文件
def getindex(path):
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):
dirs,files=getindex(path)
# 如果parentdir不为空那么htmlstr应该为"",否则会有问题,建议上次提交时的文件
if parentdirs =="":
isParentDirNull=True
else:
isParentDirNull=False
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 += genetree(current_dir,parentdirs)
else:
for dir in dirs:
# 合成ondbclick 字符串
index = path.find(project_name)
relative_path = path[index+len(project_name):]+"/"+dir
dirname = parentdirs + dir
ondbclick_str = 'ondbclick("%s","%s","%s")' %(project_id,relative_path,dirname)
# 形成li字符串
htmlstr += "<li "+ondbclick_str +" >"+ dirname
dir = os.path.join(path, dir)
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 = 'ondbclick("%s","%s","%s")' %(project_id,relative_path,file)
# 合成li
htmlstr += "<li data-jstree='{\"icon\":\"glyphicon glyphicon-leaf\"}' "+ ondbclick_str+" >"+file+"</li>"
if isParentDirNull:
htmlstr += "</ul>"
return htmlstr
def getHtml(path):
htmlstr = '<div id="jstree">'
try:
# 在这里把最开始的目录给加上
global project_name,project_id
project_name = path.split("/")[-1]
project_id = str(Project.objects.get(name=project_name).pk)
htmlstr += "<ul><li>"+ project_name
htmlstr += genetree(path,"")
htmlstr += "</li></ul>"
except:
htmlstr += genetree(path,"")
finally:
htmlstr += "</div>"
return htmlstr