Skip to content

Commit

Permalink
Introduce a new Narbar shape (#14488)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Oct 12, 2023
1 parent f377a07 commit 878b693
Show file tree
Hide file tree
Showing 37 changed files with 519 additions and 375 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using OrchardCore.Admin.Models;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;

namespace OrchardCore.Admin.Drivers;

public class VisitSiteNavbarDisplayDriver : DisplayDriver<Navbar>
{
public override IDisplayResult Display(Navbar model)
{
return View("VisitSiteNavbarItem", model)
.Location("DetailAdmin", "Content:20");
}
}
1 change: 1 addition & 0 deletions src/OrchardCore.Modules/OrchardCore.Admin/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IPermissionProvider, PermissionsAdminSettings>();
services.AddScoped<INavigationProvider, AdminMenu>();
services.AddSingleton<IPageRouteModelProvider, AdminPageRouteModelProvider>();
services.AddScoped<IDisplayDriver<Navbar>, VisitSiteNavbarDisplayDriver>();

services.Configure<AdminOptions>(_configuration.GetSection("OrchardCore_Admin"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@if (Model.Content == null)
{
return;
}

<ul class="navbar-nav user-top-navbar ms-auto d-flex flex-row">
@await DisplayAsync(Model.Content)
</ul>
8 changes: 8 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Admin/Views/Navbar.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@if (Model.Content == null)
{
return;
}

<ul class="navbar-nav user-top-navbar">
@await DisplayAsync(Model.Content)
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using OrchardCore.Admin.Models;
using OrchardCore.ContentLocalization.ViewModels;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Localization;

namespace OrchardCore.ContentLocalization.Drivers;

public class ContentCulturePickerNavbarDisplayDriver : DisplayDriver<Navbar>
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILocalizationService _localizationService;

public ContentCulturePickerNavbarDisplayDriver(
IHttpContextAccessor httpContextAccessor,
ILocalizationService localizationService)
{
_httpContextAccessor = httpContextAccessor;
_localizationService = localizationService;
}

public override async Task<IDisplayResult> DisplayAsync(Navbar model, BuildDisplayContext context)
{
var supportedCultures = (await _localizationService.GetSupportedCulturesAsync()).Select(c => CultureInfo.GetCultureInfo(c));

return Initialize<CulturePickerViewModel>("ContentCulturePicker", model =>
{
model.SupportedCultures = supportedCultures;
model.CurrentCulture = _httpContextAccessor
.HttpContext
.Features
.Get<IRequestCultureFeature>()?.RequestCulture?.Culture ?? CultureInfo.CurrentUICulture;
}).RenderWhen(() => Task.FromResult(supportedCultures.Count() > 1))
.Location("Detail", "Content:5");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OrchardCore.Admin;
using OrchardCore.Admin.Models;
using OrchardCore.ContentLocalization.Controllers;
using OrchardCore.ContentLocalization.Drivers;
using OrchardCore.ContentLocalization.Indexing;
Expand Down Expand Up @@ -82,6 +83,7 @@ public ContentPickerStartup(IShellConfiguration shellConfiguration)

public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDisplayDriver<Navbar>, ContentCulturePickerNavbarDisplayDriver>();
services.AddLiquidFilter<SwitchCultureUrlFilter>("switch_culture_url");
services.AddScoped<INavigationProvider, AdminMenu>();
services.AddScoped<IContentCulturePickerService, ContentCulturePickerService>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace OrchardCore.ContentLocalization.ViewModels;

public class CulturePickerViewModel
{
[BindNever]
public CultureInfo CurrentCulture { get; set; }

[BindNever]
public IEnumerable<CultureInfo> SupportedCultures { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@inject OrchardCore.Localization.ILocalizationService localizationService

@{
var currentCulture = Context.Features.Get<IRequestCultureFeature>()?.RequestCulture?.Culture ?? CultureInfo.CurrentUICulture;
var supportedCultures = (await localizationService.GetSupportedCulturesAsync()).Select(c => CultureInfo.GetCultureInfo(c));
}
@await DisplayAsync(await New.ContentCulturePickerContainer(
CurrentCulture: currentCulture,
SupportedCultures: supportedCultures
))
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="oc-culture-picker" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">@Model.CurrentCulture.NativeName</a>
<div class="dropdown-menu" aria-labelledby="oc-culture-picker">
@foreach (var culture in Model.SupportedCultures)
{
if (culture.Name != Model.CurrentCulture.Name)
{
<a asp-route="RedirectToLocalizedContent"
asp-route-area="OrchardCore.ContentLocalization"
asp-route-targetCulture="@culture.Name"
asp-route-contentItemUrl="@Context.Request.Path.Value"
asp-route-queryStringValue="@Context.Request.QueryString.Value"
class="dropdown-item">@culture.NativeName</a>
}
}
</div>
</li>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using OrchardCore.Admin.Models;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Localization.ViewModels;

namespace OrchardCore.Localization.Drivers;

public class AdminCulturePickerNavbarDisplayDriver : DisplayDriver<Navbar>
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILocalizationService _localizationService;

public AdminCulturePickerNavbarDisplayDriver(
IHttpContextAccessor httpContextAccessor,
ILocalizationService localizationService)
{
_httpContextAccessor = httpContextAccessor;
_localizationService = localizationService;
}

public override async Task<IDisplayResult> DisplayAsync(Navbar model, BuildDisplayContext context)
{
var supportedCultures = (await _localizationService.GetSupportedCulturesAsync()).Select(c => CultureInfo.GetCultureInfo(c));

return Initialize<AdminCulturePickerViewModel>("AdminCulturePicker", model =>
{
model.SupportedCultures = supportedCultures;
model.CurrentCulture = _httpContextAccessor
.HttpContext
.Features
.Get<IRequestCultureFeature>()?.RequestCulture?.Culture ?? CultureInfo.CurrentUICulture;
}).RenderWhen(() => Task.FromResult(supportedCultures.Count() > 1))
.Location("DetailAdmin", "Content:5");
}
}
4 changes: 2 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.Localization/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OrchardCore.Admin;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.Admin.Models;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Environment.Shell;
using OrchardCore.Localization.Drivers;
Expand Down Expand Up @@ -92,7 +92,7 @@ public CulturePickerStartup(IOptions<AdminOptions> adminOptions, ShellSettings s

public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IShapeTableProvider, AdminCulturePickerShapes>();
services.AddScoped<IDisplayDriver<Navbar>, AdminCulturePickerNavbarDisplayDriver>();

services.Configure<RequestLocalizationOptions>(options =>
options.AddInitialRequestCultureProvider(new AdminCookieCultureProvider(_shellSettings, _adminOptions)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace OrchardCore.Localization.ViewModels;

public class AdminCulturePickerViewModel
{
[BindNever]
public CultureInfo CurrentCulture { get; set; }

[BindNever]
public IEnumerable<CultureInfo> SupportedCultures { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@inject OrchardCore.Localization.ILocalizationService localizationService
@using OrchardCore.Localization
@using OrchardCore.Environment.Shell

@model AdminCulturePickerViewModel

@inject ShellSettings ShellSettings

@{
var currentCulture = Context.Features.Get<IRequestCultureFeature>()?.RequestCulture?.Culture ?? CultureInfo.CurrentUICulture;
var supportedCultures = (await localizationService.GetSupportedCulturesAsync()).Select(c => CultureInfo.GetCultureInfo(c));
var cookieName = AdminCookieCultureProvider.MakeCookieName(ShellSettings);
var cookiePath = AdminCookieCultureProvider.MakeCookiePath(Context);
var selectedCultureName = Model.CurrentCulture.Name;
}

@if (supportedCultures.Count() > 1)
{
@await DisplayAsync(await New.AdminCulturePickerContainer(
CurrentCulture: currentCulture,
SupportedCultures: supportedCultures
));
}
<li class="nav-item">
<select id="oc-admin-culture-picker" class="form-select me-3"
data-cookie-name="@cookieName"
data-cookie-path="@cookiePath"
data-selected-culture="@selectedCultureName">

@foreach (var culture in Model.SupportedCultures)
{
<option selected="@(culture.Name == selectedCultureName ? "selected" : null)" value="@culture.Name">@culture.NativeName</option>
}
</select>
</li>

<script at="Foot" depends-on="jquery">
$(function () {
$('#oc-admin-culture-picker').change(function () {
var $this = $(this);
var culture = $this.val();
var selectedCulture = $this.data('selected-culture');
if (culture != selectedCulture) {
var cookieName = $this.data('cookie-name');
var cookiePath = $this.data('cookie-path');
var cookieValue = `${cookieName}=c=${culture}|uic=${culture};path=${cookiePath}`;
document.cookie = cookieValue;
window.location = window.location.href;
}
});
});
</script>

This file was deleted.

Loading

0 comments on commit 878b693

Please sign in to comment.