Commit 7d1f2c17 authored by carlosperate's avatar carlosperate

Allow additional blocks to extend the Ardublockly functionality.

parent 192bb341
...@@ -689,19 +689,52 @@ Ardublockly.importExtraBlocks = function() { ...@@ -689,19 +689,52 @@ Ardublockly.importExtraBlocks = function() {
if (jsonDataObj === null) return Ardublockly.openNotConnectedModal(); if (jsonDataObj === null) return Ardublockly.openNotConnectedModal();
if (jsonDataObj.categories !== undefined) { if (jsonDataObj.categories !== undefined) {
var head = document.getElementsByTagName('head')[0]; var head = document.getElementsByTagName('head')[0];
for (var categoryDir in jsonDataObj.categories) { for (var catDir in jsonDataObj.categories) {
var blocksJsLoad = document.createElement('script'); var blocksJsLoad = document.createElement('script');
blocksJsLoad.src = '../blocks/' + categoryDir + '/blocks.js'; blocksJsLoad.src = '../blocks/' + catDir + '/blocks.js';
head.appendChild(blocksJsLoad); head.appendChild(blocksJsLoad);
var blocksLangJsLoad = document.createElement('script'); var blocksLangJsLoad = document.createElement('script');
blocksLangJsLoad.src = '../blocks/' + categoryDir + '/msg/' + blocksLangJsLoad.src = '../blocks/' + catDir + '/msg/' + 'messages.js';
//'lang/' + Ardublockly.LANG + '.js'; //'lang/' + Ardublockly.LANG + '.js';
'messages.js';
head.appendChild(blocksLangJsLoad); head.appendChild(blocksLangJsLoad);
var blocksGeneratorJsLoad = document.createElement('script'); var blocksGeneratorJsLoad = document.createElement('script');
blocksGeneratorJsLoad.src = '../blocks/' + categoryDir + blocksGeneratorJsLoad.src = '../blocks/' + catDir +
'/generator_arduino.js'; '/generator_arduino.js';
head.appendChild(blocksGeneratorJsLoad); head.appendChild(blocksGeneratorJsLoad);
// Check if the blocks add additional Ardublockly functionality
var extensions = jsonDataObj.categories[catDir].extensions
if (extensions) {
for (var i = 0; i < extensions.length; i++) {
var blockExtensionJsLoad = document.createElement('script');
blockExtensionJsLoad.src = '../blocks/' + catDir + '/extensions.js';
head.appendChild(blockExtensionJsLoad);
// Add function to scheduler as lazy loading has to complete first
setTimeout(function(category, extension) {
var extensionNamespaces = extension.split('.');
var extensionCall = window;
var invalidFunc = false;
for (var j = 0; j < extensionNamespaces.length; j++) {
extensionCall = extensionCall[extensionNamespaces[j]];
if (extensionCall === undefined) {
invalidFunc = true;
break;
}
}
if (typeof extensionCall != 'function') {
invalidFunc = true;
}
if (invalidFunc) {
throw 'Blocks ' + category.categoryName + ' extension "' +
extension + '" is not a valid function.';
} else {
extensionCall();
}
}, 800, jsonDataObj.categories[catDir], extensions[i]);
}
}
} }
} }
}; };
...@@ -721,21 +754,21 @@ Ardublockly.openExtraCategoriesSelect = function() { ...@@ -721,21 +754,21 @@ Ardublockly.openExtraCategoriesSelect = function() {
if (jsonDataObj === null) return Ardublockly.openNotConnectedModal(); if (jsonDataObj === null) return Ardublockly.openNotConnectedModal();
var htmlContent = document.createElement('div'); var htmlContent = document.createElement('div');
if (jsonDataObj.categories !== undefined) { if (jsonDataObj.categories !== undefined) {
for (var categoryDir in jsonDataObj.categories) { for (var catDir in jsonDataObj.categories) {
// Function required to maintain each loop variable scope separated // Function required to maintain each loop variable scope separated
(function(cat) { (function(cat) {
var clickBind = function(tickValue) { var clickBind = function(tickValue) {
if (tickValue) { if (tickValue) {
var catDom = (new DOMParser()).parseFromString( var catDom = (new DOMParser()).parseFromString(
cat.toolbox, 'text/xml'); cat.toolbox.join(''), 'text/xml').firstChild;
Ardublockly.addToolboxCategory(cat.categoryName, catDom); Ardublockly.addToolboxCategory(cat.toolboxName, catDom);
} else { } else {
Ardublockly.removeToolboxCategory(cat.categoryName); Ardublockly.removeToolboxCategory(cat.toolboxName);
} }
}; };
htmlContent.appendChild(Ardublockly.createExtraBlocksCatHtml( htmlContent.appendChild(Ardublockly.createExtraBlocksCatHtml(
cat.name, cat.description, clickBind)); cat.categoryName, cat.description, clickBind));
})(jsonDataObj.categories[categoryDir]); })(jsonDataObj.categories[catDir]);
} }
} }
Ardublockly.openAdditionalBlocksModal(htmlContent); Ardublockly.openAdditionalBlocksModal(htmlContent);
......
...@@ -225,12 +225,10 @@ Ardublockly.updateToolboxLanguage = function() { ...@@ -225,12 +225,10 @@ Ardublockly.updateToolboxLanguage = function() {
* @param {!Element} categoryDom Toolbox category to add add the end of tree. * @param {!Element} categoryDom Toolbox category to add add the end of tree.
*/ */
Ardublockly.addToolboxCategory = function(categoryTitle, categoryDom) { Ardublockly.addToolboxCategory = function(categoryTitle, categoryDom) {
var categoryNode = document.createElement('category'); categoryDom.id = 'cat' + categoryTitle.replace(/\s+/g, '');
categoryNode.id = 'cat' + categoryTitle.replace(/\s+/g, ''); categoryDom.setAttribute('name', categoryTitle);
categoryNode.setAttribute('name', categoryTitle);
categoryNode.appendChild(categoryDom.firstChild);
Ardublockly.xmlTree.appendChild(document.createElement('sep')); Ardublockly.xmlTree.appendChild(document.createElement('sep'));
Ardublockly.xmlTree.appendChild(categoryNode); Ardublockly.xmlTree.appendChild(categoryDom);
Ardublockly.workspace.updateToolbox(Ardublockly.xmlTree); Ardublockly.workspace.updateToolbox(Ardublockly.xmlTree);
}; };
......
...@@ -94,7 +94,14 @@ Ardublockly.getJsonData = function(fileLocation, jsonDataCb) { ...@@ -94,7 +94,14 @@ Ardublockly.getJsonData = function(fileLocation, jsonDataCb) {
var requestCb = function() { var requestCb = function() {
if (request.readyState == 4) { if (request.readyState == 4) {
if (request.status == 200) { if (request.status == 200) {
jsonDataCb(JSON.parse(request.responseText)); var jsonObj = null;
try {
jsonObj = JSON.parse(request.responseText);
} catch(e) {
console.error('Incorrectly formatted JSON data from ' + fileLocation);
throw e;
}
jsonDataCb(jsonObj);
} else { } else {
jsonDataCb(null); jsonDataCb(null);
} }
......
{ {
"categories": { "categories": {
"test": { "test": {
"name": "Test blocks", "categoryName": "Test blocks",
"description": "Test blocks to demonstrate the feature.", "description": "Test blocks to demonstrate the feature.",
"categoryName": "Test",
"languages": ["en", "es"], "languages": ["en", "es"],
"toolboxName": "Test",
"toolbox": [ "toolbox": [
"<block type=\"ardublockly_name_top\">", "<category>",
"</block>" " <block type=\"ardublockly_name_top\"></block>",
"</category>"
] ]
}, },
"test_2": { "test_2": {
"name": "Another Test blocks Section 2", "categoryName": "Another Test blocks Section 2",
"description": "Second test blocks to demonstrate the feature.", "description": "Second test blocks to demonstrate the feature.",
"categoryName": "Test 2",
"languages": ["en", "es"], "languages": ["en", "es"],
"toolboxName": "Test 2",
"toolbox": [ "toolbox": [
"<block type=\"ardublockly_name_bottom\">", "<category>",
"</block>" " <block type=\"ardublockly_name_bottom\"></block>",
"</category>"
] ]
} }
} }
......
{ {
"name": "Test blocks", "categoryName": "Test blocks",
"description": "Test blocks to demonstrate the feature.", "description": "Test blocks to demonstrate the feature.",
"categoryName": "Test",
"languages": ["en", "es"], "languages": ["en", "es"],
"toolboxName": "Test",
"toolbox": [ "toolbox": [
"<block type=\"ardublockly_name_top\">", "<category>",
"</block>" " <block type=\"ardublockly_name_top\"></block>",
"</category>"
] ]
} }
{ {
"name": "Another Test blocks Section 2", "categoryName": "Another Test blocks Section 2",
"description": "Second test blocks to demonstrate the feature.", "description": "Second test blocks to demonstrate the feature.",
"categoryName": "Test 2",
"languages": ["en", "es"], "languages": ["en", "es"],
"toolboxName": "Test 2",
"toolbox": [ "toolbox": [
"<block type=\"ardublockly_name_bottom\">", "<category>",
"</block>" " <block type=\"ardublockly_name_bottom\"></block>",
"</category>"
] ]
} }
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