Attachment.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. this.attachmentController.doUploadAttachment({ "site": this.json.id }, this.form.documentAction.action, "uploadAttachment", { "id": this.form.businessData.document.id }, null, function (o) {
  198. if (o.id) {
  199. this.form.documentAction.getAttachment(o.id, this.form.businessData.document.id, function (json) {
  200. if (json.data) {
  201. if (!json.data.control) json.data.control = {};
  202. this.attachmentController.addAttachment(json.data, o.messageId);
  203. this.form.businessData.attachmentList.push(json.data);
  204. }
  205. this.attachmentController.checkActions();
  206. this.fireEvent("upload", [json.data]);
  207. }.bind(this))
  208. }
  209. this.attachmentController.checkActions();
  210. }.bind(this), function (files) {
  211. if (files.length) {
  212. if ((files.length + this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount > 0) {
  213. var content = MWF.xApplication.cms.Xform.LP.uploadMore;
  214. content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  215. this.form.notice(content, "error");
  216. return false;
  217. }
  218. }
  219. return true;
  220. }.bind(this), true, accept, size);
  221. // this.uploadFileAreaNode = new Element("div");
  222. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  223. // this.uploadFileAreaNode.set("html", html);
  224. //
  225. // this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  226. // this.fileUploadNode.addEvent("change", function(){
  227. // this.validationMode();
  228. // var files = this.fileUploadNode.files;
  229. // if (files.length){
  230. // if ((files.length+this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount>0){
  231. // var content = MWF.xApplication.cms.Xform.LP.uploadMore;
  232. // content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  233. // this.form.notice(content, "error");
  234. // }else{
  235. // for (var i = 0; i < files.length; i++) {
  236. // var file = files.item(i);
  237. //
  238. // var formData = new FormData();
  239. // formData.append('file', file);
  240. // formData.append('site', this.json.id);
  241. // //formData.append('folder', folderId);
  242. //
  243. // this.form.documentAction.uploadAttachment(this.form.businessData.document.id ,function(o, text){
  244. // if (o.id){
  245. // this.form.documentAction.getAttachment(o.id, this.form.businessData.document.id, function(json){
  246. // if (json.data){
  247. // this.attachmentController.addAttachment(json.data);
  248. // this.form.businessData.attachmentList.push(json.data);
  249. // }
  250. // this.attachmentController.checkActions();
  251. //
  252. // this.fireEvent("upload", [json.data]);
  253. // }.bind(this))
  254. // }
  255. // this.attachmentController.checkActions();
  256. // }.bind(this), null, formData, file);
  257. // }
  258. // }
  259. // }
  260. // }.bind(this));
  261. },
  262. deleteAttachments: function (e, node, attachments) {
  263. var names = [];
  264. attachments.each(function (attachment) {
  265. names.push(attachment.data.name);
  266. }.bind(this));
  267. var _self = this;
  268. this.form.confirm("warn", e, MWF.xApplication.cms.Xform.LP.deleteAttachmentTitle, MWF.xApplication.cms.Xform.LP.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  269. while (attachments.length) {
  270. var attachment = attachments.shift();
  271. _self.deleteAttachment(attachment);
  272. }
  273. this.close();
  274. }, function () {
  275. this.close();
  276. }, null, null, this.form.json.confirmStyle);
  277. },
  278. deleteAttachment: function (attachment) {
  279. this.fireEvent("delete", [attachment.data]);
  280. this.form.documentAction.deleteAttachment(attachment.data.id, function (json) {
  281. this.attachmentController.removeAttachment(attachment);
  282. //this.form.businessData.attachmentList.erase( attachment.data )
  283. this.attachmentController.checkActions();
  284. if (this.form.officeList) {
  285. this.form.officeList.each(function (office) {
  286. if (office.openedAttachment) {
  287. if (office.openedAttachment.id == id) {
  288. office.loadOfficeEdit();
  289. }
  290. }
  291. }.bind(this));
  292. }
  293. this.fireEvent("afterDelete", [attachment.data]);
  294. }.bind(this));
  295. },
  296. createReplaceFileNode: function (attachment) {
  297. var accept = "*";
  298. if (!this.json.attachmentExtType || this.json.attachmentExtType.indexOf("other") != -1 && !this.json.attachmentExtOtherType) {
  299. } else {
  300. accepts = [];
  301. var otherType = this.json.attachmentExtOtherType;
  302. this.json.attachmentExtType.each(function (v) {
  303. switch (v) {
  304. case "word":
  305. accepts.push(".doc, .docx, .dot, .dotx");
  306. break;
  307. case "excel":
  308. accepts.push(".xls, .xlsx, .xlsm, .xlt, .xltx");
  309. break;
  310. case "ppt":
  311. accepts.push(".pptx, .ppt, .pot, .potx, .potm");
  312. break;
  313. case "txt":
  314. accepts.push(".txt");
  315. break;
  316. case "pic":
  317. accepts.push(".bmp, .gif, .psd, .jpeg, .jpg");
  318. break;
  319. case "pdf":
  320. accepts.push(".pdf");
  321. break;
  322. case "zip":
  323. accepts.push(".zip, .rar");
  324. break;
  325. case "audio":
  326. accepts.push(".mp3, .wav, .wma, .wmv, .flac, .ape");
  327. break;
  328. case "video":
  329. accepts.push(".avi, .mkv, .mov, .ogg, .mp4, .mpeg");
  330. break;
  331. case "other":
  332. if (this.json.attachmentExtOtherType) accepts.push(this.json.attachmentExtOtherType);
  333. break;
  334. }
  335. });
  336. accept = accepts.join(", ");
  337. }
  338. var size = 0;
  339. if (this.json.attachmentSize) size = this.json.attachmentSize.toFloat();
  340. this.attachmentController.doUploadAttachment({ "site": this.json.id }, this.form.documentAction.action, "replaceAttachment",
  341. { "id": attachment.data.id, "documentid": this.form.businessData.document.id }, null, function (o) {
  342. this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function (json) {
  343. attachment.data = json.data;
  344. attachment.reload();
  345. if (o.messageId && this.attachmentController.messageItemList) {
  346. var message = this.attachmentController.messageItemList[o.messageId];
  347. if( message && message.node )message.node.destroy();
  348. }
  349. this.attachmentController.checkActions();
  350. }.bind(this))
  351. }.bind(this), null);
  352. // this.replaceFileAreaNode = new Element("div");
  353. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  354. // this.replaceFileAreaNode.set("html", html);
  355. //
  356. // this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  357. // this.fileReplaceNode.addEvent("change", function(){
  358. // var files = this.fileReplaceNode.files;
  359. // if (files.length){
  360. // for (var i = 0; i < files.length; i++) {
  361. // var file = files.item(i);
  362. //
  363. // var formData = new FormData();
  364. // formData.append('file', file);
  365. // // formData.append('site', this.json.id);
  366. //
  367. // this.form.documentAction.replaceAttachment(attachment.data.id, this.form.businessData.document.id ,function(o, text){
  368. // this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  369. // attachment.data = json.data;
  370. // attachment.reload();
  371. // this.attachmentController.checkActions();
  372. // }.bind(this))
  373. // }.bind(this), null, formData, file);
  374. // }
  375. // }
  376. // }.bind(this));
  377. },
  378. //小程序文件是否支持打开
  379. checkMiniProgramFile: function(ext) {
  380. var exts = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"];
  381. for(var i = 0; i < exts.length; i++){
  382. if(ext === exts[i]){
  383. return true;
  384. }
  385. }
  386. return false;
  387. },
  388. downloadAttachment: function (e, node, attachments) {
  389. if (this.form.businessData.document) {
  390. attachments.each(function (att) {
  391. if (window.o2android && window.o2android.downloadAttachment) {
  392. window.o2android.downloadAttachment(att.data.id);
  393. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  394. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": this.json.id });
  395. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  396. wx.miniProgram.navigateTo({
  397. url: '../file/download?attId=' + att.data.id + '&type=cms&documentId=' + this.form.businessData.document.id
  398. });
  399. } else {
  400. if (layout.mobile) {
  401. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  402. this.form.documentAction.getAttachmentUrl(att.data.id, this.form.businessData.document.id, function (url) {
  403. var xtoken = Cookie.read("x-token");
  404. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  405. });
  406. } else {
  407. this.form.documentAction.getAttachmentStream(att.data.id, this.form.businessData.document.id);
  408. }
  409. }
  410. }.bind(this));
  411. }
  412. },
  413. openAttachment: function (e, node, attachments) {
  414. if (this.form.businessData.document) {
  415. attachments.each(function (att) {
  416. if (window.o2android && window.o2android.downloadAttachment) {
  417. window.o2android.downloadAttachment(att.data.id);
  418. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  419. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": this.json.id });
  420. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  421. wx.miniProgram.navigateTo({
  422. url: '../file/download?attId=' + att.data.id + '&type=cms&documentId=' + this.form.businessData.document.id
  423. });
  424. } else {
  425. if (layout.mobile) {
  426. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  427. this.form.documentAction.getAttachmentUrl(att.data.id, this.form.businessData.document.id, function (url) {
  428. var xtoken = Cookie.read("x-token");
  429. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  430. });
  431. } else {
  432. this.form.documentAction.getAttachmentData(att.data.id, this.form.businessData.document.id);
  433. }
  434. }
  435. }.bind(this));
  436. }
  437. //this.downloadAttachment(e, node, attachment);
  438. },
  439. getAttachmentUrl: function (attachment, callback) {
  440. if (this.form.businessData.document) {
  441. this.form.documentAction.getAttachmentUrl(attachment.data.id, this.form.businessData.document.id, callback);
  442. }
  443. },
  444. validationConfigItem: function (routeName, data) {
  445. var flag = (data.status == "all") ? true : (routeName == "publish");
  446. if (flag) {
  447. var n = this.getData();
  448. var v = (data.valueType == "value") ? n : n.length;
  449. switch (data.operateor) {
  450. case "isnull":
  451. if (!v) {
  452. this.notValidationMode(data.prompt);
  453. return false;
  454. }
  455. break;
  456. case "notnull":
  457. if (v) {
  458. this.notValidationMode(data.prompt);
  459. return false;
  460. }
  461. break;
  462. case "gt":
  463. if (v > data.value) {
  464. this.notValidationMode(data.prompt);
  465. return false;
  466. }
  467. break;
  468. case "lt":
  469. if (v < data.value) {
  470. this.notValidationMode(data.prompt);
  471. return false;
  472. }
  473. break;
  474. case "equal":
  475. if (v == data.value) {
  476. this.notValidationMode(data.prompt);
  477. return false;
  478. }
  479. break;
  480. case "neq":
  481. if (v != data.value) {
  482. this.notValidationMode(data.prompt);
  483. return false;
  484. }
  485. break;
  486. case "contain":
  487. if (v.indexOf(data.value) != -1) {
  488. this.notValidationMode(data.prompt);
  489. return false;
  490. }
  491. break;
  492. case "notcontain":
  493. if (v.indexOf(data.value) == -1) {
  494. this.notValidationMode(data.prompt);
  495. return false;
  496. }
  497. break;
  498. }
  499. }
  500. return true;
  501. }
  502. });