AttachMent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * 附件管理。
  3. * @returns {AttachMent}
  4. */
  5. if (typeof AttachMent == 'undefined') {
  6. AttachMent = {};
  7. }
  8. /**
  9. * 选择非直接上传附件时判断用flash还是html
  10. */
  11. AttachMent.addFile=function(obj){
  12. AttachMent.htmlUpLoadFile(obj);
  13. }
  14. /**
  15. * 选择直接上传附件时判断用flash还是html
  16. */
  17. AttachMent.directUpLoadFile=function(obj){
  18. AttachMent.htmlUpLoadFile(obj);
  19. }
  20. /**
  21. * flash附件非直接上传
  22. */
  23. AttachMent.FlexAddFile=function(obj){
  24. var inputObj=$(obj);
  25. var fieldName=inputObj.attr("field");
  26. var parent=inputObj.parent().parent();
  27. var rights="w";
  28. var divName="div.attachement";
  29. var inputName="input[name='" +fieldName +"'],textarea[name='" +fieldName +"']";
  30. //获取div对象。
  31. var divObj=$(divName,parent);
  32. var inputJson=$(inputName,parent);
  33. var aryJson=AttachMent.getFileJsonArray(divObj);
  34. //文件选择器
  35. FlexUploadDialog({isSingle:false,callback:function (fileIds,fileNames,filePaths,extPaths){
  36. if(fileIds==undefined || fileIds=="") return ;
  37. var aryFileId=fileIds.split(",");
  38. var aryName=fileNames.split(",");
  39. var aryExtPath=extPaths.split(",");
  40. for(var i=0;i<aryFileId.length;i++){
  41. var name=aryName[i];
  42. AttachMent.addJson(aryFileId[i],name,aryJson);
  43. }
  44. //获取json
  45. var json=JSON2.stringify(aryJson);
  46. var html=AttachMent.getHtml(aryJson,rights);
  47. divObj.empty();
  48. divObj.append($(html));
  49. inputJson.text(json);
  50. if(typeof CustomForm != "undefined"){
  51. CustomForm.validate();
  52. }
  53. }});
  54. };
  55. /**
  56. * 直接附件上传
  57. */
  58. AttachMent.directUpLoad=function(obj){
  59. var inputObj=$(obj);
  60. var fieldName=inputObj.attr("field");
  61. var parent=inputObj.parent().parent();
  62. var rights="w";
  63. var divName="div.attachement";
  64. var inputName="input[name='" +fieldName +"'],textarea[name='" +fieldName +"']";
  65. //获取div对象。
  66. var divObj=$(divName,parent);
  67. var inputJson=$(inputName,parent);
  68. var aryJson=AttachMent.getFileJsonArray(divObj);
  69. //文件上传
  70. DirectUploadDialog({callback:function (attachs){
  71. if(attachs==undefined || attachs==[]) return ;
  72. for(var i=0;i<attachs.length;i++){
  73. var fileId=attachs[i].fileId;
  74. var name=attachs[i].fileName;
  75. AttachMent.addJson(fileId,name,aryJson);
  76. }
  77. //获取json
  78. var json=JSON2.stringify(aryJson);
  79. var html=AttachMent.getHtml(aryJson,rights);
  80. divObj.empty();
  81. divObj.append($(html));
  82. inputJson.val(json);
  83. if(typeof CustomForm != "undefined"){
  84. CustomForm.validate();
  85. }
  86. }});
  87. };
  88. /**
  89. * html附件上传dialog
  90. * 附件上传后回显方法,如果需要更改附件 只需要更改此方法的JSON
  91. * @param conf
  92. */
  93. AttachMent.htmlUpLoadFile=function(obj){
  94. var inputObj=$(obj);
  95. var fieldName=inputObj.attr("field");
  96. var parent=inputObj.parent().parent();
  97. var divName="div.attachement";
  98. var rights="w";
  99. var inputName="input[name='" +fieldName +"'],textarea[name='" +fieldName +"']";
  100. //获取div对象。
  101. var divObj=$(divName,parent);
  102. var inputJson=inputObj.prev();//获取到textarea对象
  103. var aryJson=AttachMent.getFileJsonArray(divObj);
  104. //文件选择器
  105. HtmlUploadDialog({max:30,callback:function (attachs){
  106. if(attachs==undefined || attachs==[]) return ;
  107. for(var i=0;i<attachs.length;i++){
  108. var fileId=attachs[i].fileId;
  109. var name=attachs[i].fileName;
  110. AttachMent.addJson(fileId,name,aryJson);
  111. }
  112. var json=JSON2.stringify(aryJson);
  113. var html=AttachMent.getHtml(aryJson,rights);
  114. divObj.empty();
  115. divObj.append($(html));
  116. inputJson.val(json);
  117. inputJson.text(json);
  118. if(typeof CustomForm != "undefined"){
  119. CustomForm.validate();
  120. }
  121. }});
  122. };
  123. /**
  124. * 删除附件
  125. * @param obj 删除按钮。
  126. */
  127. AttachMent.delFile=function(obj){
  128. var inputObj=$(obj);
  129. var parent=inputObj.parent();
  130. var divObj=parent.parent().parent().parent();
  131. var spanObj=$("span[name='attach']",parent);
  132. var divContainer=divObj.parent();
  133. var fileId=spanObj.attr("fileId");
  134. var aryJson=AttachMent.getFileJsonArray(divObj);
  135. AttachMent.delJson(fileId,aryJson);
  136. var json=JSON2.stringify(aryJson);
  137. var inputJsonObj=$("textarea",divContainer);
  138. if(aryJson.length == 0)
  139. json = "";
  140. //设置json
  141. inputJsonObj.val(json);
  142. //删除span
  143. parent.remove();
  144. if(typeof CustomForm != "undefined"){
  145. CustomForm.validate();
  146. }
  147. };
  148. /**
  149. * 初始化表单的附件字段数据。
  150. */
  151. AttachMent.init=function(subRights,parent){
  152. $("[ctltype='attachment']").each(function(){
  153. var div=$('<div name="div_attachment_container"></div>');
  154. div.append('<div class="attachement"></div>');
  155. div.append('<textarea style="display:none" controltype="attachment" name="'+$(this).attr("name")+'" lablename="附件" validate="'+$(this).attr("validate")+'" validatable="'+$(this).attr("validatable")+'">'+this.value+'</textarea>');
  156. var onclick="AttachMent.addFile(this)";
  157. if($(this).attr("isdirectupload")=="1"){
  158. onclick="AttachMent.directUpLoadFile(this)";
  159. }
  160. div.append('<a href="javascript:;" field="'+$(this).attr("name")+'" class="link selectFile" atype="select" onclick="'+onclick+'">选择</a>');
  161. if(this.value==""||typeof(this.value)=="undefined"||this.value.indexOf("¥@@¥") != -1){
  162. $(this).after(div);
  163. $(this).remove();
  164. }
  165. });
  166. if( $.isEmpty(parent)){
  167. parent = $("div[name='div_attachment_container']");
  168. }
  169. parent.each(function(){
  170. var me=$(this),
  171. rights=me.attr("right");
  172. //如果没有权限属性,可能是子表中的附件
  173. if(!rights){
  174. rights=me.closest("[type='subtable']").attr("right");
  175. }
  176. //对于弹出框的处理
  177. if(!$.isEmpty(subRights))
  178. rights = subRights;
  179. if(rights){
  180. rights=rights.toLowerCase();
  181. }
  182. if(rights!="w" && rights!="r" && rights!="b"){
  183. //rights="w";
  184. }
  185. // else{
  186. if(rights=="r"){
  187. //$("a.attachement").remove();
  188. //$("a.selectFile").remove();
  189. $("a[field]",me).remove();
  190. }
  191. var atta =$("textarea[controltype='attachment']",me);
  192. var jsonStr = atta.val();
  193. if(!$.isEmpty(jsonStr)){
  194. jsonStr = jsonStr.replaceAll("¥@@¥","\"");
  195. atta.val(jsonStr);
  196. }
  197. var divAttachment=$("div.attachement",me);
  198. //json数据为空。
  199. AttachMent.insertHtml(divAttachment,jsonStr,rights);
  200. // }
  201. });
  202. };
  203. /**
  204. * 附件插入显示
  205. * @param {} div
  206. * @param {} jsonStr
  207. * @param {} rights 权限 如果不传,默认是r
  208. */
  209. AttachMent.insertHtml= function(div,jsonStr,rights){
  210. if($.isEmpty(jsonStr)) {
  211. div.empty();
  212. return ;
  213. }
  214. if($.isEmpty(rights)) rights ='r';
  215. var jsonObj=[];
  216. try {
  217. jsonStr = jsonStr.replaceAll("¥@@¥","\"");
  218. jsonObj = jQuery.parseJSON(jsonStr);
  219. } catch (e) {
  220. }
  221. var html=AttachMent.getHtml(jsonObj,rights);
  222. div.empty();
  223. div.append($(html));
  224. };
  225. /**
  226. * 获取文件的html。
  227. * @param aryJson
  228. * @returns {String}
  229. */
  230. AttachMent.getHtml=function(aryJson,rights){
  231. var str="";
  232. var template="";
  233. //onclick='AttachMent.handleClickItem(this,\"w\")'
  234. //onclick='AttachMent.handleClickItem(this,\"r\")'
  235. //path='#path#' <a href='javascript:;' onclick='AttachMent.download(this);' title='下载' class='download'></a>
  236. //<a href='javascript:;' onclick='AttachMent.download(this);' title='下载' class='download'></a>
  237. var templateW="<li style='margin-bottom: 10px;margin-top: 10px;'><span class='attachement-span'><span fileId='#fileId#' name='attach' file='#file#' ><a href='javascript:;' id='tt' onclick='AttachMent.download(this);' class='attachment'>#name#</a></span>&nbsp;<a href='javascript:;' onclick='AttachMent.delFile(this);' class='cancel'></a>#preview#</span></li>";
  238. var templateR="<li style='margin-bottom: 10px;margin-top: 10px;'><span class='attachement-span'><span fileId='#fileId#' name='attach' file='#file#' ><a class='attachment' target='_blank' href='javascript:;' onclick='AttachMent.download(this);' title='#title#'>#name#</a></span>#preview#</span></li>";
  239. if(rights=="w"){
  240. template=templateW;
  241. }
  242. else{
  243. template=templateR;
  244. }
  245. for(var i=0;i<aryJson.length;i++){
  246. var obj=aryJson[i];
  247. var id=obj.id;
  248. var name=obj.name;
  249. var path =__ctx +"/platform/system/sysFile/file_" +obj.id+ ".ht";
  250. var file=id +"," + name ;
  251. //PDF预览新增代码start 李猛ADD
  252. var tmp="";
  253. if(typeof(name)!="undefined"&&name.indexOf(".pdf")>0){
  254. tmp=template.replace("#file#",file).replace("#preview#","<a href='#path#' style='width:14px;height:14px;margin-left:8px;' class='link search' target='_blank'>预览</a>").replace("#path#",path).replace("#name#", AttachMent.parseName(name)).replace("#title#",name).replace("#fileId#", id);
  255. }else{
  256. tmp=template.replace("#file#",file).replace("#preview#","").replace("#path#",path).replace("#name#", AttachMent.parseName(name)).replace("#title#",name).replace("#fileId#", id);
  257. }
  258. //PDF预览新增代码end 李猛ADD
  259. //附件如果是图片就显示到后面
  260. str+=tmp;
  261. }
  262. console.log(str);
  263. str = "<ul>"+str+"</ul>"; /*改为一个附件就占领一行*/
  264. return str;
  265. };
  266. AttachMent.parseName = function(name){
  267. if(name.length >30)
  268. return name.substr(0,30)+"..."; /*暂时去掉截取字段*/
  269. return name;
  270. }
  271. /**
  272. * 添加json。
  273. * @param fileId
  274. * @param name
  275. * @param path
  276. * @param aryJson
  277. */
  278. AttachMent.addJson=function(fileId,name,aryJson){
  279. var rtn=AttachMent.isFileExist(aryJson,fileId);
  280. if(!rtn){
  281. var obj={id:fileId,name:name};
  282. aryJson.push(obj);
  283. }
  284. };
  285. /**
  286. * 删除json。
  287. * @param fileId 文件ID。
  288. * @param aryJson 文件的JSON。
  289. */
  290. AttachMent.delJson=function(fileId,aryJson){
  291. for(var i=aryJson.length-1;i>=0;i--){
  292. var obj=aryJson[i];
  293. if(obj.id==fileId){
  294. aryJson.splice(i,1);
  295. }
  296. }
  297. };
  298. /**
  299. * 判断文件是否存在。
  300. * @param aryJson
  301. * @param fileId
  302. * @returns {Boolean}
  303. */
  304. AttachMent.isFileExist=function(aryJson,fileId){
  305. for(var i=0;i<aryJson.length;i++){
  306. var obj=aryJson[i];
  307. if(obj.id==fileId){
  308. return true;
  309. }
  310. }
  311. return false;
  312. };
  313. /**
  314. * 取得文件json数组。
  315. * @param divObj
  316. * @returns {Array}
  317. */
  318. AttachMent.getFileJsonArray=function(divObj){
  319. var aryJson=[];
  320. var arySpan=$("span[name='attach']",divObj);
  321. arySpan.each(function(i){
  322. var obj=$(this);
  323. var file=obj.attr("file");
  324. var aryFile=file.split(",");
  325. var obj={id:aryFile[0],name:aryFile[1]};
  326. aryJson.push(obj);
  327. });
  328. return aryJson;
  329. };
  330. AttachMent.handleClickItem = function(obj,rights){
  331. var _this = $(obj);
  332. var span = _this.closest("span");
  333. var fileId = span.attr("fileId");
  334. var url =__ctx+"/platform/system/sysFile/getJson.ht";
  335. var sysFile;
  336. $.ajax({
  337. url:url,
  338. data:{
  339. fileId:fileId
  340. },
  341. success:function(data){
  342. if(typeof(data)=="string"){
  343. $.ligerDialog.error('系统超时请重新登录!','提示');
  344. return ;
  345. }
  346. if(data.status!=1){
  347. $.ligerDialog.error(data.msg,'提示');
  348. }else{
  349. sysFile = data.sysFile;
  350. var path = _this.attr("path");
  351. if(/(doc)|(docx)|(xls)|(xlsx)|(ppt)|(pptx)/ig.test(sysFile.ext)){
  352. var h=screen.availHeight-35;
  353. var w=screen.availWidth-5;
  354. var vars="top=0,left=0,height="+h+",width="+w+",status=no,toolbar=no,menubar=no,location=no,resizable=1,scrollbars=1";
  355. var showUrl = __ctx+"/platform/system/sysFile/office.ht?fileId=" + fileId;
  356. if(/(doc)|(docx)/ig.test(sysFile.ext)){//word才支持在线编辑
  357. if(rights=='w'){
  358. showUrl+="&rights=w";
  359. }
  360. }else{
  361. showUrl+="&rights=r";
  362. }
  363. window.open(showUrl,"myWindow",vars);
  364. }else{
  365. window.open(path,'_blank');
  366. }
  367. }
  368. }
  369. });
  370. };
  371. /**
  372. * 下载
  373. */
  374. AttachMent.download = function(obj){
  375. var me = $(obj);
  376. //var span = me.siblings("span"); 初始代码
  377. //新增代码开始
  378. var span;
  379. var clas = me.attr("class");
  380. if(clas=="download"){
  381. span=me.siblings("span");
  382. }else if(clas=="attachment"){
  383. span=me.parent("span");
  384. }
  385. //新增代码结束
  386. if(span.length >0)
  387. var fileId = span.attr("fileId");
  388. var path =__ctx+"/platform/system/sysFile/file_"+fileId+".ht?download=true";
  389. window.open(path,'_blank');
  390. }