Ejemplo n.º 1
0
        /// <summary>
        /// Generates the blockly tool box function definitions.
        /// </summary>
        /// <param name="actionList">The action list.</param>
        /// <returns></returns>
        public string GenerateBlocklyToolBoxFunctionDefinitions(List <ActionInfo> actionList)
        {
            string blockText = "var blockTextLocalSiteFunctions='';";

            foreach (var actionsGroupedByController in actionList.GroupBy(it => it.ControllerName))
            {
                var controllerName = actionsGroupedByController.Key;
                var actionHash     = actionList.First(it => it.ControllerName == controllerName).CustomGetHashCode();

                var blockColor = BlocklyStringToColor.ConvertToHue(actionHash);

                blockText += $"blockTextLocalSiteFunctions += '<category name=\"{controllerName}\" colour=\"{blockColor}\">' ; ";
                foreach (var action in actionsGroupedByController)
                {
                    blockText += $@"{Environment.NewLine}
                        blockTextLocalSiteFunctions += '<block type=""{action.GenerateCommandName()}"">';";
                    if (action.HasParams)
                    {
                        foreach (var param in action.Params)
                        {
                            var type = param.Value.type;
                            var blocklyParameterName = param.Key;
                            if (type.ConvertibleToBlocklyType())
                            {
                                var blockShadowType = type.TranslateToBlocklyBlocksType();
                                blockText += $@"{Environment.NewLine}
                                                 blockTextLocalSiteFunctions += '<value name=""val_{blocklyParameterName}""><shadow type=""{blockShadowType}"">{GenerateBlockShadowField(blockShadowType)}</shadow></value>';  
                                              ";
                            }
                            else
                            {
                                var typeWithoutBlocklyType = actionList.GetAllTypesWithNullBlocklyType().FirstOrDefault(x => x == type);
                                if (typeWithoutBlocklyType != null)
                                {
                                    var blockShadowType = type.TranslateToNewTypeName();
                                    blockText += $@"{Environment.NewLine}
                                                    blockTextLocalSiteFunctions += '<value name=""val_{blocklyParameterName}""><shadow type=""{blockShadowType}""></shadow></value>';                                                    
                                                   ";
                                }
                            }
                        }
                    }

                    blockText += "blockTextLocalSiteFunctions += '</block>';";
                }
                blockText += $"blockTextLocalSiteFunctions+='</category>';";
            }

            return(blockText);
        }
        /// <summary>
        /// Generates the function definition.
        /// blockly block definition for action
        /// </summary>
        /// <param name="actionInfo">The action information.</param>
        /// <returns></returns>
        public string GenerateFunctionDefinition(ActionInfo actionInfo)
        {
            var strPropsDefinition = GeneratePropertyDefinitions(actionInfo);

            var returnType = $@"this.setOutput(true,'{actionInfo.ReturnType.TranslateToBlocklyType()}');";
            var actionHash = actionInfo.CustomGetHashCode();

            var blockColor = BlocklyStringToColor.ConvertToHue(actionHash);

            return($@"
                Blockly.Blocks['{actionInfo.GenerateCommandName()}'] = {{
                          init: function() {{
                            this.setColour({blockColor});
                            this.appendDummyInput()
                                .appendField('{actionInfo.CommandDisplayName()}');
                                {strPropsDefinition}
                                {returnType}
                                }}//init
                        }};//{actionInfo.ActionName}
                        ");
        }
        /// <summary>
        /// Generates the function definition.
        /// blockly block definition for action
        /// </summary>
        /// <param name="actionInfo">The action information.</param>
        /// <param name="key">key</param>
        /// <returns></returns>
        public string GenerateFunctionDefinition(ActionInfo actionInfo, string key)
        {
            var strPropsDefinition = GeneratePropertyDefinitions(actionInfo);
            var returnType         = "";

            if (actionInfo.ReturnType.id != null)
            {
                returnType = $@"this.setOutput(true,'{actionInfo.ReturnType.TranslateToBlocklyType()}');";
            }
            else
            {
                returnType = $@"this.setOutput(true,'');";
            }
            var actionHash = actionInfo.CustomGetHashCode();

            string[] verbHasImage = new string[] { "patch", "get", "post", "put", "delete" };
            bool     hasImage     = verbHasImage.Contains(actionInfo.Verb.ToLower());
            var      blockColor   = BlocklyStringToColor.ConvertToHue(actionHash);
            string   site         = string.IsNullOrWhiteSpace(GenerateBlocklyFilesHostedService.InternalSiteUrl) ? "" : GenerateBlocklyFilesHostedService.InternalSiteUrl;

            return($@"
                Blockly.Blocks['{actionInfo.GenerateCommandName()}'] = {{
                          init: function() {{
                            this.setColour({blockColor});
                            this.appendDummyInput()" +

                   (hasImage?
                    $".appendField(new Blockly.FieldImage('{site}images/{actionInfo.Verb.ToLower()}.png', 90, 20, {{ alt: '{actionInfo.Verb}', flipRtl: 'FALSE' }}))"
                                :"") +
                   $".appendField('{actionInfo.CommandDisplayName(!hasImage)}');"
                   + Environment.NewLine
                   + $@"{strPropsDefinition}
                                {returnType}
                                }}//init
                        }};//{actionInfo.ActionName}
                        ");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates the blockly tool box function definitions.
        /// </summary>
        /// <param name="actionList">The action list.</param>
        /// <param name="key">The action list.</param>
        /// <returns></returns>
        public string GenerateBlocklyToolBoxFunctionDefinitions(ActionInfo[] actionList, string key)
        {
            var dictOrder = new Dictionary <string, int>()
            {
                { "get", 1 },
                { "post", 2 },
                { "put", 3 },
                { "delete", 4 },
                { "others", 5 }
            };
            var a = actionList.ToList();

            a.Sort((a, b) => {
                var res = a.ControllerName.CompareTo(b.ControllerName);
                if (res != 0)
                {
                    return(res);
                }

                var v1 = dictOrder.ContainsKey(a.Verb.ToLower()) ? dictOrder[a.Verb.ToLower()] : dictOrder["others"];
                var v2 = dictOrder.ContainsKey(b.Verb.ToLower()) ? dictOrder[b.Verb.ToLower()] : dictOrder["others"];


                res = v1.CompareTo(v2);
                if (res != 0)
                {
                    return(res);
                }

                return(a.ActionName.CompareTo(b.ActionName));
            })
            ;
            actionList = a.ToArray();
            string blockText = $"var blockTextLocalSiteFunctions{key}='';";

            foreach (var actionsGroupedByController in actionList.GroupBy(it => it.ControllerName))
            {
                var controllerName = actionsGroupedByController.Key;
                var actionHash     = actionList.First(it => it.ControllerName == controllerName).CustomGetHashCode();

                var blockColor = BlocklyStringToColor.ConvertToHue(actionHash);

                blockText += $"blockTextLocalSiteFunctions{key} += '<category name=\"{controllerName}\" colour=\"{blockColor}\">' ; ";
                foreach (var action in actionsGroupedByController)
                {
                    blockText += $@"{Environment.NewLine}
                        blockTextLocalSiteFunctions{key} += '<block type=""text_print""> <value name=""TEXT""><block type=""{action.GenerateCommandName()}"">';";
                    if (action.HasParams)
                    {
                        foreach (var param in action.Params)
                        {
                            var type = param.Value.type;
                            var blocklyParameterName = param.Key;
                            if (type.ConvertibleToBlocklyType())
                            {
                                var blockShadowType = type.TranslateToBlocklyBlocksType();
                                blockText += $@"{Environment.NewLine}
                                                 blockTextLocalSiteFunctions{key} += '<value name=""val_{blocklyParameterName}""><shadow type=""{blockShadowType}"">{GenerateBlockShadowField(blockShadowType)}</shadow></value>';  
                                              ";
                            }
                            else
                            {
                                var typeWithoutBlocklyType = actionList.ToArray().GetAllTypesWithNullBlocklyType().FirstOrDefault(x => x == type);
                                if (typeWithoutBlocklyType != null)
                                {
                                    var blockShadowType = type.TranslateToNewTypeName();
                                    blockText += $@"{Environment.NewLine}
                                                    blockTextLocalSiteFunctions{key} += '<value name=""val_{blocklyParameterName}""><shadow type=""{blockShadowType}""></shadow></value>';                                                    
                                                   ";
                                }
                            }
                        }
                    }

                    blockText += $"blockTextLocalSiteFunctions{key} += '</block></block>';";
                }
                blockText += $"blockTextLocalSiteFunctions{key}+='</category>';";
            }

            return(blockText);
        }