Skip to content

Commit

Permalink
Implement Html/Picture on mocked Item #3141
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed Aug 10, 2023
1 parent 9551da9 commit 9e3ebb3
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 81 deletions.
51 changes: 11 additions & 40 deletions Src/Sxc/ToSic.Sxc/Data/DynamicEntity/DynamicEntity_Html.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Runtime.CompilerServices;
using ToSic.Eav.Plumbing;
using ToSic.Lib.Documentation;
using ToSic.Razor.Blade;
using ToSic.Sxc.Data.Typed;
using ToSic.Sxc.Images;
using ToSic.Sxc.Services;
using static ToSic.Eav.Parameters;

namespace ToSic.Sxc.Data
Expand All @@ -20,14 +18,11 @@ public IHtmlTag Html(
bool? toolbar = default,
object imageSettings = default,
bool debug = default
)
{
// Only do compatibility check if used on DynamicEntity
if (_Cdf.CompatibilityLevel < Constants.CompatibilityLevel12)
throw new NotSupportedException($"{nameof(Html)}(...) not supported in older Razor templates. Use Hybrid14 or newer.");

return (this as ITypedItem).Html(name: name, noParamOrder: noParamOrder, container: container, toolbar: toolbar, imageSettings: imageSettings, debug: debug);
}
) => _Cdf.CompatibilityLevel < Constants.CompatibilityLevel12
// Only do compatibility check if used on DynamicEntity
? throw new NotSupportedException($"{nameof(Html)}(...) not supported in older Razor templates. Use Razor14, RazorPro or newer.")
: TypedItemHelpers.Html(_Cdf, this, name: name, noParamOrder: noParamOrder, container: container,
toolbar: toolbar, imageSettings: imageSettings, required: false, debug: debug);

IHtmlTag ITypedItem.Html(
string name,
Expand All @@ -37,14 +32,8 @@ IHtmlTag ITypedItem.Html(
object imageSettings,
bool? required,
bool debug
)
{
Protect(noParamOrder, $"{nameof(container)}, {nameof(imageSettings)}, {nameof(toolbar)}, {nameof(required)}, {nameof(debug)}...");

var kit = GetServiceKitOrThrow();
var field = (this as ITypedItem).Field(name, required: required);
return kit.Cms.Html(field, container: container, classes: null, imageSettings: imageSettings, debug: debug, toolbar: toolbar);
}
) => TypedItemHelpers.Html(_Cdf, this, name: name, noParamOrder: noParamOrder, container: container,
toolbar: toolbar, imageSettings: imageSettings, required: required, debug: debug);

