PriorityAttachment.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.Strategy.PriorityAttachment = new Class({
  3. Implements: [Options, Events],
  4. options: {
  5. "workId" : "",
  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": "default",
  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. if(this.options.workId){
  66. this.actions.listPriorityAttachment(this.options.workId, function(json){
  67. if(callback)callback(this.transportData(json));
  68. }.bind(this))
  69. }
  70. },
  71. createUploadFileNode: function () {
  72. this.uploadFileAreaNode = new Element("div");
  73. var html = "<input name=\"file\" type=\"file\" multiple/>";
  74. this.uploadFileAreaNode.set("html", html);
  75. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  76. this.fileUploadNode.addEvent("change", function () {
  77. this.isQueryUploadSuccess = true;
  78. this.fireEvent( "queryUploadAttachment" );
  79. if( this.isQueryUploadSuccess ){
  80. var files = this.fileUploadNode.files;
  81. if (files.length) {
  82. for (var i = 0; i < files.length; i++) {
  83. var file = files.item(i);
  84. var formData = new FormData();
  85. formData.append('file', file);
  86. formData.append('site', this.options.workId);
  87. this.actions.uploadPriorityAttachment(this.options.workId, function (o, text) {
  88. j = JSON.decode(text);
  89. //if (j.data.length>0 && j.data[0] && j.data[0].id) {
  90. if (j.data && j.data.id) {
  91. this.actions.getPriorityAttachment(j.data.id, this.options.workId, function (json) {
  92. json = this.transportData(json);
  93. if (json.data) {
  94. this.attachmentController.addAttachment(json.data);
  95. //this.attachmentList.push(json.data);
  96. }
  97. this.attachmentController.checkActions();
  98. this.fireEvent("upload", [json.data]);
  99. }.bind(this))
  100. }
  101. this.attachmentController.checkActions();
  102. }.bind(this), null, formData, file);
  103. }
  104. }
  105. }else{
  106. this.uploadFileAreaNode.destroy();
  107. this.uploadFileAreaNode = false;
  108. }
  109. }.bind(this));
  110. },
  111. uploadAttachment: function (e, node) {
  112. if (!this.uploadFileAreaNode) {
  113. this.createUploadFileNode();
  114. }
  115. this.fileUploadNode.click();
  116. },
  117. deleteAttachments: function (e, node, attachments) {
  118. var names = [];
  119. attachments.each(function (attachment) {
  120. names.push(attachment.data.name);
  121. }.bind(this));
  122. var _self = this;
  123. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  124. while (attachments.length) {
  125. attachment = attachments.shift();
  126. _self.deleteAttachment(attachment);
  127. }
  128. this.close();
  129. }, function () {
  130. this.close();
  131. }, null);
  132. },
  133. deleteAttachment: function (attachment) {
  134. this.fireEvent("delete", [attachment.data]);
  135. this.actions.deletePriorityAttachment(attachment.data.id, this.options.workId, function (json) {
  136. this.attachmentController.removeAttachment(attachment);
  137. //this.form.businessData.attachmentList.erase( attachment.data )
  138. this.attachmentController.checkActions();
  139. }.bind(this));
  140. },
  141. replaceAttachment: function (e, node, attachment) {
  142. var _self = this;
  143. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  144. _self.replaceAttachmentFile(attachment);
  145. this.close();
  146. }, function () {
  147. this.close();
  148. }, null);
  149. },
  150. createReplaceFileNode: function (attachment) {
  151. this.replaceFileAreaNode = new Element("div");
  152. var html = "<input name=\"file\" type=\"file\" multiple/>";
  153. this.replaceFileAreaNode.set("html", html);
  154. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  155. this.fileReplaceNode.addEvent("change", function () {
  156. var files = this.fileReplaceNode.files;
  157. if (files.length) {
  158. for (var i = 0; i < files.length; i++) {
  159. var file = files.item(i);
  160. var formData = new FormData();
  161. formData.append('file', file);
  162. // formData.append('site', this.json.id);
  163. this.actions.replaceAttachment(attachment.data.id, this.options.workId, function (o, text) {
  164. this.form.documentAction.getAttachment(attachment.data.id, this.options.workId, function (json) {
  165. attachment.data = json.data;
  166. attachment.reload();
  167. this.attachmentController.checkActions();
  168. }.bind(this))
  169. }.bind(this), null, formData, file);
  170. }
  171. }
  172. }.bind(this));
  173. },
  174. replaceAttachmentFile: function (attachment) {
  175. if (!this.replaceFileAreaNode) {
  176. this.createReplaceFileNode(attachment);
  177. }
  178. this.fileReplaceNode.click();
  179. },
  180. downloadAttachment: function (e, node, attachments) {
  181. attachments.each(function (att) {
  182. this.actions.getPriorityAttachmentStream(att.data.id, this.options.workId);
  183. }.bind(this));
  184. },
  185. openAttachment: function (e, node, attachments) {
  186. attachments.each(function (att) {
  187. this.actions.getPriorityAttachmentStream(att.data.id, this.options.workId);
  188. }.bind(this));
  189. },
  190. getAttachmentUrl: function (attachment, callback) {
  191. this.actions.getAttachmentUrl(attachment.data.id, this.options.workId, callback);
  192. }
  193. });