-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6d4877a
Added Common Methods and Properties
022d1e7
Added Check in js methods for disposed element
2eb2a22
Changed method accessability for better extentability
6fe092c
Added Missing Poster property
2a9dd6a
Added Method documentation
1129cad
Removed non-dynamic setters
19b6145
Fixed seekable not been readonly
949031e
Moved blazoredVideo to be complient with standard razor component beh…
a8199b5
Changed Blazored js object name reference in method calls
8b64288
Changed Blazored js object call in properties
392552c
Added TextTracks converter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,120 @@ | ||
@* | ||
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. | ||
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 | ||
} | ||
} | ||
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'] = {} | ||
} | ||
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) { | ||
window['Blazored'].setProperty = function (el, name, value) { | ||
if (!el) { | ||
return; | ||
} | ||
try { | ||
el[name] = value; | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
this['value'] = getJSON(this, eventName, payload) | ||
window['Blazored'].getProperty = function (el, name) { | ||
if (!el) { | ||
return null; | ||
} | ||
try { | ||
return el[name]; | ||
} catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}; | ||
|
||
var event | ||
if (typeof (Event) === 'function') { | ||
event = new Event('change') | ||
} else { | ||
event = document.createEvent('Event') | ||
event.initEvent('change', true, true) | ||
} | ||
window['Blazored'].invoke = function (el, name, ...arguments) { | ||
if (!el) { | ||
return null; | ||
} | ||
try { | ||
return el[name](name, ...arguments); | ||
} catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
}; | ||
|
||
this.dispatchEvent(event) | ||
} | ||
} | ||
window['Blazored']['registerCustomEventHandler'] = function (el, eventName, payload) { | ||
if (!(el && eventName)) { | ||
return false | ||
} | ||
if (!el.hasOwnProperty('customEvent')) { | ||
el['customEvent'] = function (eventName, payload) { | ||
|
||
el.addEventListener(eventName, function () { el.customEvent(eventName, payload) }); | ||
this['value'] = getJSON(this, 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] | ||
} | ||
} | ||
var event | ||
if (typeof (Event) === 'function') { | ||
event = new Event('change') | ||
} else { | ||
event = document.createEvent('Event') | ||
event.initEvent('change', true, true) | ||
} | ||
|
||
// 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> | ||
this.dispatchEvent(event) | ||
} | ||
} | ||
|
||
if ($) { | ||
$(el).on(eventName, function () { el.customEvent(eventName, payload) }); | ||
} else { | ||
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("Blazored.invoke", videoRef, "play"); | ||
} | ||
|
||
public async Task PausePlayback() | ||
{ | ||
await JS.InvokeVoidAsync("Blazored.invoke", videoRef, "pause"); | ||
} | ||
|
||
public async Task ReloadControl() | ||
{ | ||
await JS.InvokeVoidAsync("Blazored.invoke", videoRef, "load"); | ||
} | ||
|
||
public async Task<bool> CanPlayMediaType(string mediaType) | ||
{ | ||
return await JS.InvokeAsync<bool>("Blazored.invoke", videoRef, "canPlayType", mediaType); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
and then in this file,
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.