import { pathIcon } from "@src/explorer/model/pathIcon";
export default {
/**
* 动态数据处理;
* 同一用户相邻事件相同,间隔在60s内则归类到同一分组;
*
* create 创建文件;//mkdir,mkfile,upload,copy
* edit 更新文件 (编辑or上传新版本)
* move 文档移动
* moveOut 子文档移走
* share 分享动态 shareLinkAdd,shareToAdd,shareLinkRemove,shareToRemove,shareEdit
* rename 重命名 desc.from => desc.to;
* recycle 回收站事件 toRecycle,restore;
* remove 删除; sourceID是父目录;文档名--desc.content;
*
* addDesc 修改描述说明; desc.content
* addComment 添加评论 desc.content
*/
dataParseMain:function(pathInfo,list){
// 对相同事件数据进行归类;
var result = [];
var groupMinSeconds = 60*30; // 两个相同事件归类的时间间隔; N分钟;
if(!list) return result;
for(var i = 0; i < list.length; i++) {
var last = list[i];
if(!last) continue;
if(!this.checkLogItem(last)) continue;
var group = [last];
for (var j = i+1; j < list.length; j++) {
if(!list[j]) continue;
if(Math.abs(last.createTime - list[j].createTime) > groupMinSeconds) break;// 不再向后查找;
if(this.isSameEvent(pathInfo,list[j],last)){
group.push(list[j]);
list[j] = false;
}
}
result.push(this.dataParseGroup(pathInfo,group));
}
return _.filter(result);
},
isSameEvent:function(pathInfo,item,last){
if(last.userID != item.userID) return false;
if(this.dataActionGet(last) != this.dataActionGet(item)) return false;
// 删除多个时间间隔很短则归类;
var allowSame = 'remove,rename,create,edit,moveOut,addDesc'.split(','); //'remove,rename';
var sameParent = _.get(item,'parentInfo.sourceID') == _.get(last,'parentInfo.sourceID');
if( _.includes(allowSame,item.type) && sameParent) return true;
if( item.sourceID == last.sourceID) return true;// 自身;
if(!_.isEqual(last.desc,item.desc)) return false;
return true;
},
// 检测过滤日志;
checkLogItem:function(item,pathInfo){
if( item.type == 'create' && _.isObject(item.sourceInfo) &&
item.sourceInfo.type == 'file' &&
Math.abs(item.createTime - item.sourceInfo.createdTime) > 5 ){
return false; // 新建文件之后,再次创建上传忽略上传事件
}
return true;
},
dataActionGet:function(item){
var action = item.type;
if(_.includes(['share','recycle'],action)){
action += '-'+item.desc.content;
if(!item.desc.content) return false;
}else if(action == 'create'){
var type = _.get(item,'desc.createType');
if(!type){ //数据不存在;
type = item.sourceInfo.type == 'file' ? 'mkfile':'mkdir';
}
action += '-'+type;
}
return action;
},
dataParseGroup:function(pathInfo,group){
if(!this.descMap){this.descMap = this.descMapMake();}
var desc = [], item = group[0];
var action = this.dataActionGet(item);
var map = this.descMap.map;
var sourceID = pathInfo.sourceID;
if(group.length == 1){
desc = map[action+'-item'];
if(sourceID == item.sourceID){
desc = map[action];
}else if(map[action+'-current'] && sourceID == item.sourceParent ){
desc = map[action+'-current'];
}
if(action == 'edit'){// 文件编辑来源.
desc = $.objClone(desc);
desc[0] = desc[0]+'{{fromDevice}}';
}
}else{
desc = map[action+'-more'];
if( map[action+'-more-at'] & sourceID && sourceID != item.sourceParent){
desc = map[action+'-more-at'];
}
// 文件文件夹自身多次同类型操作(修改描述,重命名,编辑保存...)
if( map[action+'-more-self'] && sourceID == item.sourceID ){
desc = map[action+'-more-self'];
}
}
var itemDesc = this.dataParseHtml(desc,item,group);
if(!itemDesc.title) return false;
this.descSelfAddCout(pathInfo,action,item,itemDesc,group);
this.descModifyTimeLocal(pathInfo,action,item,itemDesc,group);
return {
user:item.userInfo,
time:item.createTime,
type:item.type,
action:action,
actionTitle:this.descMap.descTitle[action]||'',
descEvent:itemDesc.title,
descContent:itemDesc.content,
group:group,
};
},
descModifyTimeLocal:function(pathInfo,action,item,itemDesc,group){
if(action != 'create-upload' || !pathInfo.metaInfo || !pathInfo.metaInfo.modifyTimeLocal) return;
var localTime = dateShow(pathInfo.metaInfo.modifyTimeLocal,'timeMinute');
itemDesc.title += '<div class="opacity-60">'+LNG['explorer.history.lastModify'] +': ' + localTime +'</div>';
},
descSelfAddCout:function(pathInfo,action,item,itemDesc,group){
if(pathInfo.type == 'file'){
itemDesc.title = itemDesc.title.replace(/文档/g,'文件');
itemDesc.content = itemDesc.content.replace(/文档/g,'文件');
}
if(pathInfo.sourceID != item.sourceID || group.length <= 1) return;
var time = G.lang.indexOf('zh') != -1 ? '次':'';
itemDesc.title += ' <span class="opacity-60">['+ group.length +time+']</span>';
},
dataParseHtml:function(desc,item,group){
var itemDesc = {title:'',content:''};
if(!desc || !_.isArray(desc)) return itemDesc;
itemDesc.title = this.dataParseHtmlItem(desc[0],item,group);
if(desc.length == 2 && desc[1]){
itemDesc.content = this.dataParseHtmlItem(desc[1],item,group);
}
if(desc.length == 3){
var content = '',showMax = 30;
this._each(group,function(theItem,i){
var html = this.dataParseHtmlItem(desc[1],theItem,group);
if(!html) return;
if(i == showMax){
content += "<div class='group-item'>"+group.length+LNG['common.item']+"</div>";
}
if(i >= showMax){return;}// 过多内容则不再显示;
var timeInfo = LNG['common.operateTime'] + ': '+dateFormat(item.createTime,'H:i:s');
var title = 'title="'+htmlEncode(timeInfo)+'" title-timeout="100"';
content += "<div class='group-item' "+title+">"+html+'</div>';
});
if(content && group.length > 4){
content = ` <div class="group-item-more">${content}</div>
<div class="toggle-item toggle-show ripple-item">${LNG['common.showMore']}</div>
<div class="toggle-item toggle-hide ripple-item hidden">${LNG['common.showLess']}</div>`;
}
itemDesc.content = content;
}
return itemDesc;
},
dataParseHtmlItem:function(text,item,group){
if(_.includes(text,'{{file}}')){
var itemInfo = item.sourceInfo || ((item.desc && item.desc.name) || item.pathName || item.path || (LNG['common.unknow']+LNG['common.file']));
var html = this.makeHtmlFile(itemInfo);
text = text.replace(/{{file}}/g,html);
}
if(_.includes(text,'{{parent}}')){
var itemInfo = item.parentInfo || LNG['common.unknow']+LNG['common.folder'];
var html = this.makeHtmlFile(itemInfo);
text = text.replace(/{{parent}}/g,html);
}
if(_.includes(text,'{{count}}')){
var html = '<span class="item-count">'+group.length+'</span>';
text = text.replace(/{{count}}/g,html);
}
if(_.includes(text,'{{timeShow}}')){
var icon = '<i class="font-icon ri-time-line"></i>';
var html = '<span class="item-time opacity-60">'+icon+dateFormat(item.createTime,'H:i:s')+'</span> ';
text = text.replace(/{{timeShow}}/g,html);
}
if(_.includes(text,'{{fromDevice}}')){
text = text.replace(/{{fromDevice}}/g,this.descEditEventParse(item));
}
if(_.includes(text,'{{desc.from}}')){
var itemInfo = item.desc.from || item.desc.fromName || LNG['common.unknow'];
if(_.isString(item.desc.from)){
var html = htmlEncode(item.desc.from);
}else{
var html = this.makeHtmlFile(itemInfo);
}
text = text.replace(/{{desc\.from}}/g,html);
}
if(_.includes(text,'{{desc.to}}')){
var itemInfo = item.desc.to || item.desc.toName || LNG['common.unknow'];
if(_.isString(item.desc.to)){
var html = htmlEncode(item.desc.to);
}else{
var html = this.makeHtmlFile(itemInfo);
}
text = text.replace(/{{desc\.to}}/g,html);
}
if(_.includes(text,'{{desc.sourceID}}')){
var itemInfo = item.desc.name || item.desc.sourceID;
var html = this.makeHtmlFile(itemInfo);
text = text.replace(/{{desc\.sourceID}}/g,html);
}
if(_.includes(text,'{{desc.content}}')){
var content = _.get(item,'desc.content') || _.get(item,'desc.name');
if(!content) return '';
var html = '<span class="item-content">'+htmlEncode(content)+'</span>';
text = text.replace(/{{desc\.content}}/g,html);
}
return text;
},
/**
* 文件编辑
* 记录编辑来源: [webdav,client-pc,online]编辑;历史版本回退; 粘贴覆盖;上传覆盖;
*/
descEditEventParse: function(item){
// if(item.type != 'edit') return '';
if(!_.get(item,'desc.ua')) return '';
var desc = item.desc;
var editBy = LNG['common.online'];
desc.ua = desc.ua.toLowerCase();
desc.action = desc.action.toLowerCase();
if( _.includes(desc.action,'plugin.webdav') ){
editBy = 'webdav';
}
// PC客户端,本地软件编辑; [axios/0.21.1], [axios/0.21.1, kodcloud]
if( desc.ua.indexOf('axios/') === 0 &&
_.includes(desc.action,'fileupload') ){
editBy = LNG['explorer.toolbar.client'];
}
editBy = editBy + LNG.space +LNG['common.edit'];
// 客户端在线编辑;
if( _.includes(desc.ua,' kodcloud/') &&
_.includes(desc.ua,' electron/') &&
_.includes(desc.action,'filesave') ){
//editBy = LNG['common.online'] + LNG.space +LNG['common.edit']+'(PC)';//PC客户端在线编辑;
}
// 粘贴覆盖;
if( _.includes(desc.action,'pathpast') ){
editBy = LNG['explorer.past']+LNG.space+LNG['common.cover'];
}
// 历史版本切换
if( desc.action.toLowerCase() == 'explorer.history.rollback' ){
editBy = LNG['explorer.history.changeEvent'];
}
//上传新版本;
if( _.includes(desc.ua,'mozilla/') &&
_.includes(desc.action,'fileupload') ){
editBy = LNG['explorer.history.uploadNew'];
}
var title = 'title="'+htmlEncode(desc.ua)+'" title-timeout="200"';
return '<span class="edit-by" '+title+'>('+editBy+")</span>";
},
makeHtmlFile:function(pathInfo){
if(!pathInfo || _.isString(pathInfo)) return '<span class="data-path-none">['+(pathInfo ||'')+']</span>';
var title = htmlEncode(pathInfo.pathDisplay || pathInfo.name);
var name = htmlEncode(pathInfo.name);
return `<span class='data-path ripple-item'
file-path="${pathInfo.path}"
file-name="${name}"
file-parent="${pathInfo.parentID}"
file-type="${pathInfo.type}"
title="${title}" title-timeout="200">
<span class='file-icon'>`+pathIcon(pathInfo)+`</span>
<span class="name">${name}</span>
</span>`;
},
};