TaskTreeGrid.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. Ext.define('erp.view.core.grid.TaskTreeGrid',{
  2. extend: 'Ext.tree.Panel',
  3. alias: 'widget.erpTaskTreeGrid',
  4. region: 'south',
  5. layout : 'fit',
  6. id: 'treegrid',
  7. emptyText : $I18N.common.grid.emptyText,
  8. useArrows: true,
  9. rootVisible: false,
  10. frame:true,
  11. columnLines:true,
  12. saveNodes: [],
  13. updateNodes: [],
  14. deleteNodes: [],
  15. FormUtil: Ext.create('erp.util.FormUtil'),
  16. store: Ext.create('Ext.data.TreeStore', {
  17. fields: fields,
  18. root : {
  19. text: 'root',
  20. id: 'root',
  21. expanded: true
  22. }
  23. }),
  24. columns: columns,
  25. plugins: Ext.create('Ext.grid.plugin.CellEditing', {
  26. clicksToEdit: 1
  27. }),
  28. tbar: [{
  29. iconCls: 'tree-add',
  30. text: $I18N.common.button.erpAddButton,
  31. handler: function(){
  32. var treegrid = Ext.getCmp('treegrid');
  33. var record = treegrid.selModel.selected.items[0];
  34. if(record){
  35. if(record.data.tt_id!=0&&record.data.tt_id!=null){
  36. record.data['leaf'] = false;
  37. record.data['cls'] = 'x-tree-cls-parent';
  38. if(record.data['tt_isleaf']='T')
  39. {
  40. record.data['tt_isleaf'] = 'F';
  41. treegrid.updateNodes.push(record);
  42. }
  43. record.dirty = true;
  44. var o = {
  45. tt_name: 'New Task',
  46. tt_ptid:ptdata.pt_id,
  47. tt_ptname:ptdata.pt_name,
  48. tt_parentid:record.data.tt_id,
  49. tt_isleaf:'T',
  50. leaf: true,
  51. allowDrag: true
  52. };
  53. record.appendChild(o);
  54. record.expand(true);
  55. }else {
  56. warnMsg('如果在该节点下添加子节点,需先保存该节点,是否保存?', function(btn){
  57. if(btn == 'yes'){
  58. if(record.data['tt_name'] == null || record.data['tt_name'] == ''){
  59. showError('请先描述该节点');
  60. return;
  61. } else {
  62. record.data['leaf'] = false;
  63. record.data['cls'] = 'x-tree-cls-parent';
  64. record.data['tt_isleaf'] = 'F';
  65. treegrid.saveNodes.push(record);
  66. treegrid.saveNode();
  67. }
  68. } else if(btn == 'no'){
  69. return;
  70. }
  71. });
  72. }
  73. }else {
  74. showError("请选择添加任务的父节点!");
  75. }
  76. }
  77. }
  78. ,{
  79. text: '添加父任务',
  80. iconCls : 'tree-add',
  81. handler: function () {
  82. var o = {
  83. tt_name:'New Task',
  84. tt_ptid:ptdata.pt_id,
  85. tt_ptname:ptdata.pt_name,
  86. tt_parentid:0,
  87. tt_isleaf:'F',
  88. leaf: false,
  89. allowDrag: true
  90. };
  91. Ext.getCmp('treegrid').store.getRootNode().appendChild(o);
  92. }
  93. },
  94. {
  95. iconCls: 'tree-delete',
  96. text: $I18N.common.button.erpDeleteButton,
  97. handler: function(){
  98. var treegrid = Ext.getCmp('treegrid');
  99. var items = treegrid.selModel.selected.items;
  100. if(items.length > 0){
  101. if(items[0].isLeaf() == true){
  102. if(items[0].data['tt_id'] != null && items[0].data['tt_id'] !=0){
  103. warnMsg('确定删除节点权限[' + items[0].data['tt_name'] + "]?", function(btn){
  104. if(btn == 'yes'){
  105. treegrid.deleteNode(items[0]);
  106. } else if(btn == 'no'){
  107. return;
  108. }
  109. });
  110. } else {
  111. items[0].remove(true);
  112. }
  113. } else {
  114. if(items[0].data['tt_id'] != null || items[0].data['tt_id'] != 0){
  115. warnMsg('确定删除节点[' + items[0].data['tt_name'] + ']及其子节点下所有权限?', function(btn){
  116. if(btn == 'yes'){
  117. treegrid.deleteNode(items[0]);
  118. } else if(btn == 'no'){
  119. return;
  120. }
  121. });
  122. } else {
  123. items[0].remove(true);
  124. }
  125. }
  126. } else {
  127. var record = treegrid.getExpandItem();
  128. if(record){
  129. if(record.childNodes.length == 0){
  130. if(record.data['tt_id'] != null && record.data['tt_id'] !=0){
  131. warnMsg('确定删除节点[' + record.data['tt_name'] + ']?', function(btn){
  132. if(btn == 'yes'){
  133. treegrid.deleteNode(record);
  134. } else if(btn == 'no'){
  135. return;
  136. }
  137. });
  138. } else {
  139. record.remove(true);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. },{
  146. iconCls: 'tree-save',
  147. text: $I18N.common.button.erpSaveButton,
  148. handler: function(){
  149. var treegrid = Ext.getCmp('treegrid'),
  150. nodes = treegrid.store.tree.root.childNodes;
  151. treegrid.saveNodes = [];
  152. Ext.each(nodes, function(){
  153. treegrid.checkChild(this);
  154. });
  155. treegrid.saveNode();
  156. }
  157. }],
  158. bodyStyle:'background-color:#f1f1f1;',
  159. initComponent : function(){
  160. Ext.override(Ext.data.AbstractStore,{
  161. indexOf: Ext.emptyFn
  162. });
  163. this.callParent(arguments);
  164. formCondition = getUrlParam('formCondition');//从url解析参数
  165. formCondition = (formCondition == null) ? "" : formCondition.replace(/IS/g,"=");
  166. Ext.Ajax.request({//
  167. url : basePath + 'plm/project/getProjectTemplate.action',
  168. params:{
  169. condition:formCondition,
  170. caller:'ProjectTemplate'
  171. },
  172. async:false,
  173. method : 'get',
  174. callback : function(options,success,response){
  175. var res = new Ext.decode(response.responseText);
  176. if(res.success){
  177. ptdata=new Ext.decode(res.data);
  178. }if(res.exceptionInfo)
  179. {
  180. showError(res.exceptionInfo);return;
  181. }
  182. }
  183. });
  184. condition=(formCondition=="")?"":formCondition.replace('pt_id','tt_ptid');
  185. this.getTreeGridNode({parentId:0,condition:condition});
  186. },
  187. listeners: {//滚动条有时候没反应,添加此监听器
  188. scrollershow: function(scroller) {
  189. if (scroller && scroller.scrollEl) {
  190. scroller.clearManagedListeners();
  191. scroller.mon(scroller.scrollEl, 'scroll', scroller.onElScroll, scroller);
  192. }
  193. }
  194. },
  195. getTreeGridNode: function(param){
  196. var me = this;
  197. var activeTab = me.getActiveTab();
  198. activeTab.setLoading(true);
  199. Ext.Ajax.request({//拿到tree数据
  200. url : basePath + 'common/TaskTree.action',
  201. params: param,
  202. callback : function(options,success,response){
  203. var res = new Ext.decode(response.responseText);
  204. activeTab.setLoading(false);
  205. if(res.tree){
  206. var tree = res.tree;
  207. Ext.each(tree, function(t){
  208. t.tt_startdate=t.tt_startdate;
  209. t.tt_enddate=t.tt_enddate;
  210. });
  211. me.store.setRootNode({
  212. text: 'root',
  213. id: 'root',
  214. expanded: true,
  215. children: tree
  216. });
  217. Ext.each(me.store.tree.root.childNodes, function(){
  218. this.dirty = false;
  219. });
  220. } else if(res.exceptionInfo){
  221. showError(res.exceptionInfo);
  222. }
  223. }
  224. });
  225. },
  226. checkChild: function(record){
  227. var me = this;
  228. if(!record.data['leaf']){
  229. if(record.childNodes.length > 0){
  230. if(record.data['tt_id'] == null || record.data['tt_id'] == 0){
  231. warnMsg('如果在节点' + record.data['tt_name'] + '下添加子节点,需先保存该节点,是否保存?', function(btn){
  232. if(btn == 'yes'){
  233. if(items[0].data['tt_name'] == null || items[0].data['tt_name'] == ''){
  234. showError('请先描述该节点');
  235. return;
  236. } else {
  237. me.saveNodes.push(items[0]);
  238. me.saveNode();
  239. }
  240. } else if(btn == 'no'){
  241. return;
  242. }
  243. });
  244. }
  245. Ext.each(record.childNodes, function(){
  246. me.checkChild(this);
  247. });
  248. }
  249. if(record.dirty){
  250. if(record.data['tt_id'] == null || record.data['tt_id'] == 0){
  251. me.saveNodes.push(record);
  252. } else {
  253. me.updateNodes.push(record);
  254. }
  255. }
  256. } else {
  257. if(record.dirty){
  258. if(record.data['tt_id'] == null || record.data['tt_id'] == 0){
  259. me.saveNodes.push(record);
  260. } else {
  261. me.updateNodes.push(record);
  262. }
  263. }
  264. }
  265. },
  266. saveNode: function(){
  267. var me = this;
  268. me.getUpdateNodes();
  269. var save = new Array();
  270. var update = new Array();
  271. var index = 0;
  272. Ext.each(me.saveNodes, function(){
  273. if(this.data.tt_name != null && this.data.tt_name != ''){
  274. var o = {
  275. tt_id: this.data.tt_id,
  276. tt_name: this.data.tt_name,
  277. tt_parentid:this.data.tt_parentid,
  278. tt_isleaf: this.data.tt_isleaf,
  279. tt_ptid:this.data.tt_ptid,
  280. tt_ptname:this.data.tt_ptname,
  281. tt_code: this.data.tt_code
  282. };
  283. save[index++] = Ext.JSON.encode(o);
  284. }
  285. });
  286. index = 0;
  287. Ext.each(me.updateNodes, function(){
  288. if(this.data.tt_name != null && this.data.tt_name != ''){
  289. var o = {
  290. tt_id: this.data.tt_id,
  291. tt_name: this.data.tt_name,
  292. tt_parentid: this.data.tt_parentid,
  293. tt_isleaf: this.data.tt_isleaf,
  294. tt_ptid:this.data.tt_ptid,
  295. tt_ptname:this.data.tt_ptname,
  296. tt_code: this.data.tt_code
  297. };
  298. update[index++] = Ext.JSON.encode(o);
  299. }
  300. });
  301. if(save.length > 0 || update.length > 0){
  302. var activeTab = me.getActiveTab();
  303. activeTab.setLoading(true);
  304. Ext.Ajax.request({
  305. url : basePath + 'plm/task/saveTaskTemplate.action',
  306. params: {
  307. save: unescape(save.toString().replace(/\\/g,"%")),
  308. update: unescape(update.toString().replace(/\\/g,"%"))
  309. },
  310. callback : function(options,success,response){
  311. var res = new Ext.decode(response.responseText);
  312. activeTab.setLoading(false);
  313. if(res.success){
  314. me.saveNodes = [];
  315. me.updateNodes = [];
  316. me.getTreeGridNode({parentId:0,condition:condition});
  317. } else if(res.exceptionInfo){
  318. showError(res.exceptionInfo);
  319. }
  320. }
  321. });
  322. }
  323. },
  324. getExpandItem: function(root){
  325. var me = this;
  326. if(!root){
  327. root = this.store.tree.root;
  328. }
  329. var node = null;
  330. if(root.childNodes.length > 0){
  331. Ext.each(root.childNodes, function(){
  332. if(this.isExpanded()){
  333. node = this;
  334. if(this.childNodes.length > 0){
  335. var n = me.getExpandItem(this);
  336. node = n == null ? node : n;
  337. }
  338. }
  339. });
  340. }
  341. return node;
  342. },
  343. deleteNode: function(record){
  344. var me = this;
  345. if(record){
  346. var activeTab = me.getActiveTab();
  347. activeTab.setLoading(true);
  348. Ext.Ajax.request({
  349. url : basePath + 'plm/task/saveTaskTemplate.action',
  350. params: {
  351. id: Number(record.data['tt_id'])
  352. },
  353. callback : function(options,success,response){
  354. var res = new Ext.decode(response.responseText);
  355. activeTab.setLoading(false);
  356. if(res.success){
  357. record.remove(true);
  358. } else if(res.exceptionInfo){
  359. showError(res.exceptionInfo);
  360. }
  361. }
  362. });
  363. }
  364. },
  365. getUpdateNodes: function(root){
  366. var me = this;
  367. if(!root){
  368. root = this.store.tree.root;
  369. me.updateNodes = [];
  370. }
  371. if(root.childNodes.length > 0){
  372. Ext.each(root.childNodes, function(){
  373. if(this.dirty){
  374. if(this.data['tt_id'] != null && this.data['tt_id'] != 0){
  375. me.updateNodes.push(this);
  376. }
  377. }
  378. if(this.data['leaf'] == false && this.childNodes.length > 0){
  379. me.getUpdateNodes(this);
  380. }
  381. });
  382. } else {
  383. if(root.dirty){
  384. if(root.data['tt_id'] != null && root.data['tt_id'] != 0){
  385. me.updateNodes.push(root);
  386. }
  387. }
  388. }
  389. },
  390. getActiveTab: function(){
  391. var tab = null;
  392. if(Ext.getCmp("content-panel")){
  393. tab = Ext.getCmp("content-panel").getActiveTab();
  394. }
  395. if(!tab){
  396. var win = parent.Ext.ComponentQuery.query('window');
  397. if(win.length > 0){
  398. tab = win[win.length-1];
  399. }
  400. }
  401. if(!tab && parent.Ext.getCmp("content-panel"))
  402. tab = parent.Ext.getCmp("content-panel").getActiveTab();
  403. if(!tab && parent.parent.Ext.getCmp("content-panel"))
  404. tab = parent.parent.Ext.getCmp("content-panel").getActiveTab();
  405. return tab;
  406. }
  407. });