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 Common Methods and Properties #4

Merged
merged 11 commits into from
Jan 2, 2023
Merged
2 changes: 1 addition & 1 deletion Blazored.Video.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazored.Video", "src\Blazo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorServer", "samples\BlazorServer\BlazorServer.csproj", "{ADF25F83-3102-4E94-9635-77752622C0E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorWebAssembly", "samples\BlazorWebAssembly\BlazorWebAssembly.csproj", "{237424CD-14E6-4184-8374-074416E6ADAD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorWebAssembly", "samples\BlazorWebAssembly\BlazorWebAssembly.csproj", "{237424CD-14E6-4184-8374-074416E6ADAD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
3 changes: 3 additions & 0 deletions src/Blazored.Video/Blazored.Video.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<PackageReleaseNotes>Initial Release</PackageReleaseNotes>
<Version>1.1.0</Version>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Versioning happens in the github workflow when we release, this is not needed/wanted in the source

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging happens in the github workflow, this is not needed

</PropertyGroup>

<ItemGroup>
<None Include="icon.png" Pack="true" PackagePath="\" />
<Resource Include="wwwroot\blazoredVideo.js" />
</ItemGroup>

<ItemGroup>
Expand Down
98 changes: 20 additions & 78 deletions src/Blazored.Video/BlazoredVideo.razor
Original file line number Diff line number Diff line change
@@ -1,80 +1,22 @@
@*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this code is required for the component to work, the default result of this is more work for the end developer and a breaking change for existing projects.

While I can see that some devs might want the JS separate, I think it would be best if this was controlled by an optional parameter on the component, leaving the default behaviour as it is now, but enabling those devs who want to control the JS externally with that ability.

I would want something like this in BlazoredVideo.razor.cs

[Parameter] public bool UseExternalJavaScript { get; set; } = false;

and then in this file,

@if (!Configured && !UseExternalJavaScript)
{
...
}

It would then also be of benefit to raise a decent error if the developer has not included the JS required for this to run.

Media Events don't work in Blazor - I believe because they don't bubble.
Blazor attaches it's event handlers to the document, and does not register
these events as they don't bubble up to the document.
So, this uses onchange and forces the video element to have a 'value' property,
which it doesn't normally have.
I then populate the 'value' with a JSON string containing the requested data
and the event name.

Sample JSON data for an event:
{
"name":"Suspend",
"data":
{
"Autoplay":false,
"Controls":true,
"CurrentSrc":"https://res.cloudinary.com/blazoredgitter/video/upload/v1557015491/samples/elephants.mp4",
"CurrentTime":2.758966
}
}
Media Events don't work in Blazor - I believe because they don't bubble.
Blazor attaches it's event handlers to the document, and does not register
these events as they don't bubble up to the document.
So, this uses onchange and forces the video element to have a 'value' property,
which it doesn't normally have.
I then populate the 'value' with a JSON string containing the requested data
and the event name.

Sample JSON data for an event:
{
"name":"Suspend",
"data":
{
"Autoplay":false,
"Controls":true,
"CurrentSrc":"https://res.cloudinary.com/blazoredgitter/video/upload/v1557015491/samples/elephants.mp4",
"CurrentTime":2.758966
}
}
*@
<video id="@UniqueKey" @key="UniqueKey" @ref="videoRef" @attributes=@Attributes @onchange=@OnChange>@ChildContent</video>
@if (!Configured)
{
Configured = true;
<script suppress-error="BL9992">
if (!window['Blazored']) {
window['Blazored'] = {}
}

window['Blazored']['registerCustomEventHandler'] = function (el, eventName, payload) {
if (!(el && eventName)) {
return false
}
if (!el.hasOwnProperty('customEvent')) {
el['customEvent'] = function (eventName, payload) {

this['value'] = getJSON(this, eventName, payload)

var event
if (typeof (Event) === 'function') {
event = new Event('change')
} else {
event = document.createEvent('Event')
event.initEvent('change', true, true)
}

this.dispatchEvent(event)
}
}

el.addEventListener(eventName, function () { el.customEvent(eventName, payload) });

// Craft a bespoke json string to serve as a payload for the event
function getJSON(el, eventName, payload) {
if (payload && payload.length > 0) {
// this syntax copies just the properties we request from the source element
// IE 11 compatible
let data = {};
for (var obj in payload) {
var item = payload[obj];
if (el[item]) {
data[item] = el[item]
}
}

// this stringify overload eliminates undefined/null/empty values
return JSON.stringify(
{ name: eventName, state: data }
, function (k, v) { return (v === undefined || v == null || v.length === 0) ? undefined : v }
)
} else {
return JSON.stringify(
{ name: eventName }
)
}
}
}
</script>
}
<video id="@UniqueKey" @key="UniqueKey" @ref="videoRef" @attributes=@Attributes @onchange=@OnChange>@ChildContent</video>
28 changes: 28 additions & 0 deletions src/Blazored.Video/BlazoredVideo.razor.Methods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Microsoft.JSInterop;

namespace Blazored.Video
{
public partial class BlazoredVideo
{
public async Task StartPlayback()
{
await JS.InvokeVoidAsync("BlazoredVideo.invoke", videoRef, "play");
}

public async Task PausePlayback()
{
await JS.InvokeVoidAsync("BlazoredVideo.invoke", videoRef, "pause");
}

public async Task ReloadControl()
{
await JS.InvokeVoidAsync("BlazoredVideo.invoke", videoRef, "load");
}

public async Task<bool> CanPlayMediaType(string mediaType)
{
return await JS.InvokeAsync<bool>("BlazoredVideo.invoke", videoRef, "canPlayType", mediaType);
}
}
}
Loading