template.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/4/23
  7. * @version
  8. */
  9. $(document).ready(function () {
  10. // 删除规则计算参数按钮是否显示
  11. const refreshDelParamVisible = function () {
  12. if ($('#rule').children().length > 1) {
  13. $('#delParam').show();
  14. } else {
  15. $('#delParam').hide();
  16. }
  17. };
  18. // 合成新增参数的html
  19. const addParamHtml = function (html, param) {
  20. html.push('<span class="badge badge-light" title="' + param.text() + '"');
  21. html.push(' name="' + param.text() + '"');
  22. html.push(' code="' + param.attr('value') + '">');
  23. html.push(param.text());
  24. html.push('</span>');
  25. };
  26. // 弹窗设置指标计算规则
  27. $('a[name=setRule]').click(function () {
  28. const rule = $(this).attr('value');
  29. const index = this.parentNode.parentNode;
  30. $('#rule').attr('curIndex', index.attributes['index_id'].nodeValue);
  31. $('span', $('#rule')).remove();
  32. const html = [];
  33. const params = rule.split('/');
  34. for (let i = 0, iLen = params.length; i < iLen; i++) {
  35. const p = $('option[value='+ params[i] +']');
  36. addParamHtml(html, p);
  37. if (i < iLen - 1) {
  38. html.push('<span class="badge badge-light" title="/" code="/" name="/">/ </span>');
  39. }
  40. }
  41. $('#delParam').before(html.join(''));
  42. refreshDelParamVisible();
  43. $('#set-count').modal('show');
  44. });
  45. // 编辑计算规则,选择要添加的计算参数类型
  46. $('#paramType').change(function () {
  47. const param = this.selectedIndex === 0 ? 'globalParams' : (this.selectedIndex === 1 ? 'nodeParams' : 'calcParams');
  48. $('div[name=param]').hide();
  49. $('#' + param).show();
  50. });
  51. // 编辑计算规则,添加计算参数到规则
  52. $('#addParam').click(function () {
  53. const lastParam = $('#delParam').prev();
  54. const paramType = $('#paramType')[0];
  55. const addParam = function () {
  56. const paramId = paramType.selectedIndex === 0 ? 'globalParams' : (paramType.selectedIndex === 1 ? 'nodeParams' : 'calcParams');
  57. const paramSelect = $('select', $('#' + paramId))[0];
  58. if (paramSelect.children.length > 0 && paramSelect.selectedIndex >= 0) {
  59. const param = paramSelect.children[paramSelect.selectedIndex];
  60. const html = [];
  61. addParamHtml(html, $(param));
  62. $('#delParam').before(html.join(''));
  63. $('#paramAlert').hide();
  64. } else {
  65. $('#paramAlert').text('无选定参数').show();
  66. }
  67. }
  68. if (lastParam) {
  69. if (lastParam.attr('code') !== '/' && paramType.selectedIndex !== 2) {
  70. $('#paramAlert').text('2个参数之间需要一个计算式').show();
  71. } else if (lastParam.attr('code') === '/' && paramType.selectedIndex === 2 ) {
  72. $('#paramAlert').text('计算式后只可添加参数').show();
  73. } else {
  74. addParam();
  75. }
  76. } else if (paramType.selectedIndex === 2) {
  77. $('#paramAlert').text('计算式前需含有参数').show();
  78. } else {
  79. addParam();
  80. }
  81. });
  82. // 编辑计算规则,删除规则中计算参数
  83. $('#delParam').click(function () {
  84. $('#delParam').prev().remove();
  85. refreshDelParamVisible();
  86. });
  87. // 设置指标计算规则
  88. $('#ruleOk').click(function () {
  89. const lastParam = $('#delParam').prev();
  90. if (!lastParam) {
  91. $('#paramAlert').text('未设置计算规则').show();
  92. } else if (lastParam.attr('code') === lastParam.attr('name')) {
  93. $('#paramAlert').text('计算式后应添加参数').show();
  94. } else {
  95. const indexRow = $('tr[index_id=' + $('#rule').attr('curIndex') + ']');
  96. const params = $('span', $('#rule'));
  97. const data = {
  98. id: $('#rule').attr('curIndex'),
  99. rule: '',
  100. calc_rule: '',
  101. parse_rule: '',
  102. };
  103. for (const p of params) {
  104. const code = $(p).attr('code');
  105. const name = $(p).attr('name');
  106. data.rule = data.rule + name;
  107. data.calc_rule = data.calc_rule + code;
  108. data.parse_rule = code === name ? data.parse_rule + code : data.parse_rule + code + '(' + name + ')';
  109. }
  110. postData('/template/setIndexRule', data, function () {
  111. $('a', indexRow).val(data.calc_rule);
  112. $('td[name=rule]', indexRow).text(data.rule);
  113. const aHtml = $('a', indexRow)[0].outerHTML;
  114. $('td[name=parse_rule]', indexRow).empty();
  115. $('td[name=parse_rule]', indexRow).append(data.parse_rule + aHtml);
  116. $('paramAlert').hide();
  117. $('#set-count').modal('hide');
  118. }, function () {
  119. $('#paramAlert').text('提交计算规则失败,请重试').show();
  120. })
  121. }
  122. });
  123. // 指标节点,绑定分项节点
  124. $('#nodeMatchCode').blur(function () {
  125. const self = $(this);
  126. if (self.val() === self.attr('org-value')) { return; }
  127. const nodeId = GetUrlQueryString('id');
  128. postData('/template/updateNodeMatch', {
  129. id: nodeId ? nodeId : 1,
  130. match_key: $(this).val(),
  131. }, function (data) {
  132. self.attr('org-value', data.match_key);
  133. self.attr('title', data.match_type_str);
  134. self.attr('data-original-title', data.match_type_str);;
  135. }, function (data) {
  136. self.val(data.match_key);
  137. self.attr('org-value', data.match_key);
  138. self.attr('title', data.match_type_str);
  139. self.attr('data-original-title', data.match_type_str);;
  140. });
  141. });
  142. // 指标参数,绑定参数取值(分项编号)
  143. $('input[name=paramMatchCode]').blur(function () {
  144. const self = $(this);
  145. if (self.val() === self.attr('org-value')) { return; }
  146. const nodeId = GetUrlQueryString('id');
  147. const paramCode = $(this).parent().parent().prev().attr('code');
  148. postData('/template/updateParamMatch', {
  149. node_id: nodeId ? nodeId : 1,
  150. code: paramCode,
  151. match_key: $(this).val(),
  152. }, function (data) {
  153. self.attr('org-value', data.match_key);
  154. }, function (data) {
  155. self.val(data.match_key);
  156. self.attr('org-value', data.match_key);
  157. })
  158. });
  159. // 指标参数,选择取值类别
  160. $('a[name=paramMatchNum]').click(function () {
  161. const self = $(this);
  162. const newMatchNum = self.attr('value');
  163. const oldMatchNum = self.parent().prev().val();
  164. if (newMatchNum === oldMatchNum) { return; }
  165. const nodeId = GetUrlQueryString('id');
  166. const paramCode = $(this).parent().parent().parent().parent().prev().attr('code');
  167. postData('/template/updateParamMatch', {
  168. node_id: nodeId ? nodeId : 1,
  169. code: paramCode,
  170. match_num: newMatchNum,
  171. }, function (data) {
  172. self.parent().prev().val(data.match_num);
  173. self.parent().prev().text(data.match_num_str);
  174. }, function (data) {
  175. self.parent().prev().val(data.match_num);
  176. self.parent().prev().text(data.match_num_str);
  177. })
  178. });
  179. });