SQLPanel.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. Ext.define('erp.view.core.form.SQLPanel', {
  2. extend : 'Ext.form.Panel',
  3. alias : 'widget.erpSQLPanel',
  4. id : 'sqlPanel',
  5. layout : 'border',
  6. autoScroll : true,
  7. width : 488,
  8. height : 400,
  9. frame : true,
  10. baseCls : 'my-panel-no-border',
  11. _noc : 0,
  12. dispArr : null,
  13. hideArr : null,
  14. flag:false,
  15. index:0,
  16. formatDisplay:false,
  17. requires: ['erp.view.core.button.TestSQL'],
  18. initComponent : function() {
  19. flag=false;
  20. dispArr = new Array();
  21. hideArr = new Array();
  22. Ext.apply(this, {
  23. items : [{
  24. xtype : 'toolbar',
  25. region:'north',
  26. baseCls : 'my-panel-no-border',
  27. ui: 'footer',
  28. layout : 'column',
  29. height:27,
  30. items : [{
  31. xtype : 'textfield',
  32. id : 'testText',
  33. columnWidth : 0.7,
  34. fieldLabel : '输入值',
  35. maxLength : 100,
  36. style:'padding-top:2',
  37. focusCls : 'x-form-field-cir-focus',
  38. labelAlign : "right",
  39. blankText : $I18N.common.form.blankText
  40. }, {
  41. xtype : 'button',
  42. text : '测试',
  43. style:'margin-left:5px',
  44. listeners : {
  45. click : {
  46. fn : this.test,
  47. scope : this
  48. }
  49. }
  50. }]
  51. }, {
  52. id : 'dispText',
  53. anchor : '100% 64%',
  54. xtype: 'htmleditor',
  55. region:'center',
  56. enableColors: false,
  57. enableAlignments: false,
  58. columnWidth:1,
  59. enableFont: false,
  60. enableFontSize: false,
  61. enableFormat: false,
  62. enableLinks: false,
  63. enableLists: false,
  64. readOnly:true,
  65. enableSourceEdit: false,
  66. height:120,
  67. labelWidth:105
  68. }, {
  69. xtype : 'panel',
  70. region:'south',
  71. baseCls : 'my-panel-no-border',
  72. anchor : '100% 30%',
  73. defaults:{
  74. xtype:'toolbar',
  75. ui:'footer',
  76. baseCls : 'my-panel-no-border'
  77. },
  78. items : [{
  79. defaults:{
  80. handler:this.inputStr,
  81. scope:this,
  82. xtype:'erpSQLButton',
  83. highLight:true
  84. },
  85. items : [{
  86. text : '输入值'
  87. }, {
  88. text : '标准分'
  89. }, {
  90. text : '最高分'
  91. }, {
  92. text : '最低分'
  93. },'->',{
  94. text:'语法',
  95. handler:this.showDemo
  96. }]
  97. },{
  98. defaults:{
  99. handler:this.inputStr,
  100. xtype:'erpSQLButton',
  101. scope:this
  102. },
  103. items : [{
  104. text : '1'
  105. }, {
  106. text : '2'
  107. }, {
  108. text : '3'
  109. }, {
  110. text : '+'
  111. }, {
  112. text : '('
  113. }, {
  114. text : ')'
  115. }, {
  116. text : '='
  117. }, {
  118. text : '空格'
  119. }]
  120. }, {
  121. defaults:{
  122. handler:this.inputStr,
  123. xtype:'erpSQLButton',
  124. scope:this
  125. },
  126. items : [{
  127. text : '4'
  128. }, {
  129. text : '5'
  130. }, {
  131. text : '6'
  132. }, {
  133. text : '-'
  134. }, {
  135. text : '<'
  136. }, {
  137. text : '>'
  138. }, {
  139. text : '>=',
  140. }, {
  141. text : '<='
  142. }]
  143. }, {
  144. defaults:{
  145. handler:this.inputStr,
  146. xtype:'erpSQLButton',
  147. scope:this
  148. },
  149. items : [{
  150. text : '7'
  151. }, {
  152. text : '8'
  153. }, {
  154. text : '9'
  155. }, {
  156. text : '*'
  157. }, {
  158. text : '开始',
  159. highLight:true
  160. }, {
  161. text : '如果',
  162. highLight:true
  163. }, {
  164. text : '则',
  165. highLight:true
  166. }, {
  167. text : '结束',
  168. highLight:true
  169. }]
  170. }, {
  171. defaults:{
  172. handler:this.inputStr,
  173. xtype:'erpSQLButton',
  174. scope:this
  175. },
  176. items : [{
  177. text : '0'
  178. }, {
  179. text : '.'
  180. }, {
  181. text : '/'
  182. }, {
  183. text : '条件与',
  184. highLight:true
  185. }, {
  186. text : '条件或',
  187. highLight:true
  188. }, {
  189. text : '否则',
  190. highLight:true
  191. }, {
  192. text : '删除',
  193. handler:this.delStr
  194. }, {
  195. text : '清空',
  196. handler:this.celStr
  197. }]
  198. }]
  199. }]
  200. });
  201. this.callParent(arguments);
  202. this.editSpace=this.items.items[1];
  203. },
  204. mappingStore:{
  205. 'input':'输入值',
  206. 'case':'开始',
  207. 'when':'如果',
  208. 'then':'则',
  209. 'end':'结束',
  210. 'or':'条件或',
  211. 'and':'条件与',
  212. 'else':'否则',
  213. '&nbsp;':'空格'
  214. },
  215. test : function(btn) {
  216. var me=this,input =' '+Ext.getCmp('testText').getValue();
  217. var tmpArr = Ext.Array.clone(hideArr);
  218. Ext.Array.forEach(hideArr, function(str, index, array) {
  219. if (str.trim() == 'input') {
  220. tmpArr[index] = input;
  221. }
  222. });
  223. var sql='select '+tmpArr.join("")+' from dual';
  224. Ext.Ajax.request({
  225. url : basePath + 'hr/KpiRule/testSQL.action',
  226. params : {
  227. sql :sql,
  228. caller : caller
  229. },
  230. method : 'post',
  231. callback : function(options, success, response) {
  232. var res = new Ext.decode(response.responseText);
  233. if (res.success) {
  234. flag=true;
  235. alert('测试通过');
  236. me.successAfter();
  237. } else if (res.exceptionInfo) {
  238. showError(res.exceptionInfo+'测试不通过,请重新输入!');
  239. }
  240. }
  241. });
  242. },
  243. inputStr : function(btn) {
  244. var _t=btn.text;
  245. if(btn.highLight){
  246. _t=' <font style="color:#800080;font-weight:600;" >['+btn.text+']</font>';
  247. }
  248. if(btn.text=='空格') {
  249. _t='&nbsp;';
  250. hideArr.push('');
  251. }else hideArr.push(this.getValueStr(btn.text));
  252. dispArr.push(_t);
  253. this.editSpace.setValue(this.editSpace.getValue()+_t);
  254. },
  255. getValueStr:function(text){
  256. var me=this,valueF;
  257. for( var p in me.mappingStore){
  258. if(me.mappingStore[p]==text){
  259. valueF=' '+p+' ';
  260. break;
  261. }
  262. }
  263. return valueF||text;
  264. },
  265. delStr : function() {
  266. var _disppoped=dispArr.pop(),_hidepoped=hideArr.pop(),_value=this.editSpace.getValue();
  267. this.editSpace.setValue(_value.substr(0,_value.lastIndexOf(_disppoped)));
  268. },
  269. celStr: function(){
  270. dispArr=new Array();
  271. hideArr=new Array();
  272. this.editSpace.setValue();
  273. },
  274. successAfter:function(){
  275. if(flag==true){
  276. Ext.getCmp('kr_sql').setValue(dispArr.join(""));
  277. Ext.getCmp('kr_sql1').setValue(hideArr.join(""));
  278. Ext.getCmp('kpisqlwin').close();
  279. }else{
  280. alert("测试通过才能保存");
  281. }
  282. },
  283. clearStr : function() {
  284. dispArr = [];
  285. hideArr = [];
  286. Ext.getCmp('dispText').setValue();
  287. },
  288. showDemo:function(btn,e){
  289. var win=Ext.getCmp('demo-sql'),me=this,_x=btn.getEl().getXY()[0];
  290. if(!win){
  291. win=Ext.create('Ext.window.Window', {
  292. id: 'demo-sql',
  293. title: '语法样例',
  294. height: window.innerHeight * 0.8,
  295. width:250,
  296. renderTo: Ext.getBody(),
  297. animCollapse: false,
  298. constrainHeader: true,
  299. bodyBorder: false,
  300. border: false,
  301. x: screen.width-_x+142,
  302. autoScroll:true,
  303. frame:false,
  304. html:me.getHtml(),
  305. bodyStyle: {
  306. background: '#f7f7f7'
  307. }
  308. });
  309. }
  310. win.show();
  311. },
  312. getHtml:function(){
  313. return this.getAndHtml()+this.getOrHtml()+this.getOrAndHtml()+this.getCaseHtml();
  314. },
  315. getAndHtml:function(){
  316. var me=this,title='<h4 class="sample-title">条件与语句</h4>';
  317. return title+me.getHighLightHtmlByKey(['input','>60','and','input','<=80']);
  318. },
  319. getOrHtml:function(){
  320. var me=this,title='<h4 class="sample-title">条件或语句</h4>';
  321. return title+me.getHighLightHtmlByKey(['input','>60','or','input','<=80']);
  322. },
  323. getOrAndHtml:function(){
  324. var me=this,title='<h4 class="sample-title">条件与或语句</h4>';
  325. return title+me.getHighLightHtmlByKey(['(','input','>60','and','input','<=80',')','or','(','input','>80',')']);
  326. },
  327. getCaseHtml:function(){
  328. var me=this,title='<h4 class="sample-title">如果语句</h4>';
  329. return title+me.getHighLightHtmlByKey(['case','when','input',' >80','then','A','when','input','>60','and','input','<=80','then','B','else','C','end']);
  330. },
  331. getHighLightHtmlByKey:function(keys){
  332. var _h="",me=this;
  333. Ext.each(keys,function(key){
  334. if(me.mappingStore[key]){
  335. if(key=='when' || key=='else' || key=='end')_h+='</br>&nbsp;&nbsp;';
  336. _h+='<font class="x-highLight" >['+me.mappingStore[key]+']</font> ';
  337. }else _h+=key;
  338. });
  339. return _h;
  340. }
  341. });