Commit d6b647db authored by carlosperate's avatar carlosperate

Load XML file will only ask to confirm when blocks are present.

parent a1aad01d
...@@ -319,7 +319,7 @@ Ardublockly.changeIdeButtons = function(value) { ...@@ -319,7 +319,7 @@ Ardublockly.changeIdeButtons = function(value) {
Ardublockly.loadServerXmlFile = function(xmlFile) { Ardublockly.loadServerXmlFile = function(xmlFile) {
var loadXmlfileAccepted = function() { var loadXmlfileAccepted = function() {
// loadXmlBlockFile loads the file asynchronously and needs a callback // loadXmlBlockFile loads the file asynchronously and needs a callback
var loadXmlCallback = function(sucess) { var loadXmlCb = function(sucess) {
if (sucess) { if (sucess) {
Ardublockly.renderContent(); Ardublockly.renderContent();
} else { } else {
...@@ -330,18 +330,21 @@ Ardublockly.loadServerXmlFile = function(xmlFile) { ...@@ -330,18 +330,21 @@ Ardublockly.loadServerXmlFile = function(xmlFile) {
false); false);
} }
}; };
var callbackConnectionError = function() { var connectionErrorCb = function() {
Ardublockly.openNotConnectedModal(); Ardublockly.openNotConnectedModal();
}; };
Ardublockly.loadXmlBlockFile( Ardublockly.loadXmlBlockFile(xmlFile, loadXmlCb, connectionErrorCb);
xmlFile, loadXmlCallback, callbackConnectionError);
}; };
Ardublockly.alertMessage( if (Ardublockly.isWorkspaceEmpty()) {
'Load new blocks?', loadXmlfileAccepted();
'Loading a new XML file will replace the current blocks from the ' + } else {
'workspace.\nAre you sure you want to proceed?', Ardublockly.alertMessage(
true, loadXmlfileAccepted); 'Load new blocks?',
'Loading a new XML file will replace the current blocks from the ' +
'workspace.\nAre you sure you want to proceed?',
true, loadXmlfileAccepted);
}
}; };
/** /**
......
...@@ -64,51 +64,38 @@ Ardublockly.bindBlocklyEventListeners = function() { ...@@ -64,51 +64,38 @@ Ardublockly.bindBlocklyEventListeners = function() {
* Loads an XML file from the server and replaces the current blocks into the * Loads an XML file from the server and replaces the current blocks into the
* Blockly workspace. * Blockly workspace.
* @param {!string} xmlFile XML file path in a reachable server (no local path). * @param {!string} xmlFile XML file path in a reachable server (no local path).
* @param {!function} callbackFileLoaded Function to be called once the file is * @param {!function} cbSuccess Function to be called once the file is loaded.
* loaded. * @param {!function} cbError Function to be called if there is a connection
* @param {!function} callbackConectonError Function to be called if there is a * error to the XML server.
* connection error to the XML server.
*/ */
Ardublockly.loadXmlBlockFile = function(xmlFile, callbackFileLoaded, Ardublockly.loadXmlBlockFile = function(xmlFile, cbSuccess, cbError) {
callbackConectonError) {
// Create a an XML HTTP request // Create a an XML HTTP request
var request = Ardublockly.ajaxRequest(); var request = Ardublockly.ajaxRequest();
var requestCb = function() {
// If file run locally Internet explorer fails here if (request.readyState == 4) {
try { if (request.status == 200) {
request.open('GET', xmlFile, true); var success = Ardublockly.replaceBlocksfromXml(request.responseText);
} catch (e) { cbSuccess(success);
callbackConectonError(); } else {
} cbError();
}
// Once file is open, parse the XML into the workspace
request.onreadystatechange = function() {
if ((request.readyState == 4) && (request.status == 200)) {
var success = Ardublockly.replaceBlocksfromXml(request.responseText);
callbackFileLoaded(success);
} }
}; };
// If file run locally Chrome will fail here
try { try {
request.open('GET', xmlFile, true);
request.onreadystatechange = requestCb;
request.send(null); request.send(null);
} catch (e) { } catch (e) {
callbackConectonError(); cbError();
} }
}; };
/** /** @return {!string} Generated Arduino code from the Blockly workspace. */
* Generates the Arduino code from the Blockly workspace.
* @return {!string} Arduino code string.
*/
Ardublockly.generateArduino = function() { Ardublockly.generateArduino = function() {
return Blockly.Arduino.workspaceToCode(Ardublockly.workspace); return Blockly.Arduino.workspaceToCode(Ardublockly.workspace);
}; };
/** /** @return {!string} Generated XML code from the Blockly workspace. */
* Generates the XML DOM and returns it as a string.
* @return {!string} XML code string.
*/
Ardublockly.generateXml = function() { Ardublockly.generateXml = function() {
var xmlDom = Blockly.Xml.workspaceToDom(Ardublockly.workspace); var xmlDom = Blockly.Xml.workspaceToDom(Ardublockly.workspace);
var xmlText = Blockly.Xml.domToPrettyText(xmlDom); var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
...@@ -198,6 +185,11 @@ Ardublockly.discardAllBlocks = function() { ...@@ -198,6 +185,11 @@ Ardublockly.discardAllBlocks = function() {
} }
}; };
/** @return {!boolean} Indicates if the Blockly workspace has blocks. */
Ardublockly.isWorkspaceEmpty = function() {
return Ardublockly.workspace.getAllBlocks().length ? false : true;
};
/** /**
* Changes the Arduino board profile if different from the currently set one. * Changes the Arduino board profile if different from the currently set one.
* @param {string} newBoard Name of the new profile to set. * @param {string} newBoard Name of the new profile to set.
......
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