1
0

attribution.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import * as Models from '../ivprog_elements';
  2. import { LocalizedStrings } from '../../services/localizedStringsService';
  3. import * as VariableValueMenu from './variable_value_menu';
  4. import * as VariableValueMenuManagement from './variable_value_menu';
  5. import * as CommandsManagement from '../commands';
  6. import * as ExpressionManagement from './generic_expression';
  7. export function createFloatingCommand () {
  8. return $('<div class="ui attribution created_element"> <i class="ui icon small arrow left"></i> <span> x <&#8212; 1 + 1 </span></div>');
  9. }
  10. export function renderCommand (command, function_obj) {
  11. var el = $('<div class="ui attribution command_container"><i class="ui icon small arrow left command_drag"></i> <i class="ui icon times red button_remove_command"></i> <div class="var_attributed"></div> <span class="text_attr_receives span_command_spec">'+LocalizedStrings.getUI('text_receives')+'</span> '
  12. + '<div class="expression_elements"></div> </div>');
  13. el.data('command', command);
  14. VariableValueMenu.renderMenu(command, command.variable, el.find('.var_attributed'), function_obj);
  15. if (!command.expression || command.expression.length < 1) {
  16. var exp = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  17. command.expression.push(exp);
  18. }
  19. addHandlers(command, function_obj, el);
  20. ExpressionManagement.renderExpression(command, function_obj, el.find('.expression_elements'), command.expression);
  21. //renderExpressionElements(command, function_obj, el);
  22. return el;
  23. }
  24. export function manageExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, selectedItem, expression_element) {
  25. var index_to_move = expression_element.itens.indexOf(ref_object);
  26. switch (selectedItem.data('exp')) {
  27. case Models.EXPRESSION_ELEMENTS.exp_op_exp:
  28. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp, [expression_element.itens[index_to_move],
  29. Models.ARITHMETIC_TYPES.plus,
  30. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  31. expression_element.itens[index_to_move] = exp;
  32. break;
  33. case Models.EXPRESSION_ELEMENTS.op_exp:
  34. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.op_exp, [Models.ARITHMETIC_TYPES.plus,
  35. expression_element.itens[index_to_move] ]);
  36. expression_element.itens[index_to_move] = exp;
  37. break;
  38. case Models.EXPRESSION_ELEMENTS.par_exp_par:
  39. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.par_exp_par, [expression_element.itens[index_to_move]]);
  40. expression_element.itens[index_to_move] = exp;
  41. break;
  42. }
  43. renderExpressionElements(command, function_obj, dom_object);
  44. }
  45. function renderExpressionElements (command, function_obj, el) {
  46. var expression_div = el.find('.expression_elements');
  47. var command_container;
  48. if (el.hasClass("command_container") == false) {
  49. var hier = el.parentsUntil(".command_container");
  50. for (var i = 0; i < hier.length; i++) {
  51. if ($(hier[i]).hasClass("command_container")) {
  52. command_container = $(hier[i]);
  53. break;
  54. }
  55. if ($(hier[i]).hasClass("expression_elements")) {
  56. expression_div = $(hier[i]);
  57. break;
  58. }
  59. }
  60. }
  61. if (command_container) {
  62. expression_div = command_container.find('.expression_elements');
  63. }
  64. expression_div.text('');
  65. for (var i = 0; i < command.expression.length; i++) {
  66. var temp = $('<div class="expression_element"></div>');
  67. temp.data('ref_element', command.expression[i]);
  68. temp.data('ref_index', i);
  69. expression_div.append(temp);
  70. renderElement(command, function_obj, temp, command.expression[i]);
  71. }
  72. }
  73. function renderOperator (command, function_obj, temp_op, expression_element, index_op) {
  74. var context_menu = '<div class="ui dropdown"><div class="text">';
  75. switch (expression_element.itens[index_op]) {
  76. case Models.ARITHMETIC_TYPES.plus:
  77. context_menu += '+';
  78. break;
  79. case Models.ARITHMETIC_TYPES.minus:
  80. context_menu += '-';
  81. break;
  82. case Models.ARITHMETIC_TYPES.multiplication:
  83. context_menu += '*';
  84. break;
  85. case Models.ARITHMETIC_TYPES.division:
  86. context_menu += '/';
  87. break;
  88. case Models.ARITHMETIC_TYPES.module:
  89. context_menu += '%';
  90. break;
  91. case Models.ARITHMETIC_TYPES.none:
  92. context_menu += '...';
  93. break;
  94. }
  95. context_menu += '</div><div class="menu">';
  96. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.plus+'">+</div>';
  97. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.minus+'">-</div>';
  98. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.multiplication+'">*</div>';
  99. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.division+'">/</div>';
  100. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.module+'">%</div>';
  101. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.none+'" data-text="...">Nenhum</div>';
  102. context_menu += '</div></div>';
  103. context_menu = $(context_menu);
  104. temp_op.append(context_menu);
  105. context_menu.dropdown({
  106. onChange: function(value, text, $selectedItem) {
  107. expression_element.itens[index_op] = value;
  108. }
  109. });
  110. }
  111. function renderMenuAddExpression (command, function_obj, el, dom_append_menu, expression_append_new_expression) {
  112. if (el.hasClass("command_container") == false) {
  113. var hier = el.parentsUntil(".commands_list_div");
  114. for (var i = 0; i < hier.length; i++) {
  115. if ($(hier[i]).hasClass("command_container")) {
  116. el = $(hier[i]);
  117. break;
  118. }
  119. }
  120. }
  121. if (dom_append_menu.hasClass("expression_elements") == false) {
  122. var hier = el.parentsUntil(".commands_list_div");
  123. for (var i = 0; i < hier.length; i++) {
  124. if ($(hier[i]).hasClass("expression_elements")) {
  125. dom_append_menu = $(hier[i]);
  126. break;
  127. }
  128. }
  129. }
  130. var context_menu = '<div class="ui dropdown"><div class="text"></div><i class="ui icon arrow alternate circle right outline"></i><div class="menu">';
  131. context_menu += '<div class="item" data-value="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  132. context_menu += '<div class="item" data-value="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  133. context_menu += '<div class="item" data-value="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  134. context_menu += '</div></div>';
  135. context_menu = $(context_menu);
  136. dom_append_menu.append(context_menu);
  137. context_menu.dropdown({
  138. onChange: function(value, text, $selectedItem) {
  139. switch (value) {
  140. case Models.EXPRESSION_ELEMENTS.exp_op_exp:
  141. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp, [new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  142. Models.ARITHMETIC_TYPES.plus,
  143. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  144. expression_append_new_expression.push(exp);
  145. break;
  146. case Models.EXPRESSION_ELEMENTS.op_exp:
  147. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.op_exp, [Models.ARITHMETIC_TYPES.plus,
  148. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  149. expression_append_new_expression.push(exp);
  150. break;
  151. case Models.EXPRESSION_ELEMENTS.par_exp_par:
  152. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.par_exp_par, [new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  153. expression_append_new_expression.push(exp);
  154. break;
  155. }
  156. renderExpressionElements(command, function_obj, el);
  157. }
  158. });
  159. }
  160. function renderElement (command, function_obj, el, expression_element) {
  161. switch (expression_element.type_exp) {
  162. case Models.EXPRESSION_ELEMENTS.exp_op_exp:
  163. var temp_op = $('<div class="component_element"></div>');
  164. var temp_exp_1 = $('<div class="component_element"></div>');
  165. var temp_exp_2 = $('<div class="component_element"></div>');
  166. el.append(temp_exp_1);
  167. el.append(temp_op);
  168. el.append(temp_exp_2);
  169. if (expression_element.itens[0].type) {
  170. VariableValueMenu.renderMenu(command, expression_element.itens[0], temp_exp_1, function_obj, 2, expression_element);
  171. } else {
  172. renderElement(command, function_obj, temp_exp_1, expression_element.itens[0]);
  173. }
  174. renderOperator(command, function_obj, temp_op, expression_element, 1);
  175. if (expression_element.itens[2].type) {
  176. VariableValueMenu.renderMenu(command, expression_element.itens[2], temp_exp_2, function_obj, 2, expression_element);
  177. } else {
  178. renderElement(command, function_obj, temp_exp_2, expression_element.itens[2]);
  179. }
  180. break;
  181. case Models.EXPRESSION_ELEMENTS.op_exp:
  182. var temp_op = $('<div class="component_element"></div>');
  183. var temp_exp = $('<div class="component_element"></div>');
  184. el.append(temp_op);
  185. el.append(temp_exp);
  186. renderOperator(command, function_obj, temp_op, expression_element, 0);
  187. if (expression_element.itens[1].type) {
  188. VariableValueMenu.renderMenu(command, expression_element.itens[1], temp_exp, function_obj, 2, expression_element);
  189. } else {
  190. renderElement(command, function_obj, temp_exp, expression_element.itens[1]);
  191. }
  192. break;
  193. case Models.EXPRESSION_ELEMENTS.par_exp_par:
  194. var temp_par_1 = $('<div class="component_element"> ( </div>');
  195. var temp_exp = $('<div class="component_element"></div>');
  196. var temp_par_2 = $('<div class="component_element"> ) </div>');
  197. el.append(temp_par_1);
  198. el.append(temp_exp);
  199. for (var j = 0; j < expression_element.itens.length; j++) {
  200. if (expression_element.itens[j].type) {
  201. VariableValueMenu.renderMenu(command, expression_element.itens[j], temp_exp, function_obj, 2, expression_element);
  202. } else {
  203. renderElement(command, function_obj, temp_exp, expression_element.itens[j]);
  204. }
  205. }
  206. //renderMenuAddExpression(command, function_obj, el, el, expression_element.itens);
  207. el.append(temp_par_2);
  208. break;
  209. }
  210. }
  211. function renderExpression (command, function_obj, el) {
  212. var expression_div = el.find('.expression_elements');
  213. expression_div.text('');
  214. var menu_add_item = $('<div class="menu_add_item"></div>');
  215. menu_add_item.data('index_add', 0);
  216. expression_div.append(menu_add_item);
  217. for (var i = 0; i < command.expression.length; i++) {
  218. if (command.expression[i].type) {
  219. var temp = $('<div class="expression_element"></div>');
  220. temp.data('ref_element', command.expression[i]);
  221. temp.data('ref_index', i);
  222. expression_div.append(temp);
  223. VariableValueMenu.renderMenu(command, command.expression[i], temp, function_obj);
  224. } else if (command.expression[i] == "(" || command.expression[i] == ")") {
  225. var temp = $('<div class="expression_element">'+command.expression[i]+'</div>');
  226. temp.data('ref_element', command.expression[i]);
  227. temp.data('ref_index', i);
  228. expression_div.append(temp);
  229. } else {
  230. var temp = '<div class="expression_element">';
  231. switch(command.expression[i]) {
  232. case Models.ARITHMETIC_TYPES.plus:
  233. temp += '+';
  234. break;
  235. case Models.ARITHMETIC_TYPES.minus:
  236. temp += '-';
  237. break;
  238. case Models.ARITHMETIC_TYPES.multiplication:
  239. temp += '*';
  240. break;
  241. case Models.ARITHMETIC_TYPES.division:
  242. temp += '/';
  243. break;
  244. case Models.ARITHMETIC_TYPES.module:
  245. temp += '%';
  246. break;
  247. }
  248. temp += '</div>';
  249. temp = $(temp);
  250. temp.data('ref_element', command.expression[i]);
  251. temp.data('ref_index', i);
  252. expression_div.append(temp);
  253. }
  254. var menu_add_item_seq = $('<div class="menu_add_item"></div>');
  255. var index_temp = (i + 1);
  256. menu_add_item_seq.data('index_add', index_temp);
  257. expression_div.append(menu_add_item_seq);
  258. }
  259. addMenuItens(command, function_obj, el);
  260. }
  261. function addMenuItens (command, function_obj, expression_div) {
  262. var divs_expression = expression_div.find('.menu_add_item');
  263. for (var i = 0; i < divs_expression.length; i++) {
  264. var temp = $(divs_expression[i]).data('index_add');
  265. var context_menu = '<div class="ui dropdown context_menu_clear"><i class="ui icon plus square outline"></i><div class="menu">';
  266. context_menu += '<div class="item" data-option="value" data-index="'+temp+'">'+LocalizedStrings.getUI('text_value')+'</div>';
  267. context_menu += '<div class="item" data-option="operator" data-index="'+temp+'">'+LocalizedStrings.getUI('text_operator')+'</div>';
  268. context_menu += '<div class="item" data-option="parentheses" data-index="'+temp+'">'+LocalizedStrings.getUI('text_parentheses')+'</div>';
  269. context_menu += '</div></div>';
  270. context_menu = $(context_menu);
  271. $(divs_expression[i]).append(context_menu);
  272. context_menu.dropdown({
  273. on: 'hover',
  274. onChange: function(value, text, $selectedItem) {
  275. switch ($selectedItem.data('option')) {
  276. case "value":
  277. command.expression.splice($selectedItem.data('index'), 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  278. renderExpression(command, function_obj, expression_div);
  279. break;
  280. case "operator":
  281. command.expression.splice($selectedItem.data('index'), 0, Models.ARITHMETIC_TYPES.plus);
  282. renderExpression(command, function_obj, expression_div);
  283. break;
  284. case "parentheses":
  285. command.expression.splice($selectedItem.data('index'), 0, "(");
  286. command.expression.splice($selectedItem.data('index') + 1, 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  287. command.expression.splice($selectedItem.data('index') + 2, 0, ")");
  288. renderExpression(command, function_obj, expression_div);
  289. break;
  290. }
  291. }
  292. });
  293. }
  294. }
  295. function addHandlers (command, function_obj, attribution_dom) {
  296. attribution_dom.find('.button_remove_command').on('click', function() {
  297. if (CommandsManagement.removeCommand(command, function_obj, attribution_dom)) {
  298. attribution_dom.fadeOut(400, function() {
  299. attribution_dom.remove();
  300. });
  301. }
  302. });
  303. attribution_dom.find('.button_refresh_attribution').on('click', function() {
  304. renderExpressionElements(command, function_obj, attribution_dom);
  305. });
  306. }
  307. export function renderMenuOperations (command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected) {
  308. /*console.log("recebido o seguinte DOM: ");
  309. console.log(dom_object);
  310. if (dom_object.hasClass('var_attributed')) {
  311. return;
  312. } else {
  313. var hier = dom_object.parentsUntil(".command_container");
  314. for (var i = 0; i < hier.length; i++) {
  315. if ($(hier[i]).hasClass('var_attributed') || $(hier[i]).hasClass('parameters_function_called')) {
  316. return;
  317. }
  318. }
  319. }
  320. dom_object.find('.context_menu_clear').remove();
  321. var menu_operations = '<div class="ui dropdown menu_operations"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  322. for (var tm in Models.ARITHMETIC_TYPES) {
  323. menu_operations += '<div class="item" data-option="'+tm+'">'+LocalizedStrings.getUI('btn_arithmetic_' + tm)+'</div>';
  324. }
  325. menu_operations += '<div class="item" data-option="clear">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  326. menu_operations += '</div></div>';
  327. menu_operations = $(menu_operations);
  328. dom_object.append(menu_operations);
  329. menu_operations.dropdown({
  330. onChange: function(value, text, $selectedItem) {
  331. switch ($($selectedItem).data('option')) {
  332. case "clear":
  333. $(dom_object).text('');
  334. VariableValueMenu.renderMenu(command, ref_object, dom_object, function_obj);
  335. break;
  336. default:
  337. createExpressionAround(command, ref_object, dom_object, function_obj);
  338. menu_operations.find('.text').text('');
  339. }
  340. }
  341. });*/
  342. }
  343. function createExpressionAround (command, ref_object, dom_object, function_obj) {
  344. $('<span> ( </span>').insertBefore(dom_object);
  345. $('<span> ) </span>').insertAfter(dom_object);
  346. VariableValueMenu.renderMenu(command, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true), dom_object, function_obj);
  347. }