Attachment.js 8.1 KB

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