Пример #1
0
        public static IMVCGridRenderingEngine GetRenderingEngine(GridContext gridContext)
        {
            IMVCGridRenderingEngine renderingEngine = null;

            if (!String.IsNullOrWhiteSpace(gridContext.QueryOptions.RenderingEngineName))
            {
                foreach (ProviderSettings configuredEngine in gridContext.GridDefinition.RenderingEngines)
                {
                    if (String.Compare(gridContext.QueryOptions.RenderingEngineName, configuredEngine.Name, true) == 0)
                    {
                        string engineName = gridContext.QueryOptions.RenderingEngineName;

                        string typeString = gridContext.GridDefinition.RenderingEngines[engineName].Type;
                        Type engineType = Type.GetType(typeString, true);

                        renderingEngine = (IMVCGridRenderingEngine)Activator.CreateInstance(engineType, true);
                    }
                }
            }

            if (renderingEngine == null)
            {
                renderingEngine = GetRenderingEngineInternal(gridContext.GridDefinition);
            }

            return renderingEngine;
        }
        private static string GenerateClientJsonVisibility(GridContext gridContext)
        {
            var gridColumns = gridContext.GridDefinition.GetColumns();

            StringBuilder sb = new StringBuilder();

            foreach (var cv in gridContext.QueryOptions.ColumnVisibility)
            {
                var gridColumn = gridColumns.SingleOrDefault(p => p.ColumnName == cv.ColumnName);

                if (sb.Length > 0)
                {
                    sb.Append(",");
                }

                sb.AppendFormat("\"{0}\": {{", cv.ColumnName);

                sb.AppendFormat("\"{0}\": \"{1}\"", "headerText", HttpUtility.JavaScriptStringEncode(gridColumn.HeaderText));
                sb.Append(",");
                sb.AppendFormat("\"{0}\": {1}", "visible", cv.Visible.ToString().ToLower());
                sb.Append(",");
                sb.AppendFormat("\"{0}\": {1}", "allow", gridColumn.AllowChangeVisibility.ToString().ToLower());
                sb.Append("}");
            }
            return sb.ToString();
        }
Пример #3
0
        private RenderingModel PrepModel(int?totalRecords, List <Row> rows, MVCGrid.Models.GridContext gridContext)
        {
            RenderingModel model   = new RenderingModel();
            string         rootUrl = HtmlUtility.GetRootUrl();

            model.HandlerPath = HtmlUtility.GetHandlerPath(rootUrl);
            model.TableHtmlId = HtmlUtility.GetTableHtmlId(gridContext.GridName);

            PrepColumns(gridContext, model);
            model.Rows = rows;

            if (model.Rows.Count == 0)
            {
                model.NoResultsMessage = gridContext.GridDefinition.NoResultsMessage;
            }

            model.NextButtonCaption     = gridContext.GridDefinition.NextButtonCaption;
            model.PreviousButtonCaption = gridContext.GridDefinition.PreviousButtonCaption;
            model.SummaryMessage        = gridContext.GridDefinition.SummaryMessage;
            model.ProcessingMessage     = gridContext.GridDefinition.ProcessingMessage;

            model.PagingModel = null;
            if (gridContext.QueryOptions.ItemsPerPage.HasValue)
            {
                model.PagingModel = new PagingModel();

                int currentPageIndex = gridContext.QueryOptions.PageIndex.Value;

                model.PagingModel.TotalRecords = totalRecords.Value;

                model.PagingModel.FirstRecord = (currentPageIndex * gridContext.QueryOptions.ItemsPerPage.Value) + 1;
                if (model.PagingModel.FirstRecord > model.PagingModel.TotalRecords)
                {
                    model.PagingModel.FirstRecord = model.PagingModel.TotalRecords;
                }
                model.PagingModel.LastRecord = (model.PagingModel.FirstRecord + gridContext.QueryOptions.ItemsPerPage.Value) - 1;
                if (model.PagingModel.LastRecord > model.PagingModel.TotalRecords)
                {
                    model.PagingModel.LastRecord = model.PagingModel.TotalRecords;
                }
                model.PagingModel.CurrentPage = currentPageIndex + 1;

                var numberOfPagesD = (model.PagingModel.TotalRecords + 0.0) / (gridContext.QueryOptions.ItemsPerPage.Value + 0.0);
                model.PagingModel.NumberOfPages = (int)Math.Ceiling(numberOfPagesD);

                for (int i = 1; i <= model.PagingModel.NumberOfPages; i++)
                {
                    model.PagingModel.PageLinks.Add(i, HtmlUtility.MakeGotoPageLink(gridContext.GridName, i));
                }
            }

            model.ClientDataTransferHtmlBlock = MVCGrid.Web.MVCGridHtmlGenerator.GenerateClientDataTransferHtml(gridContext);

            return(model);
        }
Пример #4
0
        public void Run(IMVCGridRenderingEngine renderingEngine, GridContext gridContext, TextWriter outputStream)
        {
            if (!renderingEngine.AllowsPaging)
            {
                gridContext.QueryOptions.ItemsPerPage = null;
            }

            var model = GenerateModel(gridContext);

            renderingEngine.Render(model, gridContext, outputStream);
        }
Пример #5
0
        private IEnumerable <PropertyInfo> GetRowSelectProperties(GridContext context)
        {
            IEnumerable <PropertyInfo> properties = null;

            if (context.GridDefinition.EnableRowSelect && context.GridDefinition.ClientSideRowSelectProperties != null &&
                context.GridDefinition.ClientSideRowSelectProperties.Any())
            {
                var objectType = typeof(T1);
                properties = objectType.GetProperties()
                             .Where(x => x.CanRead && context.GridDefinition.ClientSideRowSelectProperties.Contains(x.Name, StringComparer.OrdinalIgnoreCase));
            }

            return(properties);
        }
Пример #6
0
        public RenderingModel GenerateModel(GridContext gridContext)
        {
            int? totalRecords;
            var rows = ((GridDefinitionBase)gridContext.GridDefinition).GetData(gridContext, out totalRecords);

            // if a page was requested higher than available pages, requery for first page
            if (rows.Count == 0 && totalRecords.HasValue && totalRecords.Value > 0)
            {
                gridContext.QueryOptions.PageIndex = 0;
                rows = ((GridDefinitionBase)gridContext.GridDefinition).GetData(gridContext, out totalRecords);
            }

            var model = PrepModel(totalRecords, rows, gridContext);
            return model;
        }
Пример #7
0
        internal static GridContext Create(HttpContext context, string gridName, IMVCGridDefinition grid, QueryOptions options)
        {
            var httpContext = new HttpContextWrapper(context);
            var urlHelper = new System.Web.Mvc.UrlHelper(new RequestContext(httpContext, new RouteData()));

            var gridContext = new GridContext()
            {
                GridName = gridName,
                CurrentHttpContext = context,
                GridDefinition = grid,
                QueryOptions = options,
                UrlHelper = urlHelper
            };

            return gridContext;
        }
        public void Render(RenderingModel model, GridContext gridContext, TextWriter outputStream)
        {
            _htmlImageSortAsc = "<span class='glyphicon glyphicon-triangle-top pull-right' />";
            _htmlImageSortDsc = "<span class='glyphicon glyphicon-triangle-bottom pull-right' />";

            var sbHtml = new StringBuilder();

            sbHtml.AppendFormat("<table id='{0}'", model.TableHtmlId);
            AppendCssAttribute(_defaultTableCss, sbHtml);
            sbHtml.Append(">");

            RenderHeader(model, sbHtml);

            if (model.Rows.Count > 0)
            {
                RenderBody(model, gridContext, sbHtml);
            }
            else
            {
                sbHtml.Append("<tbody>");
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td colspan='{0}'>", model.Columns.Count());
                sbHtml.Append(model.NoResultsMessage);
                sbHtml.Append("</td>");
                sbHtml.Append("</tr>");
                sbHtml.Append("</tbody>");
            }
            sbHtml.AppendLine("</table>");

            if (gridContext.GridDefinition.EnableRowSelect)
            {
                for(var i = 0; i < model.Rows.Count; i++)
                {
                    sbHtml.AppendLine(String.Format(CultureInfo.InvariantCulture, "<div id='{0}_{1}' style='display:none;'>{2}</div>",
                        gridContext.GridName, 
                        i,
                        model.Rows[i].RowSelectEventParameters));
                }
            }

            RenderPaging(model, sbHtml);

            outputStream.Write(sbHtml.ToString());
            outputStream.Write(model.ClientDataTransferHtmlBlock);
        }
        internal static string GenerateClientDataTransferHtml(GridContext gridContext)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<div id='MVCGrid_{0}_ContextJsonData' style='display: none;'>", gridContext.GridName);

            sb.Append("{");

            sb.AppendFormat("\"name\": \"{0}\"", HttpUtility.JavaScriptStringEncode(gridContext.GridName));
            sb.Append(",");
            sb.AppendFormat("\"sortColumn\": \"{0}\"", HttpUtility.JavaScriptStringEncode(gridContext.QueryOptions.SortColumnName));
            sb.Append(",");
            sb.AppendFormat("\"sortDirection\": \"{0}\"", gridContext.QueryOptions.SortDirection);
            sb.Append(",");
            sb.AppendFormat("\"itemsPerPage\": {0}", gridContext.QueryOptions.ItemsPerPage.HasValue ? gridContext.QueryOptions.ItemsPerPage.ToString() : "\"\"");
            sb.Append(",");
            sb.AppendFormat("\"pageNumber\": {0}", gridContext.QueryOptions.PageIndex.HasValue ? (gridContext.QueryOptions.PageIndex + 1).ToString() : "\"\"");
            sb.Append(",");

            sb.Append("\"columnVisibility\": {");
            sb.Append(GenerateClientJsonVisibility(gridContext));
            sb.Append("}");

            sb.Append(",");

            sb.Append("\"filters\": {");
            sb.Append(GenerateClientJsonFilter(gridContext));
            sb.Append("}");

            sb.Append(",");

            sb.Append("\"additionalQueryOptions\": {");
            sb.Append(GenerateClientJsonAdditional(gridContext));
            sb.Append("}");



            sb.Append("}");

            sb.Append("</div>");

            return sb.ToString();
        }
        public static IMVCGridRenderingEngine GetRenderingEngine(GridContext gridContext)
        {
            IMVCGridRenderingEngine renderingEngine = null;

            if (!String.IsNullOrWhiteSpace(gridContext.QueryOptions.RenderingEngineName))
            {
                if (String.Compare(gridContext.QueryOptions.RenderingEngineName, "export", true) == 0)
                {
                    renderingEngine = new CsvRenderingEngine();
                }
            }

            if (renderingEngine == null)
            {
                renderingEngine = GetRenderingEngineInternal(gridContext.GridDefinition);
            }

            return renderingEngine;
        }
        public void Render(RenderingModel model, GridContext gridContext, TextWriter outputStream)
        {
            HtmlImageSortAsc = String.Format("<img src='{0}/sortup.png' class='pull-right' />", model.HandlerPath);
            HtmlImageSortDsc = String.Format("<img src='{0}/sortdown.png' class='pull-right' />", model.HandlerPath);
            HtmlImageSort = String.Format("<img src='{0}/sort.png' class='pull-right' />", model.HandlerPath);

            string tableCss = gridContext.GridDefinition.GetAdditionalSetting<string>(SettingNameTableClass, DefaultTableCss);

            StringBuilder sbHtml = new StringBuilder();

            sbHtml.AppendFormat("<table id='{0}'", model.TableHtmlId);
            AppendCssAttribute(tableCss, sbHtml);
            sbHtml.Append(">");

            RenderHeader(model, sbHtml);

            if (model.Rows.Count > 0)
            {
                RenderBody(model, sbHtml);
            }
            else
            {
                sbHtml.Append("<tbody>");
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td colspan='{0}'>", model.Columns.Count());
                sbHtml.Append(model.NoResultsMessage);
                sbHtml.Append("</td>");
                sbHtml.Append("</tr>");
                sbHtml.Append("</tbody>");
            }
            sbHtml.AppendLine("</table>");

            RenderPaging(model, sbHtml);

            outputStream.Write(sbHtml.ToString());
            outputStream.Write(model.ClientDataTransferHtmlBlock);
        }
        private static string GenerateClientJsonAdditional(GridContext gridContext)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var aqon in gridContext.GridDefinition.AdditionalQueryOptionNames)
            {
                string val = "";
                if (gridContext.QueryOptions.AdditionalQueryOptions.ContainsKey(aqon))
                {
                    val = gridContext.QueryOptions.AdditionalQueryOptions[aqon];
                }

                if (sb.Length > 0)
                {
                    sb.Append(",");
                }
                sb.AppendFormat("\"{0}\": \"{1}\"", aqon, HttpUtility.JavaScriptStringEncode(val));
            }
            return sb.ToString();
        }
Пример #13
0
        internal override List <Row> GetData(GridContext context, out int?totalRecords)
        {
            List <Row> resultRows = new List <Row>();

            var queryResult = RetrieveData(context);

            totalRecords = queryResult.TotalRecords;

            if (context.GridDefinition.Paging && !totalRecords.HasValue)
            {
                throw new Exception("When paging is enabled, QueryResult must contain the TotalRecords");
            }

            IMVCGridTemplatingEngine templatingEngine = (IMVCGridTemplatingEngine)Activator.CreateInstance(context.GridDefinition.TemplatingEngine, true);

            foreach (var item in queryResult.Items)
            {
                Row thisRow = new Row();

                if (RowCssClassExpression != null)
                {
                    string rowCss = RowCssClassExpression(item, context);
                    if (!String.IsNullOrWhiteSpace(rowCss))
                    {
                        thisRow.CalculatedCssClass = rowCss;
                    }
                }

                foreach (var col in this.Columns)
                {
                    Cell thisCell = new Cell();
                    thisRow.Cells.Add(col.ColumnName, thisCell);

                    thisCell.HtmlText = "";

                    if (col.ValueExpression != null)
                    {
                        thisCell.HtmlText = col.ValueExpression(item, context);
                    }

                    if (!String.IsNullOrWhiteSpace(col.ValueTemplate))
                    {
                        var templateModel = new TemplateModel()
                        {
                            Item        = item,
                            GridContext = context,
                            GridColumn  = col,
                            Row         = thisRow,
                            Value       = thisCell.HtmlText
                        };

                        thisCell.HtmlText = templatingEngine.Process(col.ValueTemplate, templateModel);
                    }

                    if (col.HtmlEncode)
                    {
                        thisCell.HtmlText = System.Web.HttpUtility.HtmlEncode(thisCell.HtmlText);
                    }

                    thisCell.PlainText = thisCell.HtmlText;
                    if (col.PlainTextValueExpression != null)
                    {
                        thisCell.PlainText = col.PlainTextValueExpression(item, context);
                    }

                    if (col.CellCssClassExpression != null)
                    {
                        string cellCss = col.CellCssClassExpression(item, context);
                        if (!String.IsNullOrWhiteSpace(cellCss))
                        {
                            thisCell.CalculatedCssClass = cellCss;
                        }
                    }
                }

                resultRows.Add(thisRow);
            }

            return(resultRows);
        }
Пример #14
0
 internal abstract List <Row> GetData(GridContext context, out int?totalRecords);
        private void RenderBody(RenderingModel model, GridContext gridContext, StringBuilder sbHtml)
        {
            sbHtml.AppendLine("<tbody>");

            var rowIndex = 0;
            foreach (var row in model.Rows)
            {
                sbHtml.Append("<tr");

                if (gridContext.GridDefinition.EnableRowSelect)
                {
                    AppendCssAttribute(String.Join(" ", "row-select", row.CalculatedCssClass), sbHtml);
                    if (!String.IsNullOrEmpty(gridContext.GridDefinition.ClientSideRowSelectFunctionName))
                    {
                        sbHtml.AppendFormat(" data-row-select-id=\"{0}_{1}\"", gridContext.GridName, rowIndex);
                        sbHtml.AppendFormat(" data-row-select-callback=\"{0}\"", gridContext.GridDefinition.ClientSideRowSelectFunctionName);
                    }
                }
                else
                {
                    AppendCssAttribute(row.CalculatedCssClass, sbHtml);
                }

                sbHtml.AppendLine(">");

                foreach (var col in model.Columns)
                {
                    var cell = row.Cells[col.Name];

                    sbHtml.Append("<td");
                    AppendCssAttribute(cell.CalculatedCssClass, sbHtml);
                    sbHtml.Append(">");

                    sbHtml.Append("<span style='display:none;' class='verticalCellLabel'");
                    
                    if (!String.IsNullOrWhiteSpace(col.Onclick))
                    {
                        sbHtml.Append(" style='cursor: pointer;'");
                        sbHtml.AppendFormat(" onclick='{0}'", col.Onclick);
                    }
                    sbHtml.Append(">");
                    sbHtml.Append(col.HeaderText);

                    if (col.SortIconDirection.HasValue)
                    {
                        switch (col.SortIconDirection)
                        {
                            case SortDirection.Asc:
                                sbHtml.Append("<span class='glyphicon glyphicon-triangle-top'></span>");
                                break;
                            case SortDirection.Dsc:
                                sbHtml.Append("<span class='glyphicon glyphicon-triangle-bottom'></span>");
                                break;
                        }
                    }

                    sbHtml.Append("</span>");
                    
                    sbHtml.Append("<span class='verticalCellContent'>");
                    sbHtml.Append(cell.HtmlText);
                    sbHtml.Append("</span>");
                    sbHtml.Append("</td>");
                }
                sbHtml.AppendLine("  </tr>");

                rowIndex++;
            }

            sbHtml.AppendLine("</tbody>");
        }
        private static string GenerateClientJsonFilter(GridContext gridContext)
        {
            StringBuilder sb = new StringBuilder();

            var filterableColumns = gridContext.GridDefinition.GetColumns().Where(p => p.EnableFiltering);
            foreach (var col in filterableColumns)
            {
                string val = "";
                if (gridContext.QueryOptions.Filters.ContainsKey(col.ColumnName))
                {
                    val = gridContext.QueryOptions.Filters[col.ColumnName];
                }

                if (sb.Length > 0)
                {
                    sb.Append(",");
                }
                sb.AppendFormat("\"{0}\": \"{1}\"", col.ColumnName, HttpUtility.JavaScriptStringEncode(val));
            }
            return sb.ToString();
        }
Пример #17
0
        public bool CheckAuthorization(GridContext gridContext)
        {
            bool allowAccess = false;

            switch (gridContext.GridDefinition.AuthorizationType)
            {
                case AuthorizationType.AllowAnonymous:
                    allowAccess = true;
                    break;
                case AuthorizationType.Authorized:
                    allowAccess = (gridContext.CurrentHttpContext.User.Identity.IsAuthenticated);
                    break;
                default:
                    throw new Exception("Unsupported AuthorizationType");
            }

            return allowAccess;
        }