/// <inheritdoc/>
IResponsivePicture ITypedItem.Picture(
Expand All @@ -57,26 +46,8 @@ IResponsivePicture ITypedItem.Picture(
string imgAltFallback,
string imgClass,
object recipe
)
{
Protect(noParamOrder, $"{nameof(settings)}, {nameof(factor)}, {nameof(width)}, {nameof(imgAlt)}...");
var kit = GetServiceKitOrThrow();
var field = (this as ITypedItem).Field(name, required: true);
return field.Url.IsEmptyOrWs()
? null
: kit.Image.Picture(field, settings: settings, factor: factor, width: width, imgAlt: imgAlt, imgAltFallback: imgAltFallback, imgClass: imgClass, recipe: recipe);
}


private ServiceKit14 GetServiceKitOrThrow([CallerMemberName] string cName = default)
{
if (_Cdf == null)
throw new NotSupportedException(
$"Trying to use {cName}(...) in a scenario where the {nameof(_Cdf)} is not available.");

var kit = _Cdf.GetServiceKit14();
return kit ?? throw new NotSupportedException(
$"Trying to use {cName}(...) in a scenario where the {nameof(ServiceKit14)} is not available.");
}
) => TypedItemHelpers.Picture(cdf: _Cdf, item: this, name: name, noParamOrder: noParamOrder, settings: settings,
factor: factor, width: width, imgAlt: imgAlt,
imgAltFallback: imgAltFallback, imgClass: imgClass, recipe: recipe);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ IFile ITypedItem.File(string name, string noParamOrder, bool? required)
var typedThis = this as ITypedItem;
// Case 1: The field contains a direct reference to a file
var field = typedThis.Field(name, required: required);
var file = GetServiceKitOrThrow().Adam.File(field);
var file = _Cdf.GetServiceKitOrThrow().Adam.File(field);
// Case 2: No direct reference, just get the first file in the folder of this field
return file ?? typedThis.Folder(name).Files.FirstOrDefault();
}
Expand Down
7 changes: 0 additions & 7 deletions Src/Sxc/ToSic.Sxc/Data/Factory/CodeDataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using ToSic.Lib.Helpers;
using ToSic.Sxc.Adam;
using ToSic.Sxc.Blocks;
using ToSic.Sxc.Code;
using ToSic.Sxc.Context;
using ToSic.Sxc.Data.Wrapper;
using ToSic.Sxc.Services;
Expand Down Expand Up @@ -51,12 +50,6 @@ public void SetFallbacks(ISite site, int? compatibility = default, AdamManager a
private int _compatibilityLevel = Constants.CompatibilityLevel10;


#region Kit - used by some things created by ASC

public ServiceKit14 GetServiceKit14() => _DynCodeRoot.GetKit<ServiceKit14>();

#endregion

#region CodeDataServices

public CodeDataServices Services => _codeDataServices.Value;
Expand Down
26 changes: 26 additions & 0 deletions Src/Sxc/ToSic.Sxc/Data/Factory/CodeDataFactoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Runtime.CompilerServices;
using System;
using ToSic.Sxc.Services;
using ToSic.Sxc.Code;

namespace ToSic.Sxc.Data
{
internal static class CodeDataFactoryExtensions
{
/// <summary>
/// Will check if the CodeDataFactory exists and try to get the ServiceKit - or throw an error.
/// </summary>
/// <exception cref="NotSupportedException"></exception>
internal static ServiceKit16 GetServiceKitOrThrow(this CodeDataFactory cdf, [CallerMemberName] string cName = default)
{
if (cdf == null)
throw new NotSupportedException(
$"Trying to use {cName}(...) in a scenario where the {nameof(cdf)} is not available.");

var kit = cdf._DynCodeRoot.GetKit<ServiceKit16>();
return kit ?? throw new NotSupportedException(
$"Trying to use {cName}(...) in a scenario where the {nameof(ServiceKit16)} is not available.");
}

}
}
7 changes: 3 additions & 4 deletions Src/Sxc/ToSic.Sxc/Data/Field/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ internal Field(ITypedItem parent, string name, CodeDataFactory cdf)
private string _url;


public IMetadata Metadata => _dynMeta.Get(() => new Metadata(MetadataOfItem, Parent.Entity, _cdf));
public IMetadata Metadata => _dynMeta.Get(() => new Metadata(MetadataOfValue, Parent.Entity, _cdf));
private readonly GetOnce<IMetadata> _dynMeta = new GetOnce<IMetadata>();


private IMetadataOf MetadataOfItem => _itemMd.Get(() =>
private IMetadataOf MetadataOfValue => _itemMd.Get(() =>
{
if (!(Raw is string rawString) || string.IsNullOrWhiteSpace(rawString)) return null;
var appState = _cdf?.BlockOrNull?.Context?.AppState;
var md = appState?.GetMetadataOf(TargetTypes.CmsItem, rawString, "");

ImageDecorator.AddRecommendations(md, Value as string);
return md;
});
Expand All @@ -57,6 +56,6 @@ internal Field(ITypedItem parent, string name, CodeDataFactory cdf)
_imgDec2.Get(() => ImageDecorator.GetOrNull(this, _cdf.Dimensions));
private readonly GetOnce<ImageDecorator> _imgDec2 = new GetOnce<ImageDecorator>();

IMetadataOf IHasMetadata.Metadata => MetadataOfItem;
IMetadataOf IHasMetadata.Metadata => MetadataOfValue;
}
}
1 change: 0 additions & 1 deletion Src/Sxc/ToSic.Sxc/Data/Field/IField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public interface IField: IHasLink, IHasMetadata
/// </summary>
object Value { get; }


/// <summary>
/// Metadata of the thing in the field - if it has such metadata.
///
Expand Down
51 changes: 51 additions & 0 deletions Src/Sxc/ToSic.Sxc/Data/Typed/TypedItemHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using ToSic.Eav.Plumbing;
using ToSic.Razor.Blade;
using ToSic.Sxc.Images;
using static ToSic.Eav.Parameters;

