Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #47 from umco/develop
Browse files Browse the repository at this point in the history
Preparing v1.2.0 release
  • Loading branch information
leekelleher authored Jul 16, 2018
2 parents 720df6d + c3b2201 commit 634a201
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 51 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
image: Visual Studio 2017

# version format
version: 1.1.0.{build}
version: 1.2.0.{build}

# UMBRACO_PACKAGE_PRERELEASE_SUFFIX if a rtm release build this should be blank, otherwise if empty will default to alpha
# example UMBRACO_PACKAGE_PRERELEASE_SUFFIX=beta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

namespace Our.Umbraco.StackedContent.Converters
{
[PropertyValueType(typeof(IEnumerable<IPublishedContent>))]
public class StackedContentValueConverter : InnerContentValueConverter
public class StackedContentValueConverter : InnerContentValueConverter, IPropertyValueConverterMeta
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
Expand All @@ -22,18 +21,14 @@ public override bool IsConverter(PublishedPropertyType propertyType)

public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null)
return null;

var str = source.ToString();
if (string.IsNullOrWhiteSpace(str))
var value = source?.ToString();
if (value == null || string.IsNullOrWhiteSpace(value))
return null;

try
{
var rawValue = JsonConvert.DeserializeObject<JArray>(str);

return ConvertInnerContentDataToSource(rawValue, null, 1, preview);
var items = JsonConvert.DeserializeObject<JArray>(value);
return ConvertInnerContentDataToSource(items, null, 1, preview);
}
catch (Exception ex)
{
Expand All @@ -42,5 +37,15 @@ public override object ConvertDataToSource(PublishedPropertyType propertyType, o

return null;
}

public Type GetPropertyValueType(PublishedPropertyType propertyType)
{
return typeof(IEnumerable<IPublishedContent>);
}

public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
{
return PropertyCacheLevel.Content;
}
}
}
125 changes: 125 additions & 0 deletions src/Our.Umbraco.StackedContent/Models/UnpublishedContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web.Models;

namespace Our.Umbraco.StackedContent.Models
{
internal class UnpublishedContent : PublishedContentWithKeyBase
{
private readonly IContent content;

private readonly Lazy<IEnumerable<IPublishedContent>> children;
private readonly Lazy<PublishedContentType> contentType;
private readonly Lazy<string> creatorName;
private readonly Lazy<IPublishedContent> parent;
private readonly Lazy<Dictionary<string, IPublishedProperty>> properties;
private readonly Lazy<string> urlName;
private readonly Lazy<string> writerName;

public UnpublishedContent(int id, ServiceContext serviceContext)
: this(serviceContext.ContentService.GetById(id), serviceContext)
{ }

public UnpublishedContent(IContent content, ServiceContext serviceContext)
: base()
{
Mandate.ParameterNotNull(content, nameof(content));
Mandate.ParameterNotNull(serviceContext, nameof(serviceContext));

var userService = new Lazy<IUserService>(() => serviceContext.UserService);

this.content = content;

this.children = new Lazy<IEnumerable<IPublishedContent>>(() => this.content.Children().Select(x => new UnpublishedContent(x, serviceContext)).ToList());
this.contentType = new Lazy<PublishedContentType>(() => PublishedContentType.Get(this.ItemType, this.DocumentTypeAlias));
this.creatorName = new Lazy<string>(() => this.content.GetCreatorProfile(userService.Value).Name);
this.parent = new Lazy<IPublishedContent>(() => new UnpublishedContent(this.content.Parent(), serviceContext));
this.properties = new Lazy<Dictionary<string, IPublishedProperty>>(() => MapProperties(PropertyEditorResolver.Current, serviceContext));
this.urlName = new Lazy<string>(() => this.content.Name.ToUrlSegment());
this.writerName = new Lazy<string>(() => this.content.GetWriterProfile(userService.Value).Name);
}

public override Guid Key => this.content.Key;

public override PublishedItemType ItemType => PublishedItemType.Content;

public override int Id => this.content.Id;

public override int TemplateId => this.content.Template?.Id ?? default(int);

public override int SortOrder => this.content.SortOrder;

public override string Name => this.content.Name;

public override string UrlName => this.urlName.Value;

public override string DocumentTypeAlias => this.content.ContentType?.Alias;

public override int DocumentTypeId => this.content.ContentType?.Id ?? default(int);

public override string WriterName => this.writerName.Value;

public override string CreatorName => this.creatorName.Value;

public override int WriterId => this.content.WriterId;

public override int CreatorId => this.content.CreatorId;

public override string Path => this.content.Path;

public override DateTime CreateDate => this.content.CreateDate;

public override DateTime UpdateDate => this.content.UpdateDate;

public override Guid Version => this.content.Version;

public override int Level => this.content.Level;

public override bool IsDraft => true;

public override IPublishedContent Parent => this.parent.Value;

public override IEnumerable<IPublishedContent> Children => this.children.Value;

public override PublishedContentType ContentType => this.contentType.Value;

public override ICollection<IPublishedProperty> Properties => this.properties.Value.Values;

public override IPublishedProperty GetProperty(string alias)
{
return this.properties.Value.TryGetValue(alias, out IPublishedProperty property) ? property : null;
}

private Dictionary<string, IPublishedProperty> MapProperties(PropertyEditorResolver resolver, ServiceContext services)
{
var contentType = this.contentType.Value;
var properties = this.content.Properties;

var items = new Dictionary<string, IPublishedProperty>(StringComparer.InvariantCultureIgnoreCase);

foreach (var propertyType in contentType.PropertyTypes)
{
var property = properties.FirstOrDefault(x => x.Alias.InvariantEquals(propertyType.PropertyTypeAlias));
var value = property?.Value;
if (value != null)
{
var propertyEditor = resolver.GetByAlias(propertyType.PropertyEditorAlias);
if (propertyEditor != null)
{
value = propertyEditor.ValueEditor.ConvertDbToString(property, property.PropertyType, services.DataTypeService);
}
}

items.Add(propertyType.PropertyTypeAlias, new UnpublishedProperty(propertyType, value));
}

return items;
}
}
}
38 changes: 38 additions & 0 deletions src/Our.Umbraco.StackedContent/Models/UnpublishedProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;

namespace Our.Umbraco.StackedContent.Models
{
internal class UnpublishedProperty : IPublishedProperty
{
private readonly PublishedPropertyType propertyType;
private readonly object dataValue;
private readonly Lazy<bool> hasValue;
private readonly Lazy<object> sourceValue;
private readonly Lazy<object> objectValue;
private readonly Lazy<object> xpathValue;

public UnpublishedProperty(PublishedPropertyType propertyType, object value)
{
this.propertyType = propertyType;

this.dataValue = value;
this.hasValue = new Lazy<bool>(() => value != null && value.ToString().Trim().Length > 0);

this.sourceValue = new Lazy<object>(() => this.propertyType.ConvertDataToSource(this.dataValue, true));
this.objectValue = new Lazy<object>(() => this.propertyType.ConvertSourceToObject(this.sourceValue.Value, true));
this.xpathValue = new Lazy<object>(() => this.propertyType.ConvertSourceToXPath(this.sourceValue.Value, true));
}

public string PropertyTypeAlias => this.propertyType.PropertyTypeAlias;

public bool HasValue => this.hasValue.Value;

public object DataValue => this.dataValue;

public object Value => this.objectValue.Value;

public object XPathValue => this.xpathValue.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Converters\StackedContentValueConverter.cs" />
<Compile Include="Models\UnpublishedContent.cs" />
<Compile Include="Models\UnpublishedProperty.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\VersionInfo.cs" />
<Compile Include="PropertyEditors\StackedContentPropertyEditor.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Umbraco.Core.PropertyEditors;
using Our.Umbraco.InnerContent.PropertyEditors;
using Our.Umbraco.InnerContent.PropertyEditors;
using Umbraco.Core.PropertyEditors;

namespace Our.Umbraco.StackedContent.PropertyEditors
{
Expand All @@ -16,6 +16,11 @@ public StackedContentPropertyEditor()
DefaultPreValues.Add("disablePreview", "0");
}

protected override PropertyValueEditor CreateValueEditor()
{
return new SimpleInnerContentPropertyValueEditor(base.CreateValueEditor());
}

protected override PreValueEditor CreatePreValueEditor()
{
return new StackedContentPreValueEditor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Web.Http;
using Newtonsoft.Json.Linq;
using Our.Umbraco.InnerContent.Helpers;
using Our.Umbraco.StackedContent.Models;
using Our.Umbraco.StackedContent.Web.Helpers;
using Umbraco.Core.Models;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;

Expand All @@ -13,32 +16,38 @@ namespace Our.Umbraco.StackedContent.Web.Controllers
public class StackedContentApiController : UmbracoAuthorizedApiController
{
[HttpPost]
public HttpResponseMessage GetPreviewMarkup([FromBody] JObject item, int parentId)
public HttpResponseMessage GetPreviewMarkup([FromBody] JObject item, int pageId)
{
// Get parent to container node
//TODO: Convert IContent if no published content?
var parent = UmbracoContext.ContentCache.GetById(parentId);
var page = default(IPublishedContent);

// If the page is new, then the ID will be zero
if (pageId > 0)
{
// Get page container node
page = UmbracoContext.ContentCache.GetById(pageId);
if (page == null)
{
// If unpublished, then fake PublishedContent (with IContent object)
page = new UnpublishedContent(pageId, Services);
}
}

// Convert item
var content = InnerContentHelper.ConvertInnerContentToPublishedContent(item, parent);
var content = InnerContentHelper.ConvertInnerContentToPublishedContent(item, page);

// Construct preview model
var model = new PreviewModel { Page = parent, Item = content };
var model = new PreviewModel { Page = page, Item = content };

// Render view
var markup = ViewHelper.RenderPartial(content.DocumentTypeAlias, model, new[]
{
"~/Views/Partials/Stack/{0}.cshtml",
"~/Views/Partials/Stack/Default.cshtml",
});
var markup = ViewHelper.RenderPartial(content.DocumentTypeAlias, model);

// Return response
var response = new HttpResponseMessage
{
Content = new StringContent(markup ?? string.Empty)
};

response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
response.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Text.Html);

return response;
}
Expand Down
40 changes: 28 additions & 12 deletions src/Our.Umbraco.StackedContent/Web/Helpers/ViewHelper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core.Logging;

namespace Our.Umbraco.StackedContent.Web.Helpers
{
internal static class ViewHelper
public static class ViewHelper
{
class DummyController : Controller { }
private class DummyController : Controller { }

public static string RenderPartial(string partialName, object model, string[] viewLocations)
private static readonly RazorViewEngine ViewEngine = new RazorViewEngine
{
PartialViewLocationFormats = new[]
{
"~/Views/Partials/Stack/{0}.cshtml",
"~/Views/Partials/Stack/Default.cshtml"
}
};

public static void AddViewLocationFormats(params string[] viewLocationFormats)
{
var newFormats = ViewEngine
.PartialViewLocationFormats
.Union(viewLocationFormats)
.ToArray();

ViewEngine.PartialViewLocationFormats = newFormats;
}

internal static string RenderPartial(string partialName, object model)
{
using (var sw = new StringWriter())
{
Expand All @@ -19,16 +40,11 @@ public static string RenderPartial(string partialName, object model, string[] vi
routeData.Values.Add("controller", "DummyController");

var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new DummyController());

var viewEngine = new RazorViewEngine
{
PartialViewLocationFormats = viewLocations
};

var viewResult = viewEngine.FindPartialView(controllerContext, partialName, false);

var viewResult = ViewEngine.FindPartialView(controllerContext, partialName, false);
if (viewResult.View == null)
{
// TODO: Log lack of view?
LogHelper.Warn(typeof(ViewHelper), $"No view found for partial '{partialName}'");
return null;
}

Expand All @@ -38,4 +54,4 @@ public static string RenderPartial(string partialName, object model, string[] vi
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
position: relative;
display: block;
margin: 0;
z-index: 10;
z-index: 9;
}

.stack__preview-wrapper {
Expand Down
Loading

0 comments on commit 634a201

Please sign in to comment.