Attachment.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.Report = MWF.xApplication.Report || {};
  3. MWF.xApplication.Report.Attachment = new Class({
  4. Implements: [Options, Events],
  5. options: {
  6. "documentId" : "",
  7. "isNew": false,
  8. "isEdited" : true,
  9. "size" : "max",
  10. "isSizeChange" : true
  11. },
  12. initialize: function (node, app, actions, lp, options) {
  13. this.setOptions(options);
  14. this.app = app;
  15. this.node = $(node);
  16. this.actions = actions;
  17. this.lp = lp;
  18. },
  19. load: function () {
  20. this.loadAttachmentController();
  21. },
  22. loadAttachmentController: function () {
  23. var options = {
  24. "style": "default",
  25. "title": "附件区域",
  26. "size": this.options.size ,
  27. "resize": true,
  28. //"attachmentCount": this.json.attachmentCount || 0,
  29. "isUpload": (this.options.isNew || this.options.isEdited) ? true : false,
  30. "isDelete": (this.options.isNew || this.options.isEdited) ? true : false,
  31. "isReplace": false,
  32. "isDownload": true,
  33. "isSizeChange": this.options.isSizeChange,
  34. "readonly": (!this.options.isNew && !this.options.isEdited ) ? true : false
  35. };
  36. this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
  37. this.attachmentController.load();
  38. //this.actions.listAttachmentInfo.each(function (att) {
  39. // this.attachmentController.addAttachment(att);
  40. //}.bind(this));
  41. if( this.data ){
  42. this.data.each(function (att) {
  43. this.attachmentController.addAttachment(att);
  44. }.bind(this));
  45. }else if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  46. this.listAttachment( function( json ){
  47. json.data.each(function (att) {
  48. this.attachmentController.addAttachment(att);
  49. }.bind(this));
  50. }.bind(this))
  51. }
  52. },
  53. transportData : function( json ){
  54. if( typeOf(json.data) == "array" ){
  55. json.data.each(function(d){
  56. d.person = d.creatorUid;
  57. d.lastUpdateTime = d.updateTime;
  58. })
  59. }else if( typeOf(json.data) == "object" ){
  60. var d = json.data;
  61. d.person = d.creatorUid;
  62. d.lastUpdateTime = d.updateTime;
  63. }else{
  64. json.each(function(d){
  65. d.person = d.creatorUid;
  66. d.lastUpdateTime = d.updateTime;
  67. })
  68. }
  69. return json;
  70. },
  71. listAttachment: function( callback ){
  72. if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  73. //this.actions.listAttachment(this.options.documentId, function(json){
  74. // if(callback)callback(this.transportData(json));
  75. //}.bind(this))
  76. }
  77. },
  78. createUploadFileNode: function () {
  79. this.uploadFileAreaNode = new Element("div");
  80. var html = "<input name=\"file\" type=\"file\" multiple/>";
  81. this.uploadFileAreaNode.set("html", html);
  82. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  83. this.fileUploadNode.addEvent("change", function () {
  84. this.isQueryUploadSuccess = true;
  85. this.fireEvent( "queryUploadAttachment" );
  86. if( this.isQueryUploadSuccess ){
  87. var files = this.fileUploadNode.files;
  88. if (files.length) {
  89. for (var i = 0; i < files.length; i++) {
  90. var file = files.item(i);
  91. var formData = new FormData();
  92. formData.append('file', file);
  93. formData.append('site', this.options.documentId);
  94. this.actions.uploadAttachment(this.options.documentId, function (o, text) {
  95. j = JSON.decode(text);
  96. if ( j.data ) {
  97. //j.userMessage
  98. var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  99. this.actions.getAttachment(aid, this.options.documentId, function (json) {
  100. json = this.transportData(json);
  101. if (json.data) {
  102. this.attachmentController.addAttachment(json.data);
  103. //this.attachmentList.push(json.data);
  104. }
  105. this.attachmentController.checkActions();
  106. this.fireEvent("upload", [json.data]);
  107. }.bind(this))
  108. }
  109. this.attachmentController.checkActions();
  110. }.bind(this), null, formData, file);
  111. }
  112. }
  113. }else{
  114. this.uploadFileAreaNode.destroy();
  115. this.uploadFileAreaNode = false;
  116. }
  117. }.bind(this));
  118. },
  119. uploadAttachment: function (e, node) {
  120. if (!this.uploadFileAreaNode) {
  121. this.createUploadFileNode();
  122. }
  123. this.fileUploadNode.click();
  124. },
  125. deleteAttachments: function (e, node, attachments) {
  126. var names = [];
  127. attachments.each(function (attachment) {
  128. names.push(attachment.data.name);
  129. }.bind(this));
  130. var _self = this;
  131. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  132. while (attachments.length) {
  133. attachment = attachments.shift();
  134. _self.deleteAttachment(attachment);
  135. }
  136. this.close();
  137. }, function () {
  138. this.close();
  139. }, null);
  140. },
  141. deleteAttachment: function (attachment) {
  142. this.fireEvent("delete", [attachment.data]);
  143. this.actions.deleteAttachment(attachment.data.id, this.documentId, function (json) {
  144. this.attachmentController.removeAttachment(attachment);
  145. //this.form.businessData.attachmentList.erase( attachment.data )
  146. this.attachmentController.checkActions();
  147. }.bind(this));
  148. },
  149. replaceAttachment: function (e, node, attachment) {
  150. var _self = this;
  151. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  152. _self.replaceAttachmentFile(attachment);
  153. this.close();
  154. }, function () {
  155. this.close();
  156. }, null);
  157. },
  158. createReplaceFileNode: function (attachment) {
  159. this.replaceFileAreaNode = new Element("div");
  160. var html = "<input name=\"file\" type=\"file\" multiple/>";
  161. this.replaceFileAreaNode.set("html", html);
  162. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  163. this.fileReplaceNode.addEvent("change", function () {
  164. var files = this.fileReplaceNode.files;
  165. if (files.length) {
  166. for (var i = 0; i < files.length; i++) {
  167. var file = files.item(i);
  168. var formData = new FormData();
  169. formData.append('file', file);
  170. // formData.append('site', this.json.id);
  171. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  172. this.form.documentAction.getAttachment(attachment.data.id, this.opetions.documentId, function (json) {
  173. attachment.data = json.data;
  174. attachment.reload();
  175. this.attachmentController.checkActions();
  176. }.bind(this))
  177. }.bind(this), null, formData, file);
  178. }
  179. }
  180. }.bind(this));
  181. },
  182. replaceAttachmentFile: function (attachment) {
  183. if (!this.replaceFileAreaNode) {
  184. this.createReplaceFileNode(attachment);
  185. }
  186. this.fileReplaceNode.click();
  187. },
  188. downloadAttachment: function (e, node, attachments) {
  189. if( this.app.access.isAnonymousDynamic() ){
  190. this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  191. }else {
  192. attachments.each(function (att) {
  193. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  194. }.bind(this));
  195. }
  196. },
  197. openAttachment: function (e, node, attachments) {
  198. if( this.app.access.isAnonymousDynamic() ){
  199. this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  200. }else{
  201. attachments.each(function (att) {
  202. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  203. }.bind(this));
  204. }
  205. },
  206. getAttachmentUrl: function (attachment, callback) {
  207. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  208. },
  209. getAttachmentData : function(){
  210. var data = [];
  211. this.attachmentController.attachments.each(function( att ){
  212. data.push(att.data)
  213. });
  214. return data;
  215. },
  216. getAttachmentIds : function(){
  217. var ids = [];
  218. this.attachmentController.attachments.each(function( att ){
  219. ids.push(att.data.id)
  220. });
  221. return ids;
  222. },
  223. loadAttachmentSelecter: function( option, callback ){
  224. MWF.require("MWF.widget.AttachmentSelector", function() {
  225. var options = {
  226. "style" : "cms",
  227. "title": "选择附件",
  228. "listStyle": "icon",
  229. "selectType" : "all",
  230. "size": "max",
  231. "attachmentCount": 0,
  232. "isUpload": true,
  233. "isDelete": true,
  234. "isReplace": true,
  235. "isDownload": true,
  236. "toBase64" : true,
  237. "base64MaxSize" : 800,
  238. "readonly": false
  239. };
  240. options = Object.merge( options, option );
  241. if (this.readonly) options.readonly = true;
  242. this.attachmentController = new MWF.widget.AttachmentSelector(document.body, this, options);
  243. this.attachmentController.load();
  244. this.postSelect = callback;
  245. if( this.data ){
  246. this.data.each(function (att) {
  247. this.attachmentController.addAttachment(att);
  248. }.bind(this));
  249. }else{
  250. this.listAttachment( function( json ){
  251. json.data.each(function (att) {
  252. this.attachmentController.addAttachment(att);
  253. }.bind(this));
  254. }.bind(this))
  255. }
  256. }.bind(this));
  257. },
  258. selectAttachment: function(e, node, attachments){
  259. if( attachments.length > 0 ){
  260. var data = attachments[attachments.length-1].data;
  261. this.actions.getAttachmentUrl( data.id, this.options.documentId, function(url){
  262. if( this.attachmentController.options.toBase64 ){
  263. this.actions.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  264. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  265. if(this.postSelect)this.postSelect( url , data, base64Code )
  266. }.bind(this) )
  267. }else{
  268. if(this.postSelect)this.postSelect( url , data )
  269. }
  270. }.bind(this))
  271. }
  272. }
  273. });