private static List <RowViewComponent> ArrangeArticles(IEnumerable <Entry> articles, List <int> layout)
        {
            LinkedList <Entry> list = new LinkedList <Entry>(articles);
            var head = list.First;

            List <RowViewComponent>    rows    = new List <RowViewComponent>();
            List <ColumnViewComponent> columns = new List <ColumnViewComponent>();

            List <ViewComponent> components = new List <ViewComponent>();
            bool filledColumn  = false;
            int  currentLength = 0;
            bool stacking      = false;
            int  maxWidth      = 0;
            bool loop          = true;

            for (int i = 0; i < layout.Count() && loop; ++i)
            {
                if (head != null)
                {
                    var width = layout[i];

                    switch (width)
                    {
                    case 48:
                        filledColumn = true;
                        break;

                    case 24:
                        stacking = true;
                        break;

                    case 9:
                        components.Add(HorizontalLargeBlockViewComponent.ToViewModel(head.Value, columns.Count > 0));

                        head = head.Next;

                        if (width > maxWidth)
                        {
                            maxWidth = width;
                        }
                        break;

                    case 3:
                        components.Add(VerticalNarrowBlockViewComponent.ToViewModel(head.Value, columns.Count > 0));

                        head = head.Next;

                        if (width > maxWidth)
                        {
                            maxWidth = width;
                        }
                        break;

                    case 6:
                    case 8:
                    case 5:
                    case 7:
                    case 4:
                        components.Add(stacking ? (ViewComponent)HorizontalMediumBlockViewComponent.ToViewModel(head.Value, columns.Count > 0) :
                                       VerticalBlockViewComponent.ToViewModel(head.Value, columns.Count > 0));

                        head = head.Next;

                        if (width > maxWidth)
                        {
                            maxWidth = width;
                        }
                        break;
                    }
                }
                else
                {
                    loop = false;

                    if (components.Any())
                    {
                        filledColumn = true;
                    }
                }

                if (filledColumn)
                {
                    columns.Add(new ColumnViewComponent()
                    {
                        Width      = maxWidth,
                        Components = components
                    });

                    currentLength += maxWidth;

                    components   = new List <ViewComponent>();
                    filledColumn = false;
                    maxWidth     = 0;
                    stacking     = false;
                }

                if (currentLength >= 12)
                {
                    rows.Add(new RowViewComponent()
                    {
                        Columns = columns
                    });

                    columns       = new List <ColumnViewComponent>();
                    currentLength = 0;
                }
            }

            if (columns.Count > 0)
            {
                rows.Add(new RowViewComponent()
                {
                    Columns = columns
                });
            }

            return(rows);
        }
Example #2
0
        private GroupingViewComponent GroupArticles(
            List <BasicArticle> entries,
            IEnumerable <int> layout)
        {
            List <RowViewComponent> rows = new List <RowViewComponent>();

            int currWidth = 0;
            List <ViewComponent>       components = new List <ViewComponent>();
            List <ColumnViewComponent> columns    = new List <ColumnViewComponent>();

            int i = 0;

            foreach (var block in layout)
            {
                switch (block)
                {
                case 3:
                    columns.Add(new ColumnViewComponent()
                    {
                        Width      = block,
                        Components = new List <ViewComponent>()
                        {
                            VerticalNarrowBlockViewComponent.ToViewModel(entries[i], columns.Count > 0)
                        },
                        Rows = null
                    });

                    currWidth += block;
                    break;

                case 9:
                    columns.Add(new ColumnViewComponent()
                    {
                        Width      = block,
                        Components = new List <ViewComponent>()
                        {
                            HorizontalLargeBlockViewComponent.ToViewModel(entries[i], columns.Count > 0)
                        },
                        Rows = null
                    });

                    currWidth += block;
                    break;

                case 5:
                case 6:
                    columns.Add(new ColumnViewComponent()
                    {
                        Width      = block,
                        Components = new List <ViewComponent>()
                        {
                            VerticalBlockViewComponent.ToViewModel(entries[i], columns.Count > 0)
                        },
                        Rows = null
                    });

                    currWidth += block;
                    break;

                case 7:
                    if (components.Any())
                    {
                        components.Add(HorizontalMediumBlockViewComponent.ToViewModel(entries[i], columns.Count > 0));

                        columns.Add(new ColumnViewComponent()
                        {
                            Width      = block,
                            Components = components,
                            Rows       = null
                        });

                        currWidth += block;
                        components = new List <ViewComponent>();
                    }
                    else
                    {
                        components.Add(HorizontalMediumBlockViewComponent.ToViewModel(entries[i], columns.Count > 0));
                    }
                    break;
                }

                if (currWidth >= 12)
                {
                    rows.Add(new RowViewComponent()
                    {
                        Columns = columns
                    });

                    currWidth = 0;
                    columns   = new List <ColumnViewComponent>();
                }

                ++i;
            }

            return(GroupingViewComponent.ToViewModel(new GroupingViewModel()
            {
                Grid = rows
            }));
        }