Attachment.js 11 KB

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