|
@@ -9,6 +9,15 @@
|
|
|
*/
|
|
|
|
|
|
$(document).ready(function () {
|
|
|
+ /**
|
|
|
+ * 判断是否是 加减乘除 符号
|
|
|
+ * @param value
|
|
|
+ * @returns {boolean}
|
|
|
+ */
|
|
|
+ const isOperate1 = function (value) {
|
|
|
+ const operatorString = "+-*/";
|
|
|
+ return operatorString.indexOf(value) > -1;
|
|
|
+ };
|
|
|
// 删除规则计算参数按钮是否显示
|
|
|
const refreshDelParamVisible = function () {
|
|
|
if ($('#rule').children().length > 1) {
|
|
@@ -19,6 +28,7 @@ $(document).ready(function () {
|
|
|
};
|
|
|
// 合成新增参数的html
|
|
|
const addParamHtml = function (html, param) {
|
|
|
+ if (!param || param.length === 0) { return; }
|
|
|
html.push('<span class="badge badge-light" title="' + param.text() + '"');
|
|
|
html.push(' name="' + param.text() + '"');
|
|
|
html.push(' code="' + param.attr('value') + '">');
|
|
@@ -27,17 +37,52 @@ $(document).ready(function () {
|
|
|
};
|
|
|
// 弹窗设置指标计算规则
|
|
|
$('a[name=setRule]').click(function () {
|
|
|
+ const isOperator = function (value) {
|
|
|
+ const operatorString = "+-*/()";
|
|
|
+ return operatorString.indexOf(value) > -1;
|
|
|
+ };
|
|
|
+ const splitByOperator = function (expr) {
|
|
|
+ const exprStack = [];
|
|
|
+ const result = [];
|
|
|
+ for(let i = 0, len = expr.length; i < len; i++){
|
|
|
+ const cur = expr[i];
|
|
|
+ if(cur !== ' ' ){
|
|
|
+ exprStack.push(cur);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let param = '', isParamBefore = false;
|
|
|
+ while(exprStack.length > 0){
|
|
|
+ const cur = exprStack.shift();
|
|
|
+ if (isOperator(cur)) {
|
|
|
+ if (isParamBefore) {
|
|
|
+ result.push(param);
|
|
|
+ param = '';
|
|
|
+ }
|
|
|
+ result.push(cur);
|
|
|
+ isParamBefore = false;
|
|
|
+ } else {
|
|
|
+ param = param + cur;
|
|
|
+ isParamBefore = true;
|
|
|
+ if (exprStack.length === 0) {
|
|
|
+ result.push(param);
|
|
|
+ param = '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ };
|
|
|
const rule = $(this).attr('value');
|
|
|
const index = this.parentNode.parentNode;
|
|
|
$('#rule').attr('curIndex', index.attributes['index_id'].nodeValue);
|
|
|
$('span', $('#rule')).remove();
|
|
|
const html = [];
|
|
|
- const params = rule.split('/');
|
|
|
+ const params = splitByOperator(rule);
|
|
|
for (let i = 0, iLen = params.length; i < iLen; i++) {
|
|
|
- const p = $('option[value='+ params[i] +']');
|
|
|
- addParamHtml(html, p);
|
|
|
- if (i < iLen - 1) {
|
|
|
- html.push('<span class="badge badge-light" title="/" code="/" name="/">/ </span>');
|
|
|
+ if (isOperator(params[i])) {
|
|
|
+ html.push('<span class="badge badge-light" title="' + params[i] + '" code="' + params[i] + '" name="' + params[i] + '">' + params[i] + '</span>');
|
|
|
+ } else {
|
|
|
+ const p = $('option[value='+ params[i] +']:first');
|
|
|
+ addParamHtml(html, p);
|
|
|
}
|
|
|
}
|
|
|
$('#delParam').before(html.join(''));
|
|
@@ -66,17 +111,73 @@ $(document).ready(function () {
|
|
|
} else {
|
|
|
$('#paramAlert').text('无选定参数').show();
|
|
|
}
|
|
|
- }
|
|
|
- if (lastParam) {
|
|
|
- if (lastParam.attr('code') !== '/' && paramType.selectedIndex !== 2) {
|
|
|
- $('#paramAlert').text('2个参数之间需要一个计算式').show();
|
|
|
- } else if (lastParam.attr('code') === '/' && paramType.selectedIndex === 2 ) {
|
|
|
- $('#paramAlert').text('计算式后只可添加参数').show();
|
|
|
+ };
|
|
|
+ if (lastParam && lastParam.length > 0) {
|
|
|
+ const calcSelect = $('select', $('#calcParams'))[0];
|
|
|
+ if (isOperate1(lastParam.attr('code'))) {
|
|
|
+ if (paramType.selectedIndex === 2) {
|
|
|
+ if (calcSelect.selectedIndex !== 4) {
|
|
|
+ $('#paramAlert').text('计算式后请输入参数').show();
|
|
|
+ } else {
|
|
|
+ addParam();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ addParam();
|
|
|
+ }
|
|
|
+ } else if (lastParam.attr('code') === '(') {
|
|
|
+ if (paramType.selectedIndex === 2) {
|
|
|
+ $('#paramAlert').text('左括号后请输入参数').show();
|
|
|
+ } else {
|
|
|
+ addParam();
|
|
|
+ }
|
|
|
+ } else if (lastParam.attr('code') === ')') {
|
|
|
+ if (paramType.selectedIndex === 2) {
|
|
|
+ if (calcSelect.selectedIndex < 4) {
|
|
|
+ addParam();
|
|
|
+ } else {
|
|
|
+ $('#paramAlert').text('右括号后请输入计算式').show();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $('#paramAlert').text('右括号后请输入计算式').show();
|
|
|
+ }
|
|
|
} else {
|
|
|
- addParam();
|
|
|
+ if (paramType.selectedIndex === 2) {
|
|
|
+ if (calcSelect.selectedIndex === 4) {
|
|
|
+ $('#paramAlert').text('2个参数之间需要一个计算式').show();
|
|
|
+ } else {
|
|
|
+ addParam();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $('#paramAlert').text('2个参数之间需要一个计算式').show();
|
|
|
+ }
|
|
|
}
|
|
|
+ // if (!isOperate1(lastParam.attr('code')) && paramType.selectedIndex !== 2) {
|
|
|
+ // $('#paramAlert').text('2个参数之间需要一个计算式').show();
|
|
|
+ // } else if (isOperate1(lastParam.attr('code')) && paramType.selectedIndex === 2 ) {
|
|
|
+ // const paramSelect = $('select', $('#calcParams'));
|
|
|
+ // if (paramSelect.selectedIndex === 4) {
|
|
|
+ // addParam();
|
|
|
+ // } else if (paramSelect.selectedIndex === 5) {
|
|
|
+ // const leftBracket = $('span[code=(]');
|
|
|
+ // const rightBracket = $('span[code=)]');
|
|
|
+ // if (leftBracket.length > rightBracket.length) {
|
|
|
+ // addParam();
|
|
|
+ // } else {
|
|
|
+ // $('#paramAlert').text('添加")"前请先添加"("').show();
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // $('#paramAlert').text('计算式后只可添加参数').show();
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // addParam();
|
|
|
+ // }
|
|
|
} else if (paramType.selectedIndex === 2) {
|
|
|
- $('#paramAlert').text('计算式前需含有参数').show();
|
|
|
+ const paramSelect = $('select', $('#calcParams'));
|
|
|
+ if (paramSelect.selectedIndex === 4) {
|
|
|
+ addParam()
|
|
|
+ } else {
|
|
|
+ $('#paramAlert').text('计算式前需含有参数').show();
|
|
|
+ }
|
|
|
} else {
|
|
|
addParam();
|
|
|
}
|
|
@@ -89,10 +190,13 @@ $(document).ready(function () {
|
|
|
// 设置指标计算规则
|
|
|
$('#ruleOk').click(function () {
|
|
|
const lastParam = $('#delParam').prev();
|
|
|
+ const leftBracket = $("span[code='(']").length, rightBracket = $("span[code=')']").length;
|
|
|
if (!lastParam) {
|
|
|
$('#paramAlert').text('未设置计算规则').show();
|
|
|
- } else if (lastParam.attr('code') === lastParam.attr('name')) {
|
|
|
+ } else if (isOperate1(lastParam.attr('code'))) {
|
|
|
$('#paramAlert').text('计算式后应添加参数').show();
|
|
|
+ } else if (lastParam.attr('code') === '(' || leftBracket !== rightBracket) {
|
|
|
+ $('#paramAlert').text('计算式错误,请检查').show();
|
|
|
} else {
|
|
|
const indexRow = $('tr[index_id=' + $('#rule').attr('curIndex') + ']');
|
|
|
const params = $('span', $('#rule'));
|