Attachment.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.cms.FormDesigner.widget.AttachmentController,
  5. checkDeleteAction: function(){
  6. if (this.options.readonly){
  7. this.setActionDisabled(this.deleteAction);
  8. this.setActionDisabled(this.min_deleteAction);
  9. return false;
  10. }
  11. if (this.options.isDeleteOption!=="y" && this.options.isDeleteOption!=="n"){
  12. if (this.selectedAttachments.length){
  13. var flag = true;
  14. if (this.options.isDeleteOption==="o"){
  15. for (var i=0; i<this.selectedAttachments.length; i++){
  16. if (this.selectedAttachments[i].data.person!==layout.desktop.session.user.distinguishedName){
  17. flag = false;
  18. break;
  19. }
  20. }
  21. }else if (this.options.isDeleteOption==="a"){
  22. for (var i=0; i<this.selectedAttachments.length; i++){
  23. if (this.selectedAttachments[i].data.activity!==this.module.form.businessData.activity.id){
  24. flag = false;
  25. break;
  26. }
  27. }
  28. }else if (this.options.isDeleteOption==="ao"){
  29. for (var i=0; i<this.selectedAttachments.length; i++){
  30. if ((this.selectedAttachments[i].data.activity!==this.module.form.businessData.activity.id) || (this.selectedAttachments[i].data.person!==layout.desktop.session.user.distinguishedName)){
  31. flag = false;
  32. break;
  33. }
  34. }
  35. }
  36. if (flag){
  37. this.setActionEnabled(this.deleteAction);
  38. this.setActionEnabled(this.min_deleteAction);
  39. }else{
  40. this.setActionDisabled(this.deleteAction);
  41. this.setActionDisabled(this.min_deleteAction);
  42. }
  43. }else{
  44. this.setActionDisabled(this.deleteAction);
  45. this.setActionDisabled(this.min_deleteAction);
  46. }
  47. }else{
  48. if (!this.options.isDelete){
  49. this.setActionDisabled(this.deleteAction);
  50. this.setActionDisabled(this.min_deleteAction);
  51. }else{
  52. if (this.selectedAttachments.length){
  53. this.setActionEnabled(this.deleteAction);
  54. this.setActionEnabled(this.min_deleteAction);
  55. }else{
  56. this.setActionDisabled(this.deleteAction);
  57. this.setActionDisabled(this.min_deleteAction);
  58. }
  59. }
  60. }
  61. },
  62. checkReplaceAction: function(){
  63. if (this.options.readonly){
  64. this.setActionDisabled(this.replaceAction);
  65. this.setActionDisabled(this.min_replaceAction);
  66. return false;
  67. }
  68. if (this.options.isReplaceOption!=="y" && this.options.isReplaceOption!=="n") {
  69. if (this.selectedAttachments.length && this.selectedAttachments.length===1){
  70. var flag;
  71. if (this.options.isReplaceOption==="o"){
  72. flag = this.selectedAttachments[0].data.person === layout.desktop.session.user.distinguishedName;
  73. }
  74. if (this.options.isReplaceOption==="a"){
  75. flag = this.selectedAttachments[0].data.activity===this.module.form.businessData.activity.id;
  76. }
  77. if (this.options.isReplaceOption==="ao"){
  78. flag = (this.selectedAttachments[0].data.person === layout.desktop.session.user.distinguishedName && this.selectedAttachments[0].data.activity===this.module.form.businessData.activity.id);
  79. }
  80. if (flag) {
  81. this.setActionEnabled(this.replaceAction);
  82. this.setActionEnabled(this.min_replaceAction);
  83. }else{
  84. this.setActionDisabled(this.replaceAction);
  85. this.setActionDisabled(this.min_replaceAction);
  86. }
  87. } else {
  88. this.setActionDisabled(this.replaceAction);
  89. this.setActionDisabled(this.min_replaceAction);
  90. }
  91. }else{
  92. if (!this.options.isReplace){
  93. this.setActionDisabled(this.replaceAction);
  94. this.setActionDisabled(this.min_replaceAction);
  95. }else{
  96. if (this.selectedAttachments.length && this.selectedAttachments.length===1){
  97. this.setActionEnabled(this.replaceAction);
  98. this.setActionEnabled(this.min_replaceAction);
  99. }else{
  100. this.setActionDisabled(this.replaceAction);
  101. this.setActionDisabled(this.min_replaceAction);
  102. }
  103. }
  104. }
  105. }
  106. });
  107. MWF.xApplication.cms.Xform.Attachment = MWF.CMSAttachment = new Class({
  108. Extends: MWF.APPAttachment,
  109. options: {
  110. "moduleEvents": ["upload", "delete"]
  111. },
  112. initialize: function(node, json, form, options){
  113. this.node = $(node);
  114. this.node.store("module", this);
  115. this.json = json;
  116. this.form = form;
  117. this.field = true;
  118. },
  119. _loadUserInterface: function(){
  120. this.node.empty();
  121. this.loadAttachmentController();
  122. },
  123. loadAttachmentController: function(){
  124. //MWF.require("MWF.widget.AttachmentController", function() {
  125. var options = {
  126. //"style" : "cms",
  127. "title": "附件区域",
  128. "listStyle": this.json.listStyle || "icon",
  129. "size": this.json.size || "max",
  130. "resize": (this.json.size=="true") ? true : false,
  131. "attachmentCount": this.json.attachmentCount || 0,
  132. "isUpload": (this.json.isUpload=="y") ? true : false,
  133. "isDelete": (this.json.isDelete=="y") ? true : false,
  134. "isReplace": (this.json.isReplace=="y") ? true : false,
  135. "isDownload": (this.json.isDownload=="y") ? true : false,
  136. "isSizeChange": (this.json.isSizeChange=="y") ? true : false,
  137. "readonly": (this.json.readonly=="y") ? true : false,
  138. "toolbarGroupHidden" : this.json.toolbarGroupHidden || []
  139. //"downloadEvent" : this.json.downloadEvent
  140. };
  141. if (this.readonly) options.readonly = true;
  142. this.attachmentController = new MWF.xApplication.cms.Xform.AttachmentController(this.node, this, options);
  143. this.attachmentController.load();
  144. this.form.businessData.attachmentList.each(function (att) {
  145. if (att.site==this.json.id) this.attachmentController.addAttachment(att);
  146. //if (att.fileType.toLowerCase()==this.json.id.toLowerCase()) this.attachmentController.addAttachment(att);
  147. }.bind(this));
  148. //}.bind(this));
  149. },
  150. loadAttachmentSelecter: function( option, callback ){
  151. MWF.require("MWF.widget.AttachmentSelector", function() {
  152. var options = {
  153. //"style" : "cms",
  154. "title": "选择附件",
  155. "listStyle": "icon",
  156. "selectType" : "all",
  157. "size": "max",
  158. "attachmentCount": 0,
  159. "isUpload": true,
  160. "isDelete": true,
  161. "isReplace": true,
  162. "isDownload": true,
  163. "toBase64" : true,
  164. "base64MaxSize" : 800,
  165. "readonly": false
  166. };
  167. options = Object.merge( options, option );
  168. if (this.readonly) options.readonly = true;
  169. this.attachmentController = new MWF.widget.AttachmentSelector(this.node, this, options);
  170. this.attachmentController.load();
  171. this.postSelect = callback;
  172. this.form.businessData.attachmentList.each(function (att) {
  173. this.attachmentController.addAttachment(att);
  174. }.bind(this));
  175. }.bind(this));
  176. },
  177. selectAttachment: function(e, node, attachments){
  178. //if( attachments.length > 0 ){
  179. // this.form.documentAction.getAttachmentUrl(attachments[attachments.length-1].data.id, this.form.businessData.document.id, function(url){
  180. // if(this.postSelect)this.postSelect( url )
  181. // }.bind(this))
  182. //}
  183. if( attachments.length > 0 ){
  184. var data = attachments[attachments.length-1].data;
  185. this.form.documentAction.getAttachmentUrl( data.id, this.form.businessData.document.id, function(url){
  186. if( this.attachmentController.options.toBase64 ){
  187. this.form.documentAction.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  188. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  189. if(this.postSelect)this.postSelect( url , data, base64Code )
  190. }.bind(this) )
  191. }else{
  192. if(this.postSelect)this.postSelect( url , data )
  193. }
  194. }.bind(this))
  195. }
  196. },
  197. createUploadFileNode: function(){
  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. 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));
  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. getData: function(){
  263. return this.attachmentController.getAttachmentNames();
  264. },
  265. //getInputData : function(){
  266. // return this.getData();
  267. //},
  268. deleteAttachment: function(attachment){
  269. this.fireEvent("delete", [attachment.data]);
  270. this.form.documentAction.deleteAttachment(attachment.data.id, function(json ){
  271. this.attachmentController.removeAttachment(attachment);
  272. //this.form.businessData.attachmentList.erase( attachment.data )
  273. this.attachmentController.checkActions();
  274. }.bind(this));
  275. },
  276. createReplaceFileNode: function(attachment){
  277. this.attachmentController.doUploadAttachment({"site": this.json.id}, this.form.documentAction.action, "replaceAttachment",
  278. {"id": attachment.data.id, "documentid": this.form.businessData.document.id}, null, function(o){
  279. this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  280. attachment.data = json.data;
  281. attachment.reload();
  282. this.attachmentController.checkActions();
  283. }.bind(this))
  284. }.bind(this), null);
  285. // this.replaceFileAreaNode = new Element("div");
  286. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  287. // this.replaceFileAreaNode.set("html", html);
  288. //
  289. // this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  290. // this.fileReplaceNode.addEvent("change", function(){
  291. // var files = this.fileReplaceNode.files;
  292. // if (files.length){
  293. // for (var i = 0; i < files.length; i++) {
  294. // var file = files.item(i);
  295. //
  296. // var formData = new FormData();
  297. // formData.append('file', file);
  298. // // formData.append('site', this.json.id);
  299. //
  300. // this.form.documentAction.replaceAttachment(attachment.data.id, this.form.businessData.document.id ,function(o, text){
  301. // this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  302. // attachment.data = json.data;
  303. // attachment.reload();
  304. // this.attachmentController.checkActions();
  305. // }.bind(this))
  306. // }.bind(this), null, formData, file);
  307. // }
  308. // }
  309. // }.bind(this));
  310. },
  311. downloadAttachment: function(e, node, attachments){
  312. if (this.form.businessData.document){
  313. attachments.each(function(att){
  314. this.form.documentAction.getAttachmentStream(att.data.id, this.form.businessData.document.id);
  315. }.bind(this));
  316. }
  317. },
  318. openAttachment: function(e, node, attachments){
  319. if (this.form.businessData.document){
  320. attachments.each(function(att){
  321. this.form.documentAction.getAttachmentData(att.data.id, this.form.businessData.document.id);
  322. }.bind(this));
  323. }
  324. //this.downloadAttachment(e, node, attachment);
  325. },
  326. getAttachmentUrl: function(attachment, callback){
  327. if (this.form.businessData.document){
  328. this.form.documentAction.getAttachmentUrl(attachment.data.id, this.form.businessData.document.id, callback);
  329. }
  330. },
  331. validationConfigItem: function(routeName, data){
  332. var flag = (data.status=="all") ? true: (routeName == "publish");
  333. if (flag){
  334. var n = this.getData();
  335. var v = (data.valueType=="value") ? n : n.length;
  336. switch (data.operateor){
  337. case "isnull":
  338. if (!v){
  339. this.notValidationMode(data.prompt);
  340. return false;
  341. }
  342. break;
  343. case "notnull":
  344. if (v){
  345. this.notValidationMode(data.prompt);
  346. return false;
  347. }
  348. break;
  349. case "gt":
  350. if (v>data.value){
  351. this.notValidationMode(data.prompt);
  352. return false;
  353. }
  354. break;
  355. case "lt":
  356. if (v<data.value){
  357. this.notValidationMode(data.prompt);
  358. return false;
  359. }
  360. break;
  361. case "equal":
  362. if (v==data.value){
  363. this.notValidationMode(data.prompt);
  364. return false;
  365. }
  366. break;
  367. case "neq":
  368. if (v!=data.value){
  369. this.notValidationMode(data.prompt);
  370. return false;
  371. }
  372. break;
  373. case "contain":
  374. if (v.indexOf(data.value)!=-1){
  375. this.notValidationMode(data.prompt);
  376. return false;
  377. }
  378. break;
  379. case "notcontain":
  380. if (v.indexOf(data.value)==-1){
  381. this.notValidationMode(data.prompt);
  382. return false;
  383. }
  384. break;
  385. }
  386. }
  387. return true;
  388. }
  389. });