// iVProg - www.usp.br/line/ivprog
// LInE - Free Education, Private Data
import * as Models from '../ivprog_elements';
import { LocalizedStrings } from '../../services/localizedStringsService';
import * as VariableValueMenu from './variable_value_menu';
import * as VariableValueMenuManagement from './variable_value_menu';
import * as CommandsManagement from '../commands';
import * as ExpressionManagement from './generic_expression';
import * as CodeGenerator from '../code_generator';
import { renderAlgorithm } from '../algorithm';
export function createFloatingCommand () {
return $('
x <— 1 + 1
');
}
export function renderCommand (command, function_obj) {
var el = $('
'+LocalizedStrings.getUI('text_receives')+' '
+ '
');
el.data('command', command);
VariableValueMenu.renderMenu(command, command.variable, el.find('.var_attributed'), function_obj);
if (!command.expression || command.expression.length < 1) {
var exp = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
command.expression.push(exp);
}
addHandlers(command, function_obj, el);
ExpressionManagement.renderExpression(command, function_obj, el.find('.expression_elements'), command.expression);
if (command.lockexpression) {
if (command.expression) {
try {
var text = CodeGenerator.elementExpressionCode(command.expression);
if (text) {
$(el.find('.expression_elements')[0]).toggle();
$(el.find('.textual_expression')[0]).text(text);
$(el.find('.textual_expression')[0]).toggle();
$(el.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
}
} catch (e) {
command.lockexpression = false;
}
}
}
el.find('.unlock').popup({
content : LocalizedStrings.getUI("text_lock_expression"),
delay: { show: 750, hide: 0 }
});
el.find('.lock').popup({
content : LocalizedStrings.getUI("text_unlock_expression"),
delay: { show: 750, hide: 0 }
});
el.find('.button_write_expression').popup({
content : LocalizedStrings.getUI("text_edit_expression"),
delay: { show: 750, hide: 0 }
});
return el;
}
export function manageExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, selectedItem, expression_element) {
var index_to_move = expression_element.itens.indexOf(ref_object);
switch (selectedItem.data('exp')) {
case Models.EXPRESSION_ELEMENTS.exp_op_exp:
var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp, [expression_element.itens[index_to_move],
Models.ARITHMETIC_TYPES.plus,
new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
expression_element.itens[index_to_move] = exp;
break;
case Models.EXPRESSION_ELEMENTS.op_exp:
var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.op_exp, [Models.ARITHMETIC_TYPES.plus,
expression_element.itens[index_to_move] ]);
expression_element.itens[index_to_move] = exp;
break;
case Models.EXPRESSION_ELEMENTS.par_exp_par:
var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.par_exp_par, [expression_element.itens[index_to_move]]);
expression_element.itens[index_to_move] = exp;
break;
}
renderExpressionElements(command, function_obj, dom_object);
}
function renderExpressionElements (command, function_obj, el) {
var expression_div = el.find('.expression_elements');
var command_container;
if (el.hasClass("command_container") == false) {
var hier = el.parentsUntil(".command_container");
for (var i = 0; i < hier.length; i++) {
if ($(hier[i]).hasClass("command_container")) {
command_container = $(hier[i]);
break;
}
if ($(hier[i]).hasClass("expression_elements")) {
expression_div = $(hier[i]);
break;
}
}
}
if (command_container) {
expression_div = command_container.find('.expression_elements');
}
expression_div.text('');
for (var i = 0; i < command.expression.length; i++) {
var temp = $('');
temp.data('ref_element', command.expression[i]);
temp.data('ref_index', i);
expression_div.append(temp);
renderElement(command, function_obj, temp, command.expression[i]);
}
}
function renderOperator (command, function_obj, temp_op, expression_element, index_op) {
var context_menu = '
';
switch (expression_element.itens[index_op]) {
case Models.ARITHMETIC_TYPES.plus:
context_menu += '+';
break;
case Models.ARITHMETIC_TYPES.minus:
context_menu += '-';
break;
case Models.ARITHMETIC_TYPES.multiplication:
context_menu += '*';
break;
case Models.ARITHMETIC_TYPES.division:
context_menu += '/';
break;
case Models.ARITHMETIC_TYPES.module:
context_menu += '%';
break;
case Models.ARITHMETIC_TYPES.none:
context_menu += '...';
break;
}
context_menu += '
';
context_menu += '
+
';
context_menu += '
-
';
context_menu += '
*
';
context_menu += '
/
';
context_menu += '
%
';
context_menu += '
Nenhum
';
context_menu += '
';
context_menu = $(context_menu);
temp_op.append(context_menu);
context_menu.dropdown({
onChange: function(value, text, $selectedItem) {
expression_element.itens[index_op] = value;
}
});
}
function renderMenuAddExpression (command, function_obj, el, dom_append_menu, expression_append_new_expression) {
if (el.hasClass("command_container") == false) {
var hier = el.parentsUntil(".commands_list_div");
for (var i = 0; i < hier.length; i++) {
if ($(hier[i]).hasClass("command_container")) {
el = $(hier[i]);
break;
}
}
}
if (dom_append_menu.hasClass("expression_elements") == false) {
var hier = el.parentsUntil(".commands_list_div");
for (var i = 0; i < hier.length; i++) {
if ($(hier[i]).hasClass("expression_elements")) {
dom_append_menu = $(hier[i]);
break;
}
}
}
var context_menu = '
';
context_menu += '
EXP OP EXP
';
context_menu += '
OP EXP
';
context_menu += '
( EXP )
';
context_menu += '
';
context_menu = $(context_menu);
dom_append_menu.append(context_menu);
context_menu.dropdown({
onChange: function(value, text, $selectedItem) {
switch (value) {
case Models.EXPRESSION_ELEMENTS.exp_op_exp:
var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
[new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true),
Models.ARITHMETIC_TYPES.plus,
new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
expression_append_new_expression.push(exp);
break;
case Models.EXPRESSION_ELEMENTS.op_exp:
var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.op_exp, [Models.ARITHMETIC_TYPES.plus,
new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
expression_append_new_expression.push(exp);
break;
case Models.EXPRESSION_ELEMENTS.par_exp_par:
var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.par_exp_par,
[new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
expression_append_new_expression.push(exp);
break;
}
renderExpressionElements(command, function_obj, el);
}
});
}
function renderElement (command, function_obj, el, expression_element) {
switch (expression_element.type_exp) {
case Models.EXPRESSION_ELEMENTS.exp_op_exp:
var temp_op = $('');
var temp_exp_1 = $('');
var temp_exp_2 = $('');
el.append(temp_exp_1);
el.append(temp_op);
el.append(temp_exp_2);
if (expression_element.itens[0].type) {
VariableValueMenu.renderMenu(command, expression_element.itens[0], temp_exp_1, function_obj, 2, expression_element);
} else {
renderElement(command, function_obj, temp_exp_1, expression_element.itens[0]);
}
renderOperator(command, function_obj, temp_op, expression_element, 1);
if (expression_element.itens[2].type) {
VariableValueMenu.renderMenu(command, expression_element.itens[2], temp_exp_2, function_obj, 2, expression_element);
} else {
renderElement(command, function_obj, temp_exp_2, expression_element.itens[2]);
}
break;
case Models.EXPRESSION_ELEMENTS.op_exp:
var temp_op = $('');
var temp_exp = $('');
el.append(temp_op);
el.append(temp_exp);
renderOperator(command, function_obj, temp_op, expression_element, 0);
if (expression_element.itens[1].type) {
VariableValueMenu.renderMenu(command, expression_element.itens[1], temp_exp, function_obj, 2, expression_element);
} else {
renderElement(command, function_obj, temp_exp, expression_element.itens[1]);
}
break;
case Models.EXPRESSION_ELEMENTS.par_exp_par:
var temp_par_1 = $('
(
');
var temp_exp = $('');
var temp_par_2 = $('
)
');
el.append(temp_par_1);
el.append(temp_exp);
for (var j = 0; j < expression_element.itens.length; j++) {
if (expression_element.itens[j].type) {
VariableValueMenu.renderMenu(command, expression_element.itens[j], temp_exp, function_obj, 2, expression_element);
} else {
renderElement(command, function_obj, temp_exp, expression_element.itens[j]);
}
}
//renderMenuAddExpression(command, function_obj, el, el, expression_element.itens);
el.append(temp_par_2);
break;
}
} // function renderElement(command, function_obj, el, expression_element)
function renderExpression (command, function_obj, el) {
//D console.log("src/ivprog/js/visualUI/commands/attribution.js: renderExpression(.): command="+JSON.stringify(command)+"\nfunction_obj=|"+JSON.stringify(function_obj)+"|\n");
var expression_div = el.find('.expression_elements');
expression_div.text('');
var menu_add_item = $('');
menu_add_item.data('index_add', 0);
expression_div.append(menu_add_item);
for (var i = 0; i < command.expression.length; i++) {
//D console.log(i + " : " + JSON.stringify(command.expression));
if (command.expression[i].type) {
var temp = $('');
temp.data('ref_element', command.expression[i]);
temp.data('ref_index', i);
expression_div.append(temp);
VariableValueMenu.renderMenu(command, command.expression[i], temp, function_obj);
} else if (command.expression[i] == "(" || command.expression[i] == ")") {
var temp = $('