let creatingVariable = false; let variables = []; /* * Workspace position modal attributes */ let $workspacePositionModal = $("#workspacePositionModal"); let $workspacePositionModalBody = $("#workspacePositionModalBody"); let $workspacePositionModalPositionsSelect = $( "#workspacePositionModalPositionsSelect" ); let workspaceToBeAddId = null; let workspaceToBeAddVariable = null; let workspaceToBeAddAfterComponent = null; function addVariable( variable = {name: "", type: dataTypes.String, value: ""}, id = null ) { subMenuOptions.style.display = "none"; if (creatingVariable === false) { // Blocking future variable creation until finish the current creation creatingVariable = true; let html = ""; html += `
`; html += `
`; html += `
`; html += `

Adicionar variável

`; html += `
`; html += `
`; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; $workspace.append(html); } } function addVariableToCode({id = null}) { let code = ""; let variable = { name: $("#variableName").val(), type: $("#variableType").val(), value: $("#variableValue").val(), }; code = mountCodeFromVariable(variable); // Sending code to screen addCodeToScreen({id: id, code: code, variable: variable}); // Removing div form for variable creation $("#variableCreationDiv").remove(); // Enabling future variable creation creatingVariable = false; } function cancelAddVariable(id = null) { // Removing div form for variable creation $("#variableCreationDiv").remove(); if (id != null) $(`#variableDiv${id}`).show(); // Enabling future variable creation creatingVariable = false; } function editVariable( id = null, variable = {name: "", type: dataTypes.String, value: ""} ) { $(`#variableDiv${id}`).hide(); addVariable(variable, id); } function deleteVariable(variableIndex = null, subMenuOptionId) { trashSound.play(); document.getElementById(subMenuOptionId).remove(); for (let i = 0; i < variables[variableIndex].attributions.length; i++) { variables[variableIndex].attributions[i].parentElement.parentElement.remove(); } for (let i = 0; i < variables[variableIndex].usages.length; i++) { variables[variableIndex].usages[i].remove(); } variables.splice(variableIndex, 1); // Refresh variables sub menu buildVariablesSubmenu(); } function addCodeToScreen({ id = null, code = "Some code", variable = {name: "", type: "String", value: ""}, }) { let html = ``; if (id != null) { $(`#mainCode${id}`).html(`${code}`); $(`#globalMenu${id}`).val(code); $(`#positionCode${id}`).html( `` ); $(`#editCode${id}`).html( `` ); $(`#variableDiv${id}`).show(); updateVariable({ id: id, name: variable.name, type: variable.type, value: variable.value, code: code, element: document.getElementById(`variableDiv${id}`), }); } else { id = universalId.next(); html += `
`; html += ` `; html += `
`; html += `
`; html += `

Variável

`; html += `
`; html += `
`; html += `
`; html += `
${getCurrentMainLineNumber()}
`; html += `
${code}
`; html += `
`; html += `
`; html += ` `; html += `
`; html += `
`; // Saving reference to variable in variables array variables.push({ id: id, name: variable.name, type: variable.type, value: variable.value, code: code, element: document.getElementById(`variableDiv${universalId.next()}`), attributions: [], usages: [] }); } } function askWhereToPlaceTheVariable( id = null, variable = {name: "", type: "String", value: ""} ) { if (buildAllWorkspaceComponentsSelect()) { $workspacePositionModal.modal("toggle"); $workspacePositionModalPositionsSelect.focus(); workspaceToBeAddId = id; workspaceToBeAddVariable = variable; } else { addVariableToMain({id: id, variable: variable}); } } function buildAllWorkspaceComponentsSelect() { let allWorkspaceComponents = $( `input[type="hidden"][name="${WORKSPACE_COMPONENTS_NAME}"]` ); let selectOptions = []; if (allWorkspaceComponents.length > 0) { for (let i = 0; i < allWorkspaceComponents.length; i++) { selectOptions.push( `` ); } $workspacePositionModalPositionsSelect.empty(); // remove old options selectOptions.forEach((object, index, array) => { $workspacePositionModalPositionsSelect.append($(object)); }); return true; } else { return false; } } $("#workspacePositionModalAddButton").click(() => { addVariableToMain({ id: workspaceToBeAddId, variable: workspaceToBeAddVariable, afterComponentWithId: $workspacePositionModalPositionsSelect.val(), }); // Reset variables for future use workspaceToBeAddId = null; workspaceToBeAddVariable = null; // Hide position modal $workspacePositionModal.modal("toggle"); }); function updateVariable( variable = { id: 0, name: "Name", type: "String", value: "Value", code: 'String Name = "value"', element: document.getElementById("body"), } ) { for (let i = 0; i < variables.length; i++) { if (variables[i].id === variable.id) { variables[i] = variable; return; } } console.warn( `Can't find variable with id: ${variable.id} to update, please verify the variable universalId` ); }