app/assets/htmls/gs-element-blockly.html in gobstones-blockly-0.19.2 vs app/assets/htmls/gs-element-blockly.html in gobstones-blockly-0.20.0

- old
+ new

@@ -39,11 +39,11 @@ <block type="Mover"></block> <block type="IrAlBorde"></block> <block type="VaciarTablero"></block> <block type="BOOM"></block> </category> - <category name="Procedimientos primitivos"> + <category name="Procedimientos primitivos" gbs_custom="PRIMITIVE_PROCEDURES"> </category> <category name="Mis procedimientos" custom="PROCEDURE_CALLS"> </category> <category name="Alternativas"> <block type="AlternativaSimple"></block> @@ -67,11 +67,11 @@ <category name="Expresiones primitivas"> <block type="hayBolitas"></block> <block type="puedeMover"></block> <block type="nroBolitas"></block> </category> - <category name="Funciones primitivas"> + <category name="Funciones primitivas" gbs_custom="PRIMITIVE_FUNCTIONS"> </category> <category name="Mis funciones" custom="FUNCTION_CALLS"> </category> <category name="Operadores"> <block type="OperadorNumerico"></block> @@ -5869,17 +5869,24 @@ 'stroke-width: 3px;', '}', ];</script> <script> - const BLOCKLY_PRIMITIVE_PROCEDURES = "Procedimientos primitivos"; - const BLOCKLY_PRIMITIVE_FUNCTIONS = "Funciones primitivas"; - Polymer({ is: 'gs-element-blockly', properties: { + primitiveProceduresCategoryName: { + type: String, + value: "Procedimientos primitivos" + }, + + primitiveFunctionsCategoryName: { + type: String, + value: "Funciones primitivas" + }, + /* * `primitiveProcedures` lista de definiciones de procedimientos primitivos. * Ej: ["Poner_FloresAl_"] Se transormara en un bloque con dos inputs inline, cuyo * codigo sera Poner_FloresAl_(arg1, arg2). */ @@ -6041,10 +6048,12 @@ const primitiveFunctions = this._getPrimitiveNames(this.primitiveFunctions); let tree = [] let xml = toolboxXML || this.$$('#toolbox').innerHTML; + xml = this._processCustomCategories(xml); + // TODO: Este parseo por strings es tremendo, y esa property `xml` de los nodos se usa en _toolboxBlockXML, _toolboxBlockXML, etc. Hay que hacer de vuelta todo lo de acciones primitivas. xml = xml.replace(/> *<\/category>/g, ">\n</category>"); let toolboxDefaultLines = xml.split("\n"); let ignore_last = false; @@ -6062,31 +6071,31 @@ } } else if(line.indexOf('<category') >= 0) { let m = line.match('name="([^"]*)"') if(m.length == 2 && ( - (m[1] == BLOCKLY_PRIMITIVE_PROCEDURES && primitiveProcedures && primitiveProcedures.length > 0) || - (m[1] == BLOCKLY_PRIMITIVE_FUNCTIONS && primitiveFunctions && primitiveFunctions.length > 0) || - (m[1] != BLOCKLY_PRIMITIVE_PROCEDURES && m[1] != BLOCKLY_PRIMITIVE_FUNCTIONS))) { + (m[1] == this.primitiveProceduresCategoryName && primitiveProcedures && primitiveProcedures.length > 0) || + (m[1] == this.primitiveFunctionsCategoryName && primitiveFunctions && primitiveFunctions.length > 0) || + (m[1] != this.primitiveProceduresCategoryName && m[1] != this.primitiveFunctionsCategoryName))) { parent.push({ type: 'category', name: m[1], child: [], xml: line }); stack.push(parent); parent = parent[parent.length - 1].child; - if(m[1] == BLOCKLY_PRIMITIVE_PROCEDURES) + if(m[1] == this.primitiveProceduresCategoryName) { for(var i in primitiveProcedures) { parent.push({ type: 'block', name: primitiveProcedures[i], }); } } - if(m[1] == BLOCKLY_PRIMITIVE_FUNCTIONS) + if(m[1] == this.primitiveFunctionsCategoryName) { for(var i in primitiveFunctions) { parent.push({ type: 'block', name: primitiveFunctions[i], @@ -6198,12 +6207,12 @@ addCustomActionsChilds(toolbox); } }; - addCustomActions("primitiveProcedures", BLOCKLY_PRIMITIVE_PROCEDURES); - addCustomActions("primitiveFunctions", BLOCKLY_PRIMITIVE_FUNCTIONS); + addCustomActions("primitiveProcedures", this.primitiveProceduresCategoryName); + addCustomActions("primitiveFunctions", this.primitiveFunctionsCategoryName); } else { toolbox = tree; } return this._toolboxTreeToXML(toolbox); @@ -6605,9 +6614,35 @@ }; } else { /*Do nothing for other types*/ } }); + }, + + _processCustomCategories(xml) { + const parsedXml = Blockly.Xml.textToDom(`<xml>${xml}</xml>`); + + const forEachNode = (node, action) => { + action(node); + node.childNodes.forEach((node) => forEachNode(node, action)); + } + + forEachNode(parsedXml, (node) => { + if (!node.getAttribute) return; + + const name = node.getAttribute("name"); + const custom = node.getAttribute("gbs_custom"); + + if (custom === "PRIMITIVE_PROCEDURES") { + this.primitiveProceduresCategoryName = name; + } + + if (custom === "PRIMITIVE_FUNCTIONS") { + this.primitiveFunctionsCategoryName = name; + } + }); + + return Blockly.Xml.domToText(parsedXml); }, _mapToolboxTree: function(toolboxTree, f){ var resultingList = []; for (var i in toolboxTree){ \ No newline at end of file