Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
bkapustik committed May 20, 2024
2 parents e3bdf42 + 5459c45 commit c0aa6c0
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
DOTNET_NOLOGO: 1

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
global-json-file: global.json

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ dotnet add package Kentico.Xperience.TagManager
- Top of the body - inserts a script immediately after the opening body tag.
- Bottom of the body - inserts a script right before the closing body tag.
- Fill in a consent if required.
- Select whether you want to display tags in the Xperience administration preview or Page Builder, or both. This option defaults to None.
5. During rendering the livesite page, the Tag manager module automatically adds custom code snippets with accepted consents to defined locations.
6. To dynamically update the rendered code snippets, for example if a consent is accepted, call javascript function `window.xperience.tagManager.updateCodeSnippets()`.

Expand Down
26 changes: 13 additions & 13 deletions src/Kentico.Xperience.TagManager/Admin/Client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Kentico.Xperience.TagManager/Admin/Client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"@kentico/xperience-admin-base": "28.3.1",
"@kentico/xperience-admin-components": "28.3.1",
"html-react-parser": "^5.1.10",
"react": "18.2.0",
"react-dom": "18.2.0",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-select": "^5.8.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class CodeSnippetConfigurationModel
[VisibleIfEqualTo(nameof(TagType), CustomSnippetFactory.TAG_TYPE_NAME)]
public string? Code { get; set; }

[RadioGroupComponent(Label = "Tag location", Order = 5, Options = CodeSnippetLocationsExtensions.FormComponentOptions)]
[RadioGroupComponent(Label = "Tag location", Order = 5, Options = CodeSnippetExtensions.LocationFormComponentOptions)]
[VisibleIfEqualTo(nameof(TagType), CustomSnippetFactory.TAG_TYPE_NAME)]
public string? Location { get; set; }

Expand All @@ -38,6 +38,9 @@ internal class CodeSnippetConfigurationModel
[ObjectIdSelectorComponent(objectType: ConsentInfo.OBJECT_TYPE, Label = "Consent", Order = 6, Placeholder = "No consent needed")]
public IEnumerable<int> ConsentIDs { get; set; } = [];

[DropDownComponent(Label = "Kentico administration Display Mode", Options = CodeSnippetExtensions.DisplayModeFormComponentOptions, Order = 7)]
public string DisplayMode { get; set; } = "None";

public void MapToChannelCodeSnippetInfo(ChannelCodeSnippetItemInfo info)
{
info.ChannelCodeSnippetItemChannelId = ChannelIDs.FirstOrDefault();
Expand All @@ -47,13 +50,6 @@ public void MapToChannelCodeSnippetInfo(ChannelCodeSnippetItemInfo info)
info.ChannelCodeSnippetItemName = Name;
info.ChannelCodeSnippetItemIdentifier = TagIdentifier;
info.ChannelCodeSnippetItemCode = Code;
info.ChannelCodeSnippetAdministrationDisplayMode = DisplayMode;
}
}

internal static class CodeSnippetLocationsExtensions
{
public const string FormComponentOptions = $"{nameof(CodeSnippetLocations.HeadTop)};Insert at the top of the head\r\n" +
$"{nameof(CodeSnippetLocations.HeadBottom)};Insert at the bottom of the head\r\n" +
$"{nameof(CodeSnippetLocations.BodyTop)};Insert at the top of the body\r\n" +
$"{nameof(CodeSnippetLocations.BodyBottom)};Insert at the bottom of the body\r\n";
}
16 changes: 16 additions & 0 deletions src/Kentico.Xperience.TagManager/Admin/CodeSnippetExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Kentico.Xperience.TagManager.Rendering;

namespace Kentico.Xperience.TagManager.Admin;

internal static class CodeSnippetExtensions
{
public const string LocationFormComponentOptions = $"{nameof(CodeSnippetLocations.HeadTop)};Insert at the top of the head\r\n" +
$"{nameof(CodeSnippetLocations.HeadBottom)};Insert at the bottom of the head\r\n" +
$"{nameof(CodeSnippetLocations.BodyTop)};Insert at the top of the body\r\n" +
$"{nameof(CodeSnippetLocations.BodyBottom)};Insert at the bottom of the body\r\n";

public const string DisplayModeFormComponentOptions = $"{nameof(CodeSnippetAdministrationDisplayMode.None)};Do not display in Administration\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.PreviewOnly)};Display in the Preview view mode only\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.PageBuilderOnly)};Display in the Page Builder only\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.Both)}Display in the Preview view mode and the Page Builder";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CMS.Helpers;
using CMS.ContentEngine;
using CMS.DataProtection;

using Kentico.Xperience.TagManager.Admin;

[assembly: RegisterObjectType(typeof(ChannelCodeSnippetItemInfo), ChannelCodeSnippetItemInfo.OBJECT_TYPE)]
Expand Down Expand Up @@ -120,7 +121,7 @@ public virtual string ChannelCodeSnippetItemLocation


/// <summary>
/// Channel code snippet GTMID.
/// Channel code snippet third party identifier.
/// </summary>
[DatabaseField]
public virtual string ChannelCodeSnippetItemIdentifier
Expand All @@ -140,6 +141,16 @@ public virtual string ChannelCodeSnippetItemCode
set => SetValue(nameof(ChannelCodeSnippetItemCode), value, String.Empty);
}

