Skip to content

Commit

Permalink
Only include the script files when required by the content model.
Browse files Browse the repository at this point in the history
  • Loading branch information
killij committed Oct 4, 2023
1 parent 8e7681c commit 9430691
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 13 deletions.
11 changes: 6 additions & 5 deletions Childrens-Social-Care-CPD/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ public async Task<IActionResult> Index(CancellationToken cancellationToken, stri
}

var contextModel = new ContextModel(
Id: pageContent.Id,
Title: pageContent.Title,
PageName: pageName,
Id: pageContent.Id,
Title: pageContent.Title,
PageName: pageName,
Category: pageContent.Category,
UseContainers: pageContent.SideMenu == null,
PreferenceSet: preferenceSet,
UseContainers: pageContent.SideMenu == null,
PreferenceSet: preferenceSet,
BackLink: pageContent.BackLink);

ViewData["ContextModel"] = contextModel;
ViewData["StateModel"] = new StateModel();
return View(pageContent);
}
}
6 changes: 6 additions & 0 deletions Childrens-Social-Care-CPD/Models/StateModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Childrens_Social_Care_CPD.Models;

public class StateModel
{
public bool IncludeMediaPlayer { get; set; } = false;
}
13 changes: 12 additions & 1 deletion Childrens-Social-Care-CPD/Views/Content/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@

<partial name="_Content" model="Model" />

@_relatedContentRenderer.Render(Model.RelatedContent)
@_relatedContentRenderer.Render(Model.RelatedContent)

@section Scripts {
@if (ViewData.TryGetValue("StateModel", out var stateModel))
{
if (((StateModel)stateModel).IncludeMediaPlayer)
{
<script src=/javascript/mediaelement-and-player.min.js></script>
<script src=/javascript/mediaelement-configuration.js></script>
}
}
}
4 changes: 0 additions & 4 deletions Childrens-Social-Care-CPD/Views/Resources/Search.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
Layout = "_SearchPageLayout";
}

@section Scripts {
<script src="/javascript/resources.js"></script>
}

@section PageBanner {
<partial name="_PageBanner" model="Model.Content" />
}
Expand Down
7 changes: 6 additions & 1 deletion Childrens-Social-Care-CPD/Views/Shared/_AudioResource.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

@model AudioResource

@{
((StateModel)ViewData["StateModel"]).IncludeMediaPlayer = true;
}

<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<audio id="audioPlayer" preload="none">
Expand All @@ -19,4 +23,5 @@
</details>
}
</div>
</div>
</div>

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
@await RenderSectionAsync("PageBanner", required: false)
}

@section Scripts {
@await RenderSectionAsync("Scripts", required: false)
}

@await RenderSectionAsync("Hero", required: false)
<div class="dfe-width-container">
@RenderBody()
Expand Down
2 changes: 0 additions & 2 deletions Childrens-Social-Care-CPD/Views/Shared/_SiteLayout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
<!-- component HTML -->
<script src="/javascript/govuk-frontend-4.7.0.min.js"></script>
<script src="/javascript/dfefrontend-0.1.8.min.js"></script>
<script src=/javascript/mediaelement-and-player.min.js></script>
<script src=/javascript/mediaelement-configuration.js></script>
<script>
window.GOVUKFrontend.initAll()
</script>
Expand Down
4 changes: 4 additions & 0 deletions Childrens-Social-Care-CPD/Views/Shared/_SubPageLayout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
@await RenderSectionAsync("PageBanner", required: false)
}

@section Scripts {
@await RenderSectionAsync("Scripts", required: false)
}

<div class="govuk-width-container dfe-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-one-quarter">
Expand Down
4 changes: 4 additions & 0 deletions Childrens-Social-Care-CPD/Views/Shared/_VideoResource.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

@model VideoResource

@{
((StateModel)ViewData["StateModel"]).IncludeMediaPlayer = true;
}

<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<video id="videoPlayer" preload="none">
Expand Down

0 comments on commit 9430691

Please sign in to comment.