Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for HtmlAttributesAsString in html module views #5242

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
993 changes: 558 additions & 435 deletions DNN Platform/DotNetNuke.Web.Client/ClientResourceManager.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions DNN Platform/Library/DotNetNuke.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@
<Compile Include="Services\Tokens\HtmlTokenReplace.cs" />
<Compile Include="Services\Tokens\PropertyAccess\AntiForgeryTokenPropertyAccess.cs" />
<Compile Include="Services\Tokens\PropertyAccess\CssPropertyAccess.cs" />
<Compile Include="Services\Tokens\PropertyAccess\JavaScriptDto.cs" />
<Compile Include="Services\Tokens\PropertyAccess\JavaScriptPropertyAccess.cs" />
<Compile Include="Services\Tokens\PropertyAccess\JsonPropertyAccess.cs" />
<Compile Include="Services\Tokens\Scope.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information

// ReSharper disable once CheckNamespace
namespace DotNetNuke.Services.Tokens
{
using System;
using System.Web.UI;

using DotNetNuke.Entities.Users;
using DotNetNuke.Framework.JavaScriptLibraries;
using DotNetNuke.Web.Client;
using DotNetNuke.Web.Client.ClientResourceManagement;
using Newtonsoft.Json;

/// <summary>
/// Data-transfer-object for javascript file registration.
/// </summary>
public class JavaScriptDto
{
/// <summary>
/// Gets or sets the name of an installed javascript library.
/// </summary>
[JsonProperty("jsname")]
public string JsName { get; set; }

/// <summary>
/// Gets or sets the path to the javascript file.
/// </summary>
[JsonProperty("path")]
public string Path { get; set; }

/// <summary>
/// Gets or sets the priority of the javascript file.
/// </summary>
[JsonProperty("priority")]
public int Priority { get; set; }

/// <summary>
/// Gets or sets the name of the provider to use. Examples: DnnPageHeaderProvider, DnnBodyProvider, DnnFormBottomProvider.
/// </summary>
[JsonProperty("provider")]
public string Provider { get; set; }

/// <summary>
/// Gets or sets the specific version to use. Examples: Latest, LatestMajor, LatestMinor, Exact.
/// </summary>
[JsonProperty("specific")]
public string Specific { get; set; }

/// <summary>
/// Gets or sets the version number to load.
/// </summary>
[JsonProperty("version")]
public string Version { get; set; }

/// <summary>
/// Gets or sets the html attributes to include in the script tag.
/// </summary>
[JsonProperty("htmlattributesasstring")]
public string HtmlAttributesAsString { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,124 +2,132 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information

// ReSharper disable once CheckNamespace
namespace DotNetNuke.Services.Tokens
{
using System;
using System.Web.UI;

using DotNetNuke.Entities.Users;
using DotNetNuke.Framework.JavaScriptLibraries;
using DotNetNuke.Web.Client;
using DotNetNuke.Web.Client.ClientResourceManagement;
using Newtonsoft.Json;

public class JavaScriptDto
{
[JsonProperty("jsname")]
public string JsName { get; set; }

[JsonProperty("path")]
public string Path { get; set; }

[JsonProperty("priority")]
public int Priority { get; set; }

[JsonProperty("provider")]
public string Provider { get; set; }

[JsonProperty("specific")]
public string Specific { get; set; }

[JsonProperty("version")]
public string Version { get; set; }
}

public class JavaScriptPropertyAccess : JsonPropertyAccess<JavaScriptDto>
{
private readonly Page _page;
// ReSharper disable once CheckNamespace
namespace DotNetNuke.Services.Tokens
{
using System;
using System.Collections.Generic;
using System.Web.UI;

using DotNetNuke.Entities.Users;
using DotNetNuke.Framework.JavaScriptLibraries;
using DotNetNuke.Web.Client;
using DotNetNuke.Web.Client.ClientResourceManagement;

/// <summary>
/// Property Access implementenation for javascript registration.
/// </summary>
public class JavaScriptPropertyAccess : JsonPropertyAccess<JavaScriptDto>
{
private readonly Page page;

/// <summary>
/// Initializes a new instance of the <see cref="JavaScriptPropertyAccess"/> class.
/// </summary>
/// <param name="page"></param>
public JavaScriptPropertyAccess(Page page)
{
this._page = page;
/// <param name="page">The current page.</param>
public JavaScriptPropertyAccess(Page page)
{
this.page = page;
}

/// <inheritdoc/>
protected override string ProcessToken(JavaScriptDto model, UserInfo accessingUser, Scope accessLevel)
{
if (string.IsNullOrEmpty(model.JsName))
{
if (string.IsNullOrEmpty(model.Path))
{
throw new ArgumentException("If the jsname property is not specified then the JavaScript token must specify a path or property.");
}

if (model.Priority == 0)
{
model.Priority = (int)FileOrder.Js.DefaultPriority;
}

if (string.IsNullOrEmpty(model.Provider))
{
ClientResourceManager.RegisterScript(this._page, model.Path, model.Priority);
}
else
{
ClientResourceManager.RegisterScript(this._page, model.Path, model.Priority, model.Provider);
}
}
else if (!string.IsNullOrEmpty(model.Path))
{
if (model.Priority == 0)
{
model.Priority = (int)FileOrder.Js.DefaultPriority;
}

if (string.IsNullOrEmpty(model.Provider))
{
ClientResourceManager.RegisterScript(this._page, model.Path, model.Priority, string.Empty, model.JsName, model.Version);
}
else
{
ClientResourceManager.RegisterScript(this._page, model.Path, model.Priority, model.Provider, model.JsName, model.Version);
}
}
else
{
Version version = null;
SpecificVersion specific = SpecificVersion.Latest;
if (!string.IsNullOrEmpty(model.Version))
{
version = new Version(model.Version);

if (!string.IsNullOrEmpty(model.Specific))
{
switch (model.Specific)
{
case "Exact":
specific = SpecificVersion.Exact;
break;
case "LatestMajor":
specific = SpecificVersion.LatestMajor;
break;
case "LatestMinor":
specific = SpecificVersion.LatestMinor;
break;
default:
specific = SpecificVersion.Latest;
break;
}
}
}

JavaScript.RequestRegistration(model.JsName, version, specific);
}

return string.Empty;
}
}
}
protected override string ProcessToken(JavaScriptDto model, UserInfo accessingUser, Scope accessLevel)
{
if (string.IsNullOrEmpty(model.JsName) && string.IsNullOrEmpty(model.Path))
{
throw new ArgumentException("If the jsname property is not specified then the JavaScript token must specify a path or property.");
}

if (model.Priority == 0)
{
model.Priority = (int)FileOrder.Js.DefaultPriority;
}

if (!string.IsNullOrEmpty(model.JsName) && string.IsNullOrEmpty(model.Path))
{
this.RegisterInstalledLibrary(model);
return string.Empty;
}

this.RegisterPath(model);

return string.Empty;
}

private void RegisterInstalledLibrary(JavaScriptDto model)
{
Version version = null;
SpecificVersion specific = SpecificVersion.Latest;
if (!string.IsNullOrEmpty(model.Version))
{
version = new Version(model.Version);

if (!string.IsNullOrEmpty(model.Specific))
{
switch (model.Specific)
{
case "Exact":
specific = SpecificVersion.Exact;
break;
case "LatestMajor":
specific = SpecificVersion.LatestMajor;
break;
case "LatestMinor":
specific = SpecificVersion.LatestMinor;
break;
default:
specific = SpecificVersion.Latest;
break;
}
}
}

JavaScript.RequestRegistration(model.JsName, version, specific);
}

private void RegisterPath(JavaScriptDto model)
{
var hasName = !string.IsNullOrEmpty(model.JsName);
var hasProvider = !string.IsNullOrEmpty(model.Provider);
var hasPath = !string.IsNullOrEmpty(model.Path);

var htmlAttributes = new Dictionary<string, string>();
try
{
if (!string.IsNullOrEmpty(model.HtmlAttributesAsString))
{
var attributes = model.HtmlAttributesAsString.Split(',');
foreach (var attribute in attributes)
{
var attributeParts = attribute.Split(':');
htmlAttributes.Add(attributeParts[0], attributeParts[1]);
}
}
}
catch (Exception)
{
throw new ArgumentException("HtmlAttributesAsString is malformed.");
}

if (hasName && hasProvider && hasPath)
{
ClientResourceManager.RegisterScript(this.page, model.Path, model.Priority, model.Provider, model.JsName, model.Version, htmlAttributes);
return;
}

if (hasName && hasProvider)
{
ClientResourceManager.RegisterScript(this.page, model.Path, model.Priority, string.Empty, model.JsName, model.Version, htmlAttributes);
return;
}

if (hasProvider)
{
ClientResourceManager.RegisterScript(this.page, model.Path, model.Priority, model.Provider, htmlAttributes);
return;
}

ClientResourceManager.RegisterScript(this.page, model.Path, model.Priority, htmlAttributes);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dnn-resource-manager",
"version": "9.10.2",
"version": "9.11.0",
"description": "Resource Manager",
"private": true,
"main": "dist/index.cjs.js",
Expand Down
6 changes: 3 additions & 3 deletions DNN Platform/Modules/ResourceManager/View.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[AntiForgeryToken:True]
[JavaScript:{ path: "/DesktopModules/ResourceManager/Scripts/dnn-resource-manager/dnn-resource-manager.esm.js", htmlAttributesAsString: "type:module"}]
[JavaScript:{ path: "/DesktopModules/ResourceManager/Scripts/dnn-resource-manager/dnn-resource-manager.js", htmlAttributesAsString: "nomodule:nomodule"}]
<style type="text/css">
:root {
--dnn-color-primary: #3792ED;
--dnn-color-primary-light: #6CB6F3;
--dnn-color-primary-dark: #0D569E;
--dnn-color-primary-contrast: #FFF;
--dnn-color-secondary:#CCC;
--dnn-color-secondary: #CCC;
--dnn-color-secondary-light: #EEE;
--dnn-color-secondary-dark: #AAA;
--dnn-color-secondary-contrast: #222;
Expand All @@ -17,6 +19,4 @@
--dnn-controls-padding: 5px;
}
</style>
<script type="module" src="/DesktopModules/ResourceManager/Scripts/dnn-resource-manager/dnn-resource-manager.esm.js"></script>
<script nomodule="" src="/DesktopModules/ResourceManager/Scripts/dnn-resource-manager/dnn-resource-manager.js"></script>
<dnn-resource-manager module-id="[ModuleContext:ModuleId]"></dnn-resource-manager>
4 changes: 2 additions & 2 deletions SolutionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
[assembly: AssemblyProduct("https://dnncommunity.org")]
[assembly: AssemblyCopyright("DNN Platform is copyright 2002-2020 by .NET Foundation. All Rights Reserved.")]
[assembly: AssemblyTrademark("DNN")]
[assembly: AssemblyVersion("9.11.0.0")]
[assembly: AssemblyVersion("9.11.0")]
[assembly: AssemblyFileVersion("9.11.0.0")]
[assembly: AssemblyInformationalVersion("9.11.0.0 Custom build")]
[assembly: AssemblyInformationalVersion("9.11.0 Custom build")]