Component
所有UI组件的基类。 组件是UI对象,它们表示DOM对象和DOM中的元素。 他们可以是其他组件的子组件,也可以有自己的子组件。
组件也可以使用EventTarget的方法
Constructor
new Component(player, optionsopt, readyopt)
创建此类的实例。
Parameters:
Name | Type | Attributes | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
player |
Player |
这个类应该被附加到的播放器。 |
|||||||||
options |
Object | <optional> |
关于播放器选项的键/值存储。Properties
|
||||||||
ready |
Component~ReadyCallback | <optional> |
组件准备就绪时调用的函数。 |
Methods
static getComponent(name) → {Component}
-
Get a
Component
based on the name it was registered with.Parameters:
Name Type Description name
string
The Name of the component to get.Returns:
The
Component
that got registered under the given name.- Deprecated:
-
- In `videojs` 6 this will not return `Component`s that were not registered using Component.registerComponent. Currently we check the global `videojs` object for a `Component` name and return that if it exists.
static registerComponent(name, ComponentToRegister) → {Component}
-
Register a
Component
withvideojs
given the name and the component.NOTE: Techs should not be registered as a
Component
. Techs should be registered using Tech.registerTech or videojs:videojs.registerTech.NOTE: This function can also be seen on videojs as videojs:videojs.registerComponent.
Parameters:
Name Type Description name
string
The name of theComponent
to register.ComponentToRegister
Component
TheComponent
class to register.Returns:
The
Component
that was registered.
$(selector, contextopt) → {Element|null}
-
Find a single DOM element matching a
selector
. This can be within theComponent
scontentEl()
or another custom context.Parameters:
Name Type Attributes Default Description selector
string
A valid CSS selector, which will be passed toquerySelector
.context
Element | string <optional> this.contentEl()
A DOM element within which to query. Can also be a selector string in which case the first matching element will get used as context. If missingthis.contentEl()
gets used. Ifthis.contentEl()
returns nothing it falls back todocument
.Returns:
Element | null -
the dom element that was found, or null
$$(selector, contextopt) → {NodeList}
-
Finds all DOM element matching a
selector
. This can be within theComponent
scontentEl()
or another custom context.Parameters:
Name Type Attributes Default Description selector
string
A valid CSS selector, which will be passed toquerySelectorAll
.context
Element | string <optional> this.contentEl()
A DOM element within which to query. Can also be a selector string in which case the first matching element will get used as context. If missingthis.contentEl()
gets used. Ifthis.contentEl()
returns nothing it falls back todocument
.Returns:
NodeList -
a list of dom elements that were found
addChild(child, optionsopt, indexopt) → {Component}
-
Add a child
Component
inside the currentComponent
.Parameters:
Name Type Attributes Default Description child
string | Component
The name or instance of a child to add.options
Object <optional> {}
The key/value store of options that will get passed to children of the child.index
number <optional> this.children_.length
The index to attempt to add a child into.Returns:
The
Component
that gets added as a child. When using a string theComponent
will get created by this process.
addClass(classToAdd)
-
Add a CSS class name to the
Component
s element.Parameters:
Name Type Description classToAdd
string
CSS class name to add
blur()
-
Remove the focus from this component
abstract buildCSSClass() → {string}
-
Builds the default DOM class name. Should be overriden by sub-components.
Returns:
string -
The DOM class name for this object.
cancelAnimationFrame(id) → {number}
-
Cancels a queued callback passed to Component#requestAnimationFrame (rAF).
If you queue an rAF callback via Component#requestAnimationFrame, use this function instead of
window.cancelAnimationFrame
. If you don‘t, your dispose listener will not get cleaned up until Component#dispose!Parameters:
Name Type Description id
number
The rAF ID to clear. The return value of Component#requestAnimationFrame.Returns:
number -
Returns the rAF ID that was cleared.
- See:
children() → {Array}
-
Get an array of all child components
Returns:
Array -
The children
clearInterval(intervalId) → {number}
-
Clears an interval that gets created via
window.setInterval
or Component#setInterval. If you set an inteval via Component#setInterval use this function instead ofwindow.clearInterval
. If you don‘t your dispose listener will not get cleaned up until Component#dispose!Parameters:
Name Type Description intervalId
number
The id of the interval to clear. The return value of Component#setInterval orwindow.setInterval
.Returns:
number -
Returns the interval id that was cleared.
- See:
clearTimeout(timeoutId) → {number}
-
Clears a timeout that gets created via
window.setTimeout
or Component#setTimeout. If you set a timeout via Component#setTimeout use this function instead ofwindow.clearTimout
. If you don‘t your dispose listener will not get cleaned up until Component#dispose!Parameters:
Name Type Description timeoutId
number
The id of the timeout to clear. The return value of Component#setTimeout orwindow.setTimeout
.Returns:
number -
Returns the timeout id that was cleared.
- See:
contentEl() → {Element}
-
Return the
Component
s DOM element. This is where children get inserted. This will usually be the the same as the element returned in Component#el.Returns:
Element -
The content element for this
Component
.
createEl(tagNameopt, propertiesopt, attributesopt) → {Element}
-
Create the
Component
s DOM element.Parameters:
Name Type Attributes Description tagName
string <optional>
Element‘s DOM node type. e.g. ‘div‘properties
Object <optional>
An object of properties that should be set.attributes
Object <optional>
An object of attributes that should be set.Returns:
Element -
The element that gets created.
currentDimension(widthOrHeight) → {number}
-
Get the width or the height of the
Component
elements computed style. Useswindow.getComputedStyle
.Parameters:
Name Type Description widthOrHeight
string
A string containing ‘width‘ or ‘height‘. Whichever one you want to get.Returns:
number -
The dimension that gets asked for or 0 if nothing was set for that dimension.
currentDimensions() → {Component~DimensionObject}
-
Get an object that contains width and height values of the
Component
s computed style.Returns:
The dimensions of the components element
currentHeight() → {number}
-
Get the height of the
Component
s computed style. Useswindow.getComputedStyle
.Returns:
number -
height The height of the
Component
s computed style.
currentWidth() → {number}
-
Get the width of the
Component
s computed style. Useswindow.getComputedStyle
.Returns:
number -
width The width of the
Component
s computed style.
dimension(widthOrHeight, numopt, skipListenersopt) → {number}
-
Get or set width or height of the
Component
element. This is the shared code for the Component#width and Component#height.Things to know:
- If the width or height in an number this will return the number postfixed with ‘px‘.
- If the width/height is a percent this will return the percent postfixed with ‘%‘
- Hidden elements have a width of 0 with
window.getComputedStyle
. This function defaults to theComponent
sstyle.width
and falls back towindow.getComputedStyle
. Seethis for more information - If you want the computed style of the component, use Component#currentWidth and {Component#currentHeight
Parameters:
Name Type Attributes Description widthOrHeight
string
8 ‘width‘ or ‘height‘num
number | string <optional>
8 New dimensionskipListeners
boolean <optional>
Skip componentresize event triggerFires:
Returns:
number -
The dimension when getting or 0 if unset
dimensions(width, height)
-
Set both the width and height of the
Component
element at the same time.Parameters:
Name Type Description width
number | string
Width to set theComponent
s element to.height
number | string
Height to set theComponent
s element to.
dispose()
-
Dispose of the
Component
and all child components.Fires:
el() → {Element}
-
Get the
Component
s DOM elementReturns:
Element -
The DOM element for this
Component
.
enableTouchActivity()
-
This function reports user activity whenever touch events happen. This can get turned off by any sub-components that wants touch events to act another way.
Report user touch activity when touch events occur. User activity gets used to determine when controls should show/hide. It is simple when it comes to mouse events, because any mouse event should show the controls. So we capture mouse events that bubble up to the player and report activity when that happens. With touch events it isn‘t as easy as
touchstart
andtouchend
toggle player controls. So touch events can‘t help us at the player level either.User activity gets checked asynchronously. So what could happen is a tap event on the video turns the controls off. Then the
touchend
event bubbles up to the player. Which, if it reported user activity, would turn the controls right back on. We also don‘t want to completely block touch events from bubbling up. Furthermore atouchmove
event and anything other than a tap, should not turn controls back on.Listens to Events:
- Component#event:touchstart
- Component#event:touchmove
- Component#event:touchend
- Component#event:touchcancel
focus()
-
Set the focus to this component
getAttribute(attribute) → {string|null}
-
Get the value of an attribute on the
Component
s element.Parameters:
Name Type Description attribute
string
Name of the attribute to get the value from.Returns:
string | null -
- The value of the attribute that was asked for.
- Can be an empty string on some browsers if the attribute does not exist or has no value - Most browsers will return null if the attibute does not exist or has no value.
- See:
- The value of the attribute that was asked for.
getChild(name) → {Component|undefined}
-
Returns the child
Component
with the givenname
.Parameters:
Name Type Description name
string
The name of the childComponent
to get.Returns:
Component | undefined -
The child
Component
with the givenname
or undefined.
getChildById(id) → {Component|undefined}
-
Returns the child
Component
with the givenid
.Parameters:
Name Type Description id
string
The id of the childComponent
to get.Returns:
Component | undefined -
The child
Component
with the givenid
or undefined.
hasClass(classToCheck) → {boolean}
-
Check if a component‘s element has a CSS class name.
Parameters:
Name Type Description classToCheck
string
CSS class name to check.Returns:
boolean -
- True if the
Component
has the class.- False if the `Component` does not have the class`
- True if the
height(numopt, skipListenersopt) → {number|string}
-
Get or set the height of the component based upon the CSS styles. See Component#dimension for more detailed information.
Parameters:
Name Type Attributes Description num
number | string <optional>
The height that you want to set postfixed with ‘%‘, ‘px‘ or nothing.skipListeners
boolean <optional>
Skip the componentresize event triggerReturns:
number | string -
The width when getting, zero if there is no width. Can be a string postpixed with ‘%‘ or ‘px‘.
hide()
-
Hide the
Component
s element if it is currently showing by adding the ‘vjs-hidden` class name to it.
id() → {string}
-
Get this
Component
s IDReturns:
string -
The id of this
Component
initChildren()
-
Add and initialize default child
Component
s based upon options.
localize(string, tokensopt, defaultValueopt) → {string}
-
Localize a string given the string in english.
If tokens are provided, it‘ll try and run a simple token replacement on the provided string. The tokens it loooks for look like
{1}
with the index being 1-indexed into the tokens array.If a
defaultValue
is provided, it‘ll use that overstring
, if a value isn‘t found in provided language files. This is useful if you want to have a descriptive key for token replacement but have a succinct localized string and not requireen.json
to be included.Currently, it is used for the progress bar timing.
{ "progress bar timing: currentTime={1} duration={2}": "{1} of {2}" }
It is then used like so:
this.localize(‘progress bar timing: currentTime={1} duration{2}‘, [this.player_.currentTime(), this.player_.duration()], ‘{1} of {2}‘);
Which outputs something like:
01:23 of 24:56
.Parameters:
Name Type Attributes Description string
string
The string to localize and the key to lookup in the language files.tokens
Array.<string> <optional>
If the current item has token replacements, provide the tokens here.defaultValue
string <optional>
Defaults tostring
. Can be a default value to use for token replacement if the lookup key is needed to be separate.Returns:
string -
The localized string or if no localization exists the english string.
name() → {string}
-
Get the
Component
s name. The name gets used to reference theComponent
and is set during registration.Returns:
string -
The name of this
Component
.
options(obj) → {Object}
-
Deep merge of options objects with new options.
Note: When both
obj
andoptions
contain properties whose values are objects. The two properties get merged using module:mergeOptionsParameters:
Name Type Description obj
Object
The object that contains new options.Returns:
Object -
A new object of
this.options_
andobj
merged together.- Deprecated:
-
- since version 5
player() → {Player}
-
Return the Player that the
Component
has attached to.Returns:
Player -
The player that this
Component
has attached to.
ready() → {Component}
-
Bind a listener to the component‘s ready state. Different from event listeners in that if the ready event has already happened it will trigger the function immediately.
Returns:
Returns itself; method can be chained.
removeAttribute(attribute)
-
Remove an attribute from the
Component
s element.Parameters:
Name Type Description attribute
string
Name of the attribute to remove.- See:
removeChild(component)
-
Remove a child
Component
from thisComponent
s list of children. Also removes the childComponent
s element from thisComponent
s element.Parameters:
Name Type Description component
Component
The childComponent
to remove.
removeClass(classToRemove)
-
Remove a CSS class name from the
Component
s element.Parameters:
Name Type Description classToRemove
string
CSS class name to remove
requestAnimationFrame(fn) → {number}
-
Queues up a callback to be passed to requestAnimationFrame (rAF), but with a few extra bonuses:
- Supports browsers that do not support rAF by falling back to Component#setTimeout.
- The callback is turned into a Component~GenericCallback (i.e. bound to the component).
- Automatic cancellation of the rAF callback is handled if the component is disposed before it is called.
Parameters:
Name Type Description fn
Component~GenericCallback
A function that will be bound to this component and executed just before the browser‘s next repaint.Listens to Events:
Returns:
number -
Returns an rAF ID that gets used to identify the timeout. It can also be used in Component#cancelAnimationFrame to cancel the animation frame callback.
- See:
setAttribute(attribute, value)
-
Set the value of an attribute on the
Component
‘s elementParameters:
Name Type Description attribute
string
Name of the attribute to set.value
string
Value to set the attribute to.- See:
setInterval(fn, interval) → {number}
-
Creates a function that gets run every
x
milliseconds. This function is a wrapper aroundwindow.setInterval
. There are a few reasons to use this one instead though.- It gets cleared via Component#clearInterval when Component#dispose gets called.
- The function callback will be a Component~GenericCallback
Parameters:
Name Type Description fn
Component~GenericCallback
The function to run everyx
seconds.interval
number
Execute the specified function everyx
milliseconds.Listens to Events:
Returns:
number -
Returns an id that can be used to identify the interval. It can also be be used in Component#clearInterval to clear the interval.
- See:
setTimeout(fn, timeout) → {number}
-
Creates a function that runs after an
x
millisecond timeout. This function is a wrapper aroundwindow.setTimeout
. There are a few reasons to use this one instead though:- It gets cleared via Component#clearTimeout when Component#dispose gets called.
- The function callback will gets turned into a Component~GenericCallback
Note: You can use
window.clearTimeout
on the id returned by this function. This will cause its dispose listener not to get cleaned up! Please use Component#clearTimeout or Component#dispose.Parameters:
Name Type Description fn
Component~GenericCallback
The function that will be run aftertimeout
.timeout
number
Timeout in milliseconds to delay before executing the specified function.Listens to Events:
Returns:
number -
Returns a timeout ID that gets used to identify the timeout. It can also get used in Component#clearTimeout to clear the timeout that was set.
- See:
show()
-
Show the
Component
s element if it is hidden by removing the ‘vjs-hidden‘ class name from it.
toggleClass(classToToggle, predicateopt)
-
Add or remove a CSS class name from the component‘s element.
classToToggle
gets added when Component#hasClass would return false.classToToggle
gets removed when Component#hasClass would return true.
Parameters:
Name Type Attributes Description classToToggle
string
The class to add or remove based on (@link Component#hasClass}predicate
boolean | Dom~predicate <optional>
An Dom~predicate function or a boolean
triggerReady()
-
Trigger all the ready listeners for this
Component
.Fires:
width(numopt, skipListenersopt) → {number|string}
-
Get or set the width of the component based upon the CSS styles. See Component#dimension for more detailed information.
Parameters:
Name Type Attributes Description num
number | string <optional>
The width that you want to set postfixed with ‘%‘, ‘px‘ or nothing.skipListeners
boolean <optional>
Skip the componentresize event triggerReturns:
number | string -
The width when getting, zero if there is no width. Can be a string postpixed with ‘%‘ or ‘px‘.
Type Definitions
DimensionObject
-
An object that contains width and height values of the
Component
s computed style. Useswindow.getComputedStyle
.Properties:
Name Type Description width
number
The width of theComponent
s computed style.height
number
The height of theComponent
s computed style.
GenericCallback()
-
A callback that has no parameters and is bound into
Component
s context.This:
ReadyCallback()
-
A callback that is called when a component is ready. Does not have any paramters and any callback value will be ignored.
This:
Events
componentresize
-
Triggered when a component is resized.
Type:
dispose
-
Triggered when a
Component
is disposed.Type:
Listeners of This Event:
- BigPlayButton#requestAnimationFrame
- BigPlayButton#setInterval
- BigPlayButton#setTimeout
- Button#requestAnimationFrame
- Button#setInterval
- Button#setTimeout
- ClickableComponent#requestAnimationFrame
- ClickableComponent#setInterval
- ClickableComponent#setTimeout
- CloseButton#requestAnimationFrame
- CloseButton#setInterval
- CloseButton#setTimeout
- Component#requestAnimationFrame
- Component#setInterval
- Component#setTimeout
- ControlBar#requestAnimationFrame
- ControlBar#setInterval
- ControlBar#setTimeout
- CurrentTimeDisplay#requestAnimationFrame
- CurrentTimeDisplay#setInterval
- CurrentTimeDisplay#setTimeout
- CustomControlSpacer#requestAnimationFrame
- CustomControlSpacer#setInterval
- CustomControlSpacer#setTimeout
- DurationDisplay#requestAnimationFrame
- DurationDisplay#setInterval
- DurationDisplay#setTimeout
- FullscreenToggle#requestAnimationFrame
- FullscreenToggle#setInterval
- FullscreenToggle#setTimeout
- LiveDisplay#requestAnimationFrame
- LiveDisplay#setInterval
- LiveDisplay#setTimeout
- LoadingSpinner#requestAnimationFrame
- LoadingSpinner#setInterval
- LoadingSpinner#setTimeout
- LoadProgressBar#requestAnimationFrame
- LoadProgressBar#setInterval
- LoadProgressBar#setTimeout
- MediaLoader#requestAnimationFrame
- MediaLoader#setInterval
- MediaLoader#setTimeout
- Menu#requestAnimationFrame
- Menu#setInterval
- Menu#setTimeout
- MenuButton#requestAnimationFrame
- MenuButton#setInterval
- MenuButton#setTimeout
- MenuItem#requestAnimationFrame
- MenuItem#setInterval
- MenuItem#setTimeout
- ModalDialog#requestAnimationFrame
- ModalDialog#setInterval
- ModalDialog#setTimeout
- MouseTimeDisplay#requestAnimationFrame
- MouseTimeDisplay#setInterval
- MouseTimeDisplay#setTimeout
- MuteToggle#requestAnimationFrame
- MuteToggle#setInterval
- MuteToggle#setTimeout
- Player#requestAnimationFrame
- Player#setInterval
- Player#setTimeout
- PlayProgressBar#requestAnimationFrame
- PlayProgressBar#setInterval
- PlayProgressBar#setTimeout
- PlayToggle#requestAnimationFrame
- PlayToggle#setInterval
- PlayToggle#setTimeout
- Popup#requestAnimationFrame
- Popup#setInterval
- Popup#setTimeout
- PopupButton#requestAnimationFrame
- PopupButton#setInterval
- PopupButton#setTimeout
- PosterImage#requestAnimationFrame
- PosterImage#setInterval
- PosterImage#setTimeout
- ProgressControl#requestAnimationFrame
- ProgressControl#setInterval
- ProgressControl#setTimeout
- RemainingTimeDisplay#requestAnimationFrame
- RemainingTimeDisplay#setInterval
- RemainingTimeDisplay#setTimeout
- Slider#requestAnimationFrame
- Slider#setInterval
- Slider#setTimeout
- Spacer#requestAnimationFrame
- Spacer#setInterval
- Spacer#setTimeout
- Tech#requestAnimationFrame
- Tech#setInterval
- Tech#setTimeout
- TextTrackDisplay#requestAnimationFrame
- TextTrackDisplay#setInterval
- TextTrackDisplay#setTimeout
- TextTrackSettings#requestAnimationFrame
- TextTrackSettings#setInterval
- TextTrackSettings#setTimeout
- TimeDivider#requestAnimationFrame
- TimeDivider#setInterval
- TimeDivider#setTimeout
- TimeTooltip#requestAnimationFrame
- TimeTooltip#setInterval
- TimeTooltip#setTimeout
- VolumeControl#requestAnimationFrame
- VolumeControl#setInterval
- VolumeControl#setTimeout
- VolumeLevel#requestAnimationFrame
- VolumeLevel#setInterval
- VolumeLevel#setTimeout
- VolumePanel#requestAnimationFrame
- VolumePanel#setInterval
- VolumePanel#setTimeout
Properties:
Name Type Attributes Default Description bubbles
boolean <optional> false
set to false so that the close event does not bubble up
ready
-
Triggered when a
Component
is ready.Type:
tap
-
Triggered when a
Component
is tapped.Type: