From ac1ea2004cdb34c83115d11b0c3a0c9c2f93a726 Mon Sep 17 00:00:00 2001
From: Timotej Lazar <timotej.lazar@araneo.org>
Date: Tue, 22 Sep 2015 11:33:54 +0200
Subject: Add support for run/stop in the Python interface

---
 js/codeq/comms.js  | 11 ++++++++---
 js/codeq/python.js | 31 +++++++++++++++++++++++++++----
 2 files changed, 35 insertions(+), 7 deletions(-)

(limited to 'js/codeq')

diff --git a/js/codeq/comms.js b/js/codeq/comms.js
index 9e69526..b04a2fc 100644
--- a/js/codeq/comms.js
+++ b/js/codeq/comms.js
@@ -250,13 +250,18 @@
             return this.send(query);
         },
 
-        sendPush: function commsSendPush (json) {
+        sendPythonExec: function commsSendPythonExec (json) {
+            json['action'] = 'python_exec';
+            return this.send(json);
+        },
+
+        sendPythonPush: function commsSendPythonPush (json) {
             json['action'] = 'python_push';
             return this.send(json);
         },
 
-        sendPull: function commsSendPull (json) {
-            json['action'] = 'python_pull';
+        sendPythonStop: function commsSendPythonStop (json) {
+            json['action'] = 'python_stop';
             return this.send(json);
         },
 
diff --git a/js/codeq/python.js b/js/codeq/python.js
index e5b59c2..510c4c1 100644
--- a/js/codeq/python.js
+++ b/js/codeq/python.js
@@ -20,7 +20,10 @@
         jqBtnPlan = jqScreen.find('.btn-plan'),
         jqBtnHint = jqScreen.find('.btn-hint'),
         jqBtnTest = jqScreen.find('.btn-test'),
-        jqAllButtons = jqBtnPlan.add(jqBtnHint).add(jqBtnTest), // all the buttons
+        jqBtnRun = jqScreen.find('.btn-run'),
+        jqBtnStop = jqScreen.find('.btn-stop'),
+        jqInfoButtons = jqBtnPlan.add(jqBtnHint).add(jqBtnTest), // all info-focusing buttons
+        jqAllButtons = jqInfoButtons.add(jqBtnRun).add(jqBtnStop), // all buttons
         // misc
         currentSubState = null,
         transitionEventName = 'mousedown',//event name of the event which will trigger the transition between these substates - the most common transition at least (there are some corner cases on the hint and test buttons -> see the code below)
@@ -76,10 +79,15 @@
 /*            Q.delay(100).then(function(){
              jqAllQuadrants.addClass('transition');//for smooth animations - need to be delayed, because otherwise we get some weird "animations" while the page is loading
              }).done();*/
-            jqAllButtons.on(transitionEventName, function (event) {
+            jqInfoButtons.on(transitionEventName, function (event) {
                 subScreens.transition('info'); // set focus on the hints quadrant
                 event.stopPropagation(); // don't allow the event to go on and trigger further transition
             });
+            jqBtnRun.on(transitionEventName, function (event) {
+                subScreens.transition('console'); // set focus on the hints quadrant
+                event.stopPropagation(); // don't allow the event to go on and trigger further transition
+            });
+
             jqAllQuadrants.on(transitionEventName, function () {
                 subScreens.transition($(this).data(stateNameTag));
             });
@@ -120,7 +128,8 @@
             };
 
         terminal.onInput = function (text) {
-            return codeq.comms.sendPush({
+            terminal.leftmostCol = 0;
+            return codeq.comms.sendPythonPush({
                 'text': text + '\n'
             }).then(tcs, tcf);
         };
@@ -260,9 +269,23 @@
                 }
             ).done();
         });
+        jqBtnRun.on('click', function () {
+            var program = editor.getDoc().getValue();
+            // TODO error handling
+            codeq.comms.sendPythonStop({});
+            codeq.comms.sendPythonExec({
+                'program': program
+            });
+            // focus the terminal
+            jqTerminal.click();
+        });
+        jqBtnStop.on('click', function () {
+            // TODO error handling
+            codeq.comms.sendPythonStop({});
+        });
 
         // TODO first line of interpreter output is buffered without this, why?
-        codeq.comms.sendPush({
+        codeq.comms.sendPythonPush({
             'text': ''
         });
 
-- 
cgit v1.2.1