Skip to content

Commit

Permalink
consolidate youtube embed code to a partial view
Browse files Browse the repository at this point in the history
  • Loading branch information
adelikat committed Oct 15, 2023
1 parent 804827d commit e77463f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 54 deletions.
28 changes: 1 addition & 27 deletions TASVideos/Pages/Forum/Topics/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,7 @@
{
<div condition="@(!string.IsNullOrWhiteSpace(Model.EncodeEmbedLink))" class="d-flex justify-content-center">
<div class="col-12 col-lg-6">
@*Boilerplate bootstrap stuff *@
<div class="ratio ratio-16x9">
<div id="ytplayer"></div>
</div>
<script>
const youtubeScriptId = 'youtube-api';
let youtubeScript = document.getElementById(youtubeScriptId);
let player;
@*Loads the scripts which run the Youtube Player API asynchronously*@
if (youtubeScript === null) {
let tag = document.createElement('script');
let firstScript = document.getElementsByTagName('script')[0];
tag.src = 'https://www.youtube.com/iframe_api';
tag.id = youtubeScriptId;
firstScript.parentNode.insertBefore(tag, firstScript);
}
@*Populates the youtube player after the API script is ready*@
if (!player) {
window.onYouTubeIframeAPIReady = () => {
player = new YT.Player('ytplayer', {
videoId: "@Model.EncodeEmbedLink!.Split('/').Last()"
});
}
}
</script>
<a href="@Model.EncodeEmbedLink">(Link to video)</a>
<partial name="_YoutubeEmbed" model="Model.EncodeEmbedLink" />
</div>
</div>
<card class="mt-2 mb-2">
Expand Down
29 changes: 29 additions & 0 deletions TASVideos/Pages/Shared/_YoutubeEmbed.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@model string

@*Boilerplate bootstrap stuff *@
<div class="ratio ratio-16x9">
<div id="ytplayer"></div>
</div>
<script>
const youtubeScriptId = 'youtube-api';
let youtubeScript = document.getElementById(youtubeScriptId);
let player;
@*Loads the scripts which run the Youtube Player API asynchronously*@
if (youtubeScript === null) {
let tag = document.createElement('script');
let firstScript = document.getElementsByTagName('script')[0];
tag.src = 'https://www.youtube.com/iframe_api';
tag.id = youtubeScriptId;
firstScript.parentNode.insertBefore(tag, firstScript);
}
@*Populates the youtube player after the API script is ready*@
if (!player) {
window.onYouTubeIframeAPIReady = () => {
player = new YT.Player('ytplayer', {
videoId: "@Model!.Split('/').Last()"
});
}
}
</script>
<a href="@Model">(Link to video)</a>
28 changes: 1 addition & 27 deletions TASVideos/Pages/Submissions/View.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,7 @@

<row class="mt-2">
<div class="col-lg-6" condition="hasEncode">
@*Boilerplate bootstrap stuff *@
<div class="ratio ratio-16x9">
<div id="ytplayer"></div>
</div>
<script>
const youtubeScriptId = 'youtube-api';
let youtubeScript = document.getElementById(youtubeScriptId);
let player;
@*Loads the scripts which run the Youtube Player API asynchronously*@
if (youtubeScript === null) {
let tag = document.createElement('script');
let firstScript = document.getElementsByTagName('script')[0];
tag.src = 'https://www.youtube.com/iframe_api';
tag.id = youtubeScriptId;
firstScript.parentNode.insertBefore(tag, firstScript);
}
@*Populates the youtube player after the API script is ready*@
if (!player) {
window.onYouTubeIframeAPIReady = () => {
player = new YT.Player('ytplayer', {
videoId: "@Model.Submission.EncodeEmbedLink!.Split('/').Last()"
});
}
}
</script>
<a href="@Model.Submission.EncodeEmbedLink">(Link to video)</a>
<partial name="_YoutubeEmbed" model="Model.Submission.EncodeEmbedLink" />
</div>
<div class="@(hasEncode ? "col-lg-6" : "col-lg-12")">
<div class="alert alert-@(statusColor)" role="alert">
Expand Down
3 changes: 3 additions & 0 deletions TASVideos/TASVideos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
<Content Update="Pages\Shared\Components\FirstEditionTas\Default.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Pages\Shared\_YoutubeEmbed.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Update="Pages\Shared\_Diff.cshtml">
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
Expand Down

0 comments on commit e77463f

Please sign in to comment.