zTree_v3
zTree_v3 copied to clipboard
文件操作复制粘贴后新增节点问题
文件操作复制粘贴后新增节点问题

新增的节点,好像没有了我的新图片路径,图片路径还是复制前的路径,我要刷新页面才能正常,我少操作了什么吗?还要改变什么才能实现粘贴后与原文件路径分开?
var filepath = '';
function openfile() {
var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
var node = treeObj.getSelectedNodes();
$('#filename').html(node[0].name);
if (node[0].name.indexOf('.png') == -1 && node[0].name.indexOf('.jpg') == -1 && node[0].name.indexOf('.gif') == -1 && node[0].name.indexOf('.ico') == -1 && node[0].name.indexOf('.') != -1) {
$('#codeeditor').show();
$('#imgeditor').hide();
$('#toolberfooter').show();
$('.iscode').show();
$.get("/api/templates", { path: node[0].path }, function (data, textStatus) {
if (node[0].name.indexOf('.css') > 0) {
editor.getSession().setMode("ace/mode/css");
$('#filepath').val(node[0].path);
editor.setValue(data);
editor.navigateTo(0, 0);//跳转到指定行,并且显示到可视区域内
}
if (node[0].name.indexOf('.cshtml') > 0) {
editor.getSession().setMode("ace/mode/html");
$('#filepath').val(node[0].path);
editor.setValue(data);
editor.navigateTo(0, 0);//跳转到指定行,并且显示到可视区域内
}
if (node[0].name.indexOf('.html') > 0) {
editor.getSession().setMode("ace/mode/html");
$('#filepath').val(node[0].path);
editor.setValue(data);
editor.navigateTo(0, 0);//跳转到指定行,并且显示到可视区域内
}
if (node[0].name.indexOf('.htm') > 0) {
editor.getSession().setMode("ace/mode/html");
$('#filepath').val(node[0].path);
editor.setValue(data);
editor.navigateTo(0, 0);//跳转到指定行,并且显示到可视区域内
}
if (node[0].name.indexOf('.asp') > 0) {
editor.getSession().setMode("ace/mode/html");
$('#filepath').val(node[0].path);
editor.setValue(data);
editor.navigateTo(0, 0);//跳转到指定行,并且显示到可视区域内
}
if (node[0].name.indexOf('.js') > 0) {
editor.getSession().setMode("ace/mode/javascript");
$('#filepath').val(node[0].path);
editor.setValue(data);
editor.navigateTo(0, 0);//跳转到指定行,并且显示到可视区域内
}
if (node[0].name.indexOf('.txt') > 0) {
editor.getSession().setMode("ace/mode/txt");
$('#filepath').val(node[0].path);
editor.setValue(data);
editor.navigateTo(0, 0);//跳转到指定行,并且显示到可视区域内
}
});
} else {
$('.iscode').hide();
$('#codeeditor').hide();
$('#imgeditor').show();
$('#toolberfooter').hide();
if (node[0].name.indexOf('.') == -1) {
$('.wu-example').removeClass('wu-example').addClass('wu-examples');
$('#imgview').attr('src', '');
$('#prompt').show();
} else {
$('.wu-examples').removeClass('wu-examples').addClass('wu-example');
$('#imgview').attr('src', node[0].path + '?t=' + new Date().getTime());
$('#prompt').hide();
}
}
$('#savepath').val(node[0].path);
}
function remove() {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getSelectedNodes(),
treeNode = nodes[0];
if (nodes.length == 0) {
alert("请先选择要删除的文件");
return;
}
zTree.removeNode(treeNode);
};
var setting = {
async: {
enable: true,
url: "/api/filelist",
autoParam: ["path=path"],
otherParam: { "otherParam": "zTreeAsyncTest" },
dataFilter: filter
}
};
function filter(treeId, parentNode, childNodes) {
if (!childNodes) return null;
for (var i = 0, l = childNodes.length; i < l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
childNodes[i].path = childNodes[i].path.replace(/\.n/g, '.');
}
return childNodes;
}
$(document).ready(function () {
if ($("#treeDemo")[0]) {
$.fn.zTree.init($("#treeDemo"), setting);
}
});
$(function () {
$('#imgeditor').show();
var $list = $('#thelist'),
$btn = $('#ctlBtn'),
state = 'pending',
uploader;
uploader = WebUploader.create({
// 不压缩image
//resize: true,
compress: null,
quality: 100,
oCompressIfLarger: false,
// swf文件路径
swf: BASE_URL + '/Uploader.swf',
fileVal: 'fromfile',
chunkSize: 512 * 10240,
disableGlobalDnd: true,
fileNumLimit: 300,
fileSizeLimit: 200 * 1024 * 1024, // 200 M
fileSingleSizeLimit: 500 * 1024 * 1024, // 50 M
// 文件接收服务端。
server: '/api/uploader',
// 选择文件的按钮。可选。
// 内部根据当前运行是创建,可能是input元素,也可能是flash.
pick: '#picker'
});
// 当有文件添加进来的时候
uploader.on('fileQueued', function (file) {
$list.append('<div id="' + file.id + '" class="item">' +
'<h4 class="info">' + file.name + '</h4>' +
'<p class="state">等待上传...</p>' +
'</div>');
});
// 文件上传过程中创建进度条实时显示。
uploader.on('uploadProgress', function (file, percentage) {
var $li = $('#' + file.id),
$percent = $li.find('.progress .progress-bar');
// 避免重复创建
if (!$percent.length) {
$percent = $('<div class="progress progress-striped active">' +
'<div class="progress-bar" role="progressbar" style="width: 0%">' +
'</div>' +
'</div>').appendTo($li).find('.progress-bar');
}
$li.find('p.state').text('上传中');
$percent.css('width', percentage * 100 + '%');
});
uploader.on('uploadSuccess', function (file, ret) {
if ($('#imgview').attr('src') != "") {
$('#imgview').attr('src', ret.src + '?t=' + new Date().getTime());
} else {
add(ret.src);
isp = false;
$('#' + file.id).find('p.state').text('已上传');
$('#' + file.id).fadeOut('slow');
}
});
uploader.on('uploadError', function (file) {
$('#' + file.id).find('p.state').text('上传出错');
});
uploader.on('uploadComplete', function (file) {
$('#' + file.id).find('.progress').fadeOut();
});
uploader.on('all', function (type) {
if (type === 'startUpload') {
state = 'uploading';
} else if (type === 'stopUpload') {
state = 'paused';
} else if (type === 'uploadFinished') {
isp = true;
state = 'done';
}
if (state === 'uploading') {
$btn.text('暂停上传');
} else {
$btn.text('开始上传');
}
});
$btn.on('click', function () {
if (state === 'uploading') {
uploader.stop();
} else {
uploader.upload();
}
});
});
function Delete() {
var d = dialog({
title: '提示',
content: '你确定要删除选中的文件吗?',
okValue: '确定',
ok: function () {
this.title('提交中…');
$.ajax("/api/file/foldermove", {
type: "post",
data: {
"path": $('#savepath').val()
},
dataType: "json"
}).done(function (data) {
if (data.isSuccess) {
remove();
//$('#row-' + data.DeleteId).fadeOut('slow');
}
});
this.remove();
return false;
},
cancelValue: '取消',
cancel: function () { }
});
d.show();
}
var newCount = 1;
var isp = true;
function add(returnValue) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
isParent = false,
nodes = zTree.getSelectedNodes(),
treeNode = nodes[0];
var newpaths = returnValue.substring(0, returnValue.lastIndexOf('/'));
var newnames = returnValue.substring(returnValue.lastIndexOf('/') + 1);
var names = newpaths.substring(newpaths.lastIndexOf('/') + 1);
var oldpaths = returnValue.substring(0, returnValue.lastIndexOf('/'));
//alert(newpaths);
//alert(newnames);
//alert(names);
//alert(oldpaths);
var childZNode = { id: (100 + newCount), pId: treeNode.pId, isParent: true, name: names, path: newpaths }; //构造子节点
if (oldpaths != treeNode.path && isp) {
zTree.addNodes(treeNode, childZNode);
isp = false;
}
//alert(treeNode.path);
//alert(returnValue);
if (treeNode) {
//alert(oldpaths != treeNode.path&&isp);
if (oldpaths != treeNode.path) {
//alert(oldpaths != treeNode.path&&isp+1);
zTree.addNodes(childZNode, { id: (100 + newCount), pId: treeNode.pId, isParent: isParent, name: newnames, path: returnValue });
} else {
zTree.addNodes(treeNode, { id: (100 + newCount), pId: treeNode.pId, isParent: isParent, name: newnames, path: returnValue });
//alert(oldpaths != treeNode.path&&isp+2);
}
} else {
zTree.addNodes(null, { id: (100 + newCount), pId: 0, isParent: isParent, name: "new node" + (newCount++) });
//alert(oldpaths != treeNode.path&&isp+3);
}
if (treeNode) {
zTree.editName(treeNode[0]);
} else {
alert("叶子节点被锁定,无法增加子节点");
}
newCount++;
}
function fontCss(treeNode) {
var aObj = $("#" + treeNode.tId + "_a");
aObj.removeClass("copy").removeClass("cut");
if (treeNode === curSrcNode) {
if (curType == "copy") {
aObj.addClass(curType);
} else {
aObj.addClass(curType);
}
}
}
function beforeDrag(treeId, treeNodes) {
return false;
}
function beforeClick(treeId, treeNode) {
return !treeNode.isCur;
}
var curSrcNode, curType;
function setCurSrcNode(treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
if (curSrcNode) {
delete curSrcNode.isCur;
var tmpNode = curSrcNode;
curSrcNode = null;
fontCss(tmpNode);
}
curSrcNode = treeNode;
if (!treeNode) return;
curSrcNode.isCur = true;
zTree.cancelSelectedNode();
fontCss(curSrcNode);
}
function copy(e) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getSelectedNodes();
if (nodes.length == 0) {
alert("请先选择一个节点");
return;
}
curType = "copy";
setCurSrcNode(nodes[0]);
}
function cut(e) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getSelectedNodes();
if (nodes.length == 0) {
alert("请先选择一个节点");
return;
}
curType = "cut";
setCurSrcNode(nodes[0]);
}
function paste(filename) {
if (!curSrcNode) {
alert("请先选择一个节点进行 复制 / 剪切");
return;
}
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getSelectedNodes(),
targetNode = nodes.length > 0 ? nodes[0] : curSrcNode.getParentNode();
if (curSrcNode === targetNode) {
alert("不能移动,源节点 与 目标节点相同");
return;
} else if (curType === "cut" && ((!!targetNode && curSrcNode.parentTId === targetNode.tId) || (!targetNode && !curSrcNode.parentTId))) {
alert("不能移动,源节点 已经存在于 目标节点中");
return;
} else if (curType === "copy") {
curSrcNode.path = filename;
curSrcNode.name = filename.substring(filename.lastIndexOf('/') + 1);
targetNode = zTree.copyNode(targetNode, curSrcNode, "inner");
$('#filename').html(curSrcNode.name);
} else if (curType === "cut") {
targetNode = zTree.moveNode(targetNode, curSrcNode, "inner");
if (!targetNode) {
alert("剪切失败,源节点是目标节点的父节点");
}
targetNode = curSrcNode;
}
setCurSrcNode();
delete targetNode.isCur;
zTree.selectNode(targetNode);
}
function Copy() {
var value = $('#savepath').val();
var d = dialog({
width: '30em',
title: '复制到的文件位置路径',
okValue: '复制',
content: '<input id="returnValue" class="form-control" value="' + value + '" />',
ok: function () {
this.title('复制文件中…');
var value = $('#returnValue').val();
this.close(value);
this.remove();
},
cancelValue: '取消',
cancel: function () { }
});
d.addEventListener('close', function () {
if (this.returnValue != '' && this.returnValue != value) {
var value = this.returnValue;
$.ajax({
type: "POST",
url: "/api/file/copy",
data: { path: $('#savepath').val(), returnValue: this.returnValue },
success: function (data, textStatus) {
if (data.isSuccess) {
copy();
paste(value);
$('#savepath').val(value);
$('#filepath').val(value);
//$(thisid).html(data.returnValue);
}
},
error: function () {
//请求出错处理
//$(thisid).html(this.returnValue);
}
});
}
});
d.show();
}