TrashField.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * 显示附件名称、大小;
  3. * 附件删除、下载、显示
  4. */
  5. Ext.define('erp.view.core.trigger.TrashField', {
  6. extend : 'Ext.form.field.Trigger',
  7. alias : 'widget.trashfield',
  8. trigger1Cls: 'x-form-trash-trigger',
  9. trigger2Cls: 'x-form-download-trigger',
  10. fieldStyle: 'background:#C6E2FF;color:#D9D9D9;',
  11. autoShow: true,
  12. initComponent : function() {
  13. this.callParent(arguments);
  14. },
  15. listeners : {
  16. afterrender : function() {
  17. var me = this, c = Ext.Function.bind(me.onTrigger2Click, me);
  18. Ext.EventManager.on(me.inputEl, {
  19. click : c,
  20. scope : me,
  21. buffer : 100
  22. });
  23. Ext.DomHelper.applyStyles(me.inputEl.dom, 'border:none;');
  24. /* // 只读时,隐藏删除trigger
  25. Ext.defer(function(){
  26. var form = me.up('form');
  27. console.log(me);
  28. console.log(form);
  29. console.log(me.ownerCt);
  30. if(form.readOnly || me.ownerCt.readOnly) {
  31. me.getEl().down("." + me.trigger1Cls).applyStyles({display: 'none'});
  32. }
  33. }, 200);*/
  34. }
  35. },
  36. onTrigger1Click: function(){
  37. var me = this;
  38. warnMsg('确定删除' + me.value, function(btn){
  39. if(btn == 'yes'){
  40. var v = me.ownerCt.down('hidden').value.replace(me.filepath + ';', '');
  41. me.updateAttachField(v, '删除附件', function(){
  42. me.ownerCt.down('hidden').setValue(v);
  43. me.ownerCt.filesize -= me.filesize;
  44. me.ownerCt.setTitle('<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>附件' +
  45. '(总大小:' + Ext.util.Format.fileSize(me.ownerCt.filesize) + ")");
  46. });
  47. }
  48. });
  49. },
  50. onTrigger2Click: function(e){
  51. var path = this.realpath;
  52. if(this.isImage(path)) {
  53. var menu = this.createMenu();
  54. menu.path = path;
  55. menu.showAt(e.getXY());
  56. } else {
  57. this.download();
  58. }
  59. },
  60. download : function() {
  61. var me = this;
  62. if (!Ext.fly('ext-attach-download')) {
  63. var frm = document.createElement('form');
  64. frm.id = 'ext-attach-download';
  65. frm.name = id;
  66. frm.className = 'x-hidden';
  67. document.body.appendChild(frm);
  68. }
  69. Ext.Ajax.request({
  70. url: basePath + 'common/download.action?fileName=' + me.fileName,
  71. method: 'post',
  72. form: Ext.fly('ext-attach-download'),
  73. isUpload: true,
  74. params: {
  75. escape : 1,
  76. path : unescape(me.realpath)
  77. }
  78. });
  79. },
  80. createMenu : function() {
  81. var me = this, menu = this.menu;
  82. if (!menu) {
  83. menu = this.menu = Ext.create('Ext.menu.Menu', {
  84. items : [{
  85. text : '查看',
  86. handler : function() {
  87. me.showAttach(menu.path);
  88. }
  89. }, {
  90. text : '下载',
  91. handler : function() {
  92. me.download();
  93. }
  94. }]
  95. });
  96. }
  97. return menu;
  98. },
  99. showAttach : function() {
  100. var me = this, resizer = me.resizer,
  101. imageframe = document.getElementById('ext-image-frame'),
  102. src = basePath + 'common/download.action?path=' + me.realpath.replace(/\+/g, '%2B');
  103. if (!imageframe) {
  104. var el = Ext.DomHelper.append(document.body, '<img id="ext-image-frame" src="' + src +
  105. '" width="500" height="400" style="position:absolute;left:0;top:0;"/>', true);
  106. imageframe = el.dom;
  107. } else {
  108. imageframe.src = src;
  109. }
  110. if (!resizer) {
  111. resizer = this.resizer = Ext.create('Ext.resizer.Resizer', {
  112. target: 'ext-image-frame',
  113. pinned: true,
  114. width: 510,
  115. height: 410,
  116. minWidth: 100,
  117. minHeight: 80,
  118. preserveRatio: true,
  119. handles: 'all',
  120. dynamic: true
  121. });
  122. var resizerEl = resizer.getEl();
  123. resizerEl.on('dblclick', function(){
  124. resizerEl.hide(true);
  125. });
  126. }
  127. resizer.getEl().center();
  128. resizer.getEl().show(true);
  129. Ext.DomHelper.applyStyles(imageframe, 'position:absolute;z-index:100;');
  130. },
  131. isImage : function(path) {
  132. if (Ext.isEmpty(path)) {
  133. return false;
  134. }
  135. if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(path)) {
  136. return false;
  137. }
  138. return true;
  139. },
  140. updateAttachField: function(value, type, fn) {
  141. var form = this.ownerCt.ownerCt;
  142. if(form.keyField ){
  143. var field = this.ownerCt.name,me = this, val = Ext.getCmp(form.keyField).value;
  144. if(!Ext.isEmpty(val) && this.ownerCt.autoUpdate) {
  145. Ext.Ajax.request({
  146. url: basePath + 'common/attach/change.action',
  147. params: {
  148. caller: caller,
  149. table: form.tablename.toUpperCase().split("LEFT")[0],
  150. update: field + '=\'' + value + '\'',
  151. condition: form.keyField + '=' + val,
  152. type: type
  153. },
  154. callback: function(opt, s, r) {
  155. var res = Ext.decode(r.responseText);
  156. if(res.success) {
  157. if(fn) {
  158. fn.call();
  159. me.destroy();
  160. }
  161. } else {
  162. showError(res.exceptionInfo);
  163. }
  164. }
  165. });
  166. } else {
  167. fn && fn.call();
  168. this.destroy();
  169. }
  170. }
  171. }
  172. });