Browse Source

feat: added some logic to avoid some repeat of code

Marcelo Vilas Boas Correa Filho 2 years ago
parent
commit
16c538a070
1 changed files with 25 additions and 1 deletions
  1. 25 1
      js/accessibleUI/modules/operations/operations.js

+ 25 - 1
js/accessibleUI/modules/operations/operations.js

@@ -175,8 +175,32 @@ export function updateOperationOperator (operationHash, operatorHash, newValue)
             break;
         case 'OPERATOR':
             operator.operator = getOperationTypeByValue(newValue);
-            if (operator.operator.name !== ';' && (operation.operators.indexOf(operator) === (operation.operators.length - 1)))
+            const operationTypeSelect = document.getElementById(`operation${operation.hash}Operator${operator.hash}OperationTypeSelect`);
+
+            // If the operator isn't a semicolumn, it is the last item on the operator list and teh next elemtn is equal to null, 
+            // we need to add a new operator select
+            if (operator.operator.name !== ';' 
+                && (operation.operators.indexOf(operator) === (operation.operators.length - 1))
+                && operationTypeSelect.parentElement.nextElementSibling == null)
                 addOperatorKind(operation.hash);
+            else if (operator.operator.name === ';' 
+                && (operation.operators.indexOf(operator) === (operation.operators.length - 1))
+                && operationTypeSelect.parentElement.nextElementSibling != null)
+                operationTypeSelect.parentElement.nextElementSibling.remove();
+            else if (operator.operator.name === ';' 
+                && (operation.operators.indexOf(operator) !== (operation.operators.length - 1))
+                && operationTypeSelect.parentElement.nextElementSibling != null) {
+                    let indexOfOperator = operation.operators.indexOf(operator);
+                    operation.operators.splice(indexOfOperator);
+
+                    let elementSibling = operationTypeSelect.parentElement.nextElementSibling;
+                    let nextElementSibling = operationTypeSelect.parentElement.nextElementSibling;
+                    while(nextElementSibling != null) {
+                        nextElementSibling = elementSibling.nextElementSibling;
+                        elementSibling.remove();
+                        elementSibling = nextElementSibling;
+                    }
+                }
             break;
     }
 }