Attachment.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //该附件类不用于文档表单的展现
  2. MWF.require("MWF.widget.AttachmentController", null,false);
  3. MWF.xApplication.cms.Document.Attachment = new Class({
  4. Implements: [Options, Events],
  5. options: {
  6. "documentId" : "",
  7. "isNew": false,
  8. "isEdited" : true,
  9. "size" : "max",
  10. "isSizeChange" : true
  11. },
  12. initialize: function (node, app, actions, lp, options) {
  13. this.setOptions(options);
  14. this.app = app;
  15. this.node = $(node);
  16. this.actions = actions;
  17. this.lp = lp;
  18. },
  19. load: function () {
  20. this.loadAttachmentController();
  21. },
  22. loadAttachmentController: function () {
  23. var options = {
  24. "style": "cms",
  25. "title": MWF.xApplication.cms.Document.LP.attachmentArea, //"附件区域",
  26. "size": this.options.size ,
  27. "resize": true,
  28. //"attachmentCount": this.json.attachmentCount || 0,
  29. "isUpload": (this.options.isNew || this.options.isEdited) ? true : false,
  30. "isDelete": (this.options.isNew || this.options.isEdited) ? true : false,
  31. "isReplace": false,
  32. "isDownload": true,
  33. "isSizeChange": this.options.isSizeChange,
  34. "readonly": (!this.options.isNew && !this.options.isEdited ) ? true : false
  35. };
  36. this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
  37. this.attachmentController.load();
  38. //this.actions.listAttachmentInfo.each(function (att) {
  39. // this.attachmentController.addAttachment(att);
  40. //}.bind(this));
  41. if( this.data ){
  42. this.data.each(function (att) {
  43. this.attachmentController.addAttachment(att);
  44. }.bind(this));
  45. }else if( this.options.documentId && this.options.documentId!="" ){
  46. this.listAttachment( function( json ){
  47. json.data.each(function (att) {
  48. this.attachmentController.addAttachment(att);
  49. }.bind(this));
  50. }.bind(this))
  51. }
  52. },
  53. transportData : function( json ){
  54. if( typeOf(json.data) == "array" ){
  55. json.data.each(function(d){
  56. d.person = d.creatorUid;
  57. d.lastUpdateTime = d.updateTime;
  58. })
  59. }else if( typeOf(json.data) == "object" ){
  60. var d = json.data;
  61. d.person = d.creatorUid;
  62. d.lastUpdateTime = d.updateTime;
  63. }else{
  64. json.each(function(d){
  65. d.person = d.creatorUid;
  66. d.lastUpdateTime = d.updateTime;
  67. })
  68. }
  69. return json;
  70. },
  71. listAttachment: function( callback ){
  72. if( this.options.documentId && this.options.documentId!="" ){
  73. this.actions.listAttachment(this.options.documentId, function(json){
  74. if(callback)callback(this.transportData(json));
  75. }.bind(this))
  76. }
  77. },
  78. createUploadFileNode: function () {
  79. this.uploadFileAreaNode = new Element("div");
  80. var html = "<input name=\"file\" type=\"file\" multiple/>";
  81. this.uploadFileAreaNode.set("html", html);
  82. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  83. this.fileUploadNode.addEvent("change", function () {
  84. this.isQueryUploadSuccess = true;
  85. this.fireEvent( "queryUploadAttachment" );
  86. if( this.isQueryUploadSuccess ){
  87. var files = this.fileUploadNode.files;
  88. if (files.length) {
  89. for (var i = 0; i < files.length; i++) {
  90. var file = files.item(i);
  91. var formData = new FormData();
  92. formData.append('file', file);
  93. formData.append('site', this.options.documentId);
  94. this.actions.uploadAttachment(this.options.documentId, function (o, text) {
  95. j = JSON.decode(text);
  96. if ( j.data ) {
  97. //j.userMessage
  98. var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  99. this.actions.getAttachment( aid, this.options.documentId, function (json) {
  100. json = this.transportData(json);
  101. if (json.data) {
  102. this.attachmentController.addAttachment(json.data);
  103. //this.attachmentList.push(json.data);
  104. }
  105. this.attachmentController.checkActions();
  106. this.fireEvent("upload", [json.data]);
  107. }.bind(this))
  108. }
  109. this.attachmentController.checkActions();
  110. }.bind(this), null, formData, file);
  111. }
  112. }
  113. }else{
  114. this.uploadFileAreaNode.destroy();
  115. this.uploadFileAreaNode = false;
  116. }
  117. }.bind(this));
  118. },
  119. uploadAttachment: function (e, node) {
  120. if (!this.uploadFileAreaNode) {
  121. this.createUploadFileNode();
  122. }
  123. this.fileUploadNode.click();
  124. },
  125. deleteAttachments: function (e, node, attachments) {
  126. var names = [];
  127. attachments.each(function (attachment) {
  128. names.push(attachment.data.name);
  129. }.bind(this));
  130. var _self = this;
  131. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  132. while (attachments.length) {
  133. attachment = attachments.shift();
  134. _self.deleteAttachment(attachment);
  135. }
  136. this.close();
  137. }, function () {
  138. this.close();
  139. }, null);
  140. },
  141. deleteAttachment: function (attachment) {
  142. this.fireEvent("delete", [attachment.data]);
  143. this.actions.deleteAttachment(attachment.data.id, function (json) {
  144. this.attachmentController.removeAttachment(attachment);
  145. //this.form.businessData.attachmentList.erase( attachment.data )
  146. this.attachmentController.checkActions();
  147. }.bind(this));
  148. },
  149. replaceAttachment: function (e, node, attachment) {
  150. var _self = this;
  151. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  152. _self.replaceAttachmentFile(attachment);
  153. this.close();
  154. }, function () {
  155. this.close();
  156. }, null);
  157. },
  158. createReplaceFileNode: function (attachment) {
  159. this.replaceFileAreaNode = new Element("div");
  160. var html = "<input name=\"file\" type=\"file\" multiple/>";
  161. this.replaceFileAreaNode.set("html", html);
  162. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  163. this.fileReplaceNode.addEvent("change", function () {
  164. var files = this.fileReplaceNode.files;
  165. if (files.length) {
  166. for (var i = 0; i < files.length; i++) {
  167. var file = files.item(i);
  168. var formData = new FormData();
  169. formData.append('file', file);
  170. // formData.append('site', this.json.id);
  171. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  172. this.form.documentAction.getAttachment(attachment.data.id, this.opetions.documentId, function (json) {
  173. attachment.data = json.data;
  174. attachment.reload();
  175. this.attachmentController.checkActions();
  176. }.bind(this))
  177. }.bind(this), null, formData, file);
  178. }
  179. }
  180. }.bind(this));
  181. },
  182. replaceAttachmentFile: function (attachment) {
  183. if (!this.replaceFileAreaNode) {
  184. this.createReplaceFileNode(attachment);
  185. }
  186. this.fileReplaceNode.click();
  187. },
  188. downloadAttachment: function (e, node, attachments) {
  189. attachments.each(function (att) {
  190. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  191. }.bind(this));
  192. },
  193. openAttachment: function (e, node, attachments) {
  194. attachments.each(function (att) {
  195. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  196. }.bind(this));
  197. },
  198. getAttachmentUrl: function (attachment, callback) {
  199. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  200. },
  201. getAttachmentData : function(){
  202. var data = [];
  203. this.attachmentController.attachments.each(function( att ){
  204. data.push(att.data)
  205. });
  206. return data;
  207. },
  208. getAttachmentIds : function(){
  209. var ids = [];
  210. this.attachmentController.attachments.each(function( att ){
  211. ids.push(att.data.id)
  212. });
  213. return ids;
  214. },
  215. loadAttachmentSelecter: function( option, callback ){
  216. MWF.require("MWF.widget.AttachmentSelector", function() {
  217. var options = {
  218. "style" : "cms",
  219. "title": MWF.xApplication.cms.Document.LP.selectAttachment, //"选择附件"
  220. "listStyle": "icon",
  221. "selectType" : "all",
  222. "size": "max",
  223. "attachmentCount": 0,
  224. //"isUpload": true,
  225. //"isDelete": true,
  226. //"isReplace": true,
  227. //"isDownload": true,
  228. "isUpload": (this.options.isNew || this.options.isEdited) ? true : false,
  229. "isDelete": (this.options.isNew || this.options.isEdited) ? true : false,
  230. "isReplace": false,
  231. "isDownload": true,
  232. "toBase64" : true,
  233. "base64MaxSize" : 800,
  234. //"isSizeChange": this.options.isSizeChange,
  235. "readonly": (!this.options.isNew && !this.options.isEdited ) ? true : false
  236. //"readonly": false
  237. };
  238. options = Object.merge( options, option );
  239. if (this.readonly) options.readonly = true;
  240. this.attachmentController = new MWF.widget.AttachmentSelector(document.body, this, options);
  241. this.attachmentController.load();
  242. this.postSelect = callback;
  243. if( this.data ){
  244. this.data.each(function (att) {
  245. this.attachmentController.addAttachment(att);
  246. }.bind(this));
  247. }else{
  248. this.listAttachment( function( json ){
  249. json.data.each(function (att) {
  250. this.attachmentController.addAttachment(att);
  251. }.bind(this));
  252. }.bind(this))
  253. }
  254. }.bind(this));
  255. },
  256. selectAttachment: function(e, node, attachments){
  257. if( attachments.length > 0 ){
  258. var data = attachments[attachments.length-1].data;
  259. this.actions.getAttachmentUrl( data.id, this.options.documentId, function(url){
  260. if( this.attachmentController.options.toBase64 ){
  261. this.actions.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  262. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  263. if(this.postSelect)this.postSelect( url , data, base64Code )
  264. }.bind(this) )
  265. }else{
  266. if(this.postSelect)this.postSelect( url , data )
  267. }
  268. }.bind(this))
  269. }
  270. }
  271. });