#4 -- Fix js for conventions
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m0s

This commit is contained in:
Anton Romanov 2025-02-28 16:14:28 +04:00
parent ae309290ff
commit 5e5f9f35c8

View File

@ -1,12 +1,10 @@
// Rules parsing // Rules parsing
// antecedent // antecedent
function getAntecedent(rule) { function getAntecedent(rule) {
var withoutIf = rule.split('if'); let withoutIf = rule.split('if');
return withoutIf[1].trim().split('then')[0].trim(); return withoutIf[1].trim().split('then')[0].trim();
} }
// TODO: remove duplicate // TODO: remove duplicate
function getAntecedentComponents(antecedent) { function getAntecedentComponents(antecedent) {
return antecedent.split('and').map((i) => i.trim()); return antecedent.split('and').map((i) => i.trim());
@ -14,7 +12,7 @@ function getAntecedentComponents(antecedent) {
// consequent // consequent
function getConsequent(rule) { function getConsequent(rule) {
var withoutIf = rule.split('if'); let withoutIf = rule.split('if');
return withoutIf[1].trim().split('then')[1].trim(); return withoutIf[1].trim().split('then')[1].trim();
} }
@ -35,7 +33,7 @@ function getVariableValue(variableComponents) {
// Rules creation // Rules creation
/* exported MessageTypesEnum */ /* exported MessageTypesEnum */
var MessageTypesEnum = { let MessageTypesEnum = {
INFO: "info", INFO: "info",
SUCCESS: "success", SUCCESS: "success",
WARNING: "warning", WARNING: "warning",
@ -44,7 +42,7 @@ var MessageTypesEnum = {
Object.freeze(MessageTypesEnum); Object.freeze(MessageTypesEnum);
function isEmpty(value) { function isEmpty(value) {
if (typeof value === "function") { if (typeof value === "function") {
return false; return false;
} }
return (value == null || value.length === 0); return (value == null || value.length === 0);
@ -54,6 +52,7 @@ function isEmpty(value) {
function showFeedbackMessage(message, type) { function showFeedbackMessage(message, type) {
alert(type + ' ' + message); alert(type + ' ' + message);
} }
/* exported errorHandler */ /* exported errorHandler */
function errorHandler(response, callBack, errorCallBack) { function errorHandler(response, callBack, errorCallBack) {
if (!isEmpty(response.error)) { if (!isEmpty(response.error)) {
@ -82,11 +81,15 @@ function getFromRest(url, callBack) {
} }
/* exported createRule */ /* exported createRule */
function createRule() { function createRule() {
var ruleString = "if "; let ruleString = "if ";
var inp = $('.selectpicker.inputVar').children(':selected').map(function() {return $(this).text();}).get(); let inp = $('.selectpicker.inputVar').children(':selected').map(function () {
var inpVal = $('.selectpicker.inputVal').children(':selected').map(function() {return $(this).text();}).get(); return $(this).text();
for (var i = 0; i < inp.length; i++) { }).get();
let inpVal = $('.selectpicker.inputVal').children(':selected').map(function () {
return $(this).text();
}).get();
for (let i = 0; i < inp.length; i++) {
if (i > 0) { if (i > 0) {
ruleString += ' and '; ruleString += ' and ';
} }
@ -101,7 +104,7 @@ function fillSelect(selectElement, values, selectedVal) {
$.each(values, function (key, value) { $.each(values, function (key, value) {
$(selectElement).append($("<option />").val(value.id).text(value.name)); $(selectElement).append($("<option />").val(value.id).text(value.name));
}); });
$(selectElement).children().each(function() { $(selectElement).children().each(function () {
if ($(this).text() == selectedVal) { if ($(this).text() == selectedVal) {
$(this).prop('selected', true); $(this).prop('selected', true);
} }
@ -109,33 +112,33 @@ function fillSelect(selectElement, values, selectedVal) {
} }
function fillFuzzyTerms(variablesElement, fuzzyTermsElement, termVal) { function fillFuzzyTerms(variablesElement, fuzzyTermsElement, termVal) {
getFromRest("/rule/getFuzzyTerms/"+$(variablesElement).val(), function (fuzzyTerms) { getFromRest("/rule/getFuzzyTerms/" + $(variablesElement).val(), function (fuzzyTerms) {
var fuzzyTermsData = []; let fuzzyTermsData = [];
$.each(fuzzyTerms, function (key, value) { $.each(fuzzyTerms, function (key, value) {
fuzzyTermsData.push({ fuzzyTermsData.push({
id: value.id, id: value.id,
name: value.description name: value.description
}); });
}); });
fillSelect(fuzzyTermsElement, fuzzyTermsData, termVal); fillSelect(fuzzyTermsElement, fuzzyTermsData, termVal);
$(fuzzyTermsElement).selectpicker("refresh"); $(fuzzyTermsElement).selectpicker("refresh");
$(fuzzyTermsElement).trigger("change"); $(fuzzyTermsElement).trigger("change");
}); });
} }
function fillVariables(projectId, variablesElement, variableVal) { function fillVariables(projectId, variablesElement, variableVal) {
getFromRest("/rule/getVariables/"+projectId, function (variables) { getFromRest("/rule/getVariables/" + projectId, function (variables) {
var variablesData = []; let variablesData = [];
$.each(variables, function (key, value) { $.each(variables, function (key, value) {
variablesData.push({ variablesData.push({
id: value.id, id: value.id,
name: value.name name: value.name
}); });
}); });
fillSelect(variablesElement, variablesData, variableVal); fillSelect(variablesElement, variablesData, variableVal);
$(variablesElement).selectpicker("refresh"); $(variablesElement).selectpicker("refresh");
$(variablesElement).trigger("change"); $(variablesElement).trigger("change");
}); });
} }
function variableValueChanged(variablesElement, fuzzyTermsElement) { function variableValueChanged(variablesElement, fuzzyTermsElement) {
@ -147,34 +150,39 @@ function fuzzyTermsValueChanged() {
} }
function addVariableSelect(projectId, variableVal) { function addVariableSelect(projectId, variableVal) {
var variablesElement = $("<select class='selectpicker inputVar m-2' data-live-search='true data-width='70%'></select>"); let variablesElement = $("<select class='selectpicker inputVar m-2' data-live-search='true data-width='70%'></select>");
fillVariables(projectId, variablesElement, variableVal); fillVariables(projectId, variablesElement, variableVal);
return variablesElement; return variablesElement;
} }
function addFuzzyTermsSelect(variablesElement, termVal) { function addFuzzyTermsSelect(variablesElement, termVal) {
var fuzzyTermsElement = $("<select class='selectpicker inputVal m-2' data-live-search='true data-width='70%'></select>"); let fuzzyTermsElement = $("<select class='selectpicker inputVal m-2' data-live-search='true data-width='70%'></select>");
if ($(variablesElement).val()) { if ($(variablesElement).val()) {
fillFuzzyTerms(variablesElement, fuzzyTermsElement, termVal); fillFuzzyTerms(variablesElement, fuzzyTermsElement, termVal);
} }
return fuzzyTermsElement; return fuzzyTermsElement;
} }
function removeAntecedent(buttonElement) { function removeAntecedent(buttonElement) {
$(buttonElement).parent().remove(); $(buttonElement).parent().remove();
fuzzyTermsValueChanged(); fuzzyTermsValueChanged();
} }
function addAntecedent(parentElement, projectId, variableVal, termVal) { function addAntecedent(parentElement, projectId, variableVal, termVal) {
var rowElement = $("<div class='row'></div>"); let rowElement = $("<div class='row'></div>");
if ($(parentElement).find('.row').length) { if ($(parentElement).find('.row').length) {
$(rowElement).append("<label class='col col-md-1 m-2'>И</label>"); $(rowElement).append("<label class='col col-md-1 m-2'>И</label>");
} else { } else {
$(rowElement).append("<label class='col col-md-1 m-2'> </label>"); $(rowElement).append("<label class='col col-md-1 m-2'> </label>");
} }
var variablesElement = addVariableSelect(projectId, variableVal); let variablesElement = addVariableSelect(projectId, variableVal);
var fuzzyTermsElement = addFuzzyTermsSelect(variablesElement, termVal); let fuzzyTermsElement = addFuzzyTermsSelect(variablesElement, termVal);
$(variablesElement).on( "change", function() {variableValueChanged(variablesElement, fuzzyTermsElement)}); $(variablesElement).on("change", function () {
$(fuzzyTermsElement).on( "change", function() {fuzzyTermsValueChanged()}); variableValueChanged(variablesElement, fuzzyTermsElement)
});
$(fuzzyTermsElement).on("change", function () {
fuzzyTermsValueChanged()
});
$(rowElement).append(variablesElement); $(rowElement).append(variablesElement);
$(rowElement).append("<label class='col col-md-1 m-2'>есть</label>"); $(rowElement).append("<label class='col col-md-1 m-2'>есть</label>");
$(rowElement).append(fuzzyTermsElement); $(rowElement).append(fuzzyTermsElement);
@ -185,9 +193,9 @@ function addAntecedent(parentElement, projectId, variableVal, termVal) {
} }
function addAntecedentFromRule(parentElement, projectId, ruleContent) { function addAntecedentFromRule(parentElement, projectId, ruleContent) {
var antecedentComponents = getAntecedentComponents(getAntecedent(ruleContent)); let antecedentComponents = getAntecedentComponents(getAntecedent(ruleContent));
for (let i = 0; i < antecedentComponents.length; i++) { for (let i = 0; i < antecedentComponents.length; i++) {
var a = antecedentComponents[i]; var a = antecedentComponents[i];
addAntecedent(parentElement, projectId, getVariable(a), getVariableValue(a)); addAntecedent(parentElement, projectId, getVariable(a), getVariableValue(a));
} }
} }