template.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/4/23
  7. * @version
  8. */
  9. $(document).ready(function () {
  10. /**
  11. * 判断是否是 加减乘除 符号
  12. * @param value
  13. * @returns {boolean}
  14. */
  15. const isOperate1 = function (value) {
  16. const operatorString = "+-*/";
  17. return operatorString.indexOf(value) > -1;
  18. };
  19. // 删除规则计算参数按钮是否显示
  20. const refreshDelParamVisible = function () {
  21. if ($('#rule').children().length > 1) {
  22. $('#delParam').show();
  23. } else {
  24. $('#delParam').hide();
  25. }
  26. };
  27. // 合成新增参数的html
  28. const addParamHtml = function (html, param) {
  29. if (!param || param.length === 0) { return; }
  30. html.push('<span class="badge badge-light" title="' + param.text() + '"');
  31. html.push(' name="' + param.text() + '"');
  32. html.push(' code="' + param.attr('value') + '">');
  33. html.push(param.text());
  34. html.push('</span>');
  35. };
  36. // 弹窗设置指标计算规则
  37. $('a[name=setRule]').click(function () {
  38. const isOperator = function (value) {
  39. const operatorString = "+-*/()";
  40. return operatorString.indexOf(value) > -1;
  41. };
  42. const splitByOperator = function (expr) {
  43. const exprStack = [];
  44. const result = [];
  45. for(let i = 0, len = expr.length; i < len; i++){
  46. const cur = expr[i];
  47. if(cur !== ' ' ){
  48. exprStack.push(cur);
  49. }
  50. }
  51. let param = '', isParamBefore = false;
  52. while(exprStack.length > 0){
  53. const cur = exprStack.shift();
  54. if (isOperator(cur)) {
  55. if (isParamBefore) {
  56. result.push(param);
  57. param = '';
  58. }
  59. result.push(cur);
  60. isParamBefore = false;
  61. } else {
  62. param = param + cur;
  63. isParamBefore = true;
  64. if (exprStack.length === 0) {
  65. result.push(param);
  66. param = '';
  67. }
  68. }
  69. }
  70. return result;
  71. };
  72. const rule = $(this).attr('value');
  73. const index = this.parentNode.parentNode;
  74. $('#rule').attr('curIndex', index.attributes['index_id'].nodeValue);
  75. $('span', $('#rule')).remove();
  76. const html = [];
  77. const params = splitByOperator(rule);
  78. for (let i = 0, iLen = params.length; i < iLen; i++) {
  79. if (isOperator(params[i])) {
  80. html.push('<span class="badge badge-light" title="' + params[i] + '" code="' + params[i] + '" name="' + params[i] + '">' + params[i] + '</span>');
  81. } else {
  82. const p = $('option[value='+ params[i] +']:first');
  83. addParamHtml(html, p);
  84. }
  85. }
  86. $('#delParam').before(html.join(''));
  87. refreshDelParamVisible();
  88. $('#set-count').modal('show');
  89. });
  90. // 编辑计算规则,选择要添加的计算参数类型
  91. $('#paramType').change(function () {
  92. const param = this.selectedIndex === 0 ? 'globalParams' : (this.selectedIndex === 1 ? 'nodeParams' : 'calcParams');
  93. $('div[name=param]').hide();
  94. $('#' + param).show();
  95. });
  96. // 编辑计算规则,添加计算参数到规则
  97. $('#addParam').click(function () {
  98. const lastParam = $('#delParam').prev();
  99. const paramType = $('#paramType')[0];
  100. const addParam = function () {
  101. const paramId = paramType.selectedIndex === 0 ? 'globalParams' : (paramType.selectedIndex === 1 ? 'nodeParams' : 'calcParams');
  102. const paramSelect = $('select', $('#' + paramId))[0];
  103. if (paramSelect.children.length > 0 && paramSelect.selectedIndex >= 0) {
  104. const param = paramSelect.children[paramSelect.selectedIndex];
  105. const html = [];
  106. addParamHtml(html, $(param));
  107. $('#delParam').before(html.join(''));
  108. $('#paramAlert').hide();
  109. } else {
  110. $('#paramAlert').text('无选定参数').show();
  111. }
  112. };
  113. if (lastParam && lastParam.length > 0) {
  114. const calcSelect = $('select', $('#calcParams'))[0];
  115. if (isOperate1(lastParam.attr('code'))) {
  116. if (paramType.selectedIndex === 2) {
  117. if (calcSelect.selectedIndex !== 4) {
  118. $('#paramAlert').text('计算式后请输入参数').show();
  119. } else {
  120. addParam();
  121. }
  122. } else {
  123. addParam();
  124. }
  125. } else if (lastParam.attr('code') === '(') {
  126. if (paramType.selectedIndex === 2) {
  127. $('#paramAlert').text('左括号后请输入参数').show();
  128. } else {
  129. addParam();
  130. }
  131. } else if (lastParam.attr('code') === ')') {
  132. if (paramType.selectedIndex === 2) {
  133. if (calcSelect.selectedIndex < 4) {
  134. addParam();
  135. } else {
  136. $('#paramAlert').text('右括号后请输入计算式').show();
  137. }
  138. } else {
  139. $('#paramAlert').text('右括号后请输入计算式').show();
  140. }
  141. } else {
  142. if (paramType.selectedIndex === 2) {
  143. if (calcSelect.selectedIndex === 4) {
  144. $('#paramAlert').text('2个参数之间需要一个计算式').show();
  145. } else {
  146. addParam();
  147. }
  148. } else {
  149. $('#paramAlert').text('2个参数之间需要一个计算式').show();
  150. }
  151. }
  152. // if (!isOperate1(lastParam.attr('code')) && paramType.selectedIndex !== 2) {
  153. // $('#paramAlert').text('2个参数之间需要一个计算式').show();
  154. // } else if (isOperate1(lastParam.attr('code')) && paramType.selectedIndex === 2 ) {
  155. // const paramSelect = $('select', $('#calcParams'));
  156. // if (paramSelect.selectedIndex === 4) {
  157. // addParam();
  158. // } else if (paramSelect.selectedIndex === 5) {
  159. // const leftBracket = $('span[code=(]');
  160. // const rightBracket = $('span[code=)]');
  161. // if (leftBracket.length > rightBracket.length) {
  162. // addParam();
  163. // } else {
  164. // $('#paramAlert').text('添加")"前请先添加"("').show();
  165. // }
  166. // } else {
  167. // $('#paramAlert').text('计算式后只可添加参数').show();
  168. // }
  169. // } else {
  170. // addParam();
  171. // }
  172. } else if (paramType.selectedIndex === 2) {
  173. const paramSelect = $('select', $('#calcParams'));
  174. if (paramSelect.selectedIndex === 4) {
  175. addParam()
  176. } else {
  177. $('#paramAlert').text('计算式前需含有参数').show();
  178. }
  179. } else {
  180. addParam();
  181. }
  182. });
  183. // 编辑计算规则,删除规则中计算参数
  184. $('#delParam').click(function () {
  185. $('#delParam').prev().remove();
  186. refreshDelParamVisible();
  187. });
  188. // 设置指标计算规则
  189. $('#ruleOk').click(function () {
  190. const lastParam = $('#delParam').prev();
  191. const leftBracket = $("span[code='(']").length, rightBracket = $("span[code=')']").length;
  192. if (!lastParam) {
  193. $('#paramAlert').text('未设置计算规则').show();
  194. } else if (isOperate1(lastParam.attr('code'))) {
  195. $('#paramAlert').text('计算式后应添加参数').show();
  196. } else if (lastParam.attr('code') === '(' || leftBracket !== rightBracket) {
  197. $('#paramAlert').text('计算式错误,请检查').show();
  198. } else {
  199. const indexRow = $('tr[index_id=' + $('#rule').attr('curIndex') + ']');
  200. const params = $('span', $('#rule'));
  201. const data = {
  202. id: $('#rule').attr('curIndex'),
  203. rule: '',
  204. calc_rule: '',
  205. parse_rule: '',
  206. };
  207. for (const p of params) {
  208. const code = $(p).attr('code');
  209. const name = $(p).attr('name');
  210. data.rule = data.rule + name;
  211. data.calc_rule = data.calc_rule + code;
  212. data.parse_rule = code === name ? data.parse_rule + code : data.parse_rule + code + '(' + name + ')';
  213. }
  214. postData('/template/setIndexRule', data, function () {
  215. $('a', indexRow).val(data.calc_rule);
  216. $('td[name=rule]', indexRow).text(data.rule);
  217. const aHtml = $('a', indexRow)[0].outerHTML;
  218. $('td[name=parse_rule]', indexRow).empty();
  219. $('td[name=parse_rule]', indexRow).append(data.parse_rule + aHtml);
  220. $('paramAlert').hide();
  221. $('#set-count').modal('hide');
  222. }, function () {
  223. $('#paramAlert').text('提交计算规则失败,请重试').show();
  224. })
  225. }
  226. });
  227. // 指标节点,绑定分项节点
  228. $('#nodeMatchCode').blur(function () {
  229. const self = $(this);
  230. if (self.val() === self.attr('org-value')) { return; }
  231. const nodeId = GetUrlQueryString('id');
  232. postData('/template/updateNodeMatch', {
  233. id: nodeId ? nodeId : 1,
  234. match_key: $(this).val(),
  235. }, function (data) {
  236. self.attr('org-value', data.match_key);
  237. self.attr('title', data.match_type_str);
  238. self.attr('data-original-title', data.match_type_str);;
  239. }, function (data) {
  240. self.val(data.match_key);
  241. self.attr('org-value', data.match_key);
  242. self.attr('title', data.match_type_str);
  243. self.attr('data-original-title', data.match_type_str);;
  244. });
  245. });
  246. // 指标参数,绑定参数取值(分项编号)
  247. $('input[name=nodeParamMatchCode]').blur(function () {
  248. const self = $(this);
  249. if (self.val() === self.attr('org-value')) { return; }
  250. const nodeId = GetUrlQueryString('id');
  251. const paramCode = $(this).parent().parent().prev().attr('code');
  252. postData('/template/updateParamMatch', {
  253. node_id: nodeId ? nodeId : 1,
  254. code: paramCode,
  255. match_key: $(this).val(),
  256. }, function (data) {
  257. self.attr('org-value', data.match_key);
  258. }, function () {
  259. self.val(self.attr('org-value'));
  260. })
  261. });
  262. // 全局指标参数,绑定参数取值(分项编号)
  263. $('input[name=globalParamMatchCode]').blur(function () {
  264. const self = $(this);
  265. if (self.val() === self.attr('org-value')) { return; }
  266. const nodeId = GetUrlQueryString('id');
  267. const paramCode = $(this).parent().parent().prev().attr('code');
  268. postData('/template/updateParamMatch', {
  269. node_id: 0,
  270. code: paramCode,
  271. match_key: $(this).val(),
  272. }, function (data) {
  273. self.attr('org-value', data.match_key);
  274. }, function () {
  275. self.val(self.attr('org-value'));
  276. })
  277. });
  278. // 指标参数,选择取值类别
  279. $('a[name=nodeParamMatchNum]').click(function () {
  280. const self = $(this);
  281. const newMatchNum = self.attr('value');
  282. const oldMatchNum = self.parent().prev().val();
  283. if (newMatchNum === oldMatchNum) { return; }
  284. const nodeId = GetUrlQueryString('id');
  285. const paramCode = $(this).parent().parent().parent().parent().prev().attr('code');
  286. postData('/template/updateParamMatch', {
  287. node_id: nodeId ? nodeId : 1,
  288. code: paramCode,
  289. match_num: newMatchNum,
  290. }, function (data) {
  291. self.parent().prev().val(data.match_num);
  292. self.parent().prev().text(data.match_num_str);
  293. }, function (data) {
  294. self.parent().prev().val(data.match_num);
  295. self.parent().prev().text(data.match_num_str);
  296. })
  297. });
  298. // 全局指标参数,选择取值类别
  299. $('a[name=globalParamMatchNum]').click(function () {
  300. const self = $(this);
  301. const newMatchNum = self.attr('value');
  302. const oldMatchNum = self.parent().prev().val();
  303. if (newMatchNum === oldMatchNum) { return; }
  304. const paramCode = $(this).parent().parent().parent().parent().prev().attr('code');
  305. postData('/template/updateParamMatch', {
  306. node_id: 0,
  307. code: paramCode,
  308. match_num: newMatchNum,
  309. }, function (data) {
  310. self.parent().prev().val(data.match_num);
  311. self.parent().prev().text(data.match_num_str);
  312. }, function (data) {
  313. self.parent().prev().val(data.match_num);
  314. self.parent().prev().text(data.match_num_str);
  315. })
  316. });
  317. });