Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Created November 29, 2013 13:33
Show Gist options
  • Save yangjunjun/7705749 to your computer and use it in GitHub Desktop.
Save yangjunjun/7705749 to your computer and use it in GitHub Desktop.
/*
* @Author: xngiser
* @Date: 2013-11-28 10:50:04
* @Email: ggiiss@qq.com
* @Last Modified by: xngiser
* @Last Modified time: 2013-11-29 21:32:48
*/
(function(window, $, eo){
/***********************************************************************
* 定义常用变量
************************************************************************/
var Grid = {}; //文件表格
var Tree = {}; //部门树
var Tools = eo.tools; //导入的工具类
var Meta = eo.c; //初始化的元信息
var Basepath = ''; //常用操作的路径base
var Current = {
fid: -1, //右边显示文件列表的目录
editIndex: -1, //正在编辑的行数
act: 'scan' //正在编辑的操作
};
/***********************************************************************
* 文件类构造函数
************************************************************************/
function File(){
this.init();
this._initBind();
}
/***********************************************************************
* 文件类初始化
************************************************************************/
File.prototype.init =function(){
Basepath = Meta.host +'/File2/' + Meta.mod; //操作地址的前缀
var initTreeURL = Basepath + '/dirTree'; //部门树请求数据的接口地址
var initPid = $.parseJSON(Meta.dirInfo).id; // 登录者所在部门的id
this._initTree(initTreeURL, initPid);
this._initGrid(initPid);
}
/***********************************************************************
* 初始化部门树
************************************************************************/
File.prototype._initTree = function(url, pid){
var that = this;
Tree = this.tree = $('#tree').tree({
url:url,
onLoadSuccess:function(){
that.beforeExpand(pid);
},
onClick:function(node){
that.beforeExpand(node.id);
}
});
}
/***********************************************************************
* 初始化文件表格,没有数据
************************************************************************/
File.prototype._initGrid = function(pid){
var that = this;
Grid = that.grid = $('#file-data-grid').datagrid({
//url:Basepath +'/file?target_id=' + 634,
fitColumns:true,
autoRowHeight:false,
checkbox:true,
cache:true,
loadMsg:'正在加载数据,请稍等……',
columns:[[
{field:'id',checkbox:true},
{field:'type',title:'',align:'center',width:20,
styler:function(v,r,i){
return 'border-right-style:none;';
},formatter:function(value,row){
return '<img src="'+ Meta.pub+'/Images/file-type-icon/'+ that.getSuffixIcon(row.type) +'.png" /> ';
}},
{field:'name',title:'文件名称',width:160, editor:{
type:'text',
options:{
blur:that.rowRename
}}
},
{field:'ctime',title:'创建时间',width:120,formatter:function(value,row){
return that.getFormatDate(row.ctime);
}},
{field:'file_size',title:'大小',width:50,
formatter:function(value,row){
return that.getFormatFileSize(row.file_size);
}},
{field:'act',title:'操作',width:60},
{field:'pid',hidden:true},
{field:'file_id',hidden:true}
]],
onRowContextMenu:function(e,index,data){
e.preventDefault();
Grid.datagrid('unselectAll').datagrid('selectRow',index);
that.showContextMenu(e,index,data);
},
onDblClickRow:function(index,row){
if(row.type === 'folder'){
that.expand(row.id);
}
},
onAfterEdit:function(rowIndex, rowData, changes){
if(Current.act === 'add'){
that.add(rowIndex, rowData, changes);
}
else if(Current.act === 'edit'){
that.rename(rowIndex, rowData, changes);
}
}
});
}
/***********************************************************************
* 初始化时添加绑定事件
************************************************************************/
File.prototype._initBind = function(){
var that = this;
$('#btn-up').bind('click', that.upload);
$('#btn-new').bind('click', that.beforeAdd);
$('#btn-del').bind('click', that.beforeDel);
$('#btn-refresh').bind('click', that.refresh);
$('#btn-down').bind('click', that.download);
$("#file-data").bind({
mousedown:function(e){
if(e.which == 3){
e.preventDefault();
console.log(e);
}
}
});
}
/***********************************************************************
* 右键菜单(稍后优化)
************************************************************************/
File.prototype.showContextMenu = function(e,index,data){
var that = this;
$("#file-contextMenu").menu({
onClick:function(item){
console.log(item);
switch(item.id){
case 'addFile':
that.beforeAdd();
break;
case 'down':
that.download();
break;
case 'rename':
Current.editIndex = index; //更新正在编辑的行数
Current.act = 'edit';
Grid.datagrid('beginEdit',Current.editIndex);
break;
case 'remove':
that.del();
break;
default:
console.log('error');
}
}
}).menu('show',{
left:e.clientX,
top:e.clientY
});
}
/***********************************************************************
* 工具类(稍后版本封装)
************************************************************************/
//获取后缀对应的图标信息
File.prototype.getSuffixIcon = function(type){
var icon ='unknow';
var suffix = {
rar:['rar','zip','tar','gz','7z','tar.gz'],
image:['jpg','png','gif','jpeg'],
doc:['doc','docx'],
xls:['csv','xls'],
folder:['folder'],
txt:['txt']
}
for(var k in suffix){
if($.inArray(type, suffix[k]) > -1){
icon = k;
break;
}
}
return icon;
}
//格式化时间
File.prototype.getFormatDate = function(timestamp){
var date = new Date(timestamp * 1000);
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var hour = date.getHours().toString();
var minute= date.getMinutes().toString();
var second= date.getSeconds().toString();
hour = hour.length < 2 ? '0'+ hour : hour;
minute = minute.length < 2 ? '0'+ minute : minute;
second = second.length < 2 ? '0'+ second : second;
return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
}
//格式化文件大小
File.prototype.getFormatFileSize = function(size){
var formatSize ;
if(!size){
formatSize = '';
}
else if(size < 1024*1024){
formatSize = (size/1024).toFixed(2) + 'KB';
}
else{
formatSize = (size/1024/1024).toFixed(2) +'MB';
}
return formatSize;
}
/***********************************************************************
* 新建文件夹
************************************************************************/
File.prototype.beforeAdd = function(){
Current.act = 'add';
var rowData = {
id:'',
type:'folder',
name:'新建文件夹',
ctime:parseInt((new Date()).valueOf()/1000),
im:Meta.uid,
username:Meta.uname,
act:'',
pid:Current.fid,
}
Grid.datagrid('appendRow',rowData);
Current.editIndex = Grid.datagrid('getRows').length - 1;
Grid.datagrid('unselectAll').datagrid('selectRow',Current.editIndex);
Grid.datagrid('beginEdit', Current.editIndex);
}
File.prototype.add= function(rowIndex, rowData, changes){
var that = this;
var target_id = rowData.pid;
var data = rowData;
var url = Basepath + '/saveData';
var fn = function(result){
if(result.status == 1){
Grid.datagrid('updateRow',{
index: rowIndex,
row:{
id: result.data.id
}
});
}
else{
alert(result.msg);
Grid.datagrid('beginEdit',editIndex);
}
}
that.action(target_id, 'add', data, url, fn);
}
/***********************************************************************
* 文件夹重命名
************************************************************************/
File.prototype.rename= function(rowIndex, rowData, changes){
var target_id = rowData.id;
var url = Basepath + '/saveData';
var fn = function(data){
if(data.status == 1){
console.log('success');
}
else{
alert(data.msg);
}
}
this.action(target_id, 'edit', rowData , url, fn);
}
// 当编辑框失去焦点时触发
File.prototype.rowRename = function(){
Grid.datagrid('endEdit', Current.editIndex);
}
/***********************************************************************
* 展开目录
*
* 当初始化和点击树时,获取正确的目录id,并展开树的节点到此目录,并且在
* 右侧加载文件列表
************************************************************************/
File.prototype.beforeExpand = function(pid){
var that = this;
var trueNode;
var node = $('#tree').tree('find', pid);
var childNodes = $('#tree').tree('getChildren',node.target);
if(childNodes.length){
$.each(childNodes,function(){
if(this.attributes.is_share === "1"){
var parentNode = $('#tree').tree('getParent',this.target);
if(parentNode.id === node.id){
trueNode = this;
}
}
})
}
else {
trueNode = node;
}
Current.fid = trueNode.id;
$('#tree').tree('expandTo',trueNode.target).tree('select', trueNode.target);
that.expand(Current.fid);
}
File.prototype.expand= function(target_id){
var that = this;
Current.fid = target_id; //更新当前展开目录的id
Current.act = 'scan';
var url = Basepath + '/file';
var fn = function(data){
// console.log(data);
if(data.status == 1){
Grid.datagrid('loadData',data.data||[]);
// 判断是否需要显示“上传提示”
if(data.data){
$("#file-data-grid-bg").hide();
}else{
$("#file-data-grid-bg").show();
}
}
else{
alert(data.msg);
}
}
that.action(Current.fid, Current.act, {}, url, fn);
}
/***********************************************************************
* 上传文件
*
* 主要作用打开上传窗口,然后利用handler-file-2.0.js里面的接口
************************************************************************/
File.prototype.upload= function(){
$('#file-up-dialog').show().dialog({
title: '文件上传',
width: 415,
height: 300,
href: Basepath + '/fileUp',
closed: false,
draggable:true,
cache: true,
modal: true,
left: 125,
top:70
});
}
/***********************************************************************
* 下载文件
*
************************************************************************/
File.prototype.download= function(){
var that = this;
var checkSum = Grid.datagrid('getChecked');
if(checkSum.length <=0){
Tools.error('您还没有选中!');
return;
}
if(checkSum.length > 1){
Tools.error('一次只能下载一个文件!');
return;
}
var data = checkSum[0];
if(data.type == 'folder'){
Tools.alert('暂不支持文件夹下载!');
return;
}
var _url = Basepath +'/down?fid=' + data.file_id+'&fname='+data.file_name;
window.location.href = encodeURI(_url);
}
/***********************************************************************
* 删除文件或文件夹
************************************************************************/
File.prototype.beforeDel= function(id, act){
var that = this;
var checkSum = Grid.datagrid('getChecked');
if(checkSum.length <=0){
Tools.error('您还没有选中!');
return;
}
var tipsMsg = '只能删除空文件夹或文件,确定删除吗?';
$.messager.confirm('删除提示', tipsMsg, function(r){
if(r){
var files = [];
$.each(checkSum,function(){
files.push(this.id);
});
console.log(files.join(','));
File.prototype.del(files.join(','));
}else{
return null;
}
})
}
File.prototype.del = function(target_id){
var that = this;
var data = {
id:target_id
}
var url = Basepath + '/delData';
var fn = function(result){
console.log(result);
if(result.status == 1){
//Grid.datagrid('reload',{});
that.refresh();
console.log('reload...');
}
else{
alert(result.msg);
}
}
that.action(target_id,'del', data, url, fn);
}
/***********************************************************************
* 移动文件或文件夹
************************************************************************/
File.prototype.move= function(){
}
/***********************************************************************
* 统一规范和处理对服务器的请求
************************************************************************/
File.prototype.action = function(target_id, act, data, url, successHandle){
var that = this;
$.ajax({
url:url + '?target_id=' + target_id +'&act=' +act,
data:data,
type:"POST",
dataType:'json',
success:successHandle,
beforeSend:function(){
Grid.datagrid('loading');
console.log(act + '------------begin');
},
error:function(){
console.log(act + '------------error');
},
complete:function(){
console.log(act + '------------complete');
Grid.datagrid('loaded');
}
});
}
/***********************************************************************
* 刷新文件列表
************************************************************************/
File.prototype.refresh = function(){
File.prototype.expand(Current.fid);
}
/***********************************************************************
* 导出一些属性供其他文件使用
************************************************************************/
File.Grid = Grid;
File.Meta = Meta;
File.Current = Current;
window.File = File;
})(window, jQuery, eo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment