TreePanel.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. Ext.define('saas.view.sys.power.TreePanel', {
  2. extend: 'Ext.tree.Panel',
  3. xtype: 'sys-power-treepanel',
  4. dataUrl:'/api/account/role/list',
  5. deleteUrl:'/api/account/role/delete/',
  6. initComponent: function () {
  7. var me = this;
  8. me.store = Ext.create('Ext.data.TreeStore', {
  9. fields : ['name', 'id', 'description','code'],
  10. autoLoad:false,
  11. root : {
  12. text : 'Root',
  13. id : 0,
  14. expanded : true
  15. }
  16. }),
  17. me.columns =[{
  18. xtype: 'treecolumn',
  19. dataIndex: 'text',
  20. flex: 1
  21. }, {
  22. xtype: 'actioncolumn',
  23. width: 28,
  24. iconCls: 'x-hidden icon x-fa fa-plus',
  25. getClass: function(v, meta, rec) {
  26. if(rec.get('code')=='ROLE_ADMIN'){
  27. return 'x-hidden icon x-fa fa-key';
  28. }else if(rec.get('leaf')){
  29. return 'x-hidden icon x-fa fa-pencil';
  30. }else{
  31. return 'x-hidden icon x-fa fa-plus';
  32. }
  33. },
  34. handler: function(treeview, rowIdx, colIdx, e) {
  35. var record = treeview.store.data.getAt(rowIdx);
  36. me.addItem(record);
  37. }
  38. }, {
  39. xtype: 'actioncolumn',
  40. width: 36,
  41. iconCls: 'x-hidden icon x-fa fa-trash-o',
  42. getClass: function(v, meta, rec) {
  43. if(rec.get('code')=='ROLE_ADMIN'){
  44. return '';
  45. }else if(rec.get('leaf')){
  46. return 'x-hidden icon x-fa fa-trash-o';
  47. }else {
  48. return '';
  49. }
  50. },
  51. handler: function(treeview, rowIdx, colIdx, e) {
  52. var record = treeview.store.data.getAt(rowIdx);
  53. me.deleteItem(record);
  54. }
  55. }];
  56. me.setTree();
  57. me.callParent(arguments);
  58. },
  59. listeners: {
  60. itemmouseenter: function(th, record, item, index, e, eOpts) {
  61. var imgs = item.getElementsByClassName('icon x-fa');
  62. Ext.Array.each(imgs, function(img) {
  63. img.classList.remove('x-hidden');
  64. });
  65. },
  66. itemmouseleave: function(th, record, item, index, e, eOpts) {
  67. var imgs = item.getElementsByClassName('icon x-fa');
  68. Ext.Array.each(imgs, function(img) {
  69. img.classList.add('x-hidden');
  70. });
  71. },
  72. itemclick:function(view,record,index,d,e){
  73. if(!record.get('leaf')){
  74. return;
  75. }
  76. var classList = e.target.classList;
  77. var clickIcon = false;
  78. Ext.Array.each(classList, function(item) {
  79. if(item.indexOf('x-action-col')>-1){
  80. clickIcon = true;
  81. }
  82. });
  83. if(clickIcon){return;}
  84. //加载右边的grid
  85. var id = record.get('id');
  86. if(id&&id!=0){
  87. var grid = view.ownerCt.ownerCt.query('power-grid')[0];
  88. grid.initId = id;
  89. grid.store.load();
  90. }
  91. }
  92. },
  93. getData:function(){
  94. var me = this;
  95. var data = [];
  96. Ext.Ajax.request({
  97. url: me.dataUrl,
  98. params: '',
  99. method: 'GET',
  100. async:false,
  101. success: function(response, opts) {
  102. var _data = Ext.decode(response.responseText);
  103. if(_data&&_data.data){
  104. _data = _data.data;
  105. for (let index = 0; index < _data.length; index++) {
  106. var o = {
  107. code:_data[index].code,
  108. id: _data[index].id,
  109. text: _data[index].name,
  110. name:_data[index].name,
  111. description:_data[index].description,
  112. leaf:true,
  113. iconCls: 'x-fa fa-user',
  114. }
  115. data.push(o);
  116. }
  117. }
  118. },
  119. failure: function(response, opts) {}
  120. });
  121. return data;
  122. },
  123. setTree:function(){
  124. var me = this;
  125. var data = me.getData();
  126. me.getStore().setRootNode({
  127. text: '角色列表',
  128. id: '0',
  129. iconCls: 'x-fa fa-list',
  130. expanded: true,
  131. children: data
  132. });
  133. },
  134. addItem:function(rec){
  135. var me=this;
  136. var isleaf = rec.get('leaf');
  137. var record = isleaf?rec:null;
  138. var view = this.ownerCt.getController().getView();
  139. var document = Ext.create('saas.view.document.kind.Kind',{});
  140. this.dialog = view.add({
  141. xtype: 'document-kind-childwin',
  142. bind: {
  143. title: (record?'修改':'新增')+'角色'
  144. },
  145. dataKind:'personpower',
  146. belong:document.etc['personpower'],
  147. _parent:this,
  148. record:record,
  149. session: true,
  150. onSave:function(){
  151. var me = this;
  152. var belong = this.belong;
  153. me.setLoading(true);
  154. var form=this.down('form');
  155. var params = {};
  156. var names = belong.columns.map(column => column.dataIndex);
  157. Ext.Array.each(names,function(name) {
  158. if(name){
  159. var dataField = form.down('[name='+name+']');
  160. if(dataField&&dataField.value){
  161. params[name] = dataField.value;
  162. params._value = dataField.value;
  163. }
  164. }
  165. });
  166. var idField = form.down('[name='+belong.keyField+']');
  167. params[belong.keyField] = idField.value || 0;
  168. //保存接口
  169. saas.util.BaseUtil.request({
  170. url: idField.value?belong.updateUrl:belong.reqUrl,
  171. params: JSON.stringify(params),
  172. method: 'POST'
  173. })
  174. .then(function(localJson) {
  175. me.setLoading(false);
  176. if(localJson.success){
  177. saas.util.BaseUtil.showSuccessToast('保存成功');
  178. me._parent.setTree();
  179. form.ownerCt.close();
  180. }
  181. })
  182. .catch(function(res) {
  183. me.setLoading(false);
  184. console.error(res);
  185. saas.util.BaseUtil.showErrorToast('保存失败: ' + res.message);
  186. });
  187. }
  188. });
  189. this.dialog.show();
  190. },
  191. deleteItem:function(rec){
  192. if(rec&&(rec.get('code')=='ROLE_ADMIN'||!rec.get('leaf'))){
  193. return;
  194. }
  195. var me = this;
  196. me.setLoading(true);
  197. saas.util.BaseUtil.request({
  198. url: me.deleteUrl + rec.get('id'),
  199. params: '',
  200. method: 'POST'
  201. })
  202. .then(function(localJson) {
  203. me.setLoading(false);
  204. if(localJson.success){
  205. saas.util.BaseUtil.showSuccessToast('删除成功');
  206. me.setTree();
  207. }
  208. })
  209. .catch(function(res) {
  210. me.setLoading(false);
  211. console.error(res);
  212. saas.util.BaseUtil.showErrorToast('删除失败: ' + res.message);
  213. });
  214. }
  215. });