Attachment.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. MWF.xDesktop.requireApp("process.Xform", "Attachment", null, false);
  2. MWF.xApplication.cms.Xform.Attachment = MWF.CMSAttachment = new Class({
  3. Extends: MWF.APPAttachment,
  4. options: {
  5. "moduleEvents": ["upload", "delete"]
  6. },
  7. initialize: function(node, json, form, options){
  8. this.node = $(node);
  9. this.node.store("module", this);
  10. this.json = json;
  11. this.form = form;
  12. this.field = true;
  13. },
  14. _loadUserInterface: function(){
  15. this.node.empty();
  16. this.loadAttachmentController();
  17. },
  18. loadAttachmentController: function(){
  19. MWF.require("MWF.widget.AttachmentController", function() {
  20. var options = {
  21. //"style" : "cms",
  22. "title": "附件区域",
  23. "listStyle": this.json.listStyle || "icon",
  24. "size": this.json.size || "max",
  25. "resize": (this.json.size=="true") ? true : false,
  26. "attachmentCount": this.json.attachmentCount || 0,
  27. "isUpload": (this.json.isUpload=="y") ? true : false,
  28. "isDelete": (this.json.isDelete=="y") ? true : false,
  29. "isReplace": (this.json.isReplace=="y") ? true : false,
  30. "isDownload": (this.json.isDownload=="y") ? true : false,
  31. "isSizeChange": (this.json.isSizeChange=="y") ? true : false,
  32. "readonly": (this.json.readonly=="y") ? true : false
  33. };
  34. if (this.readonly) options.readonly = true;
  35. this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
  36. this.attachmentController.load();
  37. this.form.businessData.attachmentList.each(function (att) {
  38. if (att.site==this.json.id) this.attachmentController.addAttachment(att);
  39. //if (att.fileType.toLowerCase()==this.json.id.toLowerCase()) this.attachmentController.addAttachment(att);
  40. }.bind(this));
  41. }.bind(this));
  42. },
  43. loadAttachmentSelecter: function( option, callback ){
  44. MWF.require("MWF.widget.AttachmentSelector", function() {
  45. var options = {
  46. //"style" : "cms",
  47. "title": "选择附件",
  48. "listStyle": "icon",
  49. "selectType" : "all",
  50. "size": "max",
  51. "attachmentCount": 0,
  52. "isUpload": true,
  53. "isDelete": true,
  54. "isReplace": true,
  55. "isDownload": true,
  56. "toBase64" : true,
  57. "base64MaxSize" : 800,
  58. "readonly": false
  59. };
  60. options = Object.merge( options, option );
  61. if (this.readonly) options.readonly = true;
  62. this.attachmentController = new MWF.widget.AttachmentSelector(this.node, this, options);
  63. this.attachmentController.load();
  64. this.postSelect = callback;
  65. this.form.businessData.attachmentList.each(function (att) {
  66. this.attachmentController.addAttachment(att);
  67. }.bind(this));
  68. }.bind(this));
  69. },
  70. selectAttachment: function(e, node, attachments){
  71. //if( attachments.length > 0 ){
  72. // this.form.documentAction.getAttachmentUrl(attachments[attachments.length-1].data.id, this.form.businessData.document.id, function(url){
  73. // if(this.postSelect)this.postSelect( url )
  74. // }.bind(this))
  75. //}
  76. if( attachments.length > 0 ){
  77. var data = attachments[attachments.length-1].data;
  78. this.form.documentAction.getAttachmentUrl( data.id, this.form.businessData.document.id, function(url){
  79. if( this.attachmentController.options.toBase64 ){
  80. this.form.documentAction.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  81. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  82. if(this.postSelect)this.postSelect( url , data, base64Code )
  83. }.bind(this) )
  84. }else{
  85. if(this.postSelect)this.postSelect( url , data )
  86. }
  87. }.bind(this))
  88. }
  89. },
  90. createUploadFileNode: function(){
  91. this.uploadFileAreaNode = new Element("div");
  92. var html = "<input name=\"file\" type=\"file\" multiple/>";
  93. this.uploadFileAreaNode.set("html", html);
  94. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  95. this.fileUploadNode.addEvent("change", function(){
  96. this.validationMode();
  97. var files = this.fileUploadNode.files;
  98. if (files.length){
  99. if ((files.length+this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount>0){
  100. var content = MWF.xApplication.cms.Xform.LP.uploadMore;
  101. content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  102. this.form.notice(content, "error");
  103. }else{
  104. for (var i = 0; i < files.length; i++) {
  105. var file = files.item(i);
  106. var formData = new FormData();
  107. formData.append('file', file);
  108. formData.append('site', this.json.id);
  109. //formData.append('folder', folderId);
  110. this.form.documentAction.uploadAttachment(this.form.businessData.document.id ,function(o, text){
  111. if (o.id){
  112. this.form.documentAction.getAttachment(o.id, this.form.businessData.document.id, function(json){
  113. if (json.data){
  114. this.attachmentController.addAttachment(json.data);
  115. this.form.businessData.attachmentList.push(json.data);
  116. }
  117. this.attachmentController.checkActions();
  118. this.fireEvent("upload", [json.data]);
  119. }.bind(this))
  120. }
  121. this.attachmentController.checkActions();
  122. }.bind(this), null, formData, file);
  123. }
  124. }
  125. }
  126. }.bind(this));
  127. },
  128. getData: function(){
  129. return this.attachmentController.getAttachmentNames();
  130. },
  131. //getInputData : function(){
  132. // return this.getData();
  133. //},
  134. deleteAttachment: function(attachment){
  135. this.fireEvent("delete", [attachment.data]);
  136. this.form.documentAction.deleteAttachment(attachment.data.id, this.form.businessData.document.id, function(json ){
  137. this.attachmentController.removeAttachment(attachment);
  138. //this.form.businessData.attachmentList.erase( attachment.data )
  139. this.attachmentController.checkActions();
  140. }.bind(this));
  141. },
  142. createReplaceFileNode: function(attachment){
  143. this.replaceFileAreaNode = new Element("div");
  144. var html = "<input name=\"file\" type=\"file\" multiple/>";
  145. this.replaceFileAreaNode.set("html", html);
  146. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  147. this.fileReplaceNode.addEvent("change", function(){
  148. var files = this.fileReplaceNode.files;
  149. if (files.length){
  150. for (var i = 0; i < files.length; i++) {
  151. var file = files.item(i);
  152. var formData = new FormData();
  153. formData.append('file', file);
  154. // formData.append('site', this.json.id);
  155. this.form.documentAction.replaceAttachment(attachment.data.id, this.form.businessData.document.id ,function(o, text){
  156. this.form.documentAction.getAttachment(attachment.data.id, this.form.businessData.document.id, function(json){
  157. attachment.data = json.data;
  158. attachment.reload();
  159. this.attachmentController.checkActions();
  160. }.bind(this))
  161. }.bind(this), null, formData, file);
  162. }
  163. }
  164. }.bind(this));
  165. },
  166. downloadAttachment: function(e, node, attachments){
  167. if (this.form.businessData.document){
  168. attachments.each(function(att){
  169. this.form.documentAction.getAttachmentStream(att.data.id, this.form.businessData.document.id);
  170. }.bind(this));
  171. }
  172. },
  173. openAttachment: function(e, node, attachments){
  174. if (this.form.businessData.document){
  175. attachments.each(function(att){
  176. this.form.documentAction.getAttachmentData(att.data.id, this.form.businessData.document.id);
  177. }.bind(this));
  178. }
  179. //this.downloadAttachment(e, node, attachment);
  180. },
  181. getAttachmentUrl: function(attachment, callback){
  182. if (this.form.businessData.document){
  183. this.form.documentAction.getAttachmentUrl(attachment.data.id, this.form.businessData.document.id, callback);
  184. }
  185. },
  186. validationConfigItem: function(routeName, data){
  187. var flag = (data.status=="all") ? true: (routeName == "publish");
  188. if (flag){
  189. var n = this.getData();
  190. var v = (data.valueType=="value") ? n : n.length;
  191. switch (data.operateor){
  192. case "isnull":
  193. if (!v){
  194. this.notValidationMode(data.prompt);
  195. return false;
  196. }
  197. break;
  198. case "notnull":
  199. if (v){
  200. this.notValidationMode(data.prompt);
  201. return false;
  202. }
  203. break;
  204. case "gt":
  205. if (v>data.value){
  206. this.notValidationMode(data.prompt);
  207. return false;
  208. }
  209. break;
  210. case "lt":
  211. if (v<data.value){
  212. this.notValidationMode(data.prompt);
  213. return false;
  214. }
  215. break;
  216. case "equal":
  217. if (v==data.value){
  218. this.notValidationMode(data.prompt);
  219. return false;
  220. }
  221. break;
  222. case "neq":
  223. if (v!=data.value){
  224. this.notValidationMode(data.prompt);
  225. return false;
  226. }
  227. break;
  228. case "contain":
  229. if (v.indexOf(data.value)!=-1){
  230. this.notValidationMode(data.prompt);
  231. return false;
  232. }
  233. break;
  234. case "notcontain":
  235. if (v.indexOf(data.value)==-1){
  236. this.notValidationMode(data.prompt);
  237. return false;
  238. }
  239. break;
  240. }
  241. }
  242. return true;
  243. }
  244. });