Attachment.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. this.attachmentController.checkActions();
  202. }.bind(this))
  203. }.bind(this), null, formData, file);
  204. }
  205. }
  206. }.bind(this));
  207. },
  208. replaceAttachmentFile: function (attachment) {
  209. if (!this.replaceFileAreaNode) {
  210. this.createReplaceFileNode(attachment);
  211. }
  212. this.fileReplaceNode.click();
  213. },
  214. //小程序文件是否支持打开
  215. checkMiniProgramFile: function(ext) {
  216. var exts = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"];
  217. for(var i = 0; i < exts.length; i++){
  218. if(ext === exts[i]){
  219. return true;
  220. }
  221. }
  222. return false;
  223. },
  224. downloadAttachment: function (e, node, attachments) {
  225. //if( this.app.access.isAnonymousDynamic() ){
  226. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  227. //}else {
  228. attachments.each(function (att) {
  229. if (window.o2android && window.o2android.downloadAttachment) {
  230. window.o2android.downloadAttachment(att.data.id);
  231. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  232. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  233. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  234. wx.miniProgram.navigateTo({
  235. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  236. });
  237. } else {
  238. if (layout.mobile) {
  239. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  240. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  241. var xtoken = Cookie.read("x-token");
  242. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  243. });
  244. } else {
  245. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  246. }
  247. }
  248. }.bind(this));
  249. //}
  250. },
  251. openAttachment: function (e, node, attachments) {
  252. //if( this.app.access.isAnonymousDynamic() ){
  253. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  254. //}else{
  255. attachments.each(function (att) {
  256. if (window.o2android && window.o2android.downloadAttachment) {
  257. window.o2android.downloadAttachment(att.data.id);
  258. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  259. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  260. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  261. wx.miniProgram.navigateTo({
  262. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  263. });
  264. } else {
  265. if (layout.mobile) {
  266. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  267. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  268. var xtoken = Cookie.read("x-token");
  269. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  270. });
  271. } else {
  272. this.actions.getAttachmentData(att.data.id, this.options.documentId);
  273. }
  274. }
  275. }.bind(this));
  276. //}
  277. },
  278. getAttachmentUrl: function (attachment, callback) {
  279. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  280. },
  281. getAttachmentData : function(){
  282. var data = [];
  283. this.attachmentController.attachments.each(function( att ){
  284. data.push(att.data)
  285. });
  286. return data;
  287. },
  288. getAttachmentIds : function(){
  289. var ids = [];
  290. this.attachmentController.attachments.each(function( att ){
  291. ids.push(att.data.id)
  292. });
  293. return ids;
  294. },
  295. loadAttachmentSelecter: function( option, callback ){
  296. MWF.require("MWF.widget.AttachmentSelector", function() {
  297. var options = {
  298. "style" : "cms",
  299. "title": "选择附件",
  300. "listStyle": "icon",
  301. "selectType" : "all",
  302. "size": "max",
  303. "attachmentCount": 0,
  304. "isUpload": true,
  305. "isDelete": true,
  306. "isReplace": true,
  307. "isDownload": true,
  308. "toBase64" : true,
  309. "base64MaxSize" : 800,
  310. "readonly": false
  311. };
  312. options = Object.merge( options, option );
  313. if (this.readonly) options.readonly = true;
  314. this.attachmentController = new MWF.widget.AttachmentSelector(document.body, this, options);
  315. this.attachmentController.load();
  316. this.postSelect = callback;
  317. if( this.data ){
  318. this.data.each(function (att) {
  319. this.attachmentController.addAttachment(att);
  320. }.bind(this));
  321. }else{
  322. this.listAttachment( function( json ){
  323. json.data.each(function (att) {
  324. this.attachmentController.addAttachment(att);
  325. }.bind(this));
  326. }.bind(this))
  327. }
  328. }.bind(this));
  329. },
  330. selectAttachment: function(e, node, attachments){
  331. if( attachments.length > 0 ){
  332. var data = attachments[attachments.length-1].data;
  333. this.actions.getAttachmentUrl( data.id, this.options.documentId, function(url){
  334. if( this.attachmentController.options.toBase64 ){
  335. this.actions.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  336. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  337. if(this.postSelect)this.postSelect( url , data, base64Code )
  338. }.bind(this) )
  339. }else{
  340. if(this.postSelect)this.postSelect( url , data )
  341. }
  342. }.bind(this))
  343. }
  344. }
  345. });