<%@ Page Language="C#" AutoEventWireup="true" CodeFile="eFiles.aspx.cs" Inherits="eFrameWork.Customs.eFiles" %>
<div class="nav">您当前位置：<a href="<%=eBase.getApplicationHomeURL() %>">首页</a>&nbsp;<span class="pag">-&gt;</span>&nbsp;<asp:Literal id="litNav" runat="server" /></div>
<script>
var pid = "<%=pid%>";
</script>
<script>
    var menu_a=null;
    var menu_type=0;
    var menu_name = "";
    var menu_id = "";
    function contextmenu(evt,obj,type,name,id)
    {      
        //type 1文件夹，2文件
        if (evt.button != 2) { return; }
        var ext = $(obj).attr("ext");     
        if (type == 2)
        {
            $(".etreeMenu a:contains('查看')").show();
        }
        else {
            $(".etreeMenu a:contains('查看')").hide();
        }
        if (type == 2) {
            $(".etreeMenu a:contains('编辑')").show();
        }
        else {
            $(".etreeMenu a:contains('编辑')").hide();
        }


        if (type == 2)
        {
            var url="?";
            if (typeof (AppItem) == "string" && AppItem.length > 0) url += "AppItem=" + AppItem + "&";
            if (typeof (ModelID) == "string" && ModelID.length > 0) url += "ModelID=" + ModelID + "&";
            url+="act=download" + (pid.length>0 ? "&pid=" + pid : "") + "&name=" + name;
            $(".etreeMenu a:contains('下载')").attr("href", url);
            $(".etreeMenu a:contains('下载')").show();
        }
        else {
            $(".etreeMenu a:contains('下载')").hide();
        }

        menu_type=type;
        menu_a=obj;
        menu_name = name;
        menu_id = id;
      
        var oRect = obj.getBoundingClientRect();	
        var top = eScroll().top + oRect.top + obj.offsetHeight;
        var left = eScroll().left + oRect.right - obj.offsetWidth;	
        top= eScroll().top + evt.clientY;
        left= eScroll().left + evt.clientX;
        var menu=getobj("etreeMenu");
        menu.style.top=top + "px";
        menu.style.left=left + "px";
        show_etreeMenu();	
    };
    function bodyck(e)
    {
        hide_etreeMenu();
    };
    function bodykd(e)
    {
        e=window.event||e; 
        if(e.keyCode==27){hide_etreeMenu();}
    };
    function show_etreeMenu()
    {
        var menu=getobj("etreeMenu");
        menu.style.display="";
        if(document.body.addEventListener){document.body.addEventListener("keydown",bodykd, false);}else{document.body.attachEvent("onkeydown",bodykd);}
        if(document.body.addEventListener){document.body.addEventListener("click",bodyck, false);}else{document.body.attachEvent("onclick",bodyck);}
    };
    function hide_etreeMenu()
    {
        var menu=getobj("etreeMenu");
        menu.style.display="none";
        if (document.body.addEventListener){document.body.removeEventListener('keydown', bodykd, false);}else{document.body.detachEvent("onkeydown",bodykd);}
        if (document.body.addEventListener){document.body.removeEventListener('click', bodyck, false);}else{document.body.detachEvent("onclick",bodyck);}
    };
    $(document).ready(function()
    {
        //document.body.oncontextmenu=function(){return false;};	
        document.body.oncontextmenu = function () {
            var src = getEventObject();
            //document.title = src.tagName;
            if (src.tagName == "DT" || src.tagName == "DD" || src.tagName == "DIV" || src.tagName == "IMG") {
                return false;
            }
            return true;
        };
    });
    //重命名文件、文件夹
    function file_rename()
    {
        //ReName(menu_name,menu_type);
        var name=menu_name;
        if(menu_type==2)
        {
            var idx=menu_name.lastIndexOf(".");
            var name=menu_name.substring(0,idx);
        }
        layer.prompt({ title: '输入文件' + (menu_type == 1 ? '夹' : '') + '名称', formType: 3, value: menu_name }, function (value, index) {
            var url = "?";
            if (typeof (AppItem) == "string" && AppItem.length > 0) url += "AppItem=" + AppItem + "&";
            if (typeof (ModelID) == "string" && ModelID.length > 0) url += "ModelID=" + ModelID + "&";
            url += "act=rename" + (pid.length > 0 ? "&pid=" + pid : "") + "&type=" + menu_type + "&id=" + menu_id + "&newname=" + value;
            url += "&ajax=true&t=" + now();
            layer.close(index);
            //document.location.href = url;
            $.ajax({
                type:"get",
                async:false,
                url:url,
                dataType:"json",
                success:function(data)
                {
                    if(data.success=="1")
                    {
                        if(data.errcode=="0")
                        {
                            file_reload();
                        }
                        layer.msg(data.message);      
                    }
                }
            });

        });
    };

    //删除文件、文件夹
    function file_del()
    {
        //var _back=confirm('您确定要删除该文件' + (menu_type==1 ? '夹' : '') + '吗？'); if(!_back){return;};
        var url = "?";
        if (typeof (AppItem) == "string" && AppItem.length > 0) url += "AppItem=" + AppItem + "&";
        if (typeof (ModelID) == "string" && ModelID.length > 0) url += "ModelID=" + ModelID + "&";
        url += "act=del&type=" + menu_type + (pid.length>0 ? "&pid=" + pid : "") + "&id=" + menu_id;        
        url +="&ajax=true&t="+now();
        //document.location.assign(url);
        layer.confirm('您确定要删除该文件' + (menu_type == 1 ? '夹' : '') + '吗？', {
            title:"文件" +  (menu_type == 1 ? "夹" : "")  + "删除确认?"
            ,btn: ['删除', '取消'] //按钮
            ,shadeClose: true //点击遮罩关闭层
        }, function (index) {
            //document.location.assign(url);

            //
            $.ajax({
                type:"get",
                async:false,
                url:url,
                dataType:"json",
                success:function(data)
                {
                    layer.close(index);
                    if(data.success=="1")
                    {                       
                        file_reload();                        
                        layer.msg(data.message);                    
                    }
                }
            });

        });
    };
    var eaceditor;
    //查看文件
    function file_view(name, id) {
        menu_name = name;
        menu_id = id;
        file_edit(true);
    };
    //编辑文件
    function file_edit(readonly)
    {
        var url = "?";
        if (typeof (AppItem) == "string" && AppItem.length > 0) url += "AppItem=" + AppItem + "&";
        if (typeof (ModelID) == "string" && ModelID.length > 0) url += "ModelID=" + ModelID + "&";
        url += "act=edit" + (pid.length > 0 ? "&pid=" + pid : "") + "&id=" + menu_id;
        $.ajax({
            type:"get",
            async:false,
            url:url,
            dataType:"json",
            success:function(json)
            {
                if(json.success=="1")
                {
                    //alert(json.value);
                    var html='';
                    if (menu_name.toLowerCase().indexOf(".html") > -1)
                    {
                        html+='<div style="padding:8px;background-color:#f2f2f2;"><a href="javascript:;" onclick="showHtmlTag();" style="color:#0026ff;">插入数据</a>&nbsp;&nbsp;<a href="javascript:;" onclick="showPathSelect();" style="color:#0026ff;">插入库文件</a></div>';
                    }
                    //html+='<textarea id="content" name="content" class="autoNumber" style="width:98%;height:540px;">' + json.value + '</textarea>';

                    html+='<pre id="eaceditor" style="">';
                    html+=json.value;
                    html+='</pre>';

                  
                    ///
                    layer.open({
                    type: 1 //此处以iframe举例
					,title: menu_name
                        //,skin: 'layui-layer-molv' //加上边框 layui-layer-rim  layui-layer-lan layui-layer-molv layer-ext-moon
					,shadeClose: true //点击遮罩关闭层
					, area: ['80%', '80%']
					,shade: 0.2 //透明度
					,maxmin: false
					,resize: false
					,btnAlign: 'l' //lcr
					,moveType: 0 //拖拽模式，0或者1
					,anim: 0 //0-6的动画形式，-1不开启
					,content: html
					, btn: readonly ? [ '关闭'] : ['保存修改', '关闭'] //只是为了演示
					,yes: function(index,layero)
					{

					    if (readonly) { layer.close(index); return; }
					    //var value=$("#content").val().replace(/\r/ig,"").replace(/\n/ig,"0x\\r\\n");
					    //var value=$("#content").val().encode();
					    var value=eaceditor.getValue();
					    url = "?";
					    if (typeof (AppItem) == "string" && AppItem.length > 0) {
					        url += "AppItem=" + AppItem + "&";
					    }
					    else {
					        if (typeof (ModelID) == "string" && ModelID.length > 0) {
					            url += "ModelID=" + ModelID + "&";
					        }
					    }
					    url += "act=save" + (pid.length > 0 ? "&pid=" + pid : "") + "&id=" + menu_id;
					    $.ajax({
					        type: "post", 
					        async: true,
					        data:{value:value},
					        url: url,
					        dataType: "json",
					        success: function (data) {
					            layer.msg(data.message);
					            //layer.close(index); 不关闭、继续修改
					        },
					        error: function (XMLHttpRequest, textStatus, errorThrown) {				
					        }
					    });

					}	
					,cancel: function(index, layero)
					{
					    //layer.msg("X关闭!");
					}
					,success: function(layero, index)
					{
					    var h=$('#layui-layer'+ index).height()-140;
					    var w=$('#layui-layer'+ index).width()-6;

					    //alert($('#layui-layer'+ index).height());
                        /*
					 
					    $("#content").height(h).width(w);
					    $("#content").setTextareaCount({width: "40px",bgColor: "#f2f2f2",color: "#000",display: "inline-block"});
                        */
					    eaceditor = ace.edit("eaceditor");
					    eaceditor.setTheme("ace/theme/tomorrow");//tomorrow,twilight
					    eaceditor.session.setMode("ace/mode/html");
					    eaceditor.session.setMode("ace/mode/sql");
					    if (readonly) { eaceditor.setReadOnly(true); }
					    $("#eaceditor").height(h).width(w);
					}
                    });

                    ///
                }
            }
        });
    };

   
    //重新加载
    function file_reload()
    {
        var url = "?";
        if (typeof (AppItem) == "string" && AppItem.length > 0) url += "AppItem=" + AppItem + "&";
        if (typeof (ModelID) == "string" && ModelID.length > 0) url += "ModelID=" + ModelID + "&";
        url += "act=reload&ajax=true" + (pid.length>0 ? "&pid=" + pid : "");
        url+="&t="+now();
        $.ajax({
            type:"get",
            async:false,
            url:url,
            dataType:"json",
            success:function(data)
            {
                if(data.success=="1")
                {
                    $("#filemanage_files").html(data.body);
                }
            }
        });
    };
    //文件上传回调
    function autoupload_callback(files)
    {
        file_reload();
    };
    //新建文件
    function file_newFile()
    {
        layer.prompt({ title: '输入文件名称', formType: 3, value: '' }, function (value, index) {
            //var url ="?" + (typeof(AppItem)=="string" && AppItem.length > 0 ? "AppItem=" + AppItem  : "ModelID=" + ModelID ) + "&act=addfile" + (pid.length>0 ? "&pid=" + pid : "") + "&name=" + value;
            var url ="?";
            if (typeof (AppItem) == "string" && AppItem.length > 0) url += "AppItem=" + AppItem + "&";
            if (typeof (ModelID) == "string" && ModelID.length > 0) url += "ModelID=" + ModelID + "&";
            url+= "act=newfile" + (pid.length>0 ? "&pid=" + pid : "") + "&name=" + value;
            url+="&ajax=true&t="+now();
            layer.close(index);
            //document.location.href = url;
            $.ajax({
                type:"get",
                async:false,
                url:url,
                dataType:"json",
                success:function(data)
                {
                    if(data.success=="1")
                    {
                        if(data.errcode=="0")
                        {
                            file_reload();
                        }
                        layer.msg(data.message);      
                    }
                }
            });
        });
    };
    //新建文件夹
    function file_newFolder()
    {
        layer.prompt({ title: '输入文件夹名称', formType: 3, value: '' }, function (value, index) {
            //var url = "?" +  (typeof(AppItem)=="string" && AppItem.length > 0 ? "AppItem=" + AppItem  : "ModelID=" + ModelID ) + "&act=addfolder" + (pid.length>0 ? "&pid=" + pid : "") + "&name=" + value;
            var url ="?";
            if (typeof (AppItem) == "string" && AppItem.length > 0) url += "AppItem=" + AppItem + "&";
            if (typeof (ModelID) == "string" && ModelID.length > 0) url += "ModelID=" + ModelID + "&";
            url+= "act=newfolder" + (pid.length>0 ? "&pid=" + pid : "") + "&name=" + value;
            url+="&ajax=true&t="+now();
            layer.close(index);
            //document.location.href = url;
            $.ajax({
                type:"get",
                async:false,
                url:url,
                dataType:"json",
                success:function(data)
                {
                    if(data.success=="1")
                    {
                        file_reload();
                        layer.msg(data.message);      
                    }
                }
            });
        });
    };

</script>

<div id="etreeMenu" class="etreeMenu" style="display:none;z-index:1000;">
<a href="javascript:;" onclick="file_edit(true);">查看</a>
<%if (model.Power["edit"])
  { %><a href="javascript:;" onclick="file_edit(false);">编辑</a><%} %>

<a href="javascript:;" onclick="file_rename();">重命名</a>



<%if (model.Power["download"])
  { %><a href="javascript:;" target="_blank">下载</a><%} %>
<%if (model.Power["del"])  
  { %><a href="javascript:;" onclick="file_del();">删除</a><%} %>
</div>
<div style="margin:10px;">
<asp:Literal id="litBody" runat="server" />
</div>