Attachment.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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);
  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. this.attachmentController.checkActions();
  346. }.bind(this))
  347. }.bind(this), null);
  348. // this.replaceFileAreaNode = new Element("div");
  349. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  350. // this.replaceFileAreaNode.set("html", html);
  351. //
  352. // this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  353. // this.fileReplaceNode.addEvent("change", function(){
  354. // var files = this.fileReplaceNode.files;
  355. // if (files.length){
  356. // for (var i = 0; i < files.length; i++) {
  357. // var file = files.item(i);
  358. //
  359. // var formData = new FormData();
  360. // formData.append('file', file);
  361. // // formData.append('site', this.json.id);
  362. //
  363. // this.form.documentAction.replaceAttachment(attachment.data.id, this.form.businessData.document.id ,function(o, text){
  364. // this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  365. // attachment.data = json.data;
  366. // attachment.reload();
  367. // this.attachmentController.checkActions();
  368. // }.bind(this))
  369. // }.bind(this), null, formData, file);
  370. // }
  371. // }
  372. // }.bind(this));
  373. },
  374. downloadAttachment: function (e, node, attachments) {
  375. if (this.form.businessData.document) {
  376. attachments.each(function (att) {
  377. if (window.o2android && window.o2android.downloadAttachment) {
  378. window.o2android.downloadAttachment(att.data.id);
  379. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  380. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": this.json.id });
  381. } else {
  382. if (layout.mobile) {
  383. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  384. this.form.documentAction.getAttachmentUrl(att.data.id, this.form.businessData.document.id, function (url) {
  385. var xtoken = Cookie.read("x-token");
  386. window.location = url + "?x-token=" + xtoken;
  387. });
  388. } else {
  389. this.form.documentAction.getAttachmentStream(att.data.id, this.form.businessData.document.id);
  390. }
  391. }
  392. }.bind(this));
  393. }
  394. },
  395. openAttachment: function (e, node, attachments) {
  396. if (this.form.businessData.document) {
  397. attachments.each(function (att) {
  398. if (window.o2android && window.o2android.downloadAttachment) {
  399. window.o2android.downloadAttachment(att.data.id);
  400. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  401. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": this.json.id });
  402. } else {
  403. if (layout.mobile) {
  404. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  405. this.form.documentAction.getAttachmentUrl(att.data.id, this.form.businessData.document.id, function (url) {
  406. var xtoken = Cookie.read("x-token");
  407. window.location = url + "?x-token=" + xtoken;
  408. });
  409. } else {
  410. this.form.documentAction.getAttachmentData(att.data.id, this.form.businessData.document.id);
  411. }
  412. }
  413. }.bind(this));
  414. }
  415. //this.downloadAttachment(e, node, attachment);
  416. },
  417. getAttachmentUrl: function (attachment, callback) {
  418. if (this.form.businessData.document) {
  419. this.form.documentAction.getAttachmentUrl(attachment.data.id, this.form.businessData.document.id, callback);
  420. }
  421. },
  422. validationConfigItem: function (routeName, data) {
  423. var flag = (data.status == "all") ? true : (routeName == "publish");
  424. if (flag) {
  425. var n = this.getData();
  426. var v = (data.valueType == "value") ? n : n.length;
  427. switch (data.operateor) {
  428. case "isnull":
  429. if (!v) {
  430. this.notValidationMode(data.prompt);
  431. return false;
  432. }
  433. break;
  434. case "notnull":
  435. if (v) {
  436. this.notValidationMode(data.prompt);
  437. return false;
  438. }
  439. break;
  440. case "gt":
  441. if (v > data.value) {
  442. this.notValidationMode(data.prompt);
  443. return false;
  444. }
  445. break;
  446. case "lt":
  447. if (v < data.value) {
  448. this.notValidationMode(data.prompt);
  449. return false;
  450. }
  451. break;
  452. case "equal":
  453. if (v == data.value) {
  454. this.notValidationMode(data.prompt);
  455. return false;
  456. }
  457. break;
  458. case "neq":
  459. if (v != data.value) {
  460. this.notValidationMode(data.prompt);
  461. return false;
  462. }
  463. break;
  464. case "contain":
  465. if (v.indexOf(data.value) != -1) {
  466. this.notValidationMode(data.prompt);
  467. return false;
  468. }
  469. break;
  470. case "notcontain":
  471. if (v.indexOf(data.value) == -1) {
  472. this.notValidationMode(data.prompt);
  473. return false;
  474. }
  475. break;
  476. }
  477. }
  478. return true;
  479. }
  480. });