/// <summary>
/// Channel code snippet administration display mode.
/// </summary>
[DatabaseField]
public virtual string ChannelCodeSnippetAdministrationDisplayMode
{
get => ValidationHelper.GetString(GetValue(nameof(ChannelCodeSnippetAdministrationDisplayMode)), String.Empty);
set => SetValue(nameof(ChannelCodeSnippetAdministrationDisplayMode), value, String.Empty);
}


/// <summary>
/// Channel code snippet consent ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ private static void InstallChannelCodeSnippetClass(ResourceInfo resourceInfo)
};
formInfo.AddFormItem(formItem);

formItem = new FormFieldInfo
{
Name = nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetAdministrationDisplayMode),
Visible = false,
Precision = 0,
Size = 200,
DataType = FieldDataType.Text,
Enabled = true,
AllowEmpty = true
};
formInfo.AddFormItem(formItem);

formItem = new FormFieldInfo
{
Name = nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected override CodeSnippetConfigurationModel Model
ChannelIDs = [info.ChannelCodeSnippetItemChannelId],
Name = info.ChannelCodeSnippetItemName,
Code = info.ChannelCodeSnippetItemCode,
DisplayMode = info.ChannelCodeSnippetAdministrationDisplayMode,
TagType = info.ChannelCodeSnippetItemType,
ConsentIDs = info.ChannelCodeSnippetItemConsentId == 0 ? [] : [info.ChannelCodeSnippetItemConsentId],
TagIdentifier = info.ChannelCodeSnippetItemIdentifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kentico.Xperience.TagManager.Rendering;

public enum CodeSnippetAdministrationDisplayMode
{
None = default,
PreviewOnly,
PageBuilderOnly,
Both
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class CodeSnippetDto
public int ID { get; init; }
public string? Code { get; init; }
public CodeSnippetLocations Location { get; init; }
public CodeSnippetAdministrationDisplayMode DisplayMode { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using CMS.ContactManagement;

using Kentico.Content.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
Expand All @@ -18,14 +23,17 @@ internal class CodeSnippetTagHelperComponent : TagHelperComponent
private readonly IChannelCodeSnippetsService codeSnippetsContext;
private readonly IUrlHelperFactory urlHelperFactory;
private readonly IFileVersionProvider fileVersionProvider;
private readonly IHttpContextAccessor httpContextAccessor;

public CodeSnippetTagHelperComponent(
IChannelCodeSnippetsService codeSnippetsContext,
IUrlHelperFactory urlHelperFactory,
IFileVersionProvider fileVersionProvider)
IFileVersionProvider fileVersionProvider,
IHttpContextAccessor httpContextAccessor)
{
this.codeSnippetsContext = codeSnippetsContext;
this.urlHelperFactory = urlHelperFactory;
this.httpContextAccessor = httpContextAccessor;
this.fileVersionProvider = fileVersionProvider;
}

Expand All @@ -40,42 +48,92 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
var contact = ContactManagementContext.CurrentContact;

var codeSnippets = await codeSnippetsContext.GetConsentedCodeSnippets(contact);

if (string.Equals(context.TagName, HeadTag, StringComparison.OrdinalIgnoreCase))
{
ProcessHead(output, await codeSnippetsContext.GetConsentedCodeSnippets(contact));
ProcessHead(output, codeSnippets, httpContextAccessor.HttpContext);
}

if (string.Equals(context.TagName, BodyTag, StringComparison.OrdinalIgnoreCase))
{
ProcessBody(output, await codeSnippetsContext.GetConsentedCodeSnippets(contact));
ProcessBody(output, codeSnippets, httpContextAccessor.HttpContext);
}
}

private static void ProcessHead(
TagHelperOutput output,
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets)
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets,
HttpContext? httpContext)
{
foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.HeadTop])
bool isEditMode = httpContext.Kentico().PageBuilder().EditMode;
bool isPreviewMode = httpContext.Kentico().Preview().Enabled;

var headTopSnippets = codeSnippets[CodeSnippetLocations.HeadTop];
var headBottomSnippets = codeSnippets[CodeSnippetLocations.HeadBottom];

if (isEditMode)
{
headTopSnippets = headTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);

headBottomSnippets = headBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);
}
else if (isPreviewMode)
{
headTopSnippets = headTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);

headBottomSnippets = headBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);
}

foreach (var codeSnippet in headTopSnippets)
{
output.PreContent.AppendHtml(codeSnippet.Code);
}

foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.HeadBottom])
foreach (var codeSnippet in headBottomSnippets)
{
output.PostContent.AppendHtml(codeSnippet.Code);
}
}

private void ProcessBody(
TagHelperOutput output,
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets)
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets,
HttpContext? httpContext)
{
foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.BodyTop])
bool isEditMode = httpContext.Kentico().PageBuilder().EditMode;
bool isPreviewMode = httpContext.Kentico().Preview().Enabled;

var bodyTopSnippets = codeSnippets[CodeSnippetLocations.BodyTop];
var bodyBottomSnippets = codeSnippets[CodeSnippetLocations.BodyBottom];

if (isEditMode)
{
bodyTopSnippets = bodyTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);

bodyBottomSnippets = bodyBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);
}
else if (isPreviewMode)
{
bodyTopSnippets = bodyTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);

bodyBottomSnippets = bodyBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);
}

foreach (var codeSnippet in bodyTopSnippets)
{
output.PreContent.AppendHtml(codeSnippet.Code);
}

foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.BodyBottom])
foreach (var codeSnippet in bodyBottomSnippets)
{
output.PostContent.AppendHtml(codeSnippet.Code);
}
Expand Down
Loading

0 comments on commit c0aa6c0

Please sign in to comment.