Commit 6a30d3aa authored by carlosperate's avatar carlosperate

Update ardublockly webapp js for latest blockly updates.

Blockly implementation of multiple workspace has changed the API, this commit updates the ardublockly and ardublockly classic javascript.
parent 06cfc2f8
...@@ -11,10 +11,12 @@ ...@@ -11,10 +11,12 @@
*/ */
var ArduinoMaterial = ArduinoMaterial || {}; var ArduinoMaterial = ArduinoMaterial || {};
/** /**
* Initialize function for Ardublockly on page load. * Initialize function for Ardublockly on page load.
*/ */
window.addEventListener('load', function() { window.addEventListener('load', function load(event) {
window.removeEventListener('load', load, false);
// Inject Blockly into content_blocks // Inject Blockly into content_blocks
ArduinoMaterial.injectBlockly( ArduinoMaterial.injectBlockly(
document.getElementById('content_blocks'), 'ardublockly_toolbox.xml'); document.getElementById('content_blocks'), 'ardublockly_toolbox.xml');
...@@ -102,7 +104,7 @@ ArduinoMaterial.bindBlocklyEventListeners_ = function() { ...@@ -102,7 +104,7 @@ ArduinoMaterial.bindBlocklyEventListeners_ = function() {
if (ArduinoMaterial.BLOCKLY_INJECTED == false) { if (ArduinoMaterial.BLOCKLY_INJECTED == false) {
setTimeout(ArduinoMaterial.bindBlocklyEventListeners_, 50); setTimeout(ArduinoMaterial.bindBlocklyEventListeners_, 50);
} else { } else {
Blockly.addChangeListener(ArduinoMaterial.renderContent); ArduinoMaterial.workspace.addChangeListener(ArduinoMaterial.renderContent);
} }
}; };
...@@ -161,7 +163,7 @@ ArduinoMaterial.loadUserXmlFile = function() { ...@@ -161,7 +163,7 @@ ArduinoMaterial.loadUserXmlFile = function() {
var selectFileDom = document.createElement('INPUT'); var selectFileDom = document.createElement('INPUT');
selectFileDom.type = 'file'; selectFileDom.type = 'file';
selectFileDom.id = 'select_file'; selectFileDom.id = 'select_file';
selectFileDom.style = 'display: none'; selectFileDom.style.display = 'none';
document.body.appendChild(selectFileDom); document.body.appendChild(selectFileDom);
selectFile = document.getElementById('select_file'); selectFile = document.getElementById('select_file');
selectFile.addEventListener('change', parseInputXMLfile, false); selectFile.addEventListener('change', parseInputXMLfile, false);
...@@ -419,7 +421,7 @@ ArduinoMaterial.toogleToolbox = function() { ...@@ -419,7 +421,7 @@ ArduinoMaterial.toogleToolbox = function() {
// showToolbox() takes a callback function as its second argument // showToolbox() takes a callback function as its second argument
ArduinoMaterial.showToolbox(false, ArduinoMaterial.showToolbox(false,
function() { ArduinoMaterial.showToolboxButtonState(false); }); function() { ArduinoMaterial.showToolboxButtonState(false); });
Blockly.mainWorkspace.toolbox_.flyout_.hide(); ArduinoMaterial.workspace.toolbox_.flyout_.hide();
} else { } else {
ArduinoMaterial.showToolboxButtonState(true); ArduinoMaterial.showToolboxButtonState(true);
ArduinoMaterial.showToolbox(true); ArduinoMaterial.showToolbox(true);
......
...@@ -11,6 +11,12 @@ ...@@ -11,6 +11,12 @@
*/ */
var ArduinoMaterial = ArduinoMaterial || {}; var ArduinoMaterial = ArduinoMaterial || {};
/**
* Blockly's main workspace.
* @type Blockly.WorkspaceSvg
*/
ArduinoMaterial.workspace = null;
/** /**
* Public variable that indicates if Blockly has been injected. * Public variable that indicates if Blockly has been injected.
* @type {!boolean} * @type {!boolean}
...@@ -37,7 +43,7 @@ ArduinoMaterial.injectBlockly = function(blocklyEl, toolboxPath) { ...@@ -37,7 +43,7 @@ ArduinoMaterial.injectBlockly = function(blocklyEl, toolboxPath) {
// Once file is open, inject blockly into element with the toolbox string // Once file is open, inject blockly into element with the toolbox string
request.onreadystatechange = function() { request.onreadystatechange = function() {
if ( (request.readyState == 4) && (request.status == 200) ) { if ( (request.readyState == 4) && (request.status == 200) ) {
Blockly.inject(blocklyEl, { ArduinoMaterial.workspace = Blockly.inject(blocklyEl, {
collapse: true, collapse: true,
comments: true, comments: true,
disable: true, disable: true,
...@@ -95,7 +101,7 @@ ArduinoMaterial.loadXmlBlockFile = ...@@ -95,7 +101,7 @@ ArduinoMaterial.loadXmlBlockFile =
* @return {!string} Arduino code string. * @return {!string} Arduino code string.
*/ */
ArduinoMaterial.generateArduino = function() { ArduinoMaterial.generateArduino = function() {
return Blockly.Arduino.workspaceToCode(); return Blockly.Arduino.workspaceToCode(ArduinoMaterial.workspace);
}; };
/** /**
...@@ -103,7 +109,7 @@ ArduinoMaterial.generateArduino = function() { ...@@ -103,7 +109,7 @@ ArduinoMaterial.generateArduino = function() {
* @return {!string} XML code string. * @return {!string} XML code string.
*/ */
ArduinoMaterial.generateXml = function() { ArduinoMaterial.generateXml = function() {
var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace); var xmlDom = Blockly.Xml.workspaceToDom(ArduinoMaterial.workspace);
var xmlText = Blockly.Xml.domToPrettyText(xmlDom); var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
return xmlText; return xmlText;
}; };
...@@ -121,7 +127,7 @@ ArduinoMaterial.replaceBlocksfromXml = function(blocksXml) { ...@@ -121,7 +127,7 @@ ArduinoMaterial.replaceBlocksfromXml = function(blocksXml) {
} catch (e) { } catch (e) {
return false; return false;
} }
Blockly.mainWorkspace.clear(); ArduinoMaterial.workspace.clear();
var sucess = false; var sucess = false;
if (xmlDom) { if (xmlDom) {
sucess = ArduinoMaterial.loadBlocksfromXmlDom(xmlDom); sucess = ArduinoMaterial.loadBlocksfromXmlDom(xmlDom);
...@@ -136,7 +142,7 @@ ArduinoMaterial.replaceBlocksfromXml = function(blocksXml) { ...@@ -136,7 +142,7 @@ ArduinoMaterial.replaceBlocksfromXml = function(blocksXml) {
*/ */
ArduinoMaterial.loadBlocksfromXmlDom = function(blocksXmlDom) { ArduinoMaterial.loadBlocksfromXmlDom = function(blocksXmlDom) {
try { try {
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, blocksXmlDom); Blockly.Xml.domToWorkspace(ArduinoMaterial.workspace, blocksXmlDom);
} catch (e) { } catch (e) {
return false; return false;
} }
...@@ -169,7 +175,7 @@ ArduinoMaterial.showToolbox = function(show, callback) { ...@@ -169,7 +175,7 @@ ArduinoMaterial.showToolbox = function(show, callback) {
* Discard all blocks from the workspace. * Discard all blocks from the workspace.
*/ */
ArduinoMaterial.discard = function() { ArduinoMaterial.discard = function() {
var blockCount = Blockly.mainWorkspace.getAllBlocks().length; var blockCount = ArduinoMaterial.workspace.getAllBlocks().length;
if (blockCount == 1) { if (blockCount == 1) {
Blockly.mainWorkspace.clear(); Blockly.mainWorkspace.clear();
ArduinoMaterial.renderContent(); ArduinoMaterial.renderContent();
...@@ -187,7 +193,7 @@ ArduinoMaterial.discard = function() { ...@@ -187,7 +193,7 @@ ArduinoMaterial.discard = function() {
}; };
/** /**
* Creates an AJAX request * Creates an AJAX request.
* @return An XML HTTP Request * @return An XML HTTP Request
*/ */
ArduinoMaterial.ajaxRequest = function() { ArduinoMaterial.ajaxRequest = function() {
......
...@@ -117,9 +117,9 @@ ArduinoMaterial.resizeBlocklyWorkspace = function() { ...@@ -117,9 +117,9 @@ ArduinoMaterial.resizeBlocklyWorkspace = function() {
// Sets the toolbox toggle button width to that of the toolbox // Sets the toolbox toggle button width to that of the toolbox
if ( ArduinoMaterial.isToolboxVisible() && if ( ArduinoMaterial.isToolboxVisible() &&
Blockly.mainWorkspace.toolbox_.width ) { ArduinoMaterial.workspace.toolbox_.width ) {
// For some reason normal set style and getElementById didn't work // For some reason normal set style and getElementById didn't work
$('#button_toggle_toolbox').width(Blockly.mainWorkspace.toolbox_.width); $('#button_toggle_toolbox').width(ArduinoMaterial.workspace.toolbox_.width);
} }
}; };
......
...@@ -13,6 +13,12 @@ ...@@ -13,6 +13,12 @@
*/ */
var ArduinoClassic = {}; var ArduinoClassic = {};
/**
* Blockly's main workspace.
* @type Blockly.WorkspaceSvg
*/
ArduinoClassic.workspace = null;
/** /**
* List of tab names. * List of tab names.
* @private * @private
...@@ -47,7 +53,15 @@ ArduinoClassic.tabClick = function(clickedName) { ...@@ -47,7 +53,15 @@ ArduinoClassic.tabClick = function(clickedName) {
ArduinoClassic.selected = clickedName; ArduinoClassic.selected = clickedName;
document.getElementById('tab_' + clickedName).className = 'tabon'; document.getElementById('tab_' + clickedName).className = 'tabon';
document.getElementById('content_' + clickedName).style.display = 'block'; document.getElementById('content_' + clickedName).style.display = 'block';
// This is a workaround, something about the html layout causes the blocks to
// compress when the block tab is shown after it has been hidden
if (clickedName === 'blocks' && ArduinoClassic.workspace) {
ArduinoClassic.workspace.setVisible(false);
ArduinoClassic.workspace.setVisible(true);
}
ArduinoClassic.renderContent(); ArduinoClassic.renderContent();
Blockly.fireUiEvent(window, 'resize'); Blockly.fireUiEvent(window, 'resize');
}; };
...@@ -59,12 +73,12 @@ ArduinoClassic.renderContent = function() { ...@@ -59,12 +73,12 @@ ArduinoClassic.renderContent = function() {
// Initialize the panel // Initialize the panel
if (content.id == 'content_xml') { if (content.id == 'content_xml') {
var xmlTextarea = document.getElementById('content_xml'); var xmlTextarea = document.getElementById('content_xml');
var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace); var xmlDom = Blockly.Xml.workspaceToDom(ArduinoClassic.workspace);
var xmlText = Blockly.Xml.domToPrettyText(xmlDom); var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
xmlTextarea.value = xmlText; xmlTextarea.value = xmlText;
xmlTextarea.focus(); xmlTextarea.focus();
} else if (content.id == 'content_arduino') { } else if (content.id == 'content_arduino') {
var code = Blockly.Arduino.workspaceToCode(); var code = Blockly.Arduino.workspaceToCode(ArduinoClassic.workspace);
content.textContent = code; content.textContent = code;
if (typeof prettyPrintOne == 'function') { if (typeof prettyPrintOne == 'function') {
code = content.innerHTML; code = content.innerHTML;
...@@ -99,9 +113,9 @@ ArduinoClassic.init = function() { ...@@ -99,9 +113,9 @@ ArduinoClassic.init = function() {
el.style.width = (2 * bBox.width - el.offsetWidth) + 'px'; el.style.width = (2 * bBox.width - el.offsetWidth) + 'px';
} }
// Make the 'Blocks' tab line up with the toolbox. // Make the 'Blocks' tab line up with the toolbox.
if (Blockly.mainWorkspace.toolbox_.width) { if (ArduinoClassic.workspace.toolbox_.width) {
document.getElementById('tab_blocks').style.minWidth = document.getElementById('tab_blocks').style.minWidth =
(Blockly.mainWorkspace.toolbox_.width - 38) + 'px'; (ArduinoClassic.workspace.toolbox_.width - 38) + 'px';
// Account for the 19 pixel margin and on each side. // Account for the 19 pixel margin and on each side.
} }
}; };
...@@ -167,14 +181,14 @@ ArduinoClassic.openSettings = function() { ...@@ -167,14 +181,14 @@ ArduinoClassic.openSettings = function() {
'directories=no, titlebar=no, toolbar=no, location=no, status=no, ' + 'directories=no, titlebar=no, toolbar=no, location=no, status=no, ' +
'menubar=no, scrollbars=yes, resizable=yes, top=' + top + ', ' + 'menubar=no, scrollbars=yes, resizable=yes, top=' + top + ', ' +
'left=' + left + ', width=' + width + ', height=' + height + ''); 'left=' + left + ', width=' + width + ', height=' + height + '');
} };
/** /**
* Send the Arduino Code to the ArduServerCompiler to process. * Send the Arduino Code to the ArduServerCompiler to process.
*/ */
ArduinoClassic.loadToArduino = function() { ArduinoClassic.loadToArduino = function() {
ArduServerCompiler.sendSketchToServer( ArduServerCompiler.sendSketchToServer(
Blockly.Arduino.workspaceToCode(), Blockly.Arduino.workspaceToCode(ArduinoClassic.workspace),
ArduinoClassic.loadToArduinoReturn); ArduinoClassic.loadToArduinoReturn);
}; };
...@@ -200,10 +214,10 @@ ArduinoClassic.loadToArduinoReturn = function(data_back_el) { ...@@ -200,10 +214,10 @@ ArduinoClassic.loadToArduinoReturn = function(data_back_el) {
* Discard all blocks from the workspace. * Discard all blocks from the workspace.
*/ */
ArduinoClassic.discard = function() { ArduinoClassic.discard = function() {
var count = Blockly.mainWorkspace.getAllBlocks().length; var count = ArduinoClassic.workspace.getAllBlocks().length;
var message = 'Delete all ' + count + ' blocks?'; var message = 'Delete all ' + count + ' blocks?';
if (count < 2 || window.confirm(message)) { if (count < 2 || window.confirm(message)) {
Blockly.mainWorkspace.clear(); ArduinoClassic.workspace.clear();
window.location.hash = ''; window.location.hash = '';
} }
ArduinoClassic.renderContent(); ArduinoClassic.renderContent();
...@@ -237,7 +251,8 @@ ArduinoClassic.peekCode = function(visible) { ...@@ -237,7 +251,8 @@ ArduinoClassic.peekCode = function(visible) {
code_peek_content.style.display = 'inline-block'; code_peek_content.style.display = 'inline-block';
// Regenerate arduino code and ensure every click does as well // Regenerate arduino code and ensure every click does as well
ArduinoClassic.renderArduinoPeekCode(); ArduinoClassic.renderArduinoPeekCode();
Blockly.addChangeListener(ArduinoClassic.renderArduinoPeekCode); ArduinoClassic.workspace.addChangeListener(
ArduinoClassic.renderArduinoPeekCode);
} else { } else {
ArduinoClassic.peek_code_ = false; ArduinoClassic.peek_code_ = false;
peek_code_button.className = 'button_text'; peek_code_button.className = 'button_text';
...@@ -289,7 +304,8 @@ ArduinoClassic.sideContent = function(visible) { ...@@ -289,7 +304,8 @@ ArduinoClassic.sideContent = function(visible) {
*/ */
ArduinoClassic.renderArduinoPeekCode = function() { ArduinoClassic.renderArduinoPeekCode = function() {
var code_peak_pre = document.getElementById('arduino_pre'); var code_peak_pre = document.getElementById('arduino_pre');
code_peak_pre.textContent = Blockly.Arduino.workspaceToCode(); code_peak_pre.textContent = Blockly.Arduino.workspaceToCode(
ArduinoClassic.workspace);
if (typeof prettyPrintOne == 'function') { if (typeof prettyPrintOne == 'function') {
code_peak_pre.innerHTML = prettyPrintOne(code_peak_pre.innerHTML, 'cpp'); code_peak_pre.innerHTML = prettyPrintOne(code_peak_pre.innerHTML, 'cpp');
} }
...@@ -330,7 +346,7 @@ ArduinoClassic.injectBlockly = function(blockly_el, toolbox_path) { ...@@ -330,7 +346,7 @@ ArduinoClassic.injectBlockly = function(blockly_el, toolbox_path) {
// Once file is open, inject blockly into element with the toolbox string // Once file is open, inject blockly into element with the toolbox string
request.onreadystatechange = function() { request.onreadystatechange = function() {
if ( (request.readyState == 4) && (request.status == 200) ) { if ( (request.readyState == 4) && (request.status == 200) ) {
Blockly.inject(blockly_el, { ArduinoClassic.workspace = Blockly.inject(blockly_el, {
collapse: true, collapse: true,
comments: true, comments: true,
disable: true, disable: true,
...@@ -372,7 +388,7 @@ ArduinoClassic.loadUserXmlFile = function() { ...@@ -372,7 +388,7 @@ ArduinoClassic.loadUserXmlFile = function() {
var select_file_dom = document.createElement('INPUT'); var select_file_dom = document.createElement('INPUT');
select_file_dom.type = 'file'; select_file_dom.type = 'file';
select_file_dom.id = 'select_file'; select_file_dom.id = 'select_file';
select_file_dom.style = 'display: none'; select_file_dom.style.display = 'none';
document.body.appendChild(select_file_dom); document.body.appendChild(select_file_dom);
select_file = document.getElementById('select_file'); select_file = document.getElementById('select_file');
select_file.addEventListener('change', parseInputXMLfile, false); select_file.addEventListener('change', parseInputXMLfile, false);
...@@ -402,8 +418,8 @@ ArduinoClassic.replaceBlocksfromXml = function(blocks_xml) { ...@@ -402,8 +418,8 @@ ArduinoClassic.replaceBlocksfromXml = function(blocks_xml) {
} }
} }
if (xmlDom) { if (xmlDom) {
Blockly.mainWorkspace.clear(); ArduinoClassic.workspace.clear();
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xmlDom); Blockly.Xml.domToWorkspace(ArduinoClassic.workspace, xmlDom);
} }
return success; return success;
}; };
...@@ -414,7 +430,7 @@ ArduinoClassic.replaceBlocksfromXml = function(blocks_xml) { ...@@ -414,7 +430,7 @@ ArduinoClassic.replaceBlocksfromXml = function(blocks_xml) {
*/ */
ArduinoClassic.saveXmlFile = function() { ArduinoClassic.saveXmlFile = function() {
// Generate XML // Generate XML
var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace); var xmlDom = Blockly.Xml.workspaceToDom(ArduinoClassic.workspace);
var xmlText = Blockly.Xml.domToPrettyText(xmlDom); var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
// Create blob // Create blob
var blob = new Blob( var blob = new Blob(
......
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="img/favicon.ico"> <link rel="icon" href="img/favicon.ico">
<meta name="theme-color" content="#00979C"> <meta name="theme-color" content="#00878F">
<title>Ardublockly</title> <title>Ardublockly</title>
<!-- jQuery --> <!-- jQuery -->
<script src="js_libs/jquery-2.1.3.min.js"></script> <script src="js_libs/jquery-2.1.3.min.js"></script>
......
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