Attachment.js 16 KB

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