namespace ToSic.Sxc.Data.Typed
{
internal class TypedItemHelpers
{
public static IHtmlTag Html(
CodeDataFactory cdf,
ITypedItem item,
string name,
string noParamOrder,
object container,
bool? toolbar,
object imageSettings,
bool? required,
bool debug
)
{
Protect(noParamOrder, $"{nameof(container)}, {nameof(imageSettings)}, {nameof(toolbar)}, {nameof(required)}, {nameof(debug)}...");
var kit = cdf.GetServiceKitOrThrow();
var field = item.Field(name, required: required);
return kit.Cms.Html(field, container: container, classes: null, imageSettings: imageSettings, debug: debug, toolbar: toolbar);
}

public static IResponsivePicture Picture(
CodeDataFactory cdf,
ITypedItem item,
string name,
string noParamOrder,
object settings,
object factor,
object width,
string imgAlt,
string imgAltFallback,
string imgClass,
object recipe
)
{
Protect(noParamOrder, $"{nameof(settings)}, {nameof(factor)}, {nameof(width)}, {nameof(imgAlt)}...");
var kit = cdf.GetServiceKitOrThrow();
var field = item.Field(name, required: true);
return field.Url.IsEmptyOrWs()
? null
: kit.Image.Picture(field, settings: settings, factor: factor, width: width, imgAlt: imgAlt, imgAltFallback: imgAltFallback, imgClass: imgClass, recipe: recipe);
}

}
}
11 changes: 0 additions & 11 deletions Src/Sxc/ToSic.Sxc/Data/Typed/TypedItemValue.cs

This file was deleted.

34 changes: 20 additions & 14 deletions Src/Sxc/ToSic.Sxc/Data/Typed/WrapObjectTypedItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using ToSic.Eav.Data;
using ToSic.Eav.Data.Build;
using ToSic.Eav.Metadata;
Expand Down Expand Up @@ -32,17 +31,17 @@ public WrapObjectTypedItem(PreWrapObject preWrap, CodeDataWrapper wrapper, ILazy
bool ITypedItem.IsDemoItem => PreWrap.TryGet(nameof(ITypedItem.IsDemoItem), noParamOrder: Protector, fallback: false, required: false);

IHtmlTag ITypedItem.Html(string name, string noParamOrder, object container, bool? toolbar,
object imageSettings, bool? required, bool debug)
{
throw new NotImplementedException();
}
object imageSettings, bool? required, bool debug
) => TypedItemHelpers.Html(_cdf.Value, this, name: name, noParamOrder: noParamOrder, container: container,
toolbar: toolbar, imageSettings: imageSettings, required: required, debug: debug);

IResponsivePicture ITypedItem.Picture(string name, string noParamOrder, object settings,
object factor, object width, string imgAlt, string imgAltFallback,
string imgClass, object recipe)
{
throw new NotImplementedException();
}
string imgClass, object recipe
) => TypedItemHelpers.Picture(cdf: _cdf.Value, item: this, name: name, noParamOrder: noParamOrder,
settings: settings, factor: factor, width: width, imgAlt: imgAlt,
imgAltFallback: imgAltFallback, imgClass: imgClass, recipe: recipe);


public int Id => PreWrap.TryGet(nameof(Id), noParamOrder: Protector, fallback: 0, required: false);

Expand Down Expand Up @@ -178,13 +177,20 @@ private Metadata BuildMetadata(object raw)
public IField Field(string name, string noParamOrder, bool? required)
=> new Field(this, name, _cdf.Value);

private NotSupportedException NotSupportedEx([CallerMemberName] string cName = default)
/// <summary>
/// Override the URL, to also support checks for "file:72"
/// </summary>
string ITyped.Url(string name, string noParamOrder, string fallback, bool? required)
{
var ex = new NotSupportedException(
$"You are accessing a virtual {nameof(ITypedItem)} which is based on an object, not an {nameof(IEntity)}. The method {cName}(...) doesn't work in this scenario.");
return ex;
}
var url = PreWrap.TryGet(name, noParamOrder: noParamOrder, fallback, required: required);
if (url == null) return null;

// ReSharper disable once ConvertTypeCheckPatternToNullCheck
if (ValueConverterBase.CouldBeReference(url))
url = _cdf.Value.Services.ValueConverterOrNull?.ToValue(url, Guid.Empty) ?? url;

return Tags.SafeUrl(url).ToString();
}
#endregion
}
}
4 changes: 2 additions & 2 deletions Src/Sxc/ToSic.Sxc/Services/CmsService/CmsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public IHtmlTag Html(
return l.Return(cntHelper.Wrap(thing, defaultToolbar: false), "No field, will just treat as value");

// Get Content type and field information
var value = field.Raw as object;
var contentType = field.Parent.Entity.Type;
var value = field.Raw;
var contentType = field.Parent.Entity?.Type; // Entity can be null on mock data
if (contentType == null)
return l.Return(cntHelper.Wrap(value, defaultToolbar: false), "can't find content-type, treat as value");

Expand Down
2 changes: 1 addition & 1 deletion Src/Sxc/ToSic.Sxc/Services/ServiceKits/ServiceKit16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ToSic.Sxc.Services
public class ServiceKit16: ServiceKit
{
[PrivateApi("Public constructor for DI")]
public ServiceKit16() : base("Sxc.Kit14")
public ServiceKit16() : base("Sxc.Kit16")
{
}

Expand Down

0 comments on commit 9e3ebb3

Please sign in to comment.