Attachment.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.Execution.Attachment = 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.listAttachment(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.uploadAttachment(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))
  97. }
  98. }
  99. this.attachmentController.checkActions();
  100. }.bind(this), null, formData, file,this.options.documentId);
  101. }
  102. }
  103. }else{
  104. this.uploadFileAreaNode.destroy();
  105. this.uploadFileAreaNode = false;
  106. }
  107. }.bind(this));
  108. },
  109. uploadAttachment: function (e, node) {
  110. if (!this.uploadFileAreaNode) {
  111. this.createUploadFileNode();
  112. }
  113. this.fileUploadNode.click();
  114. },
  115. deleteAttachments: function (e, node, attachments) {
  116. var names = [];
  117. attachments.each(function (attachment) {
  118. names.push(attachment.data.name);
  119. }.bind(this));
  120. var _self = this;
  121. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  122. while (attachments.length) {
  123. attachment = attachments.shift();
  124. _self.deleteAttachment(attachment);
  125. }
  126. this.close();
  127. }, function () {
  128. this.close();
  129. }, null);
  130. },
  131. deleteAttachment: function (attachment) {
  132. this.fireEvent("delete", [attachment.data]);
  133. this.actions.deleteAttachment(attachment.data.id, this.documentId, function (json) {
  134. this.attachmentController.removeAttachment(attachment);
  135. //this.form.businessData.attachmentList.erase( attachment.data )
  136. this.attachmentController.checkActions();
  137. }.bind(this));
  138. },
  139. replaceAttachment: function (e, node, attachment) {
  140. var _self = this;
  141. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  142. _self.replaceAttachmentFile(attachment);
  143. this.close();
  144. }, function () {
  145. this.close();
  146. }, null);
  147. },
  148. createReplaceFileNode: function (attachment) {
  149. this.replaceFileAreaNode = new Element("div");
  150. var html = "<input name=\"file\" type=\"file\" multiple/>";
  151. this.replaceFileAreaNode.set("html", html);
  152. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  153. this.fileReplaceNode.addEvent("change", function () {
  154. var files = this.fileReplaceNode.files;
  155. if (files.length) {
  156. for (var i = 0; i < files.length; i++) {
  157. var file = files.item(i);
  158. var formData = new FormData();
  159. formData.append('file', file);
  160. // formData.append('site', this.json.id);
  161. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  162. this.form.documentAction.getAttachment(attachment.data.id, this.options.documentId, function (json) {
  163. attachment.data = json.data;
  164. attachment.reload();
  165. this.attachmentController.checkActions();
  166. }.bind(this))
  167. }.bind(this), null, formData, file);
  168. }
  169. }
  170. }.bind(this));
  171. },
  172. replaceAttachmentFile: function (attachment) {
  173. if (!this.replaceFileAreaNode) {
  174. this.createReplaceFileNode(attachment);
  175. }
  176. this.fileReplaceNode.click();
  177. },
  178. downloadAttachment: function (e, node, attachments) {
  179. attachments.each(function (att) {
  180. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  181. }.bind(this));
  182. },
  183. openAttachment: function (e, node, attachments) {
  184. attachments.each(function (att) {
  185. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  186. }.bind(this));
  187. },
  188. getAttachmentUrl: function (attachment, callback) {
  189. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  190. }
  191. });