-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompositeComponentController.cs
107 lines (100 loc) · 7.07 KB
/
CompositeComponentController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.IO;
using System.Web.Mvc;
using Sitecore.Diagnostics;
using Sitecore.Mvc.Common;
using Sitecore.Mvc.Pipelines;
using Sitecore.Mvc.Pipelines.Response.RenderPlaceholder;
using Sitecore.Mvc.Presentation;
using Sitecore.Sites;
using Sitecore.Text;
namespace BoC.Sitecore.CompositeComponents.Controllers
{
public class CompositeComponentController : Controller
{
private const string CompositeComponentInstanceTemplateId = "{14F4886E-D0A8-4E9C-9EE6-2C7F36C251AC}";
public ActionResult EditLayout()
{
var pageContext = PageContext.CurrentOrNull;
Assert.IsNotNull(pageContext, "Page context is required");
var stringWriter = new StringWriter();
stringWriter.Write("<html><head></head><body>");
PipelineService.Get().RunPipeline<RenderPlaceholderArgs>("mvc.renderPlaceholder",
new RenderPlaceholderArgs(pageContext.Item["PlaceholderName"] ?? "compositecontent", (TextWriter)stringWriter, new ContentRendering()));
stringWriter.Write("</body></html>");
return Content(stringWriter.ToString());
}
// GET: Snippet
public ActionResult CompositeComponent()
{
var renderingContext = RenderingContext.CurrentOrNull;
if (renderingContext == null)
throw new ApplicationException("Could not find current rendering context, aborting");
var hasDataSource = !string.IsNullOrWhiteSpace(renderingContext.Rendering.DataSource);
var dataSourceItem = hasDataSource ? renderingContext.Rendering.Item : null;
var pageContext = new PageContext()
{
RequestContext = this.ControllerContext.RequestContext,
Item = dataSourceItem
};
var oldDisplayMode = global::Sitecore.Context.Site != null ? global::Sitecore.Context.Site.DisplayMode : DisplayMode.Normal;
try
{
if (oldDisplayMode == DisplayMode.Edit && global::Sitecore.Context.Site != null)
{
//disable the editing of the nested component
global::Sitecore.Context.Site.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Temporary);
}
using (PlaceholderContext.Enter(new PlaceholderContext("/")))
using (ContextService.Get().Push<PageContext>(pageContext))
{
var stringWriter = new StringWriter();
if (oldDisplayMode == DisplayMode.Edit)
{
if (hasDataSource)
{
UrlString webSiteUrl = SiteContext.GetWebSiteUrl(global::Sitecore.Context.ContentDatabase ?? global::Sitecore.Context.Database);
webSiteUrl.Add("sc_mode", "edit");
webSiteUrl.Add("sc_itemid", pageContext.Item.ID.ToString());
webSiteUrl.Add("sc_lang", pageContext.Item.Language.ToString());
//copied style from bootstrap alert
stringWriter.Write("<div role=\"alert\" class=\"alert alert-warning\" style=\"box-sizing: border-box; margin-bottom: 20px; border-top: rgb(250,235,204) 1px solid; border-right: rgb(250,235,204) 1px solid; white-space: normal; word-spacing: 0px; border-bottom: rgb(250,235,204) 1px solid; text-transform: none; color: rgb(138,109,59); padding-bottom: 15px; padding-top: 15px; font: 14px/20px 'Helvetica Neue', helvetica, arial, sans-serif; padding-left: 15px; border-left: rgb(250,235,204) 1px solid; widows: 1; letter-spacing: normal; padding-right: 15px; background-color: rgb(252,248,227); text-indent: 0px; border-radius: 4px; -webkit-text-stroke-width: 0px\">");
stringWriter.Write("<a class=\"alert-link\" style=\"box-sizing: border-box; text-decoration: none; font-weight: 700; color: rgb(102,81,44); background-color: transparent\" href=\"");
stringWriter.Write(webSiteUrl);
stringWriter.Write("\" target=\"_blank\" onmousedown=\"window.open(this.href)\">"");
stringWriter.Write(pageContext.Item.DisplayName);
stringWriter.Write("" is a 'composite component'. Click here to open it's editor</a><br /></div>");
}
else
{
//copied style from bootstrap alert
stringWriter.Write("<div role=\"alert\" class=\"alert alert-warning\" style=\"box-sizing: border-box; margin-bottom: 20px; border-top: rgb(250,235,204) 1px solid; border-right: rgb(250,235,204) 1px solid; white-space: normal; word-spacing: 0px; border-bottom: rgb(250,235,204) 1px solid; text-transform: none; color: rgb(138,109,59); padding-bottom: 15px; padding-top: 15px; font: 14px/20px 'Helvetica Neue', helvetica, arial, sans-serif; padding-left: 15px; border-left: rgb(250,235,204) 1px solid; widows: 1; letter-spacing: normal; padding-right: 15px; background-color: rgb(252,248,227); text-indent: 0px; border-radius: 4px; -webkit-text-stroke-width: 0px\">");
stringWriter.Write("<a class=\"alert-link\" style=\"box-sizing: border-box; text-decoration: none; font-weight: 700; color: rgb(102,81,44); background-color: transparent\" href=\"\" onmousedown=\"");
stringWriter.Write("Sitecore.PageModes.PageEditor.postRequest('webedit:setdatasource(referenceId=");
stringWriter.Write(renderingContext.Rendering.UniqueId.ToString("B").ToUpper());
stringWriter.Write(",renderingId=");
stringWriter.Write(renderingContext.Rendering.RenderingItem.ID);
stringWriter.Write(",id=");
stringWriter.Write(renderingContext.Rendering.Item.ID);
stringWriter.Write(")',null,false)");
stringWriter.Write("\" target=\"_blank\">This is a 'composite component' without a datasource. Click here to associate a composite component instance</a><br /></div>");
}
}
if (hasDataSource && dataSourceItem != null &&
dataSourceItem?.TemplateID == new global::Sitecore.Data.ID(CompositeComponentInstanceTemplateId) &&
!string.IsNullOrEmpty(pageContext.Item?["PlaceholderName"]))
{
PipelineService.Get().RunPipeline<RenderPlaceholderArgs>("mvc.renderPlaceholder",
new RenderPlaceholderArgs(pageContext.Item["PlaceholderName"],
(TextWriter)stringWriter, new ContentRendering()));
}
return Content(stringWriter.ToString());
}
}
finally
{
global::Sitecore.Context.Site.SetDisplayMode(oldDisplayMode, DisplayModeDuration.Temporary);
}
}
}
}