TaskAttachment.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.TeamWork.TaskAttachment = 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. if(this.options.size=="min"){
  21. if(this.attachmentController.minActionAreaNode){
  22. this.attachmentController.minActionAreaNode.setStyles({
  23. "float":"left",
  24. //"margin-top":"10px"
  25. });
  26. }
  27. }
  28. },
  29. loadAttachmentController: function () {
  30. var options = {
  31. "style": "default",
  32. "title": "",
  33. "listStyle": "list",
  34. "size": this.options.size ,
  35. "resize": true,
  36. //"attachmentCount": this.json.attachmentCount || 0,
  37. "isUpload": (this.options.isNew || this.options.isEdited) ? true : false,
  38. "isDelete": (this.options.isNew || this.options.isEdited) ? true : false,
  39. "isReplace": false,
  40. "isDownload": true,
  41. "isSizeChange": false,
  42. "readonly": (!this.options.isNew && !this.options.isEdited ) ? true : false
  43. };
  44. this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
  45. this.attachmentController.load();
  46. this.listAttachment( function( json ){
  47. json.data.each(function (att) {
  48. this.attachmentController.addAttachment(att);
  49. }.bind(this));
  50. }.bind(this))
  51. },
  52. transportData : function( json ){
  53. if( typeOf(json.data) == "array" ){
  54. json.data.each(function(d){
  55. d.person = d.creatorUid;
  56. d.lastUpdateTime = d.updateTime;
  57. })
  58. }else if( typeOf(json.data) == "object" ){
  59. var d = json.data;
  60. d.person = d.creatorUid;
  61. d.lastUpdateTime = d.updateTime;
  62. }else{
  63. json.each(function(d){
  64. d.person = d.creatorUid;
  65. d.lastUpdateTime = d.updateTime;
  66. })
  67. }
  68. return json;
  69. },
  70. listAttachment: function( callback ){
  71. this.actions.attachmentListByTaskId(this.options.documentId, function(json){
  72. if(callback)callback(this.transportData(json));
  73. }.bind(this))
  74. },
  75. createUploadFileNode: function () {
  76. this.uploadFileAreaNode = new Element("div");
  77. var html = "<input name=\"file\" type=\"file\" multiple/>";
  78. this.uploadFileAreaNode.set("html", html);
  79. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  80. this.fileUploadNode.addEvent("change", function () {
  81. this.isQueryUploadSuccess = true;
  82. this.fireEvent( "queryUploadAttachment" );
  83. if( this.isQueryUploadSuccess ){
  84. var files = this.fileUploadNode.files;
  85. if (files.length) {
  86. for (var i = 0; i < files.length; i++) {
  87. var file = files.item(i);
  88. var formData = new FormData();
  89. formData.append('file', file);
  90. formData.append('site', this.options.documentId);
  91. this.actions.attachmentTaskUpload(this.options.documentId, function (json) {
  92. if(json.type=="success"){
  93. if(json.id){
  94. this.actions.attachmentGet(json.id, function (json) {
  95. json = this.transportData(json);
  96. if (json.data) {
  97. this.attachmentController.addAttachment(json.data);
  98. //this.attachmentList.push(json.data);
  99. }
  100. this.attachmentController.checkActions();
  101. this.fireEvent("upload", [json.data]);
  102. }.bind(this))
  103. }
  104. }
  105. this.attachmentController.checkActions();
  106. }.bind(this), null, formData, file,this.options.documentId);
  107. }
  108. }
  109. }else{
  110. this.uploadFileAreaNode.destroy();
  111. this.uploadFileAreaNode = false;
  112. }
  113. }.bind(this));
  114. },
  115. uploadAttachment: function (e, node) {
  116. if (!this.uploadFileAreaNode) {
  117. this.createUploadFileNode();
  118. }
  119. this.fileUploadNode.click();
  120. },
  121. deleteAttachments: function (e, node, attachments) {
  122. var names = [];
  123. attachments.each(function (attachment) {
  124. names.push(attachment.data.name);
  125. }.bind(this));
  126. var _self = this;
  127. this.app.confirm("warn", e, this.lp.common.confirm.removeTitle, this.lp.common.confirm.removeContent + "( " + names.join(", ") + " )", 300, 120, function () {
  128. while (attachments.length) {
  129. attachment = attachments.shift();
  130. _self.deleteAttachment(attachment);
  131. }
  132. this.close();
  133. }, function () {
  134. this.close();
  135. }, null);
  136. },
  137. deleteAttachment: function (attachment) {
  138. this.fireEvent("delete", [attachment.data]);
  139. this.actions.attachmentRemove(attachment.data.id, function (json) {
  140. this.attachmentController.removeAttachment(attachment);
  141. //this.form.businessData.attachmentList.erase( attachment.data )
  142. this.attachmentController.checkActions();
  143. }.bind(this));
  144. },
  145. replaceAttachment: function (e, node, attachment) {
  146. var _self = this;
  147. this.form.confirm("warn", e, this.lp.common.confirm.replaceTitle, this.lp.common.confirm.replaceContent + "( " + attachment.data.name + " )", 300, 120, function () {
  148. _self.replaceAttachmentFile(attachment);
  149. this.close();
  150. }, function () {
  151. this.close();
  152. }, null);
  153. },
  154. createReplaceFileNode: function (attachment) {
  155. this.replaceFileAreaNode = new Element("div");
  156. var html = "<input name=\"file\" type=\"file\" multiple/>";
  157. this.replaceFileAreaNode.set("html", html);
  158. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  159. this.fileReplaceNode.addEvent("change", function () {
  160. var files = this.fileReplaceNode.files;
  161. if (files.length) {
  162. for (var i = 0; i < files.length; i++) {
  163. var file = files.item(i);
  164. var formData = new FormData();
  165. formData.append('file', file);
  166. // formData.append('site', this.json.id);
  167. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  168. this.form.documentAction.getAttachment(attachment.data.id, this.options.documentId, function (json) {
  169. attachment.data = json.data;
  170. attachment.reload();
  171. this.attachmentController.checkActions();
  172. }.bind(this))
  173. }.bind(this), null, formData, file);
  174. }
  175. }
  176. }.bind(this));
  177. },
  178. replaceAttachmentFile: function (attachment) {
  179. if (!this.replaceFileAreaNode) {
  180. this.createReplaceFileNode(attachment);
  181. }
  182. this.fileReplaceNode.click();
  183. },
  184. downloadAttachment: function (e, node, attachments) {
  185. attachments.each(function (att) {
  186. this.actions.attachmentDownloadStream(att.data.id, this.options.documentId);
  187. }.bind(this));
  188. },
  189. openAttachment: function (e, node, attachments) {
  190. attachments.each(function (att) {
  191. this.actions.attachmentDownloadStream(att.data.id, this.options.documentId);
  192. }.bind(this));
  193. },
  194. getAttachmentUrl: function (attachment, callback) {
  195. this.actions.attachmentDownloadUrl(attachment.data.id, this.options.documentId, callback);
  196. }
  197. });