ReportAttachment.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.Execution.ReportAttachment = new Class({
  3. Implements: [Options, Events],
  4. options: {
  5. "documentId" : "",
  6. "isNew": false,
  7. "isEdited" : true,
  8. "size" : "max",
  9. "isSizeChange" : true
  10. },
  11. initialize: function (node, app, actions, lp, options) {
  12. this.setOptions(options);
  13. this.app = app;
  14. this.node = $(node);
  15. this.actions = actions;
  16. this.lp = lp;
  17. },
  18. load: function () {
  19. this.loadAttachmentController();
  20. },
  21. loadAttachmentController: function () {
  22. var options = {
  23. "style": "cms",
  24. "title": "附件区域",
  25. "size": this.options.size ,
  26. "resize": true,
  27. //"attachmentCount": this.json.attachmentCount || 0,
  28. "isUpload": (this.options.isNew || this.options.isEdited) ? true : false,
  29. "isDelete": (this.options.isNew || this.options.isEdited) ? true : false,
  30. "isReplace": false,
  31. "isDownload": true,
  32. "isSizeChange": this.options.isSizeChange,
  33. "readonly": (!this.options.isNew && !this.options.isEdited ) ? true : false
  34. }
  35. this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
  36. this.attachmentController.load();
  37. //this.actions.listAttachmentInfo.each(function (att) {
  38. // this.attachmentController.addAttachment(att);
  39. //}.bind(this));
  40. this.listAttachment( function( json ){
  41. json.data.each(function (att) {
  42. this.attachmentController.addAttachment(att);
  43. }.bind(this));
  44. }.bind(this))
  45. },
  46. transportData : function( json ){
  47. if( typeOf(json.data) == "array" ){
  48. json.data.each(function(d){
  49. d.person = d.creatorUid;
  50. d.lastUpdateTime = d.updateTime;
  51. })
  52. }else if( typeOf(json.data) == "object" ){
  53. var d = json.data;
  54. d.person = d.creatorUid;
  55. d.lastUpdateTime = d.updateTime;
  56. }else{
  57. json.each(function(d){
  58. d.person = d.creatorUid;
  59. d.lastUpdateTime = d.updateTime;
  60. })
  61. }
  62. return json;
  63. },
  64. listAttachment: function( callback ){
  65. this.actions.listReportAttachment(this.options.documentId, function(json){
  66. if(callback)callback(this.transportData(json));
  67. }.bind(this))
  68. },
  69. createUploadFileNode: function () {
  70. this.uploadFileAreaNode = new Element("div");
  71. var html = "<input name=\"file\" type=\"file\" multiple/>";
  72. this.uploadFileAreaNode.set("html", html);
  73. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  74. this.fileUploadNode.addEvent("change", function () {
  75. this.isQueryUploadSuccess = true;
  76. this.fireEvent( "queryUploadAttachment" );
  77. if( this.isQueryUploadSuccess ){
  78. var files = this.fileUploadNode.files;
  79. if (files.length) {
  80. for (var i = 0; i < files.length; i++) {
  81. var file = files.item(i);
  82. var formData = new FormData();
  83. formData.append('file', file);
  84. //formData.append('site', this.options.documentId);
  85. this.actions.uploadReportAttachment(this.options.documentId, function (json) {
  86. if(json.type=="success"){
  87. if(json.id){
  88. this.actions.getAttachment(json.id, this.options.documentId, function (json) {
  89. json = this.transportData(json);
  90. if (json.data) {
  91. this.attachmentController.addAttachment(json.data);
  92. //this.attachmentList.push(json.data);
  93. }
  94. this.attachmentController.checkActions();
  95. this.fireEvent("upload", [json.data]);
  96. }.bind(this),function(xhr,text,error){
  97. var errorText = error;
  98. if (xhr) errorMessage = xhr.responseText;
  99. var e = JSON.parse(errorMessage);
  100. if(e.message){
  101. this.app.notice( e.message,"error");
  102. }else{
  103. this.app.notice( errorText,"error");
  104. }
  105. }.bind(this))
  106. }
  107. }
  108. this.attachmentController.checkActions();
  109. }.bind(this), function(){}.bind(this), formData, file,this.options.documentId);
  110. //this.actions.uploadReportAttachment(this.options.documentId, function (o, text) {
  111. // j = JSON.decode(text);
  112. // if (j.data.length>0 && j.data[0] && j.data[0].id) {
  113. // this.actions.getAttachment(j.data[0].id, this.options.documentId, function (json) {
  114. // json = this.transportData(json);
  115. // if (json.data) {
  116. // this.attachmentController.addAttachment(json.data);
  117. // //this.attachmentList.push(json.data);
  118. // }
  119. // this.attachmentController.checkActions();
  120. //
  121. // this.fireEvent("upload", [json.data]);
  122. // }.bind(this),function(xhr,text,error){
  123. // var errorText = error;
  124. // if (xhr) errorMessage = xhr.responseText;
  125. // var e = JSON.parse(errorMessage);
  126. // if(e.message){
  127. // this.app.notice( e.message,"error");
  128. // }else{
  129. // this.app.notice( errorText,"error");
  130. // }
  131. // }.bind(this))
  132. // }
  133. // this.attachmentController.checkActions();
  134. //}.bind(this), function(){}.bind(this), formData, file);
  135. }
  136. }
  137. }else{
  138. this.uploadFileAreaNode.destroy();
  139. this.uploadFileAreaNode = false;
  140. }
  141. }.bind(this));
  142. },
  143. uploadAttachment: function (e, node) {
  144. if (!this.uploadFileAreaNode) {
  145. this.createUploadFileNode();
  146. }
  147. this.fileUploadNode.click();
  148. },
  149. deleteAttachments: function (e, node, attachments) {
  150. var names = [];
  151. attachments.each(function (attachment) {
  152. names.push(attachment.data.name);
  153. }.bind(this));
  154. var _self = this;
  155. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  156. while (attachments.length) {
  157. attachment = attachments.shift();
  158. _self.deleteAttachment(attachment);
  159. }
  160. this.close();
  161. }, function () {
  162. this.close();
  163. }, null);
  164. },
  165. deleteAttachment: function (attachment) {
  166. this.fireEvent("delete", [attachment.data]);
  167. this.actions.deleteReportAttachment(attachment.data.id, this.documentId, function (json) {
  168. this.attachmentController.removeAttachment(attachment);
  169. //this.form.businessData.attachmentList.erase( attachment.data )
  170. this.attachmentController.checkActions();
  171. }.bind(this));
  172. },
  173. replaceAttachment: function (e, node, attachment) {
  174. var _self = this;
  175. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  176. _self.replaceAttachmentFile(attachment);
  177. this.close();
  178. }, function () {
  179. this.close();
  180. }, null);
  181. },
  182. createReplaceFileNode: function (attachment) {
  183. this.replaceFileAreaNode = new Element("div");
  184. var html = "<input name=\"file\" type=\"file\" multiple/>";
  185. this.replaceFileAreaNode.set("html", html);
  186. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  187. this.fileReplaceNode.addEvent("change", function () {
  188. var files = this.fileReplaceNode.files;
  189. if (files.length) {
  190. for (var i = 0; i < files.length; i++) {
  191. var file = files.item(i);
  192. var formData = new FormData();
  193. formData.append('file', file);
  194. // formData.append('site', this.json.id);
  195. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  196. this.form.documentAction.getAttachment(attachment.data.id, this.opetions.documentId, function (json) {
  197. attachment.data = json.data;
  198. attachment.reload();
  199. this.attachmentController.checkActions();
  200. }.bind(this))
  201. }.bind(this), null, formData, file);
  202. }
  203. }
  204. }.bind(this));
  205. },
  206. replaceAttachmentFile: function (attachment) {
  207. if (!this.replaceFileAreaNode) {
  208. this.createReplaceFileNode(attachment);
  209. }
  210. this.fileReplaceNode.click();
  211. },
  212. downloadAttachment: function (e, node, attachments) {
  213. attachments.each(function (att) {
  214. this.actions.getReportAttachmentStream(att.data.id, this.options.documentId);
  215. }.bind(this));
  216. },
  217. openAttachment: function (e, node, attachments) {
  218. attachments.each(function (att) {
  219. this.actions.getReportAttachmentStream(att.data.id, this.options.documentId);
  220. }.bind(this));
  221. },
  222. getAttachmentUrl: function (attachment, callback) {
  223. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  224. }
  225. });