Commit efaae05b authored by carlosperate's avatar carlosperate

Abstracted the save file mechanism from the file data assignment.

parent 0dd88a65
...@@ -48,7 +48,7 @@ Ardublockly.init = function() { ...@@ -48,7 +48,7 @@ Ardublockly.init = function() {
Ardublockly.bindActionFunctions = function() { Ardublockly.bindActionFunctions = function() {
// Navigation buttons // Navigation buttons
Ardublockly.bindClick_('button_load', Ardublockly.loadUserXmlFile); Ardublockly.bindClick_('button_load', Ardublockly.loadUserXmlFile);
Ardublockly.bindClick_('button_save', Ardublockly.saveXmlFileAs); Ardublockly.bindClick_('button_save', Ardublockly.saveXmlFile);
Ardublockly.bindClick_('button_delete', Ardublockly.discardAllBlocks); Ardublockly.bindClick_('button_delete', Ardublockly.discardAllBlocks);
// Side menu buttons, they also close the side menu // Side menu buttons, they also close the side menu
...@@ -57,7 +57,7 @@ Ardublockly.bindActionFunctions = function() { ...@@ -57,7 +57,7 @@ Ardublockly.bindActionFunctions = function() {
$('.button-collapse').sideNav('hide'); $('.button-collapse').sideNav('hide');
}); });
Ardublockly.bindClick_('menu_save', function() { Ardublockly.bindClick_('menu_save', function() {
Ardublockly.saveXmlFileAs(); Ardublockly.saveXmlFile();
$('.button-collapse').sideNav('hide'); $('.button-collapse').sideNav('hide');
}); });
Ardublockly.bindClick_('menu_delete', function() { Ardublockly.bindClick_('menu_delete', function() {
...@@ -364,12 +364,10 @@ Ardublockly.loadUserXmlFile = function() { ...@@ -364,12 +364,10 @@ Ardublockly.loadUserXmlFile = function() {
* Creates an XML file containing the blocks from the Blockly workspace and * Creates an XML file containing the blocks from the Blockly workspace and
* prompts the users to save it into their local file system. * prompts the users to save it into their local file system.
*/ */
Ardublockly.saveXmlFileAs = function() { Ardublockly.saveXmlFile = function() {
var xmlName = document.getElementById('sketch_name').value; Ardublockly.saveTextFileAs(
var blob = new Blob( document.getElementById('sketch_name').value + '.xml',
[Ardublockly.generateXml()], Ardublockly.generateXml());
{type: 'text/plain;charset=utf-8'});
saveAs(blob, xmlName + '.xml');
}; };
/** /**
...@@ -377,12 +375,23 @@ Ardublockly.saveXmlFileAs = function() { ...@@ -377,12 +375,23 @@ Ardublockly.saveXmlFileAs = function() {
* the Blockly workspace and prompts the users to save it into their local file * the Blockly workspace and prompts the users to save it into their local file
* system. * system.
*/ */
Ardublockly.saveSketchFileAs = function() { Ardublockly.saveSketchFile = function() {
var sketchName = document.getElementById('sketch_name').value; Ardublockly.saveTextFileAs(
document.getElementById('sketch_name').value + '.ino',
ArduBlockly.generateArduino());
};
/**
* Creates an text file with the input content and files name, and prompts the
* users to save it into their local file system.
* @param {!string} fileName Name for the file to be saved.
* @param {!string} content Text datd to be saved in to the file.
*/
Ardublockly.saveTextFileAs = function(fileName, content) {
var blob = new Blob( var blob = new Blob(
[Ardublockly.generateArduino()], [content],
{type: 'text/plain;charset=utf-8'}); {type: 'text/plain;charset=utf-8'});
saveAs(blob, sketchName + '.ino'); saveAs(blob, fileName);
}; };
/** Prepares and opens the settings modal. */ /** Prepares and opens the settings modal. */
...@@ -402,6 +411,7 @@ Ardublockly.populateSettings = function() { ...@@ -402,6 +411,7 @@ Ardublockly.populateSettings = function() {
ArdublocklyServer.requestArduinoBoards(Ardublockly.setArduinoBoardsHtml); ArdublocklyServer.requestArduinoBoards(Ardublockly.setArduinoBoardsHtml);
ArdublocklyServer.requestSerialPorts(Ardublockly.setSerialPortsHtml); ArdublocklyServer.requestSerialPorts(Ardublockly.setSerialPortsHtml);
ArdublocklyServer.requestIdeOptions(Ardublockly.setIdeHtml); ArdublocklyServer.requestIdeOptions(Ardublockly.setIdeHtml);
// Language menu only set on page load within Ardublockly.initLanguage()
}; };
/** /**
...@@ -562,15 +572,12 @@ Ardublockly.setIdeSettings = function(e, preset) { ...@@ -562,15 +572,12 @@ Ardublockly.setIdeSettings = function(e, preset) {
*/ */
Ardublockly.sendCode = function() { Ardublockly.sendCode = function() {
Ardublockly.largeIdeButtonSpinner(true); Ardublockly.largeIdeButtonSpinner(true);
ArdublocklyServer.sendSketchToServer(
Ardublockly.generateArduino(), Ardublockly.sendCodeReturn);
};
/** /**
* Receives the IDE data back to be displayed and stops spinner. * Receives the IDE data back to be displayed and stops spinner.
* @param {element} jsonResponse JSON data coming back from the server. * @param {element} jsonResponse JSON data coming back from the server.
*/ */
Ardublockly.sendCodeReturn = function(jsonResponse) { var sendCodeReturn = function(jsonResponse) {
Ardublockly.largeIdeButtonSpinner(false); Ardublockly.largeIdeButtonSpinner(false);
if (jsonResponse != null) { if (jsonResponse != null) {
var dataBack = ArdublocklyServer.createElementFromJson(jsonResponse); var dataBack = ArdublocklyServer.createElementFromJson(jsonResponse);
...@@ -579,6 +586,10 @@ Ardublockly.sendCodeReturn = function(jsonResponse) { ...@@ -579,6 +586,10 @@ Ardublockly.sendCodeReturn = function(jsonResponse) {
// If the element is Null, then Ardublockly server is not running // If the element is Null, then Ardublockly server is not running
Ardublockly.openNotConnectedModal(); Ardublockly.openNotConnectedModal();
} }
};
ArdublocklyServer.sendSketchToServer(
Ardublockly.generateArduino(), sendCodeReturn);
}; };
/** Populate the workspace blocks with the XML written in the XML text area. */ /** Populate the workspace blocks with the XML written in the XML text area. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment