attribution.js 16 KB

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