Attachment.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.Forum.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),
  29. "isDelete": (this.options.isNew || this.options.isEdited),
  30. "isReplace": false,
  31. "isDownload": true,
  32. "isSizeChange": this.options.isSizeChange,
  33. "readonly": (!this.options.isNew && !this.options.isEdited )
  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. if( this.data ){
  41. this.data.each(function (att) {
  42. this.attachmentController.addAttachment(att);
  43. }.bind(this));
  44. }else if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  45. this.listAttachment( function( json ){
  46. json.data.each(function (att) {
  47. this.attachmentController.addAttachment(att);
  48. }.bind(this));
  49. }.bind(this))
  50. }
  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. if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  72. this.actions.listAttachment(this.options.documentId, function(json){
  73. if(callback)callback(this.transportData(json));
  74. }.bind(this))
  75. }
  76. },
  77. createUploadFileNode: function () {
  78. this.attachmentController.doUploadAttachment({"site": this.options.documentId}, this.actions.action, "uploadAttachment", {"id": this.options.documentId, "documentid":this.options.documentId}, null, function(o){
  79. j = o;
  80. if ( j.data ) {
  81. //j.userMessage
  82. var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  83. this.actions.getAttachment(aid, this.options.documentId, function (json) {
  84. json = this.transportData(json);
  85. if (json.data) {
  86. this.attachmentController.addAttachment(json.data);
  87. //this.attachmentList.push(json.data);
  88. }
  89. this.attachmentController.checkActions();
  90. this.fireEvent("upload", [json.data]);
  91. }.bind(this))
  92. }
  93. this.attachmentController.checkActions();
  94. }.bind(this), function(files){
  95. this.isQueryUploadSuccess = true;
  96. this.fireEvent( "queryUploadAttachment" );
  97. return this.isQueryUploadSuccess;
  98. }.bind(this));
  99. // this.uploadFileAreaNode = new Element("div");
  100. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  101. // this.uploadFileAreaNode.set("html", html);
  102. //
  103. // this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  104. // this.fileUploadNode.addEvent("change", function () {
  105. // this.isQueryUploadSuccess = true;
  106. // this.fireEvent( "queryUploadAttachment" );
  107. // if( this.isQueryUploadSuccess ){
  108. // var files = this.fileUploadNode.files;
  109. // if (files.length) {
  110. // for (var i = 0; i < files.length; i++) {
  111. // var file = files.item(i);
  112. //
  113. // var formData = new FormData();
  114. // formData.append('file', file);
  115. // formData.append('site', this.options.documentId);
  116. //
  117. // this.actions.uploadAttachment(this.options.documentId, function (o, text) {
  118. // j = JSON.decode(text);
  119. // if ( j.data ) {
  120. // //j.userMessage
  121. // var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  122. // this.actions.getAttachment(aid, this.options.documentId, function (json) {
  123. // json = this.transportData(json);
  124. // if (json.data) {
  125. // this.attachmentController.addAttachment(json.data);
  126. // //this.attachmentList.push(json.data);
  127. // }
  128. // this.attachmentController.checkActions();
  129. //
  130. // this.fireEvent("upload", [json.data]);
  131. // }.bind(this))
  132. // }
  133. // this.attachmentController.checkActions();
  134. // }.bind(this), null, formData, file);
  135. // }
  136. // }
  137. // }else{
  138. // this.uploadFileAreaNode.destroy();
  139. // this.uploadFileAreaNode = false;
  140. // }
  141. // }.bind(this));
  142. },
  143. uploadAttachment: function (e, node) {
  144. //if (!this.uploadFileAreaNode) {
  145. this.createUploadFileNode();
  146. //}
  147. //this.fileUploadNode.click();
  148. },
  149. deleteAttachments: function (e, node, attachments) {
  150. var names = [];
  151. attachments.each(function (attachment) {
  152. names.push(attachment.data.name);
  153. }.bind(this));
  154. var _self = this;
  155. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  156. while (attachments.length) {
  157. attachment = attachments.shift();
  158. _self.deleteAttachment(attachment);
  159. }
  160. this.close();
  161. }, function () {
  162. this.close();
  163. }, null);
  164. },
  165. deleteAttachment: function (attachment) {
  166. this.fireEvent("delete", [attachment.data]);
  167. this.actions.deleteAttachment(attachment.data.id, this.documentId, function (json) {
  168. this.attachmentController.removeAttachment(attachment);
  169. //this.form.businessData.attachmentList.erase( attachment.data )
  170. this.attachmentController.checkActions();
  171. }.bind(this));
  172. },
  173. replaceAttachment: function (e, node, attachment) {
  174. return false;
  175. var _self = this;
  176. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  177. _self.replaceAttachmentFile(attachment);
  178. this.close();
  179. }, function () {
  180. this.close();
  181. }, null);
  182. },
  183. createReplaceFileNode: function (attachment) {
  184. this.replaceFileAreaNode = new Element("div");
  185. var html = "<input name=\"file\" type=\"file\" multiple/>";
  186. this.replaceFileAreaNode.set("html", html);
  187. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  188. this.fileReplaceNode.addEvent("change", function () {
  189. var files = this.fileReplaceNode.files;
  190. if (files.length) {
  191. for (var i = 0; i < files.length; i++) {
  192. var file = files.item(i);
  193. var formData = new FormData();
  194. formData.append('file', file);
  195. // formData.append('site', this.json.id);
  196. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  197. this.form.documentAction.getAttachment(attachment.data.id, this.opetions.documentId, function (json) {
  198. attachment.data = json.data;
  199. attachment.reload();
  200. this.attachmentController.checkActions();
  201. }.bind(this))
  202. }.bind(this), null, formData, file);
  203. }
  204. }
  205. }.bind(this));
  206. },
  207. replaceAttachmentFile: function (attachment) {
  208. if (!this.replaceFileAreaNode) {
  209. this.createReplaceFileNode(attachment);
  210. }
  211. this.fileReplaceNode.click();
  212. },
  213. downloadAttachment: function (e, node, attachments) {
  214. //if( this.app.access.isAnonymousDynamic() ){
  215. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  216. //}else {
  217. attachments.each(function (att) {
  218. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  219. }.bind(this));
  220. //}
  221. },
  222. openAttachment: function (e, node, attachments) {
  223. //if( this.app.access.isAnonymousDynamic() ){
  224. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  225. //}else{
  226. attachments.each(function (att) {
  227. this.actions.getAttachmentData(att.data.id, this.options.documentId);
  228. }.bind(this));
  229. //}
  230. },
  231. getAttachmentUrl: function (attachment, callback) {
  232. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  233. },
  234. getAttachmentData : function(){
  235. var data = [];
  236. this.attachmentController.attachments.each(function( att ){
  237. data.push(att.data)
  238. });
  239. return data;
  240. },
  241. getAttachmentIds : function(){
  242. var ids = [];
  243. this.attachmentController.attachments.each(function( att ){
  244. ids.push(att.data.id)
  245. });
  246. return ids;
  247. },
  248. loadAttachmentSelecter: function( option, callback ){
  249. MWF.require("MWF.widget.AttachmentSelector", function() {
  250. var options = {
  251. "style" : "cms",
  252. "title": "选择附件",
  253. "listStyle": "icon",
  254. "selectType" : "all",
  255. "size": "max",
  256. "attachmentCount": 0,
  257. "isUpload": true,
  258. "isDelete": true,
  259. "isReplace": true,
  260. "isDownload": true,
  261. "toBase64" : true,
  262. "base64MaxSize" : 800,
  263. "readonly": false
  264. };
  265. options = Object.merge( options, option );
  266. if (this.readonly) options.readonly = true;
  267. this.attachmentController = new MWF.widget.AttachmentSelector(document.body, this, options);
  268. this.attachmentController.load();
  269. this.postSelect = callback;
  270. if( this.data ){
  271. this.data.each(function (att) {
  272. this.attachmentController.addAttachment(att);
  273. }.bind(this));
  274. }else{
  275. this.listAttachment( function( json ){
  276. json.data.each(function (att) {
  277. this.attachmentController.addAttachment(att);
  278. }.bind(this));
  279. }.bind(this))
  280. }
  281. }.bind(this));
  282. },
  283. selectAttachment: function(e, node, attachments){
  284. if( attachments.length > 0 ){
  285. var data = attachments[attachments.length-1].data;
  286. this.actions.getAttachmentUrl( data.id, this.options.documentId, function(url){
  287. if( this.attachmentController.options.toBase64 ){
  288. this.actions.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  289. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  290. if(this.postSelect)this.postSelect( url , data, base64Code )
  291. }.bind(this) )
  292. }else{
  293. if(this.postSelect)this.postSelect( url , data )
  294. }
  295. }.bind(this))
  296. }
  297. }
  298. });