Exemplo n.º 1
0
        public async Task <IViewComponentResult> InvokeAsync(PageComponentContext context)
        {
            ErpPage currentPage = null;

            try
            {
                #region << Init >>
                if (context.Node == null)
                {
                    return(await Task.FromResult <IViewComponentResult>(Content("Error: The node Id is required to be set as query param 'nid', when requesting this component")));
                }


                var pageFromModel = context.DataModel.GetProperty("Page");
                if (pageFromModel is ErpPage)
                {
                    currentPage = (ErpPage)pageFromModel;
                }
                else
                {
                    return(await Task.FromResult <IViewComponentResult>(Content("Error: PageModel does not have Page property or it is not from ErpPage Type")));
                }

                if (currentPage == null)
                {
                    return(await Task.FromResult <IViewComponentResult>(Content("Error: The page Id is required to be set as query param 'pid', when requesting this component")));
                }

                var baseOptions = InitPcFieldBaseOptions(context);
                var options     = PcFieldCodeOptions.CopyFromBaseOptions(baseOptions);
                if (context.Options != null)
                {
                    options = JsonConvert.DeserializeObject <PcFieldCodeOptions>(context.Options.ToString());
                }
                var modelFieldLabel = "";
                var model           = (PcFieldBaseModel)InitPcFieldBaseModel(context, options, label: out modelFieldLabel);
                if (String.IsNullOrWhiteSpace(options.LabelText))
                {
                    options.LabelText = modelFieldLabel;
                }

                ViewBag.LabelMode = options.LabelMode;
                ViewBag.Mode      = options.Mode;

                if (options.LabelMode == LabelRenderMode.Undefined && baseOptions.LabelMode != LabelRenderMode.Undefined)
                {
                    ViewBag.LabelMode = baseOptions.LabelMode;
                }

                if (options.Mode == FieldRenderMode.Undefined && baseOptions.Mode != FieldRenderMode.Undefined)
                {
                    ViewBag.Mode = baseOptions.Mode;
                }

                var componentMeta = new PageComponentLibraryService().GetComponentMeta(context.Node.ComponentName);

                var accessOverride = context.DataModel.GetPropertyValueByDataSource(options.AccessOverrideDs) as FieldAccess?;
                if (accessOverride != null)
                {
                    model.Access = accessOverride.Value;
                }
                var requiredOverride = context.DataModel.GetPropertyValueByDataSource(options.RequiredOverrideDs) as bool?;
                if (requiredOverride != null)
                {
                    model.Required = requiredOverride.Value;
                }
                else
                {
                    if (!String.IsNullOrWhiteSpace(options.RequiredOverrideDs))
                    {
                        if (options.RequiredOverrideDs.ToLowerInvariant() == "true")
                        {
                            model.Required = true;
                        }
                        else if (options.RequiredOverrideDs.ToLowerInvariant() == "false")
                        {
                            model.Required = false;
                        }
                    }
                }
                #endregion

                ViewBag.Options        = options;
                ViewBag.Model          = model;
                ViewBag.Node           = context.Node;
                ViewBag.ComponentMeta  = componentMeta;
                ViewBag.RequestContext = ErpRequestContext;
                ViewBag.AppContext     = ErpAppContext.Current;

                if (context.Mode != ComponentMode.Options && context.Mode != ComponentMode.Help)
                {
                    var isVisible   = true;
                    var isVisibleDS = context.DataModel.GetPropertyValueByDataSource(options.IsVisible);
                    if (isVisibleDS is string && !String.IsNullOrWhiteSpace(isVisibleDS.ToString()))
                    {
                        if (Boolean.TryParse(isVisibleDS.ToString(), out bool outBool))
                        {
                            isVisible = outBool;
                        }
                    }
                    else if (isVisibleDS is Boolean)
                    {
                        isVisible = (bool)isVisibleDS;
                    }
                    ViewBag.IsVisible = isVisible;

                    model.Value = context.DataModel.GetPropertyValueByDataSource(options.Value);
                }

                switch (context.Mode)
                {
                case ComponentMode.Display:
                    return(await Task.FromResult <IViewComponentResult>(View("Display")));

                case ComponentMode.Design:
                    return(await Task.FromResult <IViewComponentResult>(View("Design")));

                case ComponentMode.Options:
                    return(await Task.FromResult <IViewComponentResult>(View("Options")));

                case ComponentMode.Help:
                    return(await Task.FromResult <IViewComponentResult>(View("Help")));

                default:
                    ViewBag.Error = new ValidationException()
                    {
                        Message = "Unknown component mode"
                    };
                    return(await Task.FromResult <IViewComponentResult>(View("Error")));
                }
            }
            catch (ValidationException ex)
            {
                ViewBag.Error = ex;
                return(await Task.FromResult <IViewComponentResult>(View("Error")));
            }
            catch (Exception ex)
            {
                ViewBag.Error = new ValidationException()
                {
                    Message = ex.Message
                };
                return(await Task.FromResult <IViewComponentResult>(View("Error")));
            }
        }
