예제 #1
0
        public override async Task ProcessAsync(TagHelperContext tagContext, TagHelperOutput output)
        {
            if (Context == null)
            {
                throw new Exception("The NccGridContext is null... Please check the reference.");
            }

            _nccTagContext.CssClassGrid            = CssClassTable;
            _nccTagContext.CssClassBody            = CssClassBody;
            _nccTagContext.CssClassHeader          = CssClassHeader;
            _nccTagContext.CssClassFooter          = CssClassFooter;
            _nccTagContext.CssClassHeaderContainer = CssClassHeaderContainer;
            _nccTagContext.CssClassTableContainer  = CssClassTableContainer;
            _nccTagContext.CssClassFooterContainer = CssClassFooterContainer;

            if (!string.IsNullOrEmpty(DataKeys) && DataKeys != Context.DataKeys)
            {
                Context.DataKeys = DataKeys;
            }

            if (RenderForm.HasValue)
            {
                Context.RenderForm = RenderForm.Value;
            }
            if (AutoGenerateEditButton.HasValue)
            {
                Context.AutoGenerateEditButton = AutoGenerateEditButton.Value;
            }
            if (AllowPaging.HasValue)
            {
                Context.AllowPaging = AllowPaging.Value;
            }
            if (Context.AllowPaging)
            {
                if (PagerNavigationSize.HasValue)
                {
                    Context.PagerNavigationSize = PagerNavigationSize.Value;
                }
                if (Context.Filters.ContainsKey("pageSize"))
                {
                    Context.PageSize = Convert.ToInt32(Context.Filters["pageSize"]);
                }
                else if (PageSize.HasValue)
                {
                    Context.PageSize = PageSize.Value;
                }
                if (Context.PageSize == 0 || Context.PageSize == int.MaxValue)
                {
                    Context.PageSize = 10;
                }
            }
            else
            {
                Context.PageSize = int.MaxValue;
            }

            if (Context.Filters.ContainsKey("pageNumber"))
            {
                Context.PageNumber = Convert.ToInt32(Context.Filters["pageNumber"]);
            }

            object service = null;

            if (!string.IsNullOrEmpty(Context.EventHandlerClass))
            {
                service = NccReflectionService.NccGetClassInstance(Context.EventHandlerClass, null);
            }

            service?.NccInvokeMethod(NccEventsEnum.Load.ToString(), new object[] { new NccEventArgs {
                                                                                       NccTagContext = _nccTagContext, NccControlContext = Context, ViewContext = ViewContext
                                                                                   } });

            //Get grid id and share it with siblings parents
            if (string.IsNullOrEmpty(Context.Id))
            {
                Context.Id = Guid.NewGuid().ToString();
            }

            output.TagName = "div";
            output.Attributes.SetAttribute("id", Context.Id);
            output.Attributes.SetAttribute("style", "position:relative");

            if (Context.Visible)
            {
                tagContext.Items.Add(typeof(NccGridContext), Context);

                if (Context.RenderForm)
                {
                    var form = new TagBuilder("form")
                    {
                        TagRenderMode = TagRenderMode.StartTag
                    };

                    output.PreContent.AppendHtml(form);
                }

                NccActionsService.ExtraParameters <NccGridContext> setExtraParameters = GridService.GetExtraParameters;
                NccActionsService.DataResult <NccGridContext>      setDataResult      = GridService.SetDataResult;
                NccControlsService.BindData(Context, ViewContext.HttpContext, setExtraParameters, setDataResult);

                service?.NccInvokeMethod(NccEventsEnum.DataBound.ToString(), new object[] { new NccEventArgs {
                                                                                                NccControlContext = Context, ViewContext = ViewContext, DataObjects = Context.DataObjects
                                                                                            } });

                _nccTagContext.GridHeader = new GridRow {
                    Cells = new List <GridCell>(), CssClass = CssClassHeader
                };

                await output.GetChildContentAsync();

                var tableContainerDiv = new TagBuilder("div");
                if (!string.IsNullOrEmpty(_nccTagContext.CssClassTableContainer))
                {
                    tableContainerDiv.Attributes.Add("class", _nccTagContext.CssClassTableContainer);
                }

                tableContainerDiv.InnerHtml.AppendHtml(GridService.BuildTableHtml(_nccTagContext, Context));

                output.Content.SetHtmlContent(tableContainerDiv);

                if (Context.AllowPaging)
                {
                    output.Content.AppendHtml(GridService.BuildTablePager(_nccTagContext, Context));
                }

                output.PreContent.AppendHtml(_nccTagContext.PreContent);
                output.PostContent.AppendHtml(_nccTagContext.PostContent);

                if (Context.RenderForm)
                {
                    var antiforgeryTag = Generator.GenerateAntiforgery(ViewContext);
                    if (antiforgeryTag != null)
                    {
                        output.PostContent.AppendHtml(antiforgeryTag);
                    }
                    output.PostContent.AppendHtml(new TagBuilder("form")
                    {
                        TagRenderMode = TagRenderMode.EndTag
                    });
                }
            }

            service?.NccInvokeMethod(NccEventsEnum.PreRender.ToString(), new object[] { new NccEventArgs {
                                                                                            NccTagContext = _nccTagContext, NccControlContext = Context, ViewContext = ViewContext
                                                                                        } });

            _nccTagContext.ControlContext = Context;

            output.PostContent.AppendHtml(NccControlsService.GetEncodedContext(_protector, Context.Id, Context));
            output.PostContent.AppendHtml(NccControlsService.GetAjaxLoaderOverlay());
            output.PostContent.AppendHtml(NccControlsService.GetAjaxLoaderImage());
        }