Skip to content

Commit

Permalink
Make the Blazor Server sample project be able to test the DisableClie…
Browse files Browse the repository at this point in the history
…ntScriptAutoInjection option
  • Loading branch information
jsakamoto committed Dec 13, 2023
1 parent 54cdfcd commit 1050244
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions SampleSites/Components/SampleSite.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Toolbelt.Blazor.Gamepad" Version="9.0.0-preview.2" />
<!--<PackageReference Include="Toolbelt.Blazor.Gamepad" Version="9.0.0-preview.2" />-->
<PackageReference Include="Toolbelt.Blazor.HeadElement" Version="7.3.1" />
<!--<ProjectReference Include="..\..\Toolbelt.Blazor.Gamepad\Toolbelt.Blazor.Gamepad.csproj" />-->
<ProjectReference Include="..\..\Toolbelt.Blazor.Gamepad\Toolbelt.Blazor.Gamepad.csproj" />
</ItemGroup>

</Project>
10 changes: 9 additions & 1 deletion SampleSites/Server/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@namespace SampleSite.Server
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using SampleSite.Components
@using Microsoft.Extensions.Options
@using Toolbelt.Blazor.Gamepad

@inject IOptions<GamepadOptions> GamepadOptions

<!DOCTYPE html>
<html>
Expand All @@ -13,6 +17,7 @@
<base href="~/" />
<!-- style for blazor-error-ui -->
<link rel="stylesheet" href="data:text/css,%23blazor-error-ui%7Bbackground%3A%23ffffe0%3Bbottom%3A0%3Bbox-shadow%3A0%20-1px%202px%20rgba(0%2C0%2C0%2C.2)%3Bdisplay%3Anone%3Bleft%3A0%3Bpadding%3A.6rem%201.25rem%20.7rem%201.25rem%3Bposition%3Afixed%3Bright%3A0%3Bz-index%3A1000%7D%23blazor-error-ui%20.dismiss%7Bcursor%3Apointer%3Bposition%3Aabsolute%3Bright%3A.75rem%3Btop%3A.5rem%7D" />
<link rel="stylesheet" href="_content/SampleSite.Components/styles.css" />
</head>

<body>
Expand All @@ -32,7 +37,10 @@
</div>

<script src="_framework/blazor.server.js"></script>
@*<script src="_content/Toolbelt.Blazor.Gamepad/script.min.js"></script>*@
@if (GamepadOptions.Value.DisableClientScriptAutoInjection)
{
<script src="_content/Toolbelt.Blazor.Gamepad/script.min.js"></script>
}
</body>

</html>
8 changes: 6 additions & 2 deletions SampleSites/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using Microsoft.Extensions.Options;
using Toolbelt.Blazor.Extensions.DependencyInjection;
using Toolbelt.Blazor.Gamepad;

var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<GamepadOptions>(builder.Configuration.GetSection("Gamepad"));

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddHeadElementHelper();
builder.Services.AddGamepadList(options =>
builder.Services.AddGamepadList((services, options) =>
{
//options.DisableClientScriptAutoInjection = true;
var config = services.GetRequiredService<IOptions<GamepadOptions>>();
options.DisableClientScriptAutoInjection = config.Value.DisableClientScriptAutoInjection;
});

var app = builder.Build();
Expand Down
5 changes: 4 additions & 1 deletion SampleSites/Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Gamepad": {
"DisableClientScriptAutoInjection": false
}
}
23 changes: 18 additions & 5 deletions Toolbelt.Blazor.Gamepad/GamepadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@ public static class GamepadExtensions
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
public static IServiceCollection AddGamepadList(this IServiceCollection services)
{
return services.AddGamepadList(configure: null);
return services.AddGamepadList(configure: (_) => { });
}

/// <summary>
/// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
/// </summary>
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
/// <param name="configure"></param>
/// <param name="configure">A delegate that is used to configure the Microsoft.Extensions.DependencyInjection.Options.GamepadOptions.</param>
public static IServiceCollection AddGamepadList(this IServiceCollection services, Action<GamepadOptions>? configure)
{
var options = new GamepadOptions();
configure?.Invoke(options);
services.AddScoped(serviceProvider => new GamepadList(serviceProvider.GetRequiredService<IJSRuntime>(), options));
return services.AddGamepadList(configure: (_, options) => { configure?.Invoke(options); });
}

/// <summary>
/// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
/// </summary>
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
/// <param name="configure">A delegate that is used to configure the Microsoft.Extensions.DependencyInjection.Options.GamepadOptions.</param>
public static IServiceCollection AddGamepadList(this IServiceCollection services, Action<IServiceProvider, GamepadOptions> configure)
{
services.AddScoped(serviceProvider =>
{
var options = new GamepadOptions();
configure?.Invoke(serviceProvider, options);
return new GamepadList(serviceProvider.GetRequiredService<IJSRuntime>(), options);
});
return services;
}
}

0 comments on commit 1050244

Please sign in to comment.