Exemplo n.º 2
0
        public async Task <IViewComponentResult> InvokeAsync(PageComponentContext context)
        {
            ErpPage currentPage = null;

            try
            {
                #region << Init >>
                if (context.Node == null)
                {
                    return(await Task.FromResult <IViewComponentResult>(Content("Error: The node Id is required to be set as query param 'nid', when requesting this component")));
                }


                var pageFromModel = context.DataModel.GetProperty("Page");
                if (pageFromModel is ErpPage)
                {
                    currentPage = (ErpPage)pageFromModel;
                }
                else
                {
                    return(await Task.FromResult <IViewComponentResult>(Content("Error: PageModel does not have Page property or it is not from ErpPage Type")));
                }

                if (currentPage == null)
                {
                    return(await Task.FromResult <IViewComponentResult>(Content("Error: The page Id is required to be set as query param 'pid', when requesting this component")));
                }

                var baseOptions     = InitPcFieldBaseOptions(context);
                var instanceOptions = PcFieldCodeOptions.CopyFromBaseOptions(baseOptions);
                if (context.Options != null)
                {
                    instanceOptions = JsonConvert.DeserializeObject <PcFieldCodeOptions>(context.Options.ToString());
                }
                var modelFieldLabel = "";
                var model           = (PcFieldBaseModel)InitPcFieldBaseModel(context, instanceOptions, label: out modelFieldLabel);
                if (String.IsNullOrWhiteSpace(instanceOptions.LabelText))
                {
                    instanceOptions.LabelText = modelFieldLabel;
                }

                ViewBag.LabelMode = instanceOptions.LabelMode;
                ViewBag.Mode      = instanceOptions.Mode;

                if (instanceOptions.LabelMode == LabelRenderMode.Undefined && baseOptions.LabelMode != LabelRenderMode.Undefined)
                {
                    ViewBag.LabelMode = baseOptions.LabelMode;
                }

                if (instanceOptions.Mode == FieldRenderMode.Undefined && baseOptions.Mode != FieldRenderMode.Undefined)
                {
                    ViewBag.Mode = baseOptions.Mode;
                }

                var componentMeta = new PageComponentLibraryService().GetComponentMeta(context.Node.ComponentName);
                #endregion

                if (context.Mode != ComponentMode.Options && context.Mode != ComponentMode.Help)
                {
                    model.Value = context.DataModel.GetPropertyValueByDataSource(instanceOptions.Value);
                }

                ViewBag.Options        = instanceOptions;
                ViewBag.Model          = model;
                ViewBag.Node           = context.Node;
                ViewBag.ComponentMeta  = componentMeta;
                ViewBag.RequestContext = ErpRequestContext;
                ViewBag.AppContext     = ErpAppContext.Current;

                switch (context.Mode)
                {
                case ComponentMode.Display:
                    return(await Task.FromResult <IViewComponentResult>(View("Display")));

                case ComponentMode.Design:
                    return(await Task.FromResult <IViewComponentResult>(View("Design")));

                case ComponentMode.Options:
                    return(await Task.FromResult <IViewComponentResult>(View("Options")));

                case ComponentMode.Help:
                    return(await Task.FromResult <IViewComponentResult>(View("Help")));

                default:
                    ViewBag.ExceptionMessage = "Unknown component mode";
                    ViewBag.Errors           = new List <ValidationError>();
                    return(await Task.FromResult <IViewComponentResult>(View("Error")));
                }
            }
            catch (ValidationException ex)
            {
                ViewBag.ExceptionMessage = ex.Message;
                ViewBag.Errors           = new List <ValidationError>();
                return(await Task.FromResult <IViewComponentResult>(View("Error")));
            }
            catch (Exception ex)
            {
                ViewBag.ExceptionMessage = ex.Message;
                ViewBag.Errors           = new List <ValidationError>();
                return(await Task.FromResult <IViewComponentResult>(View("Error")));
            }
        }