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