Ejemplo n.º 1
0
        } = FieldRenderMode.Undefined;                                                 //To be inherited

        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            #region << Init >>

            if (LabelMode == LabelRenderMode.Undefined)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey(typeof(LabelRenderMode)))
                {
                    LabelMode = (LabelRenderMode)context.Items[typeof(LabelRenderMode)];
                }
                else
                {
                    LabelMode = LabelRenderMode.Stacked;
                }
            }

            if (Mode == FieldRenderMode.Undefined)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey(typeof(FieldRenderMode)))
                {
                    Mode = (FieldRenderMode)context.Items[typeof(FieldRenderMode)];
                }
                else
                {
                    Mode = FieldRenderMode.Form;
                }
            }
            #endregion

            if (Mode == FieldRenderMode.Form)
            {
                output.TagName = "form";
                if (!String.IsNullOrWhiteSpace(Id))
                {
                    output.Attributes.Add("id", Id);
                }

                if (!String.IsNullOrWhiteSpace(Name))
                {
                    output.Attributes.Add("name", Name);
                }
                if (!String.IsNullOrWhiteSpace(Action))
                {
                    output.Attributes.Add("action", Action);
                }

                if (!String.IsNullOrWhiteSpace(Enctype))
                {
                    output.Attributes.Add("enctype", Enctype);
                }

                if (!String.IsNullOrWhiteSpace(Target))
                {
                    output.Attributes.Add("target", Target);
                }
                switch (Method.ToLowerInvariant())
                {
                case "get":
                    output.Attributes.Add("method", Method);
                    break;

                default:
                    output.Attributes.Add("method", "post");
                    break;
                }

                if (NoValidate)
                {
                    output.Attributes.Add("novalidate", "novalidate");
                }

                if (AutoComplete)
                {
                    output.Attributes.Add("autocomplete", "on");
                }
                else
                {
                    output.Attributes.Add("autocomplete", "off");
                }

                if (!String.IsNullOrWhiteSpace(AcceptCharset))
                {
                    output.Attributes.Add("accept-charset", AcceptCharset);
                }

                if (Antiforgery && Method != "get")
                {
                    var antiforgeryTag = Generator.GenerateAntiforgery(ViewContext);
                    if (antiforgeryTag != null)
                    {
                        output.PreContent.AppendHtml(antiforgeryTag);
                    }
                }

                if (Validation != null)
                {
                    context.Items[typeof(ValidationException)] = Validation;
                }

                var jsCompressor = new JavaScriptCompressor();

                #region << Init Scripts >>
                var tagHelperInitialized = false;
                var scriptFileName       = "form.js";
                if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvForm) + scriptFileName))
                {
                    var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvForm) + scriptFileName];
                    tagHelperInitialized = tagHelperContext.Initialized;
                }
                if (!tagHelperInitialized && !String.IsNullOrEmpty(Name))
                {
                    var scriptContent = FileService.GetEmbeddedTextResource(scriptFileName, "WebVella.Erp.Web.TagHelpers.WvForm");
                    scriptContent = scriptContent.Replace("{{FormName}}", Name);
                    scriptContent = scriptContent.Replace("{{ElementId}}", Id);

                    var scriptEl = new TagBuilder("script");
                    scriptEl.Attributes.Add("type", "text/javascript");
                    scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                    //scriptEl.InnerHtml.AppendHtml(scriptContent);
                    output.PostElement.AppendHtml(scriptEl);

                    ViewContext.HttpContext.Items[typeof(WvForm) + scriptFileName] = new WvTagHelperContext()
                    {
                        Initialized = true
                    };
                }
                #endregion

                #region << Add Inline Init Script for this instance >>
                var initScript = new TagBuilder("script");
                initScript.Attributes.Add("type", "text/javascript");
                var scriptTemplate = @"
						$(function(){
							WebVellaErpWebComponentsPcForm_Init(""{{ElementId}}"",""{{FormName}}"");
						});"                        ;
                scriptTemplate = scriptTemplate.Replace("{{ElementId}}", Id);
                scriptTemplate = scriptTemplate.Replace("{{FormName}}", Name);
                initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                output.PostContent.AppendHtml(initScript);
                #endregion
            }
            else
            {
                output.TagName = "div";
                output.AddCssClass("form");
            }

            context.Items[typeof(LabelRenderMode)] = LabelMode;
            context.Items[typeof(FieldRenderMode)] = Mode;
            context.Items["FromAutocomplete"]      = AutoComplete;
            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        public async Task <IViewComponentResult> InvokeAsync(Guid?pageId         = null, FieldRenderMode mode = FieldRenderMode.Form,
                                                             PageType?presetType = null, Guid?presetAppId     = null, Guid?presetEntityId = null)
        {
            //var typeOptions = new List<SelectFieldOption>();
            //var entityOptions = new List<SelectFieldOption>();
            //var applicationOptions = new List<SelectFieldOption>();
            //var areaOptions = new List<SelectFieldOption>();
            //var nodeOptions = new List<SelectFieldOption>();
            var pageSelectionTree = new PageSelectionTree();
            var erpPage           = new ErpPage();

            if (pageId == null)
            {
                if (presetType != null)
                {
                    erpPage.Type = presetType ?? PageType.Site;
                }
                if (presetAppId != null)
                {
                    erpPage.AppId = presetAppId.Value;
                }
                if (presetEntityId != null)
                {
                    erpPage.EntityId = presetEntityId.Value;
                    erpPage.Type     = PageType.RecordList;
                }
            }
            var pageSrv              = new PageService();
            var typeOptionsFieldId   = Guid.NewGuid();
            var appOptionsFieldId    = Guid.NewGuid();
            var areaOptionsFieldId   = Guid.NewGuid();
            var nodeOptionsFieldId   = Guid.NewGuid();
            var entityOptionsFieldId = Guid.NewGuid();

            #region << Init >>
            var apps     = new AppService().GetAllApplications();
            var entities = new EntityManager().ReadEntities().Object;

            #region << ErpPage && Init it>>
            if (pageId != null)
            {
                erpPage = pageSrv.GetPage(pageId ?? Guid.Empty);
                if (erpPage == null)
                {
                    ViewBag.ErrorMessage = "Error: the set pageId is not found as ErpPage!";
                    return(await Task.FromResult <IViewComponentResult>(View("Error")));
                }

                if (erpPage.Type == PageType.Application && erpPage.AppId == null)
                {
                    ViewBag.ErrorMessage = "Error: Application should have AppId!";
                    return(await Task.FromResult <IViewComponentResult>(View("Error")));
                }
            }
            #endregion

            #region << Type options >>
            {
                pageSelectionTree.AllTypes = ModelExtensions.GetEnumAsSelectOptions <PageType>().OrderBy(x => x.Label).ToList();
            }
            #endregion

            #region << App options >>
            {
                foreach (var app in apps)
                {
                    pageSelectionTree.AllApps.Add(new SelectOption()
                    {
                        Value = app.Id.ToString(),
                        Label = app.Name
                    });
                    //Set App tree
                    var appSelectionTree = new AppSelectionTree();
                    appSelectionTree.AppId = app.Id;
                    foreach (var area in app.Sitemap.Areas)
                    {
                        appSelectionTree.AllAreas.Add(new SelectOption()
                        {
                            Value = area.Id.ToString(),
                            Label = area.Name
                        });
                        var areaSelectionTree = new AreaSelectionTree()
                        {
                            AreaId = area.Id
                        };
                        foreach (var node in area.Nodes)
                        {
                            areaSelectionTree.AllNodes.Add(new SelectOption()
                            {
                                Value = node.Id.ToString(),
                                Label = node.Name
                            });
                        }
                        areaSelectionTree.AllNodes = areaSelectionTree.AllNodes.OrderBy(x => x.Label).ToList();
                        appSelectionTree.AreaSelectionTree.Add(areaSelectionTree);
                    }
                    pageSelectionTree.AppSelectionTree.Add(appSelectionTree);

                    //Set Entities
                    foreach (var entity in app.Entities)
                    {
                        appSelectionTree.Entities.Add(new SelectOption()
                        {
                            Value = entity.Entity.Id.ToString(),
                            Label = entity.Entity.Name
                        });
                    }
                    appSelectionTree.Entities = appSelectionTree.Entities.OrderBy(x => x.Label).ToList();
                }
                pageSelectionTree.AllApps = pageSelectionTree.AllApps.OrderBy(x => x.Label).ToList();
            }
            #endregion

            #region << Entity options >>
            foreach (var entity in entities)
            {
                pageSelectionTree.AllEntities.Add(new SelectOption()
                {
                    Value = entity.Id.ToString(),
                    Label = entity.Name
                });
            }
            pageSelectionTree.AllEntities = pageSelectionTree.AllEntities.OrderBy(x => x.Label).ToList();
            #endregion

            #endregion

            ViewBag.PageSelectionTree    = pageSelectionTree;
            ViewBag.ErpPage              = erpPage;
            ViewBag.TypeOptionsFieldId   = typeOptionsFieldId;
            ViewBag.AppOptionsFieldId    = appOptionsFieldId;
            ViewBag.AreaOptionsFieldId   = areaOptionsFieldId;
            ViewBag.NodeOptionsFieldId   = nodeOptionsFieldId;
            ViewBag.EntityOptionsFieldId = entityOptionsFieldId;

            var pageSelectionTreeJson = JsonConvert.SerializeObject(pageSelectionTree);

            ViewBag.EmbededJs = "";
            #region << Generate js script >>
            if (mode == FieldRenderMode.Form)
            {
                var jsCompressor = new JavaScriptCompressor();

                #region << Init Scripts >>

                var fileName       = "form.js";
                var scriptEl       = "<script type=\"text/javascript\">";
                var scriptTemplate = FileService.GetEmbeddedTextResource(fileName, "WebVella.Erp.Plugins.SDK.Components.WvSdkPageSitemap", "WebVella.Erp.Plugins.SDK");

                scriptTemplate = scriptTemplate.Replace("\"{{PageSelectionTreeJson}}\";", pageSelectionTreeJson);
                scriptTemplate = scriptTemplate.Replace("{{typeOptionsFieldId}}", typeOptionsFieldId.ToString());
                scriptTemplate = scriptTemplate.Replace("{{appOptionsFieldId}}", appOptionsFieldId.ToString());
                scriptTemplate = scriptTemplate.Replace("{{areaOptionsFieldId}}", areaOptionsFieldId.ToString());
                scriptTemplate = scriptTemplate.Replace("{{nodeOptionsFieldId}}", nodeOptionsFieldId.ToString());
                scriptTemplate = scriptTemplate.Replace("{{entityOptionsFieldId}}", entityOptionsFieldId.ToString());
                scriptEl      += jsCompressor.Compress(scriptTemplate);
                //scriptEl += scriptTemplate;
                scriptEl += "</script>";

                ViewBag.EmbededJs = scriptEl;
                #endregion
            }
            #endregion

            ViewBag.RenderMode = mode;

            var applicationOptions = new List <SelectOption>();
            var areaOptions        = new List <SelectOption>();
            var nodeOptions        = new List <SelectOption>();
            var entityOptions      = new List <SelectOption>();

            #region << Init Options >>
            //AppOptions

            applicationOptions = pageSelectionTree.AllApps;
            applicationOptions.Insert(0, new SelectOption()
            {
                Value = "", Label = "not selected"
            });
            areaOptions.Insert(0, new SelectOption()
            {
                Value = "", Label = "not selected"
            });
            nodeOptions.Insert(0, new SelectOption()
            {
                Value = "", Label = "not selected"
            });
            entityOptions = pageSelectionTree.AllEntities;
            entityOptions.Insert(0, new SelectOption()
            {
                Value = "", Label = "not selected"
            });

            //App is selected
            if (erpPage.AppId != null)
            {
                var treeAppOptions = pageSelectionTree.AppSelectionTree.First(x => x.AppId == erpPage.AppId);

                areaOptions = treeAppOptions.AllAreas;
                if (erpPage.AreaId == null)
                {
                    areaOptions.Insert(0, new SelectOption()
                    {
                        Value = "", Label = "not selected"
                    });
                    nodeOptions.Insert(0, new SelectOption()
                    {
                        Value = "", Label = "not selected"
                    });
                }
                else
                {
                    var treeAreaOptions = treeAppOptions.AreaSelectionTree.FirstOrDefault(x => x.AreaId == erpPage.AreaId);
                    if (treeAreaOptions != null)
                    {
                        nodeOptions = treeAreaOptions.AllNodes;
                    }
                }

                if (treeAppOptions.Entities.Count > 0)
                {
                    entityOptions = treeAppOptions.Entities;
                }
                else
                {
                    entityOptions = pageSelectionTree.AllEntities;
                }
            }

            #endregion

            ViewBag.ApplicationOptions = applicationOptions;
            ViewBag.AreaOptions        = areaOptions;
            ViewBag.NodeOptions        = nodeOptions;
            ViewBag.EntityOptions      = entityOptions;

            if (mode == FieldRenderMode.Form)
            {
                return(await Task.FromResult <IViewComponentResult>(View("Form")));
            }
            else
            {
                return(await Task.FromResult <IViewComponentResult>(View("Display")));
            }
        }