template.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/4/23
  7. * @version
  8. */
  9. $(document).ready(function () {
  10. const refreshDelParamVisible = function () {
  11. if ($('#rule').children().length > 1) {
  12. $('#delParam').show();
  13. } else {
  14. $('#delParam').hide();
  15. }
  16. };
  17. const addParamHtml = function (html, param) {
  18. html.push('<span class="badge badge-light" title="' + param.text() + '"');
  19. html.push(' name="' + param.text() + '"');
  20. html.push(' code="' + param.attr('value') + '">');
  21. html.push(param.text());
  22. html.push('</span>');
  23. };
  24. $('a[name=setRule]').click(function () {
  25. const rule = $(this).attr('value');
  26. const index = this.parentNode.parentNode;
  27. $('#rule').attr('curIndex', index.attributes['index_id'].nodeValue);
  28. $('span', $('#rule')).remove();
  29. const html = [];
  30. const params = rule.split('/');
  31. for (let i = 0, iLen = params.length; i < iLen; i++) {
  32. const p = $('option[value='+ params[i] +']');
  33. addParamHtml(html, p);
  34. if (i < iLen - 1) {
  35. html.push('<span class="badge badge-light" title="/" code="/" name="/">/ </span>');
  36. }
  37. }
  38. $('#delParam').before(html.join(''));
  39. refreshDelParamVisible();
  40. $('#set-count').modal('show');
  41. });
  42. $('#paramType').change(function () {
  43. const param = this.selectedIndex === 0 ? 'globalParams' : (this.selectedIndex === 1 ? 'nodeParams' : 'calcParams');
  44. $('div[name=param]').hide();
  45. $('#' + param).show();
  46. });
  47. $('#addParam').click(function () {
  48. const lastParam = $('#delParam').prev();
  49. const paramType = $('#paramType')[0];
  50. const addParam = function () {
  51. const paramId = paramType.selectedIndex === 0 ? 'globalParams' : (paramType.selectedIndex === 1 ? 'nodeParams' : 'calcParams');
  52. const paramSelect = $('select', $('#' + paramId))[0];
  53. if (paramSelect.children.length > 0 && paramSelect.selectedIndex >= 0) {
  54. const param = paramSelect.children[paramSelect.selectedIndex];
  55. const html = [];
  56. addParamHtml(html, $(param));
  57. $('#delParam').before(html.join(''));
  58. $('#paramAlert').hide();
  59. } else {
  60. $('#paramAlert').text('无选定参数').show();
  61. }
  62. }
  63. if (lastParam) {
  64. console.log(lastParam.attr('code'));
  65. if (lastParam.attr('code') !== '/' && paramType.selectedIndex !== 2) {
  66. $('#paramAlert').text('2个参数之间需要一个计算式').show();
  67. } else if (lastParam.attr('code') === '/' && paramType.selectedIndex === 2 ) {
  68. $('#paramAlert').text('计算式后只可添加参数').show();
  69. } else {
  70. addParam();
  71. }
  72. } else if (paramType.selectedIndex === 2) {
  73. $('#paramAlert').text('计算式前需含有参数').show();
  74. } else {
  75. addParam();
  76. }
  77. });
  78. $('#delParam').click(function () {
  79. $('#delParam').prev().remove();
  80. refreshDelParamVisible();
  81. });
  82. $('#ruleOk').click(function () {
  83. const lastParam = $('#delParam').prev();
  84. if (!lastParam) {
  85. $('#paramAlert').text('未设置计算规则').show();
  86. } else if (lastParam.attr('code') === lastParam.attr('name')) {
  87. $('#paramAlert').text('计算式后应添加参数').show();
  88. } else {
  89. const indexRow = $('tr[index_id=' + $('#rule').attr('curIndex') + ']');
  90. const params = $('span', $('#rule'));
  91. const data = {
  92. id: $('#rule').attr('curIndex'),
  93. rule: '',
  94. calc_rule: '',
  95. parse_rule: '',
  96. };
  97. for (const p of params) {
  98. const code = $(p).attr('code');
  99. const name = $(p).attr('name');
  100. data.rule = data.rule + name;
  101. data.calc_rule = data.calc_rule + code;
  102. data.parse_rule = code === name ? data.parse_rule + code : data.parse_rule + code + '(' + name + ')';
  103. }
  104. postData('/template/setIndexRule', data, function () {
  105. $('a', indexRow).val(data.calc_rule);
  106. $('td[name=rule]', indexRow).text(data.rule);
  107. const aHtml = $('a', indexRow)[0].outerHTML;
  108. $('td[name=parse_rule]', indexRow).empty();
  109. $('td[name=parse_rule]', indexRow).append(data.parse_rule + aHtml);
  110. $('paramAlert').hide();
  111. $('#set-count').modal('hide');
  112. }, function () {
  113. $('#paramAlert').text('提交计算规则失败,请重试').show();
  114. })
  115. }
  116. });
  117. });