Attachment.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. _loadUserInterface: function () {
  72. this.node.empty();
  73. this.loadAttachmentController();
  74. this.fireEvent("load");
  75. },
  76. loadAttachmentController: function () {
  77. //MWF.require("MWF.widget.AttachmentController", function() {
  78. var options = {
  79. "style": this.json.style || "default",
  80. "title": "附件区域",
  81. "listStyle": this.json.listStyle || "icon",
  82. "size": this.json.size || "max",
  83. "resize": (this.json.resize === "y" || this.json.resize === "true"),
  84. "attachmentCount": this.json.attachmentCount || 0,
  85. "isUpload": (this.json.isUpload === "y" || this.json.isUpload === "true"),
  86. "isDelete": (this.json.isDelete === "y" || this.json.isDelete === "true"),
  87. "isReplace": (this.json.isReplace === "y" || this.json.isReplace === "true"),
  88. "isDownload": (this.json.isDownload === "y" || this.json.isDownload === "true"),
  89. "isSizeChange": (this.json.isSizeChange === "y" || this.json.isSizeChange === "true"),
  90. "readonly": (this.json.readonly === "y" || this.json.readonly === "true"),
  91. "availableListStyles": this.json.availableListStyles ? this.json.availableListStyles : ["list", "seq", "icon", "preview"],
  92. "isDeleteOption": this.json.isDelete,
  93. "isReplaceOption": this.json.isReplace,
  94. "toolbarGroupHidden": this.json.toolbarGroupHidden || []
  95. //"downloadEvent" : this.json.downloadEvent
  96. };
  97. if (this.readonly) options.readonly = true;
  98. this.attachmentController = new MWF.xApplication.cms.Xform.AttachmentController(this.node, this, options);
  99. this.attachmentController.load();
  100. this.form.businessData.attachmentList.each(function (att) {
  101. if (att.site == this.json.id) this.attachmentController.addAttachment(att);
  102. //if (att.fileType.toLowerCase()==this.json.id.toLowerCase()) this.attachmentController.addAttachment(att);
  103. }.bind(this));
  104. //}.bind(this));
  105. },
  106. loadAttachmentSelecter: function (option, callback) {
  107. MWF.require("MWF.widget.AttachmentSelector", function () {
  108. var options = {
  109. //"style" : "cms",
  110. "title": "选择附件",
  111. "listStyle": "icon",
  112. "selectType": "all",
  113. "size": "max",
  114. "attachmentCount": 0,
  115. "isUpload": true,
  116. "isDelete": true,
  117. "isReplace": true,
  118. "isDownload": true,
  119. "toBase64": true,
  120. "base64MaxSize": 800,
  121. "readonly": false
  122. };
  123. options = Object.merge(options, option);
  124. if (this.readonly) options.readonly = true;
  125. this.attachmentController = new MWF.widget.AttachmentSelector(this.node, this, options);
  126. this.attachmentController.load();
  127. this.postSelect = callback;
  128. this.form.businessData.attachmentList.each(function (att) {
  129. this.attachmentController.addAttachment(att);
  130. }.bind(this));
  131. }.bind(this));
  132. },
  133. selectAttachment: function (e, node, attachments) {
  134. //if( attachments.length > 0 ){
  135. // this.form.documentAction.getAttachmentUrl(attachments[attachments.length-1].data.id, this.form.businessData.document.id, function(url){
  136. // if(this.postSelect)this.postSelect( url )
  137. // }.bind(this))
  138. //}
  139. if (attachments.length > 0) {
  140. var data = attachments[attachments.length - 1].data;
  141. this.form.documentAction.getAttachmentUrl(data.id, this.form.businessData.document.id, function (url) {
  142. if (this.attachmentController.options.toBase64) {
  143. this.form.documentAction.getSubjectAttachmentBase64(data.id, this.attachmentController.options.base64MaxSize, function (json) {
  144. var base64Code = json.data ? "data:image/png;base64," + json.data.value : null;
  145. if (this.postSelect) this.postSelect(url, data, base64Code)
  146. }.bind(this))
  147. } else {
  148. if (this.postSelect) this.postSelect(url, data)
  149. }
  150. }.bind(this))
  151. }
  152. },
  153. createUploadFileNode: function () {
  154. var accept = "*";
  155. if (!this.json.attachmentExtType || (this.json.attachmentExtType.indexOf("other") != -1 && !this.json.attachmentExtOtherType)) {
  156. } else {
  157. accepts = [];
  158. var otherType = this.json.attachmentExtOtherType;
  159. this.json.attachmentExtType.each(function (v) {
  160. switch (v) {
  161. case "word":
  162. accepts.push(".doc, .docx, .dot, .dotx");
  163. break;
  164. case "excel":
  165. accepts.push(".xls, .xlsx, .xlsm, .xlt, .xltx");
  166. break;
  167. case "ppt":
  168. accepts.push(".pptx, .ppt, .pot, .potx, .potm");
  169. break;
  170. case "txt":
  171. accepts.push(".txt");
  172. break;
  173. case "pic":
  174. accepts.push(".bmp, .gif, .psd, .jpeg, .jpg, .png");
  175. break;
  176. case "pdf":
  177. accepts.push(".pdf");
  178. break;
  179. case "zip":
  180. accepts.push(".zip, .rar");
  181. break;
  182. case "audio":
  183. accepts.push(".mp3, .wav, .wma, .wmv, .flac, .ape");
  184. break;
  185. case "video":
  186. accepts.push(".avi, .mkv, .mov, .ogg, .mp4, .mpeg");
  187. break;
  188. case "other":
  189. if (this.json.attachmentExtOtherType) accepts.push(this.json.attachmentExtOtherType);
  190. break;
  191. }
  192. }.bind(this));
  193. accept = accepts.join(", ");
  194. }
  195. var size = 0;
  196. if (this.json.attachmentSize) size = this.json.attachmentSize.toFloat();
  197. debugger;
  198. this.attachmentController.doUploadAttachment({ "site": this.json.id }, this.form.documentAction.action, "uploadAttachment", { "id": this.form.businessData.document.id }, null, function (o) {
  199. if (o.id) {
  200. this.form.documentAction.getAttachment(o.id, this.form.businessData.document.id, function (json) {
  201. if (json.data) {
  202. if (!json.data.control) json.data.control = {};
  203. this.attachmentController.addAttachment(json.data, o.messageId);
  204. this.form.businessData.attachmentList.push(json.data);
  205. }
  206. this.attachmentController.checkActions();
  207. this.fireEvent("upload", [json.data]);
  208. }.bind(this))
  209. }
  210. this.attachmentController.checkActions();
  211. }.bind(this), function (files) {
  212. if (files.length) {
  213. if ((files.length + this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount > 0) {
  214. var content = MWF.xApplication.cms.Xform.LP.uploadMore;
  215. content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  216. this.form.notice(content, "error");
  217. return false;
  218. }
  219. }
  220. return true;
  221. }.bind(this), true, accept, size, function (o) { //错误的回调
  222. if (o.messageId && this.attachmentController.messageItemList) {
  223. var message = this.attachmentController.messageItemList[o.messageId];
  224. if( message && message.node )message.node.destroy();
  225. }
  226. }.bind(this));
  227. // this.uploadFileAreaNode = new Element("div");
  228. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  229. // this.uploadFileAreaNode.set("html", html);
  230. //
  231. // this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  232. // this.fileUploadNode.addEvent("change", function(){
  233. // this.validationMode();
  234. // var files = this.fileUploadNode.files;
  235. // if (files.length){
  236. // if ((files.length+this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount>0){
  237. // var content = MWF.xApplication.cms.Xform.LP.uploadMore;
  238. // content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  239. // this.form.notice(content, "error");
  240. // }else{
  241. // for (var i = 0; i < files.length; i++) {
  242. // var file = files.item(i);
  243. //
  244. // var formData = new FormData();
  245. // formData.append('file', file);
  246. // formData.append('site', this.json.id);
  247. // //formData.append('folder', folderId);
  248. //
  249. // this.form.documentAction.uploadAttachment(this.form.businessData.document.id ,function(o, text){
  250. // if (o.id){
  251. // this.form.documentAction.getAttachment(o.id, this.form.businessData.document.id, function(json){
  252. // if (json.data){
  253. // this.attachmentController.addAttachment(json.data);
  254. // this.form.businessData.attachmentList.push(json.data);
  255. // }
  256. // this.attachmentController.checkActions();
  257. //
  258. // this.fireEvent("upload", [json.data]);
  259. // }.bind(this))
  260. // }
  261. // this.attachmentController.checkActions();
  262. // }.bind(this), null, formData, file);
  263. // }
  264. // }
  265. // }
  266. // }.bind(this));
  267. },
  268. deleteAttachments: function (e, node, attachments) {
  269. var names = [];
  270. attachments.each(function (attachment) {
  271. names.push(attachment.data.name);
  272. }.bind(this));
  273. var _self = this;
  274. this.form.confirm("warn", e, MWF.xApplication.cms.Xform.LP.deleteAttachmentTitle, MWF.xApplication.cms.Xform.LP.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  275. while (attachments.length) {
  276. var attachment = attachments.shift();
  277. _self.deleteAttachment(attachment);
  278. }
  279. this.close();
  280. }, function () {
  281. this.close();
  282. }, null, null, this.form.json.confirmStyle);
  283. },
  284. deleteAttachment: function (attachment) {
  285. this.fireEvent("delete", [attachment.data]);
  286. this.form.documentAction.deleteAttachment(attachment.data.id, function (json) {
  287. this.attachmentController.removeAttachment(attachment);
  288. //this.form.businessData.attachmentList.erase( attachment.data )
  289. this.attachmentController.checkActions();
  290. if (this.form.officeList) {
  291. this.form.officeList.each(function (office) {
  292. if (office.openedAttachment) {
  293. if (office.openedAttachment.id == id) {
  294. office.loadOfficeEdit();
  295. }
  296. }
  297. }.bind(this));
  298. }
  299. this.fireEvent("afterDelete", [attachment.data]);
  300. }.bind(this));
  301. },
  302. createReplaceFileNode: function (attachment) {
  303. var accept = "*";
  304. if (!this.json.attachmentExtType || this.json.attachmentExtType.indexOf("other") != -1 && !this.json.attachmentExtOtherType) {
  305. } else {
  306. accepts = [];
  307. var otherType = this.json.attachmentExtOtherType;
  308. this.json.attachmentExtType.each(function (v) {
  309. switch (v) {
  310. case "word":
  311. accepts.push(".doc, .docx, .dot, .dotx");
  312. break;
  313. case "excel":
  314. accepts.push(".xls, .xlsx, .xlsm, .xlt, .xltx");
  315. break;
  316. case "ppt":
  317. accepts.push(".pptx, .ppt, .pot, .potx, .potm");
  318. break;
  319. case "txt":
  320. accepts.push(".txt");
  321. break;
  322. case "pic":
  323. accepts.push(".bmp, .gif, .psd, .jpeg, .jpg");
  324. break;
  325. case "pdf":
  326. accepts.push(".pdf");
  327. break;
  328. case "zip":
  329. accepts.push(".zip, .rar");
  330. break;
  331. case "audio":
  332. accepts.push(".mp3, .wav, .wma, .wmv, .flac, .ape");
  333. break;
  334. case "video":
  335. accepts.push(".avi, .mkv, .mov, .ogg, .mp4, .mpeg");
  336. break;
  337. case "other":
  338. if (this.json.attachmentExtOtherType) accepts.push(this.json.attachmentExtOtherType);
  339. break;
  340. }
  341. });
  342. accept = accepts.join(", ");
  343. }
  344. var size = 0;
  345. if (this.json.attachmentSize) size = this.json.attachmentSize.toFloat();
  346. this.attachmentController.doUploadAttachment({ "site": this.json.id }, this.form.documentAction.action, "replaceAttachment",
  347. { "id": attachment.data.id, "documentid": this.form.businessData.document.id }, null, function (o) {
  348. this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function (json) {
  349. attachment.data = json.data;
  350. attachment.reload();
  351. if (o.messageId && this.attachmentController.messageItemList) {
  352. var message = this.attachmentController.messageItemList[o.messageId];
  353. if( message && message.node )message.node.destroy();
  354. }
  355. this.attachmentController.checkActions();
  356. }.bind(this))
  357. }.bind(this), null, true, accept, size, function (o) { //错误的回调
  358. if (o.messageId && this.attachmentController.messageItemList) {
  359. var message = this.attachmentController.messageItemList[o.messageId];
  360. if( message && message.node )message.node.destroy();
  361. }
  362. }.bind(this));
  363. // this.replaceFileAreaNode = new Element("div");
  364. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  365. // this.replaceFileAreaNode.set("html", html);
  366. //
  367. // this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  368. // this.fileReplaceNode.addEvent("change", function(){
  369. // var files = this.fileReplaceNode.files;
  370. // if (files.length){
  371. // for (var i = 0; i < files.length; i++) {
  372. // var file = files.item(i);
  373. //
  374. // var formData = new FormData();
  375. // formData.append('file', file);
  376. // // formData.append('site', this.json.id);
  377. //
  378. // this.form.documentAction.replaceAttachment(attachment.data.id, this.form.businessData.document.id ,function(o, text){
  379. // this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  380. // attachment.data = json.data;
  381. // attachment.reload();
  382. // this.attachmentController.checkActions();
  383. // }.bind(this))
  384. // }.bind(this), null, formData, file);
  385. // }
  386. // }
  387. // }.bind(this));
  388. },
  389. //小程序文件是否支持打开
  390. checkMiniProgramFile: function(ext) {
  391. var exts = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"];
  392. for(var i = 0; i < exts.length; i++){
  393. if(ext === exts[i]){
  394. return true;
  395. }
  396. }
  397. return false;
  398. },
  399. downloadAttachment: function (e, node, attachments) {
  400. if (this.form.businessData.document) {
  401. attachments.each(function (att) {
  402. if (window.o2android && window.o2android.downloadAttachment) {
  403. window.o2android.downloadAttachment(att.data.id);
  404. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  405. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": this.json.id });
  406. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  407. wx.miniProgram.navigateTo({
  408. url: '../file/download?attId=' + att.data.id + '&type=cms&documentId=' + this.form.businessData.document.id
  409. });
  410. } else {
  411. if (layout.mobile) {
  412. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  413. this.form.documentAction.getAttachmentUrl(att.data.id, this.form.businessData.document.id, function (url) {
  414. var xtoken = Cookie.read("x-token");
  415. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  416. });
  417. } else {
  418. this.form.documentAction.getAttachmentStream(att.data.id, this.form.businessData.document.id);
  419. }
  420. }
  421. }.bind(this));
  422. }
  423. },
  424. openAttachment: function (e, node, attachments) {
  425. if (this.form.businessData.document) {
  426. attachments.each(function (att) {
  427. if (window.o2android && window.o2android.downloadAttachment) {
  428. window.o2android.downloadAttachment(att.data.id);
  429. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  430. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": this.json.id });
  431. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  432. wx.miniProgram.navigateTo({
  433. url: '../file/download?attId=' + att.data.id + '&type=cms&documentId=' + this.form.businessData.document.id
  434. });
  435. } else {
  436. if (layout.mobile) {
  437. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  438. this.form.documentAction.getAttachmentUrl(att.data.id, this.form.businessData.document.id, function (url) {
  439. var xtoken = Cookie.read("x-token");
  440. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  441. });
  442. } else {
  443. this.form.documentAction.getAttachmentData(att.data.id, this.form.businessData.document.id);
  444. }
  445. }
  446. }.bind(this));
  447. }
  448. //this.downloadAttachment(e, node, attachment);
  449. },
  450. getAttachmentUrl: function (attachment, callback) {
  451. if (this.form.businessData.document) {
  452. this.form.documentAction.getAttachmentUrl(attachment.data.id, this.form.businessData.document.id, callback);
  453. }
  454. },
  455. validationConfigItem: function (routeName, data) {
  456. var flag = (data.status == "all") ? true : (routeName == "publish");
  457. if (flag) {
  458. var n = this.getData();
  459. var v = (data.valueType == "value") ? n : n.length;
  460. switch (data.operateor) {
  461. case "isnull":
  462. if (!v) {
  463. this.notValidationMode(data.prompt);
  464. return false;
  465. }
  466. break;
  467. case "notnull":
  468. if (v) {
  469. this.notValidationMode(data.prompt);
  470. return false;
  471. }
  472. break;
  473. case "gt":
  474. if (v > data.value) {
  475. this.notValidationMode(data.prompt);
  476. return false;
  477. }
  478. break;
  479. case "lt":
  480. if (v < data.value) {
  481. this.notValidationMode(data.prompt);
  482. return false;
  483. }
  484. break;
  485. case "equal":
  486. if (v == data.value) {
  487. this.notValidationMode(data.prompt);
  488. return false;
  489. }
  490. break;
  491. case "neq":
  492. if (v != data.value) {
  493. this.notValidationMode(data.prompt);
  494. return false;
  495. }
  496. break;
  497. case "contain":
  498. if (v.indexOf(data.value) != -1) {
  499. this.notValidationMode(data.prompt);
  500. return false;
  501. }
  502. break;
  503. case "notcontain":
  504. if (v.indexOf(data.value) == -1) {
  505. this.notValidationMode(data.prompt);
  506. return false;
  507. }
  508. break;
  509. }
  510. }
  511. return true;
  512. }
  513. });