Config.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. Ext.QuickTips.init();
  2. Ext.define('erp.controller.ma.logic.Config', {
  3. extend: 'Ext.app.Controller',
  4. stores: ['TreeStore'],
  5. views: ['common.main.TreePanel','ma.logic.Config', 'core.form.ColorField',
  6. 'common.main.Toolbar','core.trigger.SearchField', 'core.trigger.DbfindTrigger'],
  7. refs: [{
  8. ref: 'tree',
  9. selector: '#tree-panel'
  10. },{
  11. ref: 'configPanel',
  12. selector: '#configPanel'
  13. },{
  14. ref: 'tabPanel',
  15. selector: '#tabpanel'
  16. }],
  17. init: function(){
  18. var me = this;
  19. me.FormUtil = Ext.create('erp.util.FormUtil');
  20. me.Toast = Ext.create('erp.view.core.window.Toast');
  21. this.control({
  22. 'erpTreePanel': {
  23. itemmousedown: function(selModel, record){
  24. Ext.defer(function(){
  25. me.onNodeClick(selModel, record);
  26. }, 20);
  27. },
  28. beforerender: function(tree) {
  29. if(window.whoami)
  30. tree.hide();
  31. }
  32. },
  33. '#configPanel': {
  34. boxready: function() {
  35. var caller = window.whoami || 'sys';
  36. me.getSetting(caller);
  37. }
  38. },
  39. 'button[id=btn-close]': {
  40. click: function(){
  41. var p = parent.Ext.getCmp('content-panel');
  42. if(p){
  43. p.getActiveTab().close();
  44. } else {
  45. window.close();
  46. }
  47. }
  48. },
  49. 'button[id=btn-save]': {
  50. click: function(btn){
  51. me.onSaveClick();
  52. }
  53. },
  54. 'dbfindtrigger': {
  55. aftertrigger: function(field, record, dbfinds) {
  56. Ext.Array.each(dbfinds, function(d){
  57. if(d.field == field.name) {
  58. field.setValue(record.get(d.dbGridField));
  59. }
  60. });
  61. }
  62. },
  63. 'button[cls=x-dd-drop-ok-add]': {
  64. click: function(btn) {
  65. var f = btn.ownerCt, c = btn.config;
  66. f.insert(f.items.length - 1, {
  67. xtype: (c.dbfind ? 'dbfindtrigger' : 'textfield'),
  68. name: c.dbfind || c.code,
  69. readOnly: !c.dbfind && c.editable == 0,
  70. editable: c.editable == 1,
  71. clearable: true
  72. });
  73. }
  74. }
  75. });
  76. },
  77. onNodeClick: function(selModel, record){
  78. var me = this;
  79. if (record.get('leaf')) {
  80. me.getSetting(record.raw.caller, record.get('text'));
  81. } else {
  82. if(record.isExpanded() && record.childNodes.length > 0){
  83. record.collapse(true, true);// 已展开则收拢
  84. } else {
  85. //未展开看是否加载了children
  86. if(record.childNodes.length == 0){
  87. me.getChildren(record);
  88. } else {
  89. record.expand(false, true);//展开
  90. }
  91. }
  92. }
  93. },
  94. /**
  95. * 从后台加载树节点
  96. */
  97. getChildren: function(record) {
  98. var tree = this.getTree();
  99. tree.setLoading(true, tree.body);
  100. Ext.Ajax.request({//拿到tree数据
  101. url : basePath + 'common/lazyTree.action',
  102. params: {
  103. parentId: record.get('id')
  104. },
  105. callback : function(opt, s, r){
  106. tree.setLoading(false);
  107. var res = new Ext.decode(r.responseText);
  108. if(res.tree && record.childNodes.length == 0){
  109. record.appendChild(res.tree);
  110. record.expand(false, true);//展开
  111. } else if(res.exceptionInfo){
  112. showError(res.exceptionInfo);
  113. }
  114. }
  115. });
  116. },
  117. getSetting: function(caller, title){
  118. var me = this;
  119. if(caller) {
  120. if(caller != me.currCaller) {
  121. me.loadConfigs(caller, function(configs){
  122. me.currCaller = caller;
  123. me.setConfigs(configs);
  124. title && me.getConfigPanel().setTitle(title);
  125. });
  126. me.loadInterceptors(caller, function(interceptors){
  127. me.setInterceptors(interceptors);
  128. var tab = me.getTabPanel();
  129. if(interceptors.length == 0 && !tab.collapsed)
  130. tab.collapse();
  131. else if(interceptors.length > 0 && tab.collapsed)
  132. tab.expand();
  133. });
  134. }
  135. } else {
  136. me.currCaller = null;
  137. }
  138. },
  139. /**
  140. * 配置参数
  141. */
  142. loadConfigs: function(caller, callback) {
  143. Ext.Ajax.request({
  144. url: basePath + 'ma/setting/configs.action?caller=' + caller,
  145. method: 'GET',
  146. callback: function(opt, s, r) {
  147. if(r && r.status == 200) {
  148. var res = Ext.JSON.decode(r.responseText);
  149. callback.call(null, res);
  150. }
  151. }
  152. });
  153. },
  154. /**
  155. * 配置逻辑
  156. */
  157. loadInterceptors: function(caller, callback) {
  158. Ext.Ajax.request({
  159. url: basePath + 'ma/setting/interceptors.action?caller=' + caller,
  160. method: 'GET',
  161. callback: function(opt, s, r) {
  162. if(r && r.status == 200) {
  163. var res = Ext.JSON.decode(r.responseText);
  164. callback.call(null, res);
  165. }
  166. }
  167. });
  168. },
  169. setConfigs: function(configs) {
  170. var me = this, pane = me.getConfigPanel(), items = [];
  171. Ext.Array.each(configs, function(c, i){
  172. switch(c.data_type) {
  173. case 'YN':
  174. items.push({
  175. xtype: 'checkbox',
  176. boxLabel: c.title,
  177. name: c.code,
  178. id: c.id,
  179. checked: c.data == 1,
  180. columnWidth: 1,
  181. margin: c.help ? '4 8 0 8' : '4 8 4 8'
  182. });
  183. break;
  184. case 'RADIO':
  185. var s = [];
  186. Ext.Array.each(c.properties, function(p){
  187. s.push({
  188. name: c.code,
  189. boxLabel: p.display,
  190. inputValue: p.value,
  191. checked: p.value == c.data
  192. });
  193. });
  194. items.push({
  195. xtype: 'radiogroup',
  196. id: c.id,
  197. fieldLabel: c.title,
  198. columnWidth: 1,
  199. columns: 2,
  200. vertical: true,
  201. items: s
  202. });
  203. break;
  204. case 'COLOR':
  205. items.push({
  206. xtype: 'colorfield',
  207. fieldLabel: c.title,
  208. id: c.id,
  209. name: c.code,
  210. value: c.data,
  211. readOnly: c.editable == 0,
  212. editable: c.editable == 1,
  213. labelWidth: 150
  214. });
  215. break;
  216. case 'NUMBER':
  217. items.push({
  218. xtype: 'numberfield',
  219. fieldLabel: c.title,
  220. id: c.id,
  221. name: c.code,
  222. value: c.data,
  223. readOnly: c.editable == 0,
  224. labelWidth: 150
  225. });
  226. break;
  227. default :
  228. if(c.multi == 1) {
  229. var data = c.data ? c.data.split('\n') : [null], s = [];
  230. Ext.Array.each(data, function(d){
  231. s.push({
  232. xtype: (c.dbfind ? 'dbfindtrigger' : 'textfield'),
  233. name: c.dbfind || c.code,
  234. value: d,
  235. readOnly: !c.dbfind && c.editable == 0,
  236. editable: c.editable == 1,
  237. clearable: true
  238. });
  239. });
  240. s.push({
  241. xtype: 'button',
  242. text: '添加',
  243. width: 22,
  244. cls: 'x-dd-drop-ok-add',
  245. iconCls: 'x-dd-drop-icon',
  246. iconAlign: 'right',
  247. config: c
  248. });
  249. items.push({
  250. xtype: 'fieldset',
  251. title: c.title,
  252. id: c.id,
  253. name: c.code,
  254. columnWidth: 1,
  255. layout: 'column',
  256. defaults: {
  257. columnWidth: .25,
  258. margin: '4 8 4 8'
  259. },
  260. items: s
  261. });
  262. } else {
  263. items.push({
  264. xtype: (c.dbfind ? 'dbfindtrigger' : 'textfield'),
  265. fieldLabel: c.title,
  266. id: c.id,
  267. name: c.dbfind || c.code,
  268. value: c.data,
  269. readOnly: !c.dbfind && c.editable == 0,
  270. editable: c.editable == 1,
  271. clearable: true,
  272. columnWidth: .5,
  273. labelWidth: 150
  274. });
  275. }
  276. break;
  277. }
  278. if(c.help) {
  279. items.push({
  280. xtype: 'displayfield',
  281. value: c.help,
  282. columnWidth: ['NUMBER', 'VARCHAR2'].indexOf(c.data_type) > -1 ? .5 : 1,
  283. cls: 'help-block',
  284. margin: '4 8 8 8'
  285. });
  286. } else {
  287. if(['NUMBER', 'VARCHAR2'].indexOf(c.data_type) > -1) {
  288. items.push({
  289. xtype: 'displayfield'
  290. });
  291. }
  292. }
  293. });
  294. pane.removeAll();
  295. if(items.length == 0)
  296. items.push({
  297. html: '没有参数配置',
  298. cls: 'x-form-empty'
  299. });
  300. pane.add(items);
  301. },
  302. /**
  303. * 字符长度
  304. */
  305. getCharLength: function(str) {
  306. if(str) {
  307. for (var len = str.length, c = 0, i = 0; i < len; i++)
  308. str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126 ? c += 2 : c++;
  309. return c;
  310. }
  311. return 0;
  312. },
  313. setInterceptors: function(interceptors) {
  314. var me = this, pane = me.getTabPanel(), panels = [];
  315. var types = Ext.Array.unique(Ext.Array.pluck(interceptors, 'type'));
  316. types = Ext.Array.sort(types, function(a, b){
  317. return me.getMethodTypes(a).weight > me.getMethodTypes(b).weight;
  318. });
  319. Ext.Array.each(types, function(type){
  320. var data = Ext.Array.filter(interceptors, function(i){
  321. return i.type == type;
  322. });
  323. Ext.Array.each(data, function(d){
  324. d.enable = d.enable == 1;
  325. });
  326. panels.push({
  327. title: me.getMethodTypes(type).text,
  328. xtype: 'grid',
  329. columns: [{
  330. text: '顺序',
  331. dataIndex: 'detno',
  332. xtype: 'numbercolumn',
  333. align: 'center',
  334. format: '0',
  335. width: 40
  336. },{
  337. text: '描述',
  338. dataIndex: 'title',
  339. flex: 10
  340. },{
  341. text: '启用',
  342. xtype: 'checkcolumn',
  343. dataIndex: 'enable',
  344. width: 60
  345. }],
  346. columnLines: true,
  347. store: new Ext.data.Store({
  348. fields: ['id', 'title', 'type', 'turn', 'detno', 'enable', 'class_', 'method'],
  349. data: data,
  350. groupField: 'turn',
  351. sorters: [{
  352. property: 'detno'
  353. }]
  354. }),
  355. features : [{
  356. ftype: 'grouping',
  357. groupHeaderTpl: '<tpl if="name == 0">前<tpl else>后</tpl> (共 {rows.length} 项)',
  358. startCollapsed: false
  359. }],
  360. viewConfig: {
  361. listeners: {
  362. render: function(view) {
  363. if (!view.tip) {
  364. view.tip = Ext.create('Ext.tip.ToolTip', {
  365. target : view.el,
  366. delegate : view.itemSelector,
  367. trackMouse : true,
  368. renderTo : Ext.getBody(),
  369. tpl: new Ext.XTemplate('<dl class="dl-horizontal">' +
  370. '<dt>类:</dt><dd>{class_}</dd>' +
  371. '<dt>方法:</dt><dd>{method}</dd>' +
  372. '</dl>'),
  373. listeners: {
  374. beforeshow: function (tip) {
  375. var record = view.getRecord(tip.triggerElement);
  376. if(record){
  377. tip.update(record.data);
  378. } else {
  379. tip.on('show', function(){
  380. Ext.defer(tip.hide, 10, tip);
  381. }, tip, {single: true});
  382. }
  383. }
  384. }
  385. });
  386. }
  387. }
  388. }
  389. }
  390. });
  391. });
  392. pane.removeAll();
  393. pane.add(panels);
  394. },
  395. getMethodTypes: function(type) {
  396. var types = {
  397. 'save': {
  398. text: '保存',
  399. weight: 1
  400. },
  401. 'update': {
  402. text: '更新',
  403. weight: 2
  404. },
  405. 'commit': {
  406. text: '提交',
  407. weight: 3
  408. },
  409. 'resCommit': {
  410. text: '反提交',
  411. weight: 4
  412. },
  413. 'audit': {
  414. text: '审核',
  415. weight: 5
  416. },
  417. 'resAudit': {
  418. text: '反审核',
  419. weight: 6
  420. },
  421. 'post': {
  422. text: '过账',
  423. weight: 7
  424. },
  425. 'resPost': {
  426. text: '反过账',
  427. weight: 8
  428. },
  429. 'print': {
  430. text: '打印',
  431. weight: 9
  432. },
  433. 'turnout': {
  434. text: '转出货',
  435. weight: 10
  436. },
  437. 'turn': {
  438. text: '转单',
  439. weight: 11
  440. },
  441. 'delete': {
  442. text: '删除',
  443. weight: 12
  444. },
  445. 'deletedetail': {
  446. text: '删除明细',
  447. weight: 13
  448. },
  449. 'finish': {
  450. text: '结案',
  451. weight: 14
  452. }
  453. };
  454. return types[type] || {text: type, weight: 99};
  455. },
  456. onSaveClick: function() {
  457. var me = this, pane = me.getConfigPanel(), tab = me.getTabPanel(),
  458. updatedConfigs = [], updatedInters = [];
  459. Ext.Array.each(pane.items.items, function(field){
  460. if(field.xtype == 'fieldset') {
  461. var vals = [];
  462. Ext.Array.each(field.items.items, function(i){
  463. if(i.name && typeof i.getValue === 'function' && !Ext.isEmpty(i.getValue())) {
  464. vals.push(i.getValue());
  465. }
  466. });
  467. updatedConfigs.push({
  468. id: field.id,
  469. data: vals.length > 0 ? vals.join('\n') : null
  470. });
  471. } else if(typeof field.isDirty === 'function' && field.isDirty()) {
  472. var value = field.getValue();
  473. updatedConfigs.push({
  474. id: field.id,
  475. data: typeof value === 'boolean' ? (value ? 1 : 0) : (field.xtype == 'radiogroup' ?
  476. Ext.Object.getValues(value)[0] : value)
  477. });
  478. }
  479. });
  480. Ext.Array.each(tab.items.items, function(grid){
  481. var modified = grid.store.getModifiedRecords();
  482. Ext.Array.each(modified, function(m){
  483. updatedInters.push({
  484. id: m.get('id'),
  485. enable: m.get('enable') ? 1 : 0,
  486. detno: m.get('detno'),
  487. turn: m.get('turn')
  488. });
  489. });
  490. });
  491. if(updatedConfigs.length > 0) {
  492. me.saveConfigs(updatedConfigs, function(){
  493. me.loadConfigs(me.currCaller, function(configs){
  494. me.setConfigs(configs);
  495. });
  496. });
  497. }
  498. if(updatedInters.length > 0) {
  499. me.saveInterceptors(updatedInters, function(){
  500. me.loadInterceptors(me.currCaller, function(interceptors){
  501. me.setInterceptors(interceptors);
  502. });
  503. });
  504. }
  505. },
  506. /**
  507. * 修改参数配置
  508. */
  509. saveConfigs: function(updated, callback) {
  510. var me = this;
  511. Ext.Ajax.request({
  512. url: basePath + 'ma/setting/configs.action',
  513. params: {
  514. updated: Ext.JSON.encode(updated)
  515. },
  516. method: 'POST',
  517. callback: function(opt, s, r) {
  518. if(r && r.status == 200) {
  519. me.Toast.info('提示', '参数修改成功');
  520. callback.call();
  521. }
  522. }
  523. });
  524. },
  525. /**
  526. * 修改逻辑配置
  527. */
  528. saveInterceptors: function(updated, callback) {
  529. var me = this;
  530. Ext.Ajax.request({
  531. url: basePath + 'ma/setting/interceptors.action',
  532. params: {
  533. updated: Ext.JSON.encode(updated)
  534. },
  535. method: 'POST',
  536. callback: function(opt, s, r) {
  537. if(r && r.status == 200) {
  538. me.Toast.info('提示', '逻辑修改成功');
  539. callback.call();
  540. }
  541. }
  542. });
  543. }
  544. });