/// <summary>
        /// Generates the blockly definition.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        internal string GenerateBlocklyDefinition(TypeArgumentBase type)
        {
            if (type.ConvertibleToBlocklyType())
            {
                return(null);
            }

            var strDef = GenerateDefinitionString(type);
            var strJS  = GenerateJSstring(type);

            return(strDef + strJS);
        }
        /// <summary>
        /// Generates the javascript string.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public string GenerateJSstring(TypeArgumentBase type)
        {
            var str = typeof(string).FullName;

            if (type.IsEnum)
            {
                string typeName = type.TypeNameForBlockly;
                return($@"
                Blockly.JavaScript['{type.TranslateToNewTypeName()}'] = function(block) {{

                    var dropdown_name = block.getFieldValue('val_{typeName}');                    
                    code = dropdown_name;
                    return [code, Blockly.JavaScript.ORDER_ATOMIC];


                }}");
            }
            ;

            var arr   = typeof(IEnumerable);
            var props = type.GetProperties()
                        .Select(prop => (prop, isArray: prop.IsArray))
                        //.Select(it => (prop: it.prop, filter: (!it.isArray) ? "" : "+'.filter(it=>it !=null)'" ))
                        //TODO: previous line can filter empty arrays, e.g. post with empty elements
                        // however , raises an error in acorn. For the moment, not implemented
                        .Select(it => (prop: it.prop, filter: (!it.isArray) ? "" : ""))
                        .ToArray();


            var objectProperties =
                string.Join(Environment.NewLine,
                            props
                            .Select(it =>
                                    $"let val{it.prop.Name} = Blockly.JavaScript.valueToCode(block, \"val_{it.prop.Name}\", Blockly.JavaScript.ORDER_ATOMIC);" +
                                    $"if(val{it.prop.Name} != ''){{" +
                                    $"val{it.prop.Name} = val{it.prop.Name};" +
                                    $"objPropString.push('\"{it.prop.Name}\":'+val{it.prop.Name}{it.filter});" +
                                    $"}}")
                            );

            var strJS = $@" Blockly.JavaScript['{type.TranslateToNewTypeName()}'] = function(block) {{
                            console.log(block);
                            var objPropString=[];
                            {objectProperties}
                            console.log(objPropString);
                            var code ='{{ '+ objPropString.join(',') +' }}';
                            console.log(code);
                            return [code, Blockly.JavaScript.ORDER_NONE];
                            }};";

            return(strJS);
        }
Ejemplo n.º 3
0
        internal void Init()
        {
            ActionName = f.Name;
            var retName = f.Type?.OfType?.Name ?? f.Type.Name;

            returnType = allTypesInGraph.FindAfterId(retName);
            string ret = "";

            if (returnType != null)
            {
                ret = string.Join(" ",
                                  returnType.GetProperties().Select(it => it.Name)
                                  );
            }

            string argsInQuery = "";

            if (f.Args?.Count > 0)
            {
                foreach (var arg in f.Args)
                {
                    var type        = arg.Type;
                    var typeInGraph = allTypesInGraph.FindAfterId(type.Name);
                    Params.Add(arg.Name, (typeInGraph, BindingSourceDefinition.Query));
                }
                argsInQuery = string.Join(",",
                                          f.Args
                                          .Select(it => new { it.Name, isString = (it.Type.Name == "String") })
                                          .Select(it => it.Name + ":"
                                                  + (it.isString ? "\"" : "")
                                                  + "{" + it.Name + "}"
                                                  + (it.isString ? "\"" : "")
                                                  )

                                          );
                argsInQuery = $"({argsInQuery})";
            }

            RelativeRequestUrl = "/graphql?query={" + f.Name + argsInQuery + "{" + ret + "}}";
            this.
            Verb       = "GET";
            ReturnType = BlocklyType.CreateValue(null);
        }
        /// <summary>
        /// Generates the definition string.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public string GenerateDefinitionString(TypeArgumentBase type)
        {
            if (type.IsEnum)
            {
                return(GenerateDefinitionStringForEnum(type));
            }


            var tooltipAndpropsDef = GenerateTooltipAndPropDef(type);
            var blocklyTypeName    = type.TranslateToNewTypeName();
            var typeName           = type.TypeNameForBlockly;



            var definitionString = $@"
                                Blockly.Blocks['{blocklyTypeName}'] = {{
                                init: function() {{
//this.setInputsInline(true);
                                    this.appendDummyInput()
                                        .appendField('{type.Name}');
                                    {tooltipAndpropsDef.propsDef}
                                    this.setTooltip('{tooltipAndpropsDef.tooltip}');
                        
                                    this.setOutput(true, '{blocklyTypeName}');
                                        }}  
                                }};

                                Blockly.Blocks['var_{blocklyTypeName}'] = {{
                                                          init: function() {{
                                                            this.setTooltip('{type.FullName}');
                                                            this.appendDummyInput()
                                                              .appendField('variable:')
                                                              .appendField(new Blockly.FieldVariable(
                                                                  'var_{typeName}',
                                                                  '{blocklyTypeName}'
                                                              ), 'FIELDNAME');
                                                          }}
                                                        }};
                             ";

            return(definitionString);
        }
        /// <summary>
        /// Generates tool box code for all properties of a type.
        /// </summary>
        /// <param name="blockText">The block text.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public string GenerateToolBoxCodeForAllPropertiesOfAType(string blockText, TypeArgumentBase type)
        {
            var validProperties = type.GetProperties();

            foreach (var property in type.GetProperties())
            {
                var propertyType = property.PropertyType;
                if (!propertyType.ConvertibleToBlocklyType())
                {
                    continue;
                }

                var typeName = type.TypeNameForBlockly;

                blockText += createBlockShadowDef(property.Name, propertyType.TranslateToBlocklyBlocksType());

                blockText += $"blockText_{typeName} += blockTextLocalSiteFunctions;";
            }

            return(blockText);
        }
        private string GenerateDefinitionStringForEnum(TypeArgumentBase type)
        {
            string typeName = type.TypeNameForBlockly;

            if (!type.IsEnum)
            {
                throw new ArgumentException($"type {type.Name} is not enum!");
            }


            var addString = "";
            var t         = type.GetValuesForEnum().First().Value;
            var q         = (dynamic)t;

            if (q.GetType() == typeof(string))
            {
                addString = @"\'";
                ;
            }
            var opt = string.Join(",",
                                  type.GetValuesForEnum().Select(it => $"['{it.Key}', '{addString}{it.Value}{addString}']")
                                  );

            var def = $@"{Environment.NewLine}
 Blockly.Blocks['{type.TranslateToNewTypeName()}'] = {{
            init: function () {{
                this.appendDummyInput()
                    .appendField('{typeName}')
                    .appendField(new Blockly.FieldDropdown([{opt}]), 'val_{typeName}');
            this.setOutput(true, 'Number');

            this.setTooltip('Enumeration {type.Name}');
            //this.setHelpUrl('');
        }}
    }};                               
";

            return(def);
        }
        internal (string tooltip, string propsDef) GenerateTooltipAndPropDef(TypeArgumentBase type)
        {
            string tooltip  = $"{type.TranslateToNewTypeName()} with props:";
            string propsDef = "";

            var validProperties = type.GetProperties();



            foreach (var property in validProperties)
            {
                tooltip  += $"{property.Name}: {property.PropertyType.TranslateToNewTypeName()};";
                propsDef += $@"{Environment.NewLine}
                                this.appendValueInput('val_{property.Name}')
                                        .setCheck('{property.PropertyType.TranslateToNewTypeName()}')
                                        .appendField('{property.Name}')
                                        ;
                              ";
            }

            return(tooltip, propsDef);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Generates the blockly definition.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 public string  GenerateBlocklyDefinition(TypeArgumentBase type)
 {
     return(_definitionGenerator.GenerateBlocklyDefinition(type));
 }