PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Modules/Orchard.Projections/Providers/Layouts/GridLayout.cs

https://github.com/kappel/OrchardCMS
C# | 57 lines | 48 code | 9 blank | 0 comment | 3 complexity | 7bb2481f52615f6d1a945355104f18c7 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Orchard.ContentManagement;
 5using Orchard.DisplayManagement;
 6using Orchard.Localization;
 7using Orchard.Projections.Descriptors.Layout;
 8using Orchard.Projections.Models;
 9using Orchard.Projections.Services;
10
11namespace Orchard.Projections.Providers.Layouts {
12    public class GridLayout : ILayoutProvider {
13        private readonly IContentManager _contentManager;
14        protected dynamic Shape { get; set; }
15
16        public GridLayout(IShapeFactory shapeFactory, IContentManager contentManager) {
17            _contentManager = contentManager;
18            Shape = shapeFactory;
19            T = NullLocalizer.Instance;
20        }
21
22        public Localizer T { get; set; }
23
24        public void Describe(DescribeLayoutContext describe) {
25            describe.For("Html", T("Html"),T("Html Layouts"))
26                .Element("Grid", T("Grid"), T("Organizes content items in a grid."),
27                    DisplayLayout,
28                    RenderLayout,
29                    "GridLayout"
30                );
31        }
32
33        public LocalizedString DisplayLayout(LayoutContext context) {
34            string columns = context.State.Columns;
35            bool horizontal = Convert.ToString(context.State.Alignment) != "vertical";
36
37            return horizontal
38                       ? T("{0} columns grid", columns)
39                       : T("{0} lines grid", columns);
40        }
41
42        public dynamic RenderLayout(LayoutContext context, IEnumerable<LayoutComponentResult> layoutComponentResults) {
43            int columns = Convert.ToInt32(context.State.Columns);
44            bool horizontal = Convert.ToString(context.State.Alignment) != "vertical"; 
45            string rowClass = context.State.RowClass;
46            string gridClass = context.State.GridClass;
47            string gridId = context.State.GridId;
48
49            IEnumerable<dynamic> shapes =
50               context.LayoutRecord.Display == (int)LayoutRecord.Displays.Content
51                   ? layoutComponentResults.Select(x => _contentManager.BuildDisplay(x.ContentItem, context.LayoutRecord.DisplayType))
52                   : layoutComponentResults.Select(x => x.Properties);
53
54            return Shape.Grid(Id: gridId, Horizontal: horizontal, Columns: columns, Items: shapes, Classes: new [] { gridClass }, RowClasses: new [] { rowClass });
55        }
56    }
57}