Attachment.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.Forum.AttachmentController = new Class({
  3. Extends: o2.widget.AttachmentController,
  4. reloadAttachments: function(){
  5. if (this.options.size==="min"){
  6. this.minContent.empty();
  7. var atts = this.attachments;
  8. this.attachments = [];
  9. while (atts.length){
  10. var att = atts.shift();
  11. this.attachments.push(new MWF.xApplication.Forum.AttachmentController.AttachmentMin(att.data, this));
  12. }
  13. }else{
  14. this.content.empty();
  15. var atts = this.attachments;
  16. this.attachments = [];
  17. while (atts.length){
  18. var att = atts.shift();
  19. this.attachments.push(new MWF.xApplication.Forum.AttachmentController.Attachment(att.data, this));
  20. }
  21. }
  22. this.checkActions();
  23. },
  24. loadMax: function(){
  25. if (!this.node) this.node = new Element("div", {"styles": this.css.container});
  26. this.createTopNode();
  27. if (!this.contentScrollNode){
  28. //this.createTopNode();
  29. this.createContentNode();
  30. if (this.options.resize){
  31. this.createBottomNode();
  32. this.createResizeNode();
  33. }
  34. this.node.inject(this.container);
  35. //if (this.options.readonly) this.setReadonly();
  36. this.checkActions();
  37. this.setEvent();
  38. }else{
  39. this.contentScrollNode.setStyle("display", "block");
  40. if (this.bottomNode) this.bottomNode.setStyle("display", "block");
  41. if (this.titleNode) this.titleNode.setStyle("display", "block");
  42. //this.topNode.setStyle("display", "block");
  43. this.content.empty();
  44. }
  45. var atts = this.attachments;
  46. this.attachments = [];
  47. while (atts.length){
  48. var att = atts.shift();
  49. this.attachments.push(new MWF.xApplication.Forum.AttachmentController.Attachment(att.data, this));
  50. }
  51. this.checkActions();
  52. //this.attachments = atts;
  53. },
  54. loadMin: function(){
  55. if (!this.node) this.node = new Element("div", {"styles": this.css.container_min});
  56. if (!this.minActionAreaNode){
  57. this.minActionAreaNode = new Element("div", {"styles": this.css.minActionAreaNode }).inject(this.node);
  58. //this.minContent = new Element("div", {"styles": this.css.minContentNode}).inject(this.node);
  59. this.loadMinActions();
  60. this.node.inject(this.container);
  61. this.setEvent();
  62. }else{
  63. this.minActionAreaNode.setStyle("display", "");
  64. this.minActionAreaNode.empty();
  65. this.loadMinActions();
  66. this.setEvent();
  67. }
  68. var hiddenGroup = this.options.toolbarGroupHidden;
  69. var flag = hiddenGroup.contains("edit") && hiddenGroup.contains("read") && hiddenGroup.contains("view");
  70. if( flag )this.minActionAreaNode.setStyle("display","none");
  71. if( !this.minContent ){
  72. this.minContent = new Element("div", {"styles":
  73. layout.mobile ? this.css.minContentNode_mobile : this.css.minContentNode
  74. }).inject(this.node);
  75. if( layout.mobile ){
  76. this.minContent.setStyle("clear","both");
  77. }
  78. }else{
  79. this.minContent.setStyle("display", "block");
  80. this.minContent.empty();
  81. }
  82. var atts = this.attachments;
  83. this.attachments = [];
  84. while (atts.length){
  85. var att = atts.shift();
  86. this.attachments.push(new MWF.xApplication.Forum.AttachmentController.AttachmentMin(att.data, this));
  87. }
  88. this.checkActions();
  89. //this.attachments = atts;
  90. },
  91. addAttachment: function(data, messageId, isCheckPosition){
  92. if (this.options.size=="min"){
  93. this.attachments.push(new MWF.xApplication.Forum.AttachmentController.AttachmentMin(data, this, messageId, isCheckPosition));
  94. }else{
  95. this.attachments.push(new MWF.xApplication.Forum.AttachmentController.Attachment(data, this, messageId, isCheckPosition));
  96. }
  97. this.checkActions();
  98. }
  99. });
  100. MWF.xApplication.Forum.AttachmentController.Attachment = new Class({
  101. Extends: o2.widget.AttachmentController.Attachment,
  102. initialize: function(data, controller, messageId, isCheckPosition){
  103. this.data = data;
  104. this.controller = controller;
  105. this.css = this.controller.css;
  106. this.listStyle = this.controller.options.listStyle;
  107. this.content = this.controller.content;
  108. this.isSelected = false;
  109. this.seq = this.controller.attachments.length+1;
  110. this.isCheckPosition = isCheckPosition;
  111. this.actions = [];
  112. if (messageId && this.controller.messageItemList) {
  113. this.message = this.controller.messageItemList[messageId];
  114. }
  115. if( this.controller.isAnonymous && this.data.creatorUid === this.controller.anonymousPerson ){
  116. this.data.person = this.controller.anonymousName;
  117. }else if(MWFForum.isUseNickName()){
  118. this.data.person = this.data.nickName;
  119. }else if(!this.data.person && this.data.creatorUid ){
  120. this.data.person = this.data.creatorUid;
  121. }
  122. this.load();
  123. },
  124. createInforNode: function(callback){
  125. var size = "";
  126. var k = this.data.length/1024;
  127. if (k>1024){
  128. var m = k/1024;
  129. m = Math.round(m*100)/100;
  130. size = m+"M";
  131. }else{
  132. k = Math.round(k*100)/100;
  133. size = k+"K";
  134. }
  135. this.inforNode = new Element("div", {"styles": this.css.attachmentInforNode});
  136. //var person = MWFForum.isUseNickName()?this.data.nickName:( this.data.person || this.data.creatorUid );
  137. var person = this.data.person;
  138. var html = "<div style='overflow:hidden; font-weight: bold'>"+this.data.name+"</div>";
  139. html += "<div style='clear: both; overflow:hidden'><div style='width:40px; float:left; font-weight: bold'>"+o2.LP.widget.uploader+": </div><div style='width:120px; float:left; margin-left:10px'>"+ person +"</div></div>";
  140. html += "<div style='clear: both; overflow:hidden'><div style='width:40px; float:left; font-weight: bold'>"+o2.LP.widget.uploadTime+": </div><div style='width:120px; float:left; margin-left:10px'>"+this.data.createTime+"</div></div>";
  141. html += "<div style='clear: both; overflow:hidden'><div style='width:40px; float:left; font-weight: bold'>"+o2.LP.widget.modifyTime+": </div><div style='width:120px; float:left; margin-left:10px'>"+this.data.lastUpdateTime+"</div></div>";
  142. if(this.data.activityName)html += "<div style='clear: both; overflow:hidden'><div style='width:40px; float:left; font-weight: bold'>"+o2.LP.widget.uploadActivity+": </div><div style='width:120px; float:left; margin-left:10px'>"+(this.data.activityName || o2.LP.widget.unknow)+"</div></div>";
  143. html += "<div style='clear: both; overflow:hidden'><div style='width:40px; float:left; font-weight: bold'>"+o2.LP.widget.size+": </div><div style='width:120px; float:left; margin-left:10px'>"+size+"</div></div>";
  144. this.inforNode.set("html", html);
  145. if (callback) callback();
  146. }
  147. });
  148. MWF.xApplication.Forum.AttachmentController.AttachmentMin = new Class({
  149. Extends: MWF.xApplication.Forum.AttachmentController.Attachment,
  150. initialize: function(data, controller, messageId, isCheckPosition){
  151. this.data = data;
  152. // if( !this.data.person && this.data.creatorUid )this.data.person = this.data.creatorUid;
  153. this.controller = controller;
  154. this.css = this.controller.css;
  155. this.content = this.controller.minContent;
  156. this.isSelected = false;
  157. this.isCheckPosition = isCheckPosition;
  158. this.seq = this.controller.attachments.length+1;
  159. if (messageId && this.controller.messageItemList) {
  160. this.message = this.controller.messageItemList[messageId];
  161. }
  162. if( this.controller.isAnonymous && this.data.creatorUid === this.controller.anonymousPerson ){
  163. this.data.person = this.controller.anonymousName;
  164. }else if(MWFForum.isUseNickName()){
  165. this.data.person = this.data.nickName;
  166. }else if(!this.data.person && this.data.creatorUid ){
  167. this.data.person = this.data.creatorUid;
  168. }
  169. this.load();
  170. },
  171. load: function(){
  172. debugger;
  173. if (this.message){
  174. this.node = new Element("div").inject(this.message.node, "after");
  175. this.message.node.destroy();
  176. delete this.controller.messageItemList[this.message.data.id];
  177. }else{
  178. this.node = new Element("div").inject(this.content);
  179. }
  180. if( this.isCheckPosition && this.isNumber(this.data.orderNumber) ){
  181. var attList = this.controller.attachments;
  182. for( var i=0; i<attList.length; i++ ){
  183. var att = attList[i];
  184. if( !this.isNumber(att.data.orderNumber) || this.data.orderNumber < att.data.orderNumber ){
  185. this.node.inject( att.node, "before" );
  186. break;
  187. }
  188. }
  189. }
  190. //this.node = new Element("div").inject(this.content);
  191. //this.loadList();
  192. switch (this.controller.options.listStyle){
  193. case "list":
  194. this.loadList();
  195. break;
  196. case "icon":
  197. this.loadIcon();
  198. break;
  199. case "preview":
  200. this.loadPreview();
  201. break;
  202. case "sequence":
  203. this.loadSequence();
  204. break;
  205. }
  206. this.createInforNode();
  207. if (!Browser.Platform.ios && !layout.mobile){
  208. this.tooltip = new mBox.Tooltip({
  209. content: this.inforNode,
  210. setStyles: {content: {padding: 15, lineHeight: 20}},
  211. attach: this.iconImgNode,
  212. transition: 'flyin'
  213. });
  214. }
  215. this.setEvent();
  216. },
  217. loadList: function() {
  218. debugger;
  219. this.node.setStyles( layout.mobile ? this.css.minAttachmentNode_list_mobile : this.css.minAttachmentNode_list);
  220. if( !layout.mobile ){
  221. this.sepNode = new Element("div", {"styles": this.css.minAttachmentSepNode_list}).inject(this.node);
  222. }
  223. this.actionAreaNode = new Element("div", {"styles": this.css.minAttachmentActionAreaNode}).inject(this.node);
  224. if ( this.controller.isAttDownloadAvailable(this) ) {
  225. this.downloadAction = this.createAction(this.actionAreaNode, "download_single", "download_single_over", o2.LP.widget.download, function (e, node) {
  226. this.controller.downloadAttachment(e, node);
  227. }.bind(this));
  228. }
  229. //this.actions.push( this.downloadAction );
  230. if ( this.controller.isAttDeleteAvailable(this) ) {
  231. this.deleteAction = this.createAction(this.actionAreaNode, "delete_single", "delete_single_over", o2.LP.widget["delete"], function (e, node) {
  232. this.controller.deleteAttachment(e, node);
  233. }.bind(this));
  234. }
  235. //this.actions.push( this.deleteAction );
  236. if (this.controller.configAttachment) {
  237. if ( this.controller.isAttConfigAvailable(this) ) {
  238. this.configAction = this.createAction(this.actionAreaNode, "config_single", "config_single_over", o2.LP.widget.configAttachment, function (e, node) {
  239. this.controller.configAttachment(e, node);
  240. }.bind(this), o2.LP.widget.configAttachmentText );
  241. //this.actions.push( this.configAction );
  242. }
  243. }
  244. if (this.isSelected) this.node.setStyles(this.css.minAttachmentNode_list_selected);
  245. this.iconNode = new Element("div", {"styles": this.css.minAttachmentIconNode_list}).inject(this.node);
  246. this.iconImgAreaNode = new Element("div", {"styles": this.css.minAttachmentIconImgAreaNode_list}).inject(this.iconNode);
  247. this.iconImgNode = new Element("img", {"styles": this.css.minAttachmentIconImgNode_list}).inject(this.iconImgAreaNode);
  248. this.iconImgNode.set({"src": this.getIcon(), "border": 0});
  249. this.textNode = new Element("div", {"styles": this.css.minAttachmentTextNode_list}).inject(this.node);
  250. this.textNode.set("text", this.data.name);
  251. var size = "";
  252. var k = this.data.length/1024;
  253. if (k>1024){
  254. var m = k/1024;
  255. m = Math.round(m*100)/100;
  256. size = m+"M";
  257. }else{
  258. k = Math.round(k*100)/100;
  259. size = k+"K";
  260. }
  261. this.textSizeNode = new Element("div", {"styles": this.css.minAttachmentSizeNode_list}).inject(this.textNode);
  262. this.textSizeNode.set("text", "("+size+")");
  263. this.node.set("title",this.data.name + "("+size+")");
  264. },
  265. loadSequence: function(){
  266. this.node.setStyles(this.css.minAttachmentNode_sequence);
  267. this.actionAreaNode = new Element("div", {"styles":this.css.minAttachmentActionAreaNode}).inject(this.node);
  268. if ( this.controller.isAttDownloadAvailable(this) ) {
  269. this.downloadAction = this.createAction(this.actionAreaNode, "download_single", "download_single_over", o2.LP.widget.download, function (e, node) {
  270. this.controller.downloadAttachment(e, node);
  271. }.bind(this));
  272. }
  273. //this.actions.push( this.downloadAction );
  274. if ( this.controller.isAttDeleteAvailable(this) ) {
  275. this.deleteAction = this.createAction(this.actionAreaNode, "delete_single", "delete_single_over", o2.LP.widget["delete"], function (e, node) {
  276. this.controller.deleteAttachment(e, node);
  277. }.bind(this));
  278. }
  279. //this.actions.push( this.deleteAction );
  280. if (this.controller.configAttachment) {
  281. if ( this.controller.isAttConfigAvailable(this) ) {
  282. this.configAction = this.createAction(this.actionAreaNode, "config_single", "config_single_over", MWF.LP.widget.configAttachment, function (e, node) {
  283. this.controller.configAttachment(e, node);
  284. }.bind(this));
  285. //this.actions.push( this.configAction );
  286. }
  287. }
  288. if (this.isSelected) this.node.setStyles(this.css.minAttachmentNode_list_selected);
  289. this.sequenceNode = new Element("div", {"styles": this.css.attachmentSeqNode_sequence, "text": (this.seq || 1)}).inject(this.node);
  290. this.iconNode = new Element("div", {"styles": this.css.minAttachmentIconNode_list}).inject(this.node);
  291. this.iconImgAreaNode = new Element("div", {"styles": this.css.minAttachmentIconImgAreaNode_list}).inject(this.iconNode);
  292. this.iconImgNode = new Element("img", {"styles": this.css.minAttachmentIconImgNode_list}).inject(this.iconImgAreaNode);
  293. this.iconImgNode.set({"src": this.getIcon(), "border": 0});
  294. this.textNode = new Element("div", {"styles": this.css.minAttachmentTextNode_list}).inject(this.node);
  295. this.textNode.set("text", this.data.name);
  296. var size = "";
  297. var k = this.data.length/1024;
  298. if (k>1024){
  299. var m = k/1024;
  300. m = Math.round(m*100)/100;
  301. size = m+"M";
  302. }else{
  303. k = Math.round(k*100)/100;
  304. size = k+"K";
  305. }
  306. this.textSizeNode = new Element("div", {"styles": this.css.minAttachmentSizeNode_list}).inject(this.textNode);
  307. this.textSizeNode.set("text", "("+size+")");
  308. },
  309. setEvent: function(){
  310. this.node.addEvents({
  311. "mouseover": function(){
  312. if (!this.isSelected){
  313. if (this.controller.options.listStyle==="list" || this.controller.options.listStyle==="sequence"){
  314. this.node.setStyles(this.css["minAttachmentNode_"+this.controller.options.listStyle+"_over"]);
  315. }else{
  316. this.node.setStyles(this.css["attachmentNode_"+this.controller.options.listStyle+"_over"]);
  317. }
  318. }
  319. }.bind(this),
  320. "mouseout": function(){
  321. if (!this.isSelected){
  322. if (this.controller.options.listStyle==="list" || this.controller.options.listStyle==="sequence"){
  323. var cssKey = "minAttachmentNode_"+this.controller.options.listStyle + ( layout.mobile ? "_mobile" : "" );
  324. this.node.setStyles(this.css[cssKey]);
  325. }else{
  326. this.node.setStyles(this.css["attachmentNode_"+this.controller.options.listStyle]);
  327. }
  328. }
  329. }.bind(this),
  330. "mousedown": function(e){this.selected(e);e.stopPropagation();}.bind(this),
  331. "click": function(e){e.stopPropagation();}.bind(this),
  332. "dblclick": function(e){this.openAttachment(e);}.bind(this)
  333. });
  334. }
  335. });
  336. MWF.xApplication.Forum.Attachment = new Class({
  337. Implements: [Options, Events],
  338. options: {
  339. "documentId" : "",
  340. "isNew": false,
  341. "isEdited" : true,
  342. "size" : "max",
  343. "isSizeChange" : true
  344. },
  345. initialize: function (node, app, actions, lp, options) {
  346. this.setOptions(options);
  347. this.app = app;
  348. this.node = $(node);
  349. this.actions = actions;
  350. this.lp = lp;
  351. },
  352. load: function () {
  353. this.loadAttachmentController();
  354. },
  355. loadAttachmentController: function () {
  356. var options = {
  357. "style": "cms",
  358. "title": this.lp.attachmentArea,
  359. "size": this.options.size ,
  360. "resize": true,
  361. //"attachmentCount": this.json.attachmentCount || 0,
  362. "isUpload": (this.options.isNew || this.options.isEdited),
  363. "isDelete": (this.options.isNew || this.options.isEdited),
  364. "isReplace": false,
  365. "isDownload": true,
  366. "isSizeChange": this.options.isSizeChange,
  367. "readonly": (!this.options.isNew && !this.options.isEdited )
  368. };
  369. this.attachmentController = new MWF.xApplication.Forum.AttachmentController(this.node, this, options);
  370. this.attachmentController.isAnonymous = this.isAnonymous;
  371. this.attachmentController.anonymousPerson = this.anonymousPerson;
  372. this.attachmentController.anonymousName = this.anonymousName;
  373. this.attachmentController.load();
  374. //this.actions.listAttachmentInfo.each(function (att) {
  375. // this.attachmentController.addAttachment(att);
  376. //}.bind(this));
  377. if( this.data ){
  378. this.data.each(function (att) {
  379. this.attachmentController.addAttachment(att);
  380. }.bind(this));
  381. }else if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  382. this.listAttachment( function( json ){
  383. json.data.each(function (att) {
  384. this.attachmentController.addAttachment(att);
  385. }.bind(this));
  386. }.bind(this))
  387. }
  388. },
  389. transportData : function( json ){
  390. if( typeOf(json.data) == "array" ){
  391. json.data.each(function(d){
  392. d.person = d.creatorUid;
  393. d.lastUpdateTime = d.updateTime;
  394. })
  395. }else if( typeOf(json.data) == "object" ){
  396. var d = json.data;
  397. d.person = d.creatorUid;
  398. d.lastUpdateTime = d.updateTime;
  399. }else{
  400. json.each(function(d){
  401. d.person = d.creatorUid;
  402. d.lastUpdateTime = d.updateTime;
  403. })
  404. }
  405. return json;
  406. },
  407. listAttachment: function( callback ){
  408. if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  409. this.actions.listAttachment(this.options.documentId, function(json){
  410. if(callback)callback(this.transportData(json));
  411. }.bind(this))
  412. }
  413. },
  414. createUploadFileNode: function () {
  415. this.attachmentController.doUploadAttachment(
  416. {"site": this.options.documentId},
  417. this.actions.action,
  418. "uploadAttachment",
  419. {"id": this.options.documentId, "documentid":this.options.documentId},
  420. null,
  421. function(o){
  422. var j = o;
  423. if ( j.data ) {
  424. //j.userMessage
  425. var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  426. this.actions.getAttachment(aid, this.options.documentId, function (json) {
  427. json = this.transportData(json);
  428. if (json.data) {
  429. this.attachmentController.addAttachment(json.data, o.messageId);
  430. //this.attachmentList.push(json.data);
  431. }
  432. this.attachmentController.checkActions();
  433. this.fireEvent("upload", [json.data]);
  434. this.fireEvent("change");
  435. }.bind(this))
  436. }
  437. this.attachmentController.checkActions();
  438. }.bind(this),
  439. function(files){
  440. this.isQueryUploadSuccess = true;
  441. this.fireEvent( "queryUploadAttachment" );
  442. return this.isQueryUploadSuccess;
  443. }.bind(this),
  444. null, null, null,
  445. function (o) { //错误的回调
  446. if (o.messageId && this.attachmentController.messageItemList) {
  447. var message = this.attachmentController.messageItemList[o.messageId];
  448. if( message && message.node )message.node.destroy();
  449. }
  450. }.bind(this));
  451. // this.uploadFileAreaNode = new Element("div");
  452. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  453. // this.uploadFileAreaNode.set("html", html);
  454. //
  455. // this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  456. // this.fileUploadNode.addEvent("change", function () {
  457. // this.isQueryUploadSuccess = true;
  458. // this.fireEvent( "queryUploadAttachment" );
  459. // if( this.isQueryUploadSuccess ){
  460. // var files = this.fileUploadNode.files;
  461. // if (files.length) {
  462. // for (var i = 0; i < files.length; i++) {
  463. // var file = files.item(i);
  464. //
  465. // var formData = new FormData();
  466. // formData.append('file', file);
  467. // formData.append('site', this.options.documentId);
  468. //
  469. // this.actions.uploadAttachment(this.options.documentId, function (o, text) {
  470. // j = JSON.decode(text);
  471. // if ( j.data ) {
  472. // //j.userMessage
  473. // var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  474. // this.actions.getAttachment(aid, this.options.documentId, function (json) {
  475. // json = this.transportData(json);
  476. // if (json.data) {
  477. // this.attachmentController.addAttachment(json.data);
  478. // //this.attachmentList.push(json.data);
  479. // }
  480. // this.attachmentController.checkActions();
  481. //
  482. // this.fireEvent("upload", [json.data]);
  483. // }.bind(this))
  484. // }
  485. // this.attachmentController.checkActions();
  486. // }.bind(this), null, formData, file);
  487. // }
  488. // }
  489. // }else{
  490. // this.uploadFileAreaNode.destroy();
  491. // this.uploadFileAreaNode = false;
  492. // }
  493. // }.bind(this));
  494. },
  495. uploadAttachment: function (e, node) {
  496. //if (!this.uploadFileAreaNode) {
  497. this.createUploadFileNode();
  498. //}
  499. //this.fileUploadNode.click();
  500. },
  501. deleteAttachments: function (e, node, attachments) {
  502. var names = [];
  503. attachments.each(function (attachment) {
  504. names.push(attachment.data.name);
  505. }.bind(this));
  506. var _self = this;
  507. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  508. while (attachments.length) {
  509. attachment = attachments.shift();
  510. _self.deleteAttachment(attachment);
  511. }
  512. this.close();
  513. }, function () {
  514. this.close();
  515. }, null);
  516. },
  517. deleteAttachment: function (attachment) {
  518. this.fireEvent("delete", [attachment.data]);
  519. this.actions.deleteAttachment(attachment.data.id, this.documentId, function (json) {
  520. this.attachmentController.removeAttachment(attachment);
  521. //this.form.businessData.attachmentList.erase( attachment.data )
  522. this.attachmentController.checkActions();
  523. }.bind(this));
  524. },
  525. replaceAttachment: function (e, node, attachment) {
  526. return false;
  527. var _self = this;
  528. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  529. _self.replaceAttachmentFile(attachment);
  530. this.close();
  531. }, function () {
  532. this.close();
  533. }, null);
  534. },
  535. createReplaceFileNode: function (attachment) {
  536. this.replaceFileAreaNode = new Element("div");
  537. var html = "<input name=\"file\" type=\"file\" multiple/>";
  538. this.replaceFileAreaNode.set("html", html);
  539. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  540. this.fileReplaceNode.addEvent("change", function () {
  541. var files = this.fileReplaceNode.files;
  542. if (files.length) {
  543. for (var i = 0; i < files.length; i++) {
  544. var file = files.item(i);
  545. var formData = new FormData();
  546. formData.append('file', file);
  547. // formData.append('site', this.json.id);
  548. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  549. this.form.documentAction.getAttachment(attachment.data.id, this.opetions.documentId, function (json) {
  550. attachment.data = json.data;
  551. attachment.reload();
  552. if (o.messageId && this.attachmentController.messageItemList) {
  553. var message = this.attachmentController.messageItemList[o.messageId];
  554. if( message && message.node )message.node.destroy();
  555. }
  556. this.attachmentController.checkActions();
  557. }.bind(this))
  558. }.bind(this), null, formData, file);
  559. }
  560. }
  561. }.bind(this));
  562. },
  563. replaceAttachmentFile: function (attachment) {
  564. if (!this.replaceFileAreaNode) {
  565. this.createReplaceFileNode(attachment);
  566. }
  567. this.fileReplaceNode.click();
  568. },
  569. //小程序文件是否支持打开
  570. checkMiniProgramFile: function(ext) {
  571. var exts = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"];
  572. for(var i = 0; i < exts.length; i++){
  573. if(ext === exts[i]){
  574. return true;
  575. }
  576. }
  577. return false;
  578. },
  579. downloadAttachment: function (e, node, attachments) {
  580. //if( this.app.access.isAnonymousDynamic() ){
  581. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  582. //}else {
  583. attachments.each(function (att) {
  584. if (window.o2android && window.o2android.downloadAttachment) {
  585. window.o2android.downloadAttachment(att.data.id);
  586. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  587. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  588. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  589. wx.miniProgram.navigateTo({
  590. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  591. });
  592. } else {
  593. if (layout.mobile) {
  594. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  595. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  596. var xtoken = Cookie.read(o2.tokenName);
  597. window.location = o2.filterUrl(url + "?"+o2.tokenName+"=" + xtoken);
  598. });
  599. } else {
  600. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  601. }
  602. }
  603. }.bind(this));
  604. //}
  605. },
  606. openAttachment: function (e, node, attachments) {
  607. //if( this.app.access.isAnonymousDynamic() ){
  608. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  609. //}else{
  610. attachments.each(function (att) {
  611. if (window.o2android && window.o2android.downloadAttachment) {
  612. window.o2android.downloadAttachment(att.data.id);
  613. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  614. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  615. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  616. wx.miniProgram.navigateTo({
  617. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  618. });
  619. } else {
  620. if (layout.mobile) {
  621. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  622. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  623. var xtoken = Cookie.read(o2.tokenName);
  624. window.location = o2.filterUrl(url + "?"+o2.tokenName+"=" + xtoken);
  625. });
  626. } else {
  627. this.actions.getAttachmentData(att.data.id, this.options.documentId);
  628. }
  629. }
  630. }.bind(this));
  631. //}
  632. },
  633. getAttachmentUrl: function (attachment, callback) {
  634. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  635. },
  636. getAttachmentData : function(){
  637. var data = [];
  638. this.attachmentController.attachments.each(function( att ){
  639. data.push(att.data)
  640. });
  641. return data;
  642. },
  643. getAttachmentIds : function(){
  644. var ids = [];
  645. this.attachmentController.attachments.each(function( att ){
  646. ids.push(att.data.id)
  647. });
  648. return ids;
  649. },
  650. loadAttachmentSelecter: function( option, callback ){
  651. MWF.require("MWF.widget.AttachmentSelector", function() {
  652. var options = {
  653. "style" : "cms",
  654. "title": this.lp.selectAttachment,
  655. "listStyle": "icon",
  656. "selectType" : "all",
  657. "size": "max",
  658. "attachmentCount": 0,
  659. "isUpload": true,
  660. "isDelete": true,
  661. "isReplace": true,
  662. "isDownload": true,
  663. "toBase64" : true,
  664. "base64MaxSize" : 800,
  665. "readonly": false
  666. };
  667. options = Object.merge( options, option );
  668. if (this.readonly) options.readonly = true;
  669. this.attachmentController = new MWF.widget.AttachmentSelector(document.body, this, options);
  670. this.attachmentController.load();
  671. this.postSelect = callback;
  672. if( this.data ){
  673. this.data.each(function (att) {
  674. this.attachmentController.addAttachment(att);
  675. }.bind(this));
  676. }else{
  677. this.listAttachment( function( json ){
  678. json.data.each(function (att) {
  679. this.attachmentController.addAttachment(att);
  680. }.bind(this));
  681. }.bind(this))
  682. }
  683. }.bind(this));
  684. },
  685. selectAttachment: function(e, node, attachments){
  686. if( attachments.length > 0 ){
  687. var data = attachments[attachments.length-1].data;
  688. this.actions.getAttachmentUrl( data.id, this.options.documentId, function(url){
  689. if( this.attachmentController.options.toBase64 ){
  690. this.actions.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  691. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  692. if(this.postSelect)this.postSelect( url , data, base64Code )
  693. }.bind(this) )
  694. }else{
  695. if(this.postSelect)this.postSelect( url , data )
  696. }
  697. }.bind(this))
  698. }
  699. }
  700. });