controllers.js- 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. var deferred = Deferred();
  2. function StartCtrl(){
  3. }
  4. function CommCtrl($scope, $rootScope){
  5. $scope.valor = 123;
  6. $scope.getSource = function(){
  7. return JSON.stringify($rootScope.getSource());
  8. }
  9. $scope.getEvaluation = function(){
  10. return $rootScope.getEvaluation();
  11. }
  12. }
  13. function IvProgCreateCtrl($scope, $rootScope, IvProgSource, $filter){
  14. $rootScope.trackAction = function(action){
  15. $.post(ilaParams.MA_PARAM_addresPOST+"&track=1", { trackingData: "html=1;"+action }, function(d){
  16. });
  17. }
  18. $rootScope.getSource = function(){
  19. $rootScope.trackAction("getSource");
  20. return {
  21. mapping: $rootScope.mapping,
  22. src: $scope.program,
  23. testCases: $scope.testCases
  24. };
  25. }
  26. $rootScope.getEvaluation = function(){
  27. $rootScope.trackAction("getEvaluation");
  28. $scope.run(true);
  29. }
  30. $rootScope.itemCount = 0;
  31. $scope.vars = [];
  32. $scope.params = [];
  33. $scope.testCases = [];
  34. $scope.addTestCase = function(){
  35. $rootScope.trackAction("addTestCase");
  36. $scope.testCases.push({ input: "", output: "", currentIndex: 0 });
  37. }
  38. $scope.removeTestCase = function(i){
  39. $rootScope.trackAction("removeTestCase");
  40. $scope.testCases.splice(i, 1);
  41. }
  42. $rootScope.mapping = {};
  43. $scope.getTeste = function(){
  44. return 1;
  45. }
  46. // undo - redo control
  47. $scope.historyStack = -1;
  48. $scope.actionsHistory = [];
  49. $scope.addSnap = true;
  50. $scope.takeSnap = function(friendlyName, applying, sp){
  51. if(sp){
  52. $scope.actionsHistory.splice($scope.historyStack, $scope.actionsHistory.length-$scope.historyStack);
  53. }
  54. $scope.actionsHistory.push({name: friendlyName, src: JSON.stringify($scope.program)});
  55. $scope.historyStack = $scope.actionsHistory.length;
  56. }
  57. $rootScope.snapshot = function(friendlyName, applying){
  58. if(!applying){
  59. $scope.$apply(function(){
  60. $scope.takeSnap(friendlyName, applying, true);
  61. });
  62. }else{
  63. $scope.takeSnap(friendlyName, applying, true);
  64. }
  65. $scope.addSnap = true;
  66. }
  67. $scope.undo = function(){
  68. if($scope.historyStack>0){
  69. if($scope.addSnap){
  70. // salvando o estado atual
  71. $scope.takeSnap('', 1);
  72. $scope.historyStack--;
  73. $scope.addSnap = false;
  74. }
  75. $scope.historyStack--;
  76. var obj = JSON.parse($scope.actionsHistory[$scope.historyStack].src);
  77. $scope.program = obj;
  78. }
  79. }
  80. $scope.redo = function(){
  81. if($scope.historyStack < $scope.actionsHistory.length-1){
  82. $scope.historyStack++;
  83. var obj = JSON.parse($scope.actionsHistory[$scope.historyStack].src);
  84. $scope.program = obj;
  85. }
  86. }
  87. $scope.currentFunction = 0;
  88. $scope.program = {
  89. programName: "firstProgram",
  90. functions: [
  91. {
  92. isMain: true,
  93. name: "Principal",
  94. vars: {},
  95. params: {},
  96. type: "main", // int, void, float
  97. nodes:[]
  98. }/*,
  99. {
  100. isMain: false,
  101. name: "fatorial",
  102. vars: {},
  103. varss: {
  104. "var_1":
  105. { name: 'newVar1', type: 'int', initialValue: 0, id: "var_1" }
  106. },
  107. params: {},
  108. type: "int", // int, void, float
  109. nodes:[],
  110. nodess: [
  111. {
  112. id: "attr_1",
  113. type: "attr",
  114. name: "attr",
  115. parent: null,
  116. variable: "",
  117. exp: []
  118. }
  119. ]
  120. }*/
  121. ]
  122. };
  123. $scope.setCurrentFunction = function(ind){
  124. $scope.currentFunction = ind;
  125. }
  126. $scope.addElVar = function(v){
  127. v.push({
  128. t: "var",
  129. v: "",
  130. o: "",
  131. p: ''//v
  132. });
  133. }
  134. $scope.addElVal = function(v){
  135. v.push({
  136. t: "val",
  137. v: 0,
  138. o: "",
  139. p: ''//v
  140. });
  141. }
  142. $scope.addElExpB = function(v){
  143. v.push({
  144. t: "expB",
  145. v: {
  146. op1: {
  147. t: "v",
  148. v: ""
  149. },
  150. op2: {
  151. t: "v",
  152. v: ""
  153. },
  154. op: ">"
  155. },
  156. o: "&&",
  157. p: ''//v
  158. });
  159. }
  160. $scope.isolar = function(item){
  161. item.t = "exp";
  162. item.v = "";
  163. item.exp = [];
  164. }
  165. $scope.addExp = function(parent){
  166. parent.push({ t: "val", v: "a", o: "+"});
  167. }
  168. $scope.getTemplate = function(x){
  169. return 'partials/elements/'+x.type+'.html'+"?t="+cacheTime;
  170. }
  171. $scope.addParam = function(){
  172. //var ind = $scope.params.length;
  173. var ind = $scope.programs[$scope.currentProgram].functions[0].params.length;
  174. //$scope.params.push({ name: 'newParam'+ind, type: 'int', initialValue: 0 } );
  175. $scope.programs[$scope.currentProgram].functions[0].params.push({ name: 'newParam'+ind, type: 'int', initialValue: 0 } );
  176. }
  177. $scope.removeParam = function(v){
  178. $scope.params.splice($scope.params.indexOf(v), 1);
  179. }
  180. $scope.varSetType = function(v, type){
  181. $rootScope.trackAction("changeVarType");
  182. var previousType = v.type;
  183. v.type = type;
  184. if(type=="string"){
  185. v.initialValue = "Olá mundo!";
  186. }else if(type=="float"){
  187. v.initialValue = 1.0;
  188. }else if(type=="int"){
  189. v.initialValue = 1;
  190. }else if(type=="boolean"){
  191. v.initialValue = true;
  192. }
  193. $scope.checkChangeTypeConsequences(v, $scope.program.functions[$scope.currentFunction].nodes, previousType);
  194. }
  195. // quando alterar o tipo de uma variavel, checar as consequencias
  196. $scope.checkChangeTypeConsequences = function(variable, where, previous){
  197. angular.forEach(where, function(item, key){
  198. if(item.type=="attr"){
  199. if(item.variable==variable.id){
  200. if(variable.type!=previous){
  201. var compatibility = ["int", "float"];
  202. if((compatibility.indexOf(variable.type)==-1)||(compatibility.indexOf(previous)==-1)){
  203. if(where[key].exp.length>0){
  204. where[key].exp = [];
  205. }
  206. }
  207. }
  208. }
  209. }
  210. if(item.nodes && item.nodes.length>0){
  211. $scope.checkChangeTypeConsequences(variable, item.nodes);
  212. }
  213. });
  214. }
  215. $scope.addVar = function(){
  216. $rootScope.trackAction("addVar");
  217. // TODO: checar se alterou o valor
  218. $rootScope.snapshot('Variável adicionada', true);
  219. var ind = $scope.itemCount;
  220. var id = "var"+$scope.itemCount++;
  221. $scope.program.functions[$scope.currentFunction].vars[id] = ({ name: 'newVar'+ind, type: 'int', initialValue: 1, id: id });
  222. }
  223. $scope.removeVarRec = function(nodes, id){
  224. $rootScope.trackAction("removeVar");
  225. angular.forEach(nodes, function(node, key){
  226. if(node.type=="write"){
  227. if(node.variable==id){
  228. node.variable = '';
  229. }
  230. }
  231. if(node.type!="attr"){
  232. if(node.nodes.length>0){
  233. $scope.removeVarRec(node.nodes, id);
  234. }
  235. }
  236. });
  237. }
  238. $scope.removeVar = function(v){
  239. $rootScope.trackAction("removeVar");
  240. $rootScope.snapshot('Variável removida', true);
  241. $scope.removeVarRec($scope.program.functions[$scope.currentFunction].nodes, v.id);
  242. delete $scope.program.functions[$scope.currentFunction].vars[v.id];
  243. }
  244. $scope.removeItem = function(parent, item){
  245. $rootScope.trackAction("removeItem");
  246. parentId = parent;
  247. // TODO: tratar para os outros functions
  248. if(parent=="root_0"){
  249. parent = $scope.program.functions[0].nodes;
  250. }else{
  251. parent = $rootScope.mapping[parent].nodes;
  252. }
  253. if($.isArray(parent)) {
  254. parent.splice(parent.indexOf(item),1);
  255. }
  256. if($rootScope.mapping[parentId]){
  257. var p1 = $rootScope.mapping[parentId].nodes1;
  258. if($.isArray(p1)) {
  259. p1.splice(p1.indexOf(item),1);
  260. }
  261. var p2 = $rootScope.mapping[parentId].nodes2;
  262. if($.isArray(p2)) {
  263. p2.splice(p2.indexOf(item),1);
  264. }
  265. }
  266. delete $rootScope.mapping[item.id];
  267. }
  268. $scope.isValidAttr = function(attr){
  269. var isValid = true;
  270. angular.forEach(attr, function(a, k){
  271. if(a.type=="var"){
  272. }
  273. });
  274. return false;
  275. }
  276. $scope.sortableOptions = {
  277. handle: '.handle',
  278. placeholder: "apps",
  279. connectWith: ".apps-container"
  280. };
  281. $scope.delete = function(data) {
  282. $rootScope.trackAction("delete");
  283. data.nodes = [];
  284. };
  285. $scope.run = function(useTestCases){
  286. cleanOutput();
  287. $rootScope.trackAction("run="+useTestCases);
  288. if(!$scope.validateEverything($scope.program)){
  289. writer("<i class='fa fa-exclamation-triangle'></i> Existem campos vazios. Preencha os campos com borda vermelha para executar o algoritmo corretamente.", false);
  290. }else{
  291. if(useTestCases){
  292. totalCasesEvaluated = 0;
  293. totalCasesPassed = 0;
  294. totalTestCases = $scope.testCases.length;
  295. angular.forEach($scope.testCases, function(item, key){
  296. $scope.testCases[key].currentIndex = 0;
  297. })
  298. testCases = $scope.testCases;
  299. var code = "";
  300. angular.forEach($scope.testCases, function(item, key){
  301. code += $scope.genCode($scope.program, true, key);
  302. });
  303. console.log(code);
  304. window.eval(code);
  305. }else{
  306. $rootScope.trackAction("run");
  307. var code = $scope.genCode($scope.program, false, 0);
  308. window.eval(code);
  309. }
  310. $("#valor").unbind('keydown');
  311. $("#valor").keydown(function( event ) {
  312. if ( event.which == 13 ) {
  313. $('#readData').modal('hide');
  314. var valor = $("#valor").val();
  315. $("#valor").val("");
  316. deferred.call(valor);
  317. event.preventDefault();
  318. }
  319. });
  320. $("#btnOk").unbind('click');
  321. $("#btnOk").click(function(){
  322. $('#readData').modal('hide');
  323. var valor = $("#valor").val();
  324. $("#valor").val("");
  325. deferred.call(valor);
  326. });
  327. }
  328. }
  329. $scope.clearOutput = function(){
  330. $rootScope.trackAction("cleanOutput");
  331. $(".output").html("");
  332. }
  333. $scope.validateEverything = function(funcs){
  334. $(".node-with-error").removeClass("node-with-error");
  335. var ret = true;
  336. angular.forEach(funcs.functions, function(func, key){
  337. ret = ret && $scope.validateNode(func.nodes, func.vars);
  338. });
  339. return ret;
  340. }
  341. $scope.validateNode = function(nodes, vars){
  342. var ret = true;
  343. angular.forEach(nodes, function(node, key){
  344. if (node.type=="write"){
  345. if(node.variable==""){
  346. $("#node"+node.id).find(".select").addClass("node-with-error");
  347. ret = false;
  348. }
  349. }
  350. if(node.type=="read"){
  351. if(node.variable==""){
  352. $("#node"+node.id).find(".select").addClass("node-with-error");
  353. ret = false;
  354. }
  355. }
  356. if(node.type=="for"){
  357. if(node.forType==1){
  358. if((node.limitType=="var")&&(node.limit=="")){
  359. $("#node"+node.id).find(".for1").find(".select").addClass("node-with-error");
  360. ret = false;
  361. }
  362. }
  363. if(node.forType==2){
  364. if((node.limitType=="var")&&(node.limit=="")){
  365. $("#node"+node.id).find(".for1").find(".select").addClass("node-with-error");
  366. ret = false;
  367. }
  368. if(node.using==""){
  369. $("#node"+node.id).find(".for2").addClass("node-with-error");
  370. ret = false;
  371. }
  372. }
  373. if(node.forType==3){
  374. if((node.limitType=="var")&&(node.limit=="")){
  375. $("#node"+node.id).find(".for1").find(".select").addClass("node-with-error");
  376. ret = false;
  377. }
  378. if(node.using==""){
  379. $("#node"+node.id).find(".for2").addClass("node-with-error");
  380. ret = false;
  381. }
  382. if((node.initialType=="var")&&(node.initial=="")){
  383. ret = false;
  384. }
  385. if((node.initialType=="val")&&(node.limitType=="val")&&(node.initial>node.limit)){
  386. ret = false;
  387. }
  388. if((node.stepType=="var")&&(node.step=="")){
  389. $("#node"+node.id).find(".for3").find(".select").addClass("node-with-error");
  390. ret = false;
  391. }
  392. }
  393. }
  394. });
  395. return ret;
  396. }
  397. $scope.genCode = function(funcs, useTestCases, testCaseIndex){
  398. var strCode = "var t"+testCaseIndex+" = function(){";
  399. var i = 0;
  400. angular.forEach(funcs.functions, function(func, key){
  401. if(i++==0){
  402. strCode+= "function "+func.name+"(){";
  403. angular.forEach(func.vars, function(variable, key){
  404. if(variable.type=="string"){
  405. strCode+="var var_"+variable.id+" = \""+variable.initialValue+"\";";
  406. }else{
  407. strCode+="var var_"+variable.id+" = "+variable.initialValue+";";
  408. }
  409. });
  410. strCode+= 'next(function(){';
  411. strCode+='/*return deferred;*/';
  412. strCode+='})';
  413. // correcao automatica - false
  414. strCode+=$scope.genNode(useTestCases, func.nodes, func.vars, testCaseIndex);
  415. if(useTestCases){
  416. // correcao automatica
  417. strCode+= '.next(function(){';
  418. //strCode+=' console.log("OUT "+getOutput());'
  419. strCode+=' endTest('+testCaseIndex+');'
  420. strCode+='});';
  421. }
  422. strCode+= "}";
  423. if(func.type=="main"){
  424. strCode+=func.name+"()";
  425. }
  426. }
  427. });
  428. strCode+="}; t"+testCaseIndex+"();";
  429. return strCode;
  430. }
  431. $scope.genNode = function(isEvaluating, nodes, vars, testCaseIndex){
  432. var strCode = "";
  433. angular.forEach(nodes, function(node, key){
  434. if(node.type=="write"){
  435. if(node.variable!=''){
  436. var v = $scope.program.functions[$scope.currentFunction].vars[node.variable];
  437. strCode += ".next(function(){";
  438. if(v.type=="boolean"){
  439. strCode+="if(var_"+node.variable+"){ writer('Verdadeiro', "+isEvaluating+"); }else{ writer('Falso', "+isEvaluating+"); }";
  440. }else{
  441. strCode += "writer(";
  442. strCode += "var_"+node.variable;
  443. strCode += ","+isEvaluating+");";
  444. }
  445. strCode += "})";
  446. }
  447. }
  448. if(node.type=="while"){
  449. // while
  450. strCode+= '.next(function(){';
  451. //strCode+= 'var i'+node.id+ ' = 0;';
  452. strCode+= 'function loop'+node.id+'(){';
  453. strCode+= ' return next(function(){})'; // apenas para poder encadear
  454. if(node.nodes.length>0){
  455. strCode+= $scope.genNode(isEvaluating, node.nodes, vars);
  456. }
  457. strCode+=' .next(function(){';
  458. //strCode+=' ++i'+node.id+';';
  459. strCode+=' if('+$scope.genExp(node.exp, 'boolean')+'){';
  460. strCode+=' return loop'+node.id+'();';
  461. strCode+=' }'
  462. strCode+=' });';
  463. strCode+='}';
  464. strCode+=' if('+$scope.genExp(node.exp, 'boolean')+'){';
  465. strCode+='return loop'+node.id+'();';
  466. strCode+='}';
  467. strCode+='})';
  468. }
  469. if(node.type=="for"){
  470. if(node.forType==1){
  471. // for simples
  472. strCode+= '.next(function(){';
  473. strCode+= 'var i'+node.id+ ' = 0;';
  474. strCode+= 'function loop'+node.id+'(){';
  475. strCode+= ' return next(function(){})'; // apenas para poder encadear
  476. if(node.nodes.length>0){
  477. strCode+= $scope.genNode(isEvaluating, node.nodes, vars);
  478. }
  479. strCode+=' .next(function(){';
  480. strCode+=' ++i'+node.id+';';
  481. if(node.limitType=="val"){
  482. strCode+=' if(i'+node.id+'<'+node.limit+'){';
  483. }else{
  484. strCode+=' if(i'+node.id+'<'+' var_'+node.limit+'){';
  485. }
  486. strCode+=' return loop'+node.id+'();';
  487. strCode+=' }'
  488. strCode+=' });';
  489. strCode+='}';
  490. if(node.limitType=="val"){
  491. strCode+=' if(i'+node.id+'<'+node.limit+'){';
  492. }else{
  493. strCode+=' if(i'+node.id+'<'+' var_'+node.limit+'){';
  494. }
  495. strCode+='return loop'+node.id+'();';
  496. strCode+='}';
  497. strCode+='})';
  498. }else if(node.forType==2){
  499. // for mediano
  500. strCode+= '.next(function(){';
  501. strCode+= ' var_'+node.using+ ' = 0;';
  502. strCode+= 'function loop'+node.id+'(){';
  503. strCode+= ' return next(function(){})'; // apenas para poder encadear
  504. if(node.nodes.length>0){
  505. strCode+= $scope.genNode(isEvaluating, node.nodes, vars);
  506. }
  507. strCode+=' .next(function(){';
  508. strCode+=' ++var_'+node.using+';';
  509. if(node.limitType=="val"){
  510. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  511. }else{
  512. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  513. }
  514. strCode+=' return loop'+node.id+'();';
  515. strCode+=' }'
  516. strCode+=' });';
  517. strCode+='}';
  518. if(node.limitType=="val"){
  519. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  520. }else{
  521. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  522. }
  523. strCode+='return loop'+node.id+'();';
  524. strCode+='}';
  525. strCode+='})';
  526. }else if(node.forType==3){
  527. // for hard rs
  528. strCode+= '.next(function(){';
  529. if(node.initialType=="val"){
  530. strCode+= ' var_'+node.using+ ' = '+node.initial+';';
  531. }else{
  532. strCode+= ' var_'+node.using+ ' = var_'+node.initial+';';
  533. }
  534. strCode+= 'function loop'+node.id+'(){';
  535. strCode+= ' return next(function(){})'; // apenas para poder encadear
  536. if(node.nodes.length>0){
  537. strCode+= $scope.genNode(isEvaluating, node.nodes, vars);
  538. }
  539. strCode+=' .next(function(){';
  540. if(node.stepType=="val"){
  541. strCode+=' var_'+node.using+'+= '+node.step+';';
  542. }else{
  543. strCode+=' var_'+node.using+'+= var_'+node.step+';';
  544. }
  545. if(node.limitType=="val"){
  546. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  547. }else{
  548. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  549. }
  550. strCode+=' return loop'+node.id+'();';
  551. strCode+=' }'
  552. strCode+=' });';
  553. strCode+='}';
  554. if(node.limitType=="val"){
  555. strCode+=' if(var_'+node.using+'<'+node.limit+'){';
  556. }else{
  557. strCode+=' if(var_'+node.using+'<'+' var_'+node.limit+'){';
  558. }
  559. strCode+='return loop'+node.id+'();';
  560. strCode+='}';
  561. strCode+='})';
  562. }
  563. }
  564. if(node.type=="attr"){
  565. if(node.variable!=""){
  566. strCode+= '.next(function () {';
  567. strCode+=" var_"+node.variable+"=";
  568. strCode+=" ("+$scope.genExp(node.exp, vars[node.variable].type)+")";
  569. strCode+=" ;";
  570. strCode+= '})';
  571. }
  572. }
  573. if(node.type=="read"){
  574. var v = $scope.program.functions[$scope.currentFunction].vars[node.variable];
  575. if(!isEvaluating){
  576. strCode+= '.next(function () {';
  577. strCode+= ' $("#msgRead").html("'+node.message+'");';
  578. strCode+= ' $("#readData").modal();';
  579. strCode+= ' $("#valor").focus();';
  580. strCode+= ' return deferred;';
  581. strCode+= '}).';
  582. strCode+= 'next(function(a){';
  583. strCode+= ' console.log("Valor lido: "+a);';
  584. strCode+= '/* '+v.type+' */';
  585. }else{
  586. strCode+= '.next(function () {';
  587. strCode+= ' var a = "'+readerInput(testCaseIndex)+'";';
  588. }
  589. if(v.type=="int"){
  590. strCode+= " var_"+node.variable +" = parseInt(a);";
  591. }else if(v.type=="float"){
  592. strCode+= " var_"+node.variable +" = parseFloat(a); /* pq cai aqui */";
  593. }else if(v.type=="boolean"){
  594. // tratar boolean depois
  595. strCode+= " var_"+node.variable +" = a;";
  596. }else if(v.type=="string"){
  597. // tratar boolean depois
  598. strCode+= " var_"+node.variable +" = a;";
  599. }else{
  600. strCode+= " var_"+node.variable +" = a; ";
  601. }
  602. strCode+= '})';
  603. }
  604. if(node.type=="if"){
  605. strCode+= '.next(function () {';
  606. strCode+= 'if('+$scope.genExp(node.exp, 'boolean')+'){';
  607. strCode+= 'return next(function () {})'+$scope.genNode(isEvaluating, node.nodes1, vars);
  608. strCode+= '}else{';
  609. strCode+= 'return next(function () {})'+$scope.genNode(isEvaluating, node.nodes2, vars);
  610. strCode+= '}';
  611. strCode+= '})';
  612. }
  613. });
  614. return strCode;
  615. }
  616. $scope.genExp = function(exp, type){
  617. var strCode = "";
  618. console.log(exp);
  619. angular.forEach(exp, function(ex, key){
  620. if(ex.t == "var"){
  621. strCode+=" var_"+ex.v+" ";
  622. }else if(ex.t == "val"){
  623. if(type=="string"){
  624. strCode+=" \" "+ex.v+"\" ";
  625. }else{
  626. strCode+=" "+ex.v+" ";
  627. }
  628. }else if(ex.t=="exp"){
  629. strCode+=" ( "+$scope.genExp(ex.v, type)+" ) ";
  630. }else if(ex.t=="expB"){
  631. strCode+=" ( "+$scope.genExp(ex.v, type)+" ) ";
  632. }else if(ex.t=="op"){
  633. strCode+= ex.v;
  634. }else if(ex.t=="opB"){
  635. strCode+= ex.v;
  636. }
  637. });
  638. return strCode;
  639. }
  640. $scope.changeForType = function(node, v){
  641. node.forType +=v;
  642. }
  643. $scope.changeForValue = function(node){
  644. node.isValue = !node.isValue;
  645. if(!node.isValue){
  646. node.simpleVariable = "";
  647. }
  648. writer(node.isValue, false);
  649. }
  650. $scope.childrenVisible = function(node){
  651. node.isChildrenVisible = !node.isChildrenVisible;
  652. }
  653. $scope.add = function(parent, parentId, type, name) {
  654. $rootScope.trackAction("add;type="+type);
  655. var newNode = {
  656. id: $scope.itemCount++,
  657. type: type,
  658. name: name,
  659. nodes: [],
  660. parent: parentId
  661. };
  662. // especifico de cada estrutura
  663. if(type=="if"){
  664. newNode.id = "if_"+newNode.id;
  665. newNode.exp = [/*
  666. { t: 'expB',
  667. v: [{"t":"val","v":""},{"t":"opB","v":""},{"t":"val","v":""}]
  668. }*/
  669. ];
  670. newNode.isChildrenVisible = true;
  671. newNode.nodes1 = [];
  672. newNode.nodes2 = [];
  673. }
  674. if(type=="read"){
  675. newNode.id = "read_"+newNode.id;
  676. newNode.message = "Por favor digite um valor:";
  677. newNode.variable = "";
  678. }
  679. if(type=="write"){
  680. newNode.id = "write_"+newNode.id;
  681. newNode.variable = "";
  682. }
  683. if(type=="while"){
  684. newNode.id = "while_"+newNode.id;
  685. newNode.exp = [];
  686. newNode.isChildrenVisible = true;
  687. newNode.nodes = [];
  688. }
  689. if(type=="for"){
  690. newNode.id = "for_"+newNode.id;
  691. newNode.forType = 1; // 1 SIMPLE, 2 +-, 3 COMPLETE
  692. newNode.initial = 1;
  693. newNode.initialType = "val";
  694. newNode.limit = 5;
  695. newNode.limitType = "val";
  696. newNode.using = "";
  697. newNode.step = 1;
  698. newNode.stepType = "val";
  699. newNode.isChildrenVisible = true;
  700. newNode.times = 5;
  701. newNode.timesType = 5;
  702. newNode.simple = true;
  703. newNode.isValue = true;
  704. newNode.simpleVariable = "";
  705. newNode.initialValue = 0;
  706. newNode.endValue = 5;
  707. newNode.increment = 1;
  708. newNode.variable = "";
  709. }
  710. if(type=="attr"){
  711. newNode.id = "attr_"+newNode.id;
  712. newNode.variable = "";
  713. //newNode.exp = [];
  714. /*newNode.exp = {
  715. op1: '',
  716. op1T : '',
  717. op: '',
  718. op2: '',
  719. op2T: ''
  720. };*/
  721. delete newNode.nodes;
  722. newNode.exp = [];
  723. newNode.isLocked = false;
  724. }
  725. parent.push(newNode);
  726. $rootScope.mapping[newNode.id] = newNode;
  727. };
  728. $scope.save = function(){
  729. $.post('save.php', { src: JSON.stringify($scope.program) }, function(id) {
  730. $("body").append("<iframe src='get.php?id=" + id + "' style='display: none;' ></iframe>");
  731. });
  732. }
  733. if(ilaParams.MA_PARAM_Proposition!=null){
  734. $.get(ilaParams.MA_PARAM_Proposition, function(d){
  735. if(d!=null){
  736. $scope.mapping = d.mapping;
  737. $scope.program = d.src;
  738. $scope.testCases = d.testCases;
  739. $scope.$apply()
  740. }
  741. }, "json");
  742. }
  743. }
  744. function IvProgAbertoCtrl($scope){
  745. $scope.delete = function(data) {
  746. data.nodes = [];
  747. };
  748. $scope.add = function(data) {
  749. var post = data.nodes.length + 1;
  750. var newName = data.name + '-' + post;
  751. data.nodes.push({name: newName,nodes: []});
  752. };
  753. $scope.tree = [{name: "Node", nodes: []}];
  754. }