コード例 #1
0
        protected static async Task <string> ParseElementAsync(IParseManager parseManager, ListInfo listInfo, List <KeyValuePair <int, Channel> > channels)
        {
            var pageInfo = parseManager.PageInfo;

            if (channels == null || channels.Count == 0)
            {
                return(string.Empty);
            }

            var builder = new StringBuilder();

            if (listInfo.Layout == Model.Layout.None)
            {
                if (!string.IsNullOrEmpty(listInfo.HeaderTemplate))
                {
                    builder.Append(listInfo.HeaderTemplate);
                }

                var isAlternative = false;
                var isSeparator   = false;
                if (!string.IsNullOrEmpty(listInfo.AlternatingItemTemplate))
                {
                    isAlternative = true;
                }
                if (!string.IsNullOrEmpty(listInfo.SeparatorTemplate))
                {
                    isSeparator = true;
                }

                for (var i = 0; i < channels.Count; i++)
                {
                    var channel = channels[i];

                    pageInfo.ChannelItems.Push(channel);
                    var templateString = isAlternative && i % 2 == 1 ? listInfo.AlternatingItemTemplate : listInfo.ItemTemplate;
                    var parsedString   = await TemplateUtility.GetChannelsItemTemplateStringAsync(templateString,
                                                                                                  listInfo.SelectedItems, listInfo.SelectedValues, string.Empty, parseManager, ParseType.Channel);

                    builder.Append(parsedString);

                    if (isSeparator && i != channels.Count - 1)
                    {
                        builder.Append(listInfo.SeparatorTemplate);
                    }
                }

                if (!string.IsNullOrEmpty(listInfo.FooterTemplate))
                {
                    builder.Append(listInfo.FooterTemplate);
                }
            }
            else
            {
                var isAlternative = !string.IsNullOrEmpty(listInfo.AlternatingItemTemplate);

                var tableAttributes = listInfo.GetTableAttributes();
                var cellAttributes  = listInfo.GetCellAttributes();

                using var table = new HtmlTable(builder, tableAttributes);
                if (!string.IsNullOrEmpty(listInfo.HeaderTemplate))
                {
                    table.StartHead();
                    using (var tHead = table.AddRow())
                    {
                        tHead.AddCell(listInfo.HeaderTemplate, cellAttributes);
                    }
                    table.EndHead();
                }

                table.StartBody();

                var columns   = listInfo.Columns <= 1 ? 1 : listInfo.Columns;
                var itemIndex = 0;

                while (true)
                {
                    using var tr = table.AddRow();
                    for (var cell = 1; cell <= columns; cell++)
                    {
                        var cellHtml = string.Empty;
                        if (itemIndex < channels.Count)
                        {
                            var channel = channels[itemIndex];

                            pageInfo.ChannelItems.Push(channel);
                            var templateString = isAlternative && itemIndex % 2 == 1
                                ? listInfo.AlternatingItemTemplate
                                : listInfo.ItemTemplate;
                            cellHtml = await TemplateUtility.GetChannelsItemTemplateStringAsync(templateString,
                                                                                                listInfo.SelectedItems, listInfo.SelectedValues, string.Empty, parseManager,
                                                                                                ParseType.Channel);
                        }

                        tr.AddCell(cellHtml, cellAttributes);
                        itemIndex++;
                    }

                    if (itemIndex >= channels.Count)
                    {
                        break;
                    }
                }

                table.EndBody();

                if (!string.IsNullOrEmpty(listInfo.FooterTemplate))
                {
                    table.StartFoot();
                    using (var tFoot = table.AddRow())
                    {
                        tFoot.AddCell(listInfo.FooterTemplate, cellAttributes);
                    }
                    table.EndFoot();
                }
            }

            return(builder.ToString());
        }