You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -36,8 +43,42 @@ In addition, there are a couple methods worth recognizing:
36
43
*`videojs.registerComponent(String name, Function Comp)`: Registers component constructors with Video.js.
37
44
*`videojs.extend(Function component, Object properties)`: Provides prototype inheritance. Can be used to extend a component's constructor, returning a new constructor with the given properties.
38
45
46
+
Creation:
47
+
48
+
```js
49
+
// adding a button to the player
50
+
var player =videojs('some-video-id');
51
+
var Component =videojs.getComponent('Component');
52
+
var button =newComponent(player);
53
+
54
+
console.log(button.el());
55
+
```
56
+
57
+
The above code will output
58
+
59
+
```html
60
+
<divclass="video-js">
61
+
<divclass="vjs-button">Button</div>
62
+
</div>
63
+
```
64
+
65
+
Adding the new button to the player
66
+
67
+
```js
68
+
// adding a button to the player
69
+
var player =videojs('some-video-id');
70
+
var button =player.addChild('button');
71
+
72
+
console.log(button.el());
73
+
// will have the same html result as the previous example
74
+
```
75
+
39
76
## Component Children
40
77
78
+
Again, refer to [the component API docs](http://docs.videojs.com/docs/api/component.html) for complete details on methods available for managing component structures.
79
+
80
+
### Basic Example
81
+
41
82
When child component is added to a parent component, Video.js inserts the element of the child into the element of the parent. For example, adding a component like this:
42
83
43
84
```js
@@ -69,7 +110,174 @@ Results in a DOM that looks like this:
69
110
</div>
70
111
```
71
112
72
-
Again, refer to [the component API docs](http://docs.videojs.com/docs/api/component.html) for complete details on methods available for managing component structures.
113
+
### Using Options
114
+
115
+
Pass in options for child constructors and options for children of the child.
116
+
117
+
```js
118
+
var player =videojs('some-vid-id');
119
+
var Component =videojs.getComponent('Component');
120
+
var myComponent =newComponent(player);
121
+
var myButton =myComponent.addChild('MyButton', {
122
+
text:'Press Me',
123
+
buttonChildExample: {
124
+
buttonChildOption:true
125
+
}
126
+
});
127
+
```
128
+
129
+
Children can also be added via options when a component is initialized.
130
+
131
+
> Note: Include a 'name' key which will be used if two child components of the same
132
+
> type that need different options.
133
+
134
+
```js
135
+
// MyComponent is from the above example
136
+
var myComp =newMyComponent(player, {
137
+
children: ['button', {
138
+
name:'button',
139
+
someOtherOption:true
140
+
}, {
141
+
name:'button',
142
+
someOtherOption:false
143
+
}]
144
+
});
145
+
```
146
+
147
+
## Event Listening
148
+
149
+
### Using `on`
150
+
151
+
```js
152
+
var player =videojs('some-player-id');
153
+
var Component =videojs.getComponent('Component');
154
+
var myComponent =newComponent(player);
155
+
varmyFunc=function() {
156
+
var myComponent =this;
157
+
console.log('myFunc called');
158
+
};
159
+
160
+
myComponent.on('eventType', myFunc);
161
+
myComponent.trigger('eventType');
162
+
// logs 'myFunc called'
163
+
```
164
+
165
+
The context of `myFunc` will be `myComponent` unless it is bound. You can add
*[on() and addEventListener()](#on-and-addeventlistener)
7
+
*[off() and removeEventListener](#off-and-removeeventlistener)
8
+
*[one()](#one)
9
+
*[trigger() and dispatchEvent](#trigger-and-dispatchevent)
10
+
11
+
## Overview
12
+
13
+
Events in video.js are setup so that they mimic the DOM API that is used on object, but also have helpful shorthand functions with the same functionality.
14
+
15
+
## `on()` and `addEventListener()`
16
+
17
+
This function is used to add an event listener to an EventTarget.
18
+
19
+
```js
20
+
var foo =newEventTarget();
21
+
varhandleBar=function() {
22
+
console.log('bar was triggered');
23
+
};
24
+
25
+
foo.on('bar', handleBar);
26
+
27
+
// This causes any `event listeners` for the `bar` event to get called
28
+
// see {@link EventTarget#trigger} for more information
29
+
foo.trigger('bar');
30
+
// logs 'bar was triggered'
31
+
```
32
+
33
+
## `off()` and `removeEventListener()`
34
+
35
+
This function is used to remove an listener function from an EventTarget.
36
+
37
+
```js
38
+
var foo =newEventTarget();
39
+
varhandleBar=function() {
40
+
console.log('bar was triggered');
41
+
};
42
+
43
+
// adds an `event listener` for the `bar` event
44
+
// see {@link EventTarget#on} for more info
45
+
foo.on('bar', handleBar);
46
+
47
+
// runs all `event listeners` for the `bar` event
48
+
// see {@link EventTarget#trigger} for more info
49
+
foo.trigger('bar');
50
+
// logs 'bar was triggered'
51
+
52
+
foo.off('bar', handleBar);
53
+
foo.trigger('bar');
54
+
// does nothing
55
+
```
56
+
57
+
## `one()`
58
+
59
+
This function is used to only have an event listener called once and never again.
60
+
61
+
Using `on()` and `off()` to mimic `one()` (not recommended)
62
+
63
+
```js
64
+
var foo =newEventTarget();
65
+
varhandleBar=function() {
66
+
console.log('bar was triggered');
67
+
// after the first trigger remove this handler
68
+
foo.off('bar', handleBar);
69
+
};
70
+
71
+
foo.on('bar', handleBar);
72
+
foo.trigger('bar');
73
+
// logs 'bar was triggered'
74
+
75
+
foo.trigger('bar');
76
+
// does nothing
77
+
```
78
+
79
+
Using `one()`
80
+
81
+
```js
82
+
var foo =newEventTarget();
83
+
varhandleBar=function() {
84
+
console.log('bar was triggered');
85
+
};
86
+
87
+
// removed after the first trigger
88
+
foo.one('bar', handleBar);
89
+
foo.trigger('bar');
90
+
// logs 'bar was triggered'
91
+
92
+
foo.trigger('bar');
93
+
// does nothing
94
+
```
95
+
96
+
## `trigger()` and `dispatchEvent()`
97
+
98
+
This function is used to trigger an event on an EventTarget which will cause all listeners to run.
99
+
100
+
> Note: if 'click' is in `EventTarget.allowedEvents_`, trigger will attempt to call the
0 commit comments