Attachment.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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, o.messageId);
  87. //this.attachmentList.push(json.data);
  88. }
  89. this.attachmentController.checkActions();
  90. this.fireEvent("upload", [json.data]);
  91. this.fireEvent("change");
  92. }.bind(this))
  93. }
  94. this.attachmentController.checkActions();
  95. }.bind(this), function(files){
  96. this.isQueryUploadSuccess = true;
  97. this.fireEvent( "queryUploadAttachment" );
  98. return this.isQueryUploadSuccess;
  99. }.bind(this));
  100. // this.uploadFileAreaNode = new Element("div");
  101. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  102. // this.uploadFileAreaNode.set("html", html);
  103. //
  104. // this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  105. // this.fileUploadNode.addEvent("change", function () {
  106. // this.isQueryUploadSuccess = true;
  107. // this.fireEvent( "queryUploadAttachment" );
  108. // if( this.isQueryUploadSuccess ){
  109. // var files = this.fileUploadNode.files;
  110. // if (files.length) {
  111. // for (var i = 0; i < files.length; i++) {
  112. // var file = files.item(i);
  113. //
  114. // var formData = new FormData();
  115. // formData.append('file', file);
  116. // formData.append('site', this.options.documentId);
  117. //
  118. // this.actions.uploadAttachment(this.options.documentId, function (o, text) {
  119. // j = JSON.decode(text);
  120. // if ( j.data ) {
  121. // //j.userMessage
  122. // var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  123. // this.actions.getAttachment(aid, this.options.documentId, function (json) {
  124. // json = this.transportData(json);
  125. // if (json.data) {
  126. // this.attachmentController.addAttachment(json.data);
  127. // //this.attachmentList.push(json.data);
  128. // }
  129. // this.attachmentController.checkActions();
  130. //
  131. // this.fireEvent("upload", [json.data]);
  132. // }.bind(this))
  133. // }
  134. // this.attachmentController.checkActions();
  135. // }.bind(this), null, formData, file);
  136. // }
  137. // }
  138. // }else{
  139. // this.uploadFileAreaNode.destroy();
  140. // this.uploadFileAreaNode = false;
  141. // }
  142. // }.bind(this));
  143. },
  144. uploadAttachment: function (e, node) {
  145. //if (!this.uploadFileAreaNode) {
  146. this.createUploadFileNode();
  147. //}
  148. //this.fileUploadNode.click();
  149. },
  150. deleteAttachments: function (e, node, attachments) {
  151. var names = [];
  152. attachments.each(function (attachment) {
  153. names.push(attachment.data.name);
  154. }.bind(this));
  155. var _self = this;
  156. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  157. while (attachments.length) {
  158. attachment = attachments.shift();
  159. _self.deleteAttachment(attachment);
  160. }
  161. this.close();
  162. }, function () {
  163. this.close();
  164. }, null);
  165. },
  166. deleteAttachment: function (attachment) {
  167. this.fireEvent("delete", [attachment.data]);
  168. this.actions.deleteAttachment(attachment.data.id, this.documentId, function (json) {
  169. this.attachmentController.removeAttachment(attachment);
  170. //this.form.businessData.attachmentList.erase( attachment.data )
  171. this.attachmentController.checkActions();
  172. }.bind(this));
  173. },
  174. replaceAttachment: function (e, node, attachment) {
  175. return false;
  176. var _self = this;
  177. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  178. _self.replaceAttachmentFile(attachment);
  179. this.close();
  180. }, function () {
  181. this.close();
  182. }, null);
  183. },
  184. createReplaceFileNode: function (attachment) {
  185. this.replaceFileAreaNode = new Element("div");
  186. var html = "<input name=\"file\" type=\"file\" multiple/>";
  187. this.replaceFileAreaNode.set("html", html);
  188. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  189. this.fileReplaceNode.addEvent("change", function () {
  190. var files = this.fileReplaceNode.files;
  191. if (files.length) {
  192. for (var i = 0; i < files.length; i++) {
  193. var file = files.item(i);
  194. var formData = new FormData();
  195. formData.append('file', file);
  196. // formData.append('site', this.json.id);
  197. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  198. this.form.documentAction.getAttachment(attachment.data.id, this.opetions.documentId, function (json) {
  199. attachment.data = json.data;
  200. attachment.reload();
  201. if (o.messageId && this.attachmentController.messageItemList) {
  202. var message = this.attachmentController.messageItemList[o.messageId];
  203. if( message && message.node )message.node.destroy();
  204. }
  205. this.attachmentController.checkActions();
  206. }.bind(this))
  207. }.bind(this), null, formData, file);
  208. }
  209. }
  210. }.bind(this));
  211. },
  212. replaceAttachmentFile: function (attachment) {
  213. if (!this.replaceFileAreaNode) {
  214. this.createReplaceFileNode(attachment);
  215. }
  216. this.fileReplaceNode.click();
  217. },
  218. //小程序文件是否支持打开
  219. checkMiniProgramFile: function(ext) {
  220. var exts = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"];
  221. for(var i = 0; i < exts.length; i++){
  222. if(ext === exts[i]){
  223. return true;
  224. }
  225. }
  226. return false;
  227. },
  228. downloadAttachment: function (e, node, attachments) {
  229. //if( this.app.access.isAnonymousDynamic() ){
  230. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  231. //}else {
  232. attachments.each(function (att) {
  233. if (window.o2android && window.o2android.downloadAttachment) {
  234. window.o2android.downloadAttachment(att.data.id);
  235. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  236. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  237. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  238. wx.miniProgram.navigateTo({
  239. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  240. });
  241. } else {
  242. if (layout.mobile) {
  243. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  244. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  245. var xtoken = Cookie.read("x-token");
  246. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  247. });
  248. } else {
  249. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  250. }
  251. }
  252. }.bind(this));
  253. //}
  254. },
  255. openAttachment: function (e, node, attachments) {
  256. //if( this.app.access.isAnonymousDynamic() ){
  257. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  258. //}else{
  259. attachments.each(function (att) {
  260. if (window.o2android && window.o2android.downloadAttachment) {
  261. window.o2android.downloadAttachment(att.data.id);
  262. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  263. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  264. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  265. wx.miniProgram.navigateTo({
  266. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  267. });
  268. } else {
  269. if (layout.mobile) {
  270. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  271. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  272. var xtoken = Cookie.read("x-token");
  273. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  274. });
  275. } else {
  276. this.actions.getAttachmentData(att.data.id, this.options.documentId);
  277. }
  278. }
  279. }.bind(this));
  280. //}
  281. },
  282. getAttachmentUrl: function (attachment, callback) {
  283. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  284. },
  285. getAttachmentData : function(){
  286. var data = [];
  287. this.attachmentController.attachments.each(function( att ){
  288. data.push(att.data)
  289. });
  290. return data;
  291. },
  292. getAttachmentIds : function(){
  293. var ids = [];
  294. this.attachmentController.attachments.each(function( att ){
  295. ids.push(att.data.id)
  296. });
  297. return ids;
  298. },
  299. loadAttachmentSelecter: function( option, callback ){
  300. MWF.require("MWF.widget.AttachmentSelector", function() {
  301. var options = {
  302. "style" : "cms",
  303. "title": "选择附件",
  304. "listStyle": "icon",
  305. "selectType" : "all",
  306. "size": "max",
  307. "attachmentCount": 0,
  308. "isUpload": true,
  309. "isDelete": true,
  310. "isReplace": true,
  311. "isDownload": true,
  312. "toBase64" : true,
  313. "base64MaxSize" : 800,
  314. "readonly": false
  315. };
  316. options = Object.merge( options, option );
  317. if (this.readonly) options.readonly = true;
  318. this.attachmentController = new MWF.widget.AttachmentSelector(document.body, this, options);
  319. this.attachmentController.load();
  320. this.postSelect = callback;
  321. if( this.data ){
  322. this.data.each(function (att) {
  323. this.attachmentController.addAttachment(att);
  324. }.bind(this));
  325. }else{
  326. this.listAttachment( function( json ){
  327. json.data.each(function (att) {
  328. this.attachmentController.addAttachment(att);
  329. }.bind(this));
  330. }.bind(this))
  331. }
  332. }.bind(this));
  333. },
  334. selectAttachment: function(e, node, attachments){
  335. if( attachments.length > 0 ){
  336. var data = attachments[attachments.length-1].data;
  337. this.actions.getAttachmentUrl( data.id, this.options.documentId, function(url){
  338. if( this.attachmentController.options.toBase64 ){
  339. this.actions.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  340. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  341. if(this.postSelect)this.postSelect( url , data, base64Code )
  342. }.bind(this) )
  343. }else{
  344. if(this.postSelect)this.postSelect( url , data )
  345. }
  346. }.bind(this))
  347. }
  348. }
  349. });