Attachment.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. MWF.xDesktop.requireApp("process.Xform", "Attachment", null, false);
  2. //MWF.xDesktop.requireApp("cms.FormDesigner", "widget.AttachmentController", null, false);
  3. MWF.xApplication.cms.Xform.AttachmentController = new Class({
  4. Extends: MWF.xApplication.process.Xform.AttachmentController,
  5. "options": {
  6. "checkTextEnable" : false
  7. },
  8. openInOfficeControl: function(att, office){
  9. if (office){
  10. if (!office.openedAttachment || office.openedAttachment.id!==att.id){
  11. office.save();
  12. MWF.Actions.get("x_cms_assemble_control").getAttachmentUrl(att.id, this.module.form.businessData.document.id, function(url){
  13. office.openedAttachment = {"id": att.id, "site": this.module.json.name, "name": att.name};
  14. office.officeOCX.BeginOpenFromURL(url, true, this.readonly);
  15. }.bind(this));
  16. }
  17. }
  18. },
  19. setAttachmentConfig: function(readInput, editInput, controllerInput){
  20. if (this.selectedAttachments.length){
  21. var readList = readInput.retrieve("data-value");
  22. var editList = editInput.retrieve("data-value");
  23. var controllerList = controllerInput.retrieve("data-value");
  24. var readUnitList = [];
  25. var readIdentityList = [];
  26. var editUnitList = [];
  27. var editIdentityList = [];
  28. var controllerUnitList = [];
  29. var controllerIdentityList = [];
  30. if (readList){
  31. readList.each(function(v){
  32. var vName = (typeOf(v)==="string") ? v : v.distinguishedName;
  33. var len = vName.length;
  34. var flag = vName.substring(len-1,len);
  35. if (flag==="U") readUnitList.push(vName);
  36. if (flag==="I") readIdentityList.push(vName);
  37. });
  38. }
  39. if (editList){
  40. editList.each(function(v){
  41. var vName = (typeOf(v)==="string") ? v : v.distinguishedName;
  42. var len = vName.length;
  43. var flag = vName.substring(len-1,len);
  44. if (flag==="U") editUnitList.push(vName);
  45. if (flag==="I") editIdentityList.push(vName);
  46. });
  47. }
  48. if (controllerList){
  49. controllerList.each(function(v){
  50. var vName = (typeOf(v)==="string") ? v : v.distinguishedName;
  51. var len = vName.length;
  52. var flag = vName.substring(len-1,len);
  53. if (flag==="U") controllerUnitList.push(vName);
  54. if (flag==="I") controllerIdentityList.push(vName);
  55. });
  56. }
  57. this.selectedAttachments.each(function(att){
  58. att.data.readUnitList = readUnitList;
  59. att.data.readIdentityList = readIdentityList;
  60. att.data.editUnitList = editUnitList;
  61. att.data.editIdentityList = editIdentityList;
  62. att.data.controllerUnitList = controllerUnitList;
  63. att.data.controllerIdentityList = controllerIdentityList;
  64. o2.Actions.get("x_cms_assemble_control").configAttachment(att.data.id, this.module.form.businessData.document.id, att.data);
  65. }.bind(this));
  66. }
  67. }
  68. });
  69. MWF.xApplication.cms.Xform.Attachment = MWF.CMSAttachment = new Class({
  70. Extends: MWF.APPAttachment,
  71. loadAttachmentController: function(){
  72. //MWF.require("MWF.widget.AttachmentController", function() {
  73. var options = {
  74. "style" : this.json.style || "default",
  75. "title": "附件区域",
  76. "listStyle": this.json.listStyle || "icon",
  77. "size": this.json.size || "max",
  78. "resize": (this.json.resize ==="y" || this.json.resize==="true"),
  79. "attachmentCount": this.json.attachmentCount || 0,
  80. "isUpload": (this.json.isUpload==="y" || this.json.isUpload==="true"),
  81. "isDelete": (this.json.isDelete==="y" || this.json.isDelete==="true"),
  82. "isReplace": (this.json.isReplace==="y" || this.json.isReplace==="true"),
  83. "isDownload": (this.json.isDownload==="y" || this.json.isDownload==="true"),
  84. "isSizeChange": (this.json.isSizeChange==="y" || this.json.isSizeChange==="true"),
  85. "readonly": (this.json.readonly==="y" || this.json.readonly==="true"),
  86. "availableListStyles" : this.json.availableListStyles ? this.json.availableListStyles : ["list","seq","icon","preview"],
  87. "isDeleteOption": this.json.isDelete,
  88. "isReplaceOption": this.json.isReplace,
  89. "toolbarGroupHidden" : this.json.toolbarGroupHidden || []
  90. //"downloadEvent" : this.json.downloadEvent
  91. };
  92. if (this.readonly) options.readonly = true;
  93. this.attachmentController = new MWF.xApplication.cms.Xform.AttachmentController(this.node, this, options);
  94. this.attachmentController.load();
  95. this.form.businessData.attachmentList.each(function (att) {
  96. if (att.site==this.json.id) this.attachmentController.addAttachment(att);
  97. //if (att.fileType.toLowerCase()==this.json.id.toLowerCase()) this.attachmentController.addAttachment(att);
  98. }.bind(this));
  99. //}.bind(this));
  100. },
  101. loadAttachmentSelecter: function( option, callback ){
  102. MWF.require("MWF.widget.AttachmentSelector", function() {
  103. var options = {
  104. //"style" : "cms",
  105. "title": "选择附件",
  106. "listStyle": "icon",
  107. "selectType" : "all",
  108. "size": "max",
  109. "attachmentCount": 0,
  110. "isUpload": true,
  111. "isDelete": true,
  112. "isReplace": true,
  113. "isDownload": true,
  114. "toBase64" : true,
  115. "base64MaxSize" : 800,
  116. "readonly": false
  117. };
  118. options = Object.merge( options, option );
  119. if (this.readonly) options.readonly = true;
  120. this.attachmentController = new MWF.widget.AttachmentSelector(this.node, this, options);
  121. this.attachmentController.load();
  122. this.postSelect = callback;
  123. this.form.businessData.attachmentList.each(function (att) {
  124. this.attachmentController.addAttachment(att);
  125. }.bind(this));
  126. }.bind(this));
  127. },
  128. selectAttachment: function(e, node, attachments){
  129. //if( attachments.length > 0 ){
  130. // this.form.documentAction.getAttachmentUrl(attachments[attachments.length-1].data.id, this.form.businessData.document.id, function(url){
  131. // if(this.postSelect)this.postSelect( url )
  132. // }.bind(this))
  133. //}
  134. if( attachments.length > 0 ){
  135. var data = attachments[attachments.length-1].data;
  136. this.form.documentAction.getAttachmentUrl( data.id, this.form.businessData.document.id, function(url){
  137. if( this.attachmentController.options.toBase64 ){
  138. this.form.documentAction.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  139. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  140. if(this.postSelect)this.postSelect( url , data, base64Code )
  141. }.bind(this) )
  142. }else{
  143. if(this.postSelect)this.postSelect( url , data )
  144. }
  145. }.bind(this))
  146. }
  147. },
  148. createUploadFileNode: function(){
  149. var accept = "*";
  150. if (!this.json.attachmentExtType || (this.json.attachmentExtType.indexOf("other")!=-1 && !this.json.attachmentExtOtherType)){
  151. }else{
  152. accepts = [];
  153. var otherType = this.json.attachmentExtOtherType;
  154. this.json.attachmentExtType.each(function(v){
  155. switch (v) {
  156. case "word":
  157. accepts.push(".doc, .docx, .dot, .dotx");
  158. break;
  159. case "excel":
  160. accepts.push(".xls, .xlsx, .xlsm, .xlt, .xltx");
  161. break;
  162. case "ppt":
  163. accepts.push(".pptx, .ppt, .pot, .potx, .potm");
  164. break;
  165. case "txt":
  166. accepts.push(".txt");
  167. break;
  168. case "pic":
  169. accepts.push(".bmp, .gif, .psd, .jpeg, .jpg");
  170. break;
  171. case "pdf":
  172. accepts.push(".pdf");
  173. break;
  174. case "zip":
  175. accepts.push(".zip, .rar");
  176. break;
  177. case "audio":
  178. accepts.push(".mp3, .wav, .wma, .wmv, .flac, .ape");
  179. break;
  180. case "video":
  181. accepts.push(".avi, .mkv, .mov, .ogg, .mp4, .mpeg");
  182. break;
  183. case "other":
  184. if (this.json.attachmentExtOtherType) accepts.push(this.json.attachmentExtOtherType);
  185. break;
  186. }
  187. });
  188. accept = accepts.join(", ");
  189. }
  190. var size = 0;
  191. if (this.json.attachmentSize) size = this.json.attachmentSize.toFloat();
  192. this.attachmentController.doUploadAttachment({"site": this.json.id}, this.form.documentAction.action, "uploadAttachment", {"id": this.form.businessData.document.id}, null, function(o){
  193. if (o.id){
  194. this.form.documentAction.getAttachment(o.id, this.form.businessData.document.id, function(json){
  195. if (json.data){
  196. if (!json.data.control) json.data.control={};
  197. this.attachmentController.addAttachment(json.data);
  198. this.form.businessData.attachmentList.push(json.data);
  199. }
  200. this.attachmentController.checkActions();
  201. this.fireEvent("upload", [json.data]);
  202. }.bind(this))
  203. }
  204. this.attachmentController.checkActions();
  205. }.bind(this), function(files){
  206. if (files.length){
  207. if ((files.length+this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount>0){
  208. var content = MWF.xApplication.cms.Xform.LP.uploadMore;
  209. content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  210. this.form.notice(content, "error");
  211. return false;
  212. }
  213. }
  214. return true;
  215. }.bind(this), true, accept, size);
  216. // this.uploadFileAreaNode = new Element("div");
  217. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  218. // this.uploadFileAreaNode.set("html", html);
  219. //
  220. // this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  221. // this.fileUploadNode.addEvent("change", function(){
  222. // this.validationMode();
  223. // var files = this.fileUploadNode.files;
  224. // if (files.length){
  225. // if ((files.length+this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount>0){
  226. // var content = MWF.xApplication.cms.Xform.LP.uploadMore;
  227. // content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  228. // this.form.notice(content, "error");
  229. // }else{
  230. // for (var i = 0; i < files.length; i++) {
  231. // var file = files.item(i);
  232. //
  233. // var formData = new FormData();
  234. // formData.append('file', file);
  235. // formData.append('site', this.json.id);
  236. // //formData.append('folder', folderId);
  237. //
  238. // this.form.documentAction.uploadAttachment(this.form.businessData.document.id ,function(o, text){
  239. // if (o.id){
  240. // this.form.documentAction.getAttachment(o.id, this.form.businessData.document.id, function(json){
  241. // if (json.data){
  242. // this.attachmentController.addAttachment(json.data);
  243. // this.form.businessData.attachmentList.push(json.data);
  244. // }
  245. // this.attachmentController.checkActions();
  246. //
  247. // this.fireEvent("upload", [json.data]);
  248. // }.bind(this))
  249. // }
  250. // this.attachmentController.checkActions();
  251. // }.bind(this), null, formData, file);
  252. // }
  253. // }
  254. // }
  255. // }.bind(this));
  256. },
  257. deleteAttachments: function(e, node, attachments){
  258. var names = [];
  259. attachments.each(function(attachment){
  260. names.push(attachment.data.name);
  261. }.bind(this));
  262. var _self = this;
  263. this.form.confirm("warn", e, MWF.xApplication.cms.Xform.LP.deleteAttachmentTitle, MWF.xApplication.cms.Xform.LP.deleteAttachment+"( "+names.join(", ")+" )", 300, 120, function(){
  264. while (attachments.length){
  265. var attachment = attachments.shift();
  266. _self.deleteAttachment(attachment);
  267. }
  268. this.close();
  269. }, function(){
  270. this.close();
  271. }, null, null, this.form.json.confirmStyle);
  272. },
  273. deleteAttachment: function(attachment){
  274. this.fireEvent("delete", [attachment.data]);
  275. this.form.documentAction.deleteAttachment(attachment.data.id, function(json ){
  276. this.attachmentController.removeAttachment(attachment);
  277. //this.form.businessData.attachmentList.erase( attachment.data )
  278. this.attachmentController.checkActions();
  279. if (this.form.officeList){
  280. this.form.officeList.each(function(office){
  281. if (office.openedAttachment){
  282. if (office.openedAttachment.id == id){
  283. office.loadOfficeEdit();
  284. }
  285. }
  286. }.bind(this));
  287. }
  288. this.fireEvent("afterDelete", [attachment.data]);
  289. }.bind(this));
  290. },
  291. createReplaceFileNode: function(attachment){
  292. var accept = "*";
  293. if (!this.json.attachmentExtType || this.json.attachmentExtType.indexOf("other")!=-1 && !this.json.attachmentExtOtherType){
  294. }else{
  295. accepts = [];
  296. var otherType = this.json.attachmentExtOtherType;
  297. this.json.attachmentExtType.each(function(v){
  298. switch (v) {
  299. case "word":
  300. accepts.push(".doc, .docx, .dot, .dotx");
  301. break;
  302. case "excel":
  303. accepts.push(".xls, .xlsx, .xlsm, .xlt, .xltx");
  304. break;
  305. case "ppt":
  306. accepts.push(".pptx, .ppt, .pot, .potx, .potm");
  307. break;
  308. case "txt":
  309. accepts.push(".txt");
  310. break;
  311. case "pic":
  312. accepts.push(".bmp, .gif, .psd, .jpeg, .jpg");
  313. break;
  314. case "pdf":
  315. accepts.push(".pdf");
  316. break;
  317. case "zip":
  318. accepts.push(".zip, .rar");
  319. break;
  320. case "audio":
  321. accepts.push(".mp3, .wav, .wma, .wmv, .flac, .ape");
  322. break;
  323. case "video":
  324. accepts.push(".avi, .mkv, .mov, .ogg, .mp4, .mpeg");
  325. break;
  326. case "other":
  327. if (this.json.attachmentExtOtherType) accepts.push(this.json.attachmentExtOtherType);
  328. break;
  329. }
  330. });
  331. accept = accepts.join(", ");
  332. }
  333. var size = 0;
  334. if (this.json.attachmentSize) size = this.json.attachmentSize.toFloat();
  335. this.attachmentController.doUploadAttachment({"site": this.json.id}, this.form.documentAction.action, "replaceAttachment",
  336. {"id": attachment.data.id, "documentid": this.form.businessData.document.id}, null, function(o){
  337. this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  338. attachment.data = json.data;
  339. attachment.reload();
  340. this.attachmentController.checkActions();
  341. }.bind(this))
  342. }.bind(this), null);
  343. // this.replaceFileAreaNode = new Element("div");
  344. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  345. // this.replaceFileAreaNode.set("html", html);
  346. //
  347. // this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  348. // this.fileReplaceNode.addEvent("change", function(){
  349. // var files = this.fileReplaceNode.files;
  350. // if (files.length){
  351. // for (var i = 0; i < files.length; i++) {
  352. // var file = files.item(i);
  353. //
  354. // var formData = new FormData();
  355. // formData.append('file', file);
  356. // // formData.append('site', this.json.id);
  357. //
  358. // this.form.documentAction.replaceAttachment(attachment.data.id, this.form.businessData.document.id ,function(o, text){
  359. // this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  360. // attachment.data = json.data;
  361. // attachment.reload();
  362. // this.attachmentController.checkActions();
  363. // }.bind(this))
  364. // }.bind(this), null, formData, file);
  365. // }
  366. // }
  367. // }.bind(this));
  368. },
  369. downloadAttachment: function(e, node, attachments){
  370. if (this.form.businessData.document){
  371. attachments.each(function(att){
  372. if (window.o2android && window.o2android.downloadAttachment){
  373. window.o2android.downloadAttachment(att.data.id);
  374. }else if(window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  375. window.webkit.messageHandlers.downloadAttachment.postMessage({"id": att.data.id, "site": this.json.id});
  376. }else{
  377. this.form.documentAction.getAttachmentStream(att.data.id, this.form.businessData.document.id);
  378. }
  379. }.bind(this));
  380. }
  381. },
  382. openAttachment: function(e, node, attachments){
  383. if (this.form.businessData.document){
  384. attachments.each(function(att){
  385. if (window.o2android && window.o2android.downloadAttachment){
  386. window.o2android.downloadAttachment(att.data.id);
  387. }else if(window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  388. window.webkit.messageHandlers.downloadAttachment.postMessage({"id": att.data.id, "site": this.json.id});
  389. }else {
  390. this.form.documentAction.getAttachmentData(att.data.id, this.form.businessData.document.id);
  391. }
  392. }.bind(this));
  393. }
  394. //this.downloadAttachment(e, node, attachment);
  395. },
  396. getAttachmentUrl: function(attachment, callback){
  397. if (this.form.businessData.document){
  398. this.form.documentAction.getAttachmentUrl(attachment.data.id, this.form.businessData.document.id, callback);
  399. }
  400. },
  401. validationConfigItem: function(routeName, data){
  402. var flag = (data.status=="all") ? true: (routeName == "publish");
  403. if (flag){
  404. var n = this.getData();
  405. var v = (data.valueType=="value") ? n : n.length;
  406. switch (data.operateor){
  407. case "isnull":
  408. if (!v){
  409. this.notValidationMode(data.prompt);
  410. return false;
  411. }
  412. break;
  413. case "notnull":
  414. if (v){
  415. this.notValidationMode(data.prompt);
  416. return false;
  417. }
  418. break;
  419. case "gt":
  420. if (v>data.value){
  421. this.notValidationMode(data.prompt);
  422. return false;
  423. }
  424. break;
  425. case "lt":
  426. if (v<data.value){
  427. this.notValidationMode(data.prompt);
  428. return false;
  429. }
  430. break;
  431. case "equal":
  432. if (v==data.value){
  433. this.notValidationMode(data.prompt);
  434. return false;
  435. }
  436. break;
  437. case "neq":
  438. if (v!=data.value){
  439. this.notValidationMode(data.prompt);
  440. return false;
  441. }
  442. break;
  443. case "contain":
  444. if (v.indexOf(data.value)!=-1){
  445. this.notValidationMode(data.prompt);
  446. return false;
  447. }
  448. break;
  449. case "notcontain":
  450. if (v.indexOf(data.value)==-1){
  451. this.notValidationMode(data.prompt);
  452. return false;
  453. }
  454. break;
  455. }
  456. }
  457. return true;
  458. }
  459. });