<commit_before>var workspace;

BLOCKLY_LANGUAGE = 'blockly'
PYTHON_LANGUAGE = 'python'

CodeParser = {
    tab_size: 2,

    tab : function() {
        return " ".repeat(CodeParser.tab_size);
    },

    indent_lines: function (code) {
        var tab = CodeParser.tab();
        var code_lines = code.split("\n");
        for (var i in code_lines){
            code_lines[i] = tab + code_lines[i]
        }
        return code_lines.join("\n")
    },

    std_code_prepend: function (){
        var tab = CodeParser.tab();
        return `from manus.robot_arm import *


arm = RobotArm()

# Wait for one second ...
arm.wait(1000)
# ... then start execution
`
    },

    prepare_std_code: function(code){
        // code = CodeParser.indent_lines(code);
        return CodeParser.std_code_prepend()+code+"\n\n";
    }

}


Program = {

  init: function() {

    $.ajax('/data/blockly-toolbox.xml').done(function(toolbox) {

      var blocklyContainer = $("#blockly");
      var blocklyArea = document.getElementById('blockly-area');
      var blocklyDiv = document.getElementById('blockly-workspace');
      blocklyContainer.append(Blockly.Xml.domToText(toolbox));

      Program.blockly = {};

      Program.blockly.running_app = null;
      Program.blockly.workspace = Blockly.inject('blockly-workspace',{
          grid:{
              spacing: 25,
              length: 3,
              colour: '#ddd',
              snap: true
          },
          media: 'media/',
          toolbox: document.getElementById('blockly-toolbox'),
          zoom: {controls: true, wheel: true}
      });

      // Ace code editor init
      Program.python = {};
      Program.python.shown = false;
      Program.python.in_use = false;
      Program.python.ace = ace.edit("python-editor");
      Program.python.ace.setTheme("ace/theme/xcode");
      Program.python.ace.getSession().setMode("ace/mode/python");
      Program.python.ace.getSession().setTabSize(Program.python.tab_size);
      Program.python.ace.setOptions({
          enableBasicAutocompletion: true,
          enableLiveAutocompletion: true
      });
      Program.python.ace.completers.push({
        getCompletions: function(editor, session, pos, prefix, callback) {
          callback(null, [
              {value: "arm.move_joint(joint, angle)", score: 1000, meta: "Robot arm: Move one joint to specific angle."},
              {value: "arm.cotrol_gripper(cmd)", score: 1000, meta: "Robot arm: Open, half close or close gripper."},
              {value: "arm.wait(milisecs)", score: 1000, meta: "Robot arm: Wait/Sleep for milisecs."},
              {value: "arm.move_to_coordinates(p)", score: 1000, meta: "Robot arm: Move arm to coordinates/point."},
              {value: "arm.detect_blocks()", score: 1000, meta: "Robot arm: Detect all blocks."},
              {value: "arm.get_joint_position(joint)", score: 1000, meta: "Robot arm: Get joints position."},
              {value: "retrieve_coordinate_from_point(comp, point)", score: 1000, meta: "Robot arm: Retrieve coordinate from point/coordinates."},
              {value: "retrieve_color_from_block(block)", score: 1000, meta: "Robot arm: Retrieve color from block."}
          ]);
        }
      });

      // Register & call on resize function
      var onresize = function(e) {
          blocklyDiv.style.left = blocklyArea.offsetLeft + 'px';
          blocklyDiv.style.top = blocklyArea.offsetTop  + 'px';
          blocklyDiv.style.width = blocklyArea.offsetWidth + 'px';
          blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';
      };

      window.addEventListener('resize', onresize, false);
      //onresize();
      //Blockly.svgResize(workspace);

      var initial = true;

      // Register on show callback
      $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
          if (Program.blockly.workspace) {
              onresize();
              Blockly.svgResize(Program.blockly.workspace);
              if (initial) {
                initial = false;
                Program.pull();
              }
          }
      })

      Program.blockly.workspace.addChangeListener(function(event){
          if (event.type == Blockly.Events.CREATE || 
              event.type == Blockly.Events.DELETE || 
              event.type == Blockly.Events.CHANGE || 
              event.type == Blockly.Events.MOVE 
          ) {
              Program.push();
          }   
      });

      Program.python.ace.on("input", function() { Program.push(); } );

    });

    var logconsole = $("#console .content");
    logconsole.click(function() {

      if ($("#console").hasClass("terminated")) {
          Program.show();
      }

    });

    var run_button = $.manus.widgets.fancybutton({
      callback : function() {  
          Program.run();
          logconsole.empty();
      }, icon: "play", tooltip: "Run"
    }).addClass("button-codetime");

    var new_program = $.manus.widgets.fancybutton({
      callback : function() {  
          Interface.dialog("New document",
              "Do you want to start from scratch? Make sure that you have saved your work first.",
              {
                "Cancel" : function() { return true; },
                "Blocky" : function() {
                  Program.current(null, "blockly");
                  return true;
                },
                "Python" : function() {
                  Program.current(null, "python");
                  return true;
                }

              }
            );
      },
      icon: "file", 
      tooltip: "New program"
    }).addClass("button-codetime");

    var convert_python = $.manus.widgets.fancybutton({
      callback : function() {          
        Program.python.ace.setValue(CodeParser.prepare_std_code(Blockly.Python.workspaceToCode(Program.blockly.workspace)));
        Program.python.shown = true;
        Program.python.in_use = true;
        Program.show("python");
      },
      icon: "align-left", 
      tooltip: "Convert to Python"
    }).addClass("button-blockly");

    var stop_button = $.manus.widgets.fancybutton({
      callback : function() {  
          $.ajax('/api/apps?run=').done(function(data) {});
      }, icon: "stop", tooltip: "Stop"
    }).addClass("button-runtime").hide();

    var load_button = $.manus.widgets.fancybutton({
      callback : function() {  
          Program.load();
      }, icon: "open", tooltip: "Load"
    }).addClass("button-codetime");<commit_msg>Adding notification when converting to python.<commit_after>var workspace;

BLOCKLY_LANGUAGE = 'blockly'
PYTHON_LANGUAGE = 'python'

CodeParser = {
    tab_size: 2,

    tab : function() {
        return " ".repeat(CodeParser.tab_size);
    },

    indent_lines: function (code) {
        var tab = CodeParser.tab();
        var code_lines = code.split("\n");
        for (var i in code_lines){
            code_lines[i] = tab + code_lines[i]
        }
        return code_lines.join("\n")
    },

    std_code_prepend: function (){
        var tab = CodeParser.tab();
        return `from manus.robot_arm import *


arm = RobotArm()

# Wait for one second ...
arm.wait(1000)
# ... then start execution
`
    },

    prepare_std_code: function(code){
        // code = CodeParser.indent_lines(code);
        return CodeParser.std_code_prepend()+code+"\n\n";
    }

}


Program = {

  init: function() {

    $.ajax('/data/blockly-toolbox.xml').done(function(toolbox) {

      var blocklyContainer = $("#blockly");
      var blocklyArea = document.getElementById('blockly-area');
      var blocklyDiv = document.getElementById('blockly-workspace');
      blocklyContainer.append(Blockly.Xml.domToText(toolbox));

      Program.blockly = {};

      Program.blockly.running_app = null;
      Program.blockly.workspace = Blockly.inject('blockly-workspace',{
          grid:{
              spacing: 25,
              length: 3,
              colour: '#ddd',
              snap: true
          },
          media: 'media/',
          toolbox: document.getElementById('blockly-toolbox'),
          zoom: {controls: true, wheel: true}
      });

      // Ace code editor init
      Program.python = {};
      Program.python.shown = false;
      Program.python.in_use = false;
      Program.python.ace = ace.edit("python-editor");
      Program.python.ace.setTheme("ace/theme/xcode");
      Program.python.ace.getSession().setMode("ace/mode/python");
      Program.python.ace.getSession().setTabSize(Program.python.tab_size);
      Program.python.ace.setOptions({
          enableBasicAutocompletion: true,
          enableLiveAutocompletion: true
      });
      Program.python.ace.completers.push({
        getCompletions: function(editor, session, pos, prefix, callback) {
          callback(null, [
              {value: "arm.move_joint(joint, angle)", score: 1000, meta: "Robot arm: Move one joint to specific angle."},
              {value: "arm.cotrol_gripper(cmd)", score: 1000, meta: "Robot arm: Open, half close or close gripper."},
              {value: "arm.wait(milisecs)", score: 1000, meta: "Robot arm: Wait/Sleep for milisecs."},
              {value: "arm.move_to_coordinates(p)", score: 1000, meta: "Robot arm: Move arm to coordinates/point."},
              {value: "arm.detect_blocks()", score: 1000, meta: "Robot arm: Detect all blocks."},
              {value: "arm.get_joint_position(joint)", score: 1000, meta: "Robot arm: Get joints position."},
              {value: "retrieve_coordinate_from_point(comp, point)", score: 1000, meta: "Robot arm: Retrieve coordinate from point/coordinates."},
              {value: "retrieve_color_from_block(block)", score: 1000, meta: "Robot arm: Retrieve color from block."}
          ]);
        }
      });

      // Register & call on resize function
      var onresize = function(e) {
          blocklyDiv.style.left = blocklyArea.offsetLeft + 'px';
          blocklyDiv.style.top = blocklyArea.offsetTop  + 'px';
          blocklyDiv.style.width = blocklyArea.offsetWidth + 'px';
          blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';
      };

      window.addEventListener('resize', onresize, false);
      //onresize();
      //Blockly.svgResize(workspace);

      var initial = true;

      // Register on show callback
      $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
          if (Program.blockly.workspace) {
              onresize();
              Blockly.svgResize(Program.blockly.workspace);
              if (initial) {
                initial = false;
                Program.pull();
              }
          }
      })

      Program.blockly.workspace.addChangeListener(function(event){
          if (event.type == Blockly.Events.CREATE || 
              event.type == Blockly.Events.DELETE || 
              event.type == Blockly.Events.CHANGE || 
              event.type == Blockly.Events.MOVE 
          ) {
              Program.push();
          }   
      });

      Program.python.ace.on("input", function() { Program.push(); } );

    });

    var logconsole = $("#console .content");
    logconsole.click(function() {

      if ($("#console").hasClass("terminated")) {
          Program.show();
      }

    });

    var run_button = $.manus.widgets.fancybutton({
      callback : function() {  
          Program.run();
          logconsole.empty();
      }, icon: "play", tooltip: "Run"
    }).addClass("button-codetime");

    var new_program = $.manus.widgets.fancybutton({
      callback : function() {  
          Interface.dialog("New document",
              "Do you want to start from scratch? Make sure that you have saved your work first.",
              {
                "Cancel" : function() { return true; },
                "Blocky" : function() {
                  Program.current(null, "blockly");
                  return true;
                },
                "Python" : function() {
                  Program.current(null, "python");
                  return true;
                }

              }
            );
      },
      icon: "file", 
      tooltip: "New program"
    }).addClass("button-codetime");

    var convert_python = $.manus.widgets.fancybutton({
      callback : function() {          

        Interface.confirmation("Convert to Python", "If you would like to keep your Blockly program make sure that you have saved it first.",
          function() {
            Program.python.ace.setValue(CodeParser.prepare_std_code(Blockly.Python.workspaceToCode(Program.blockly.workspace)));
            Program.python.shown = true;
            Program.python.in_use = true;
            Program.show("python");
          });
      },
      icon: "align-left", 
      tooltip: "Convert to Python"
    }).addClass("button-blockly");

    var stop_button = $.manus.widgets.fancybutton({
      callback : function() {  
          $.ajax('/api/apps?run=').done(function(data) {});
      }, icon: "stop", tooltip: "Stop"
    }).addClass("button-runtime").hide();

    var load_button = $.manus.widgets.fancybutton({
      callback : function() {  
          Program.load();
      }, icon: "open", tooltip: "Load"
    }).addClass("button-codetime");
{'fraction_non_alphanumeric': 0.14258540668036218, 'fraction_numerical': 0.006086890359887393, 'mean_word_length': 2.13026911169326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3821357', 'n_tokens_mistral': 3912, 'n_tokens_neox': 3718, 'n_words': 775}