|
| 1 | +import { Component, Input, Output, ElementRef, Renderer, OnInit, EventEmitter } from '@angular/core'; |
| 2 | +import { FBMLAttribute, FBMLComponent, FBMLInstanceMethod } from './fbml-component'; |
| 3 | +declare var FB: any; |
| 4 | + |
| 5 | +/** |
| 6 | + * @usage |
| 7 | + * ```html |
| 8 | + * <!-- basic usage --> |
| 9 | + * <fb-video href="path/to/video"></fb-video> |
| 10 | + * |
| 11 | + * <!-- event emitters --> |
| 12 | + * <fb-video href="path/to/video" (startPlaying)="onVideoStartPlaying($event)" (paused)="onVideoPaused($event)"></fb-video> |
| 13 | + * ``` |
| 14 | + * |
| 15 | + * To manually interact with the video player, fetch it using `ViewChild`. |
| 16 | + * |
| 17 | + * ```ts |
| 18 | + * import { Component, ViewChild } from '@angular/core'; |
| 19 | + * import { FBVideoComponent } from 'ng2-facebook-sdk'; |
| 20 | + * |
| 21 | + * @Component(...) |
| 22 | + * export class MyComponent { |
| 23 | + * |
| 24 | + * @ViewChild(FBVideoComponent) video: FBVideoComponent; |
| 25 | + * |
| 26 | + * ngAfterViewInit() { |
| 27 | + * this.video.play(); |
| 28 | + * this.video.pause(); |
| 29 | + * this.video.getVolume(); |
| 30 | + * } |
| 31 | + * |
| 32 | + * } |
| 33 | + * |
| 34 | + * ``` |
| 35 | + */ |
| 36 | +@Component({ |
| 37 | + selector: 'fb-video', |
| 38 | + template: '' |
| 39 | +}) |
| 40 | +export class FBVideoComponent extends FBMLComponent implements OnInit { |
| 41 | + |
| 42 | + private _instance: any; |
| 43 | + |
| 44 | + /** |
| 45 | + * The absolute URL of the video. |
| 46 | + */ |
| 47 | + @Input() |
| 48 | + @FBMLAttribute |
| 49 | + href: string; |
| 50 | + |
| 51 | + /** |
| 52 | + * Allow the video to be played in fullscreen mode. Can be false or true. Defaults to false; |
| 53 | + */ |
| 54 | + @Input() |
| 55 | + @FBMLAttribute |
| 56 | + allowfullscreen: boolean; |
| 57 | + |
| 58 | + /** |
| 59 | + * Automatically start playing the video when the page loads. The video will be played without sound (muted). People can turn on sound via the video player controls. This setting does not apply to mobile devices. Can be false or true. Defaults to false. |
| 60 | + */ |
| 61 | + @Input() |
| 62 | + @FBMLAttribute |
| 63 | + autoplay: boolean; |
| 64 | + |
| 65 | + /** |
| 66 | + * The width of the video container. Min. 220px. |
| 67 | + */ |
| 68 | + @Input() |
| 69 | + @FBMLAttribute |
| 70 | + width: string; |
| 71 | + |
| 72 | + /** |
| 73 | + * Set to true to include the text from the Facebook post associated with the video, if any. |
| 74 | + */ |
| 75 | + @Input() |
| 76 | + @FBMLAttribute |
| 77 | + showText: boolean; |
| 78 | + |
| 79 | + /** |
| 80 | + * Set to true to show captions (if available) by default. Captions are only available on desktop. |
| 81 | + */ |
| 82 | + @Input() |
| 83 | + @FBMLAttribute |
| 84 | + showCaptions: boolean; |
| 85 | + |
| 86 | + /** |
| 87 | + * Fired when video starts to play. |
| 88 | + * @type {EventEmitter<any>} |
| 89 | + */ |
| 90 | + @Output() |
| 91 | + startedPlaying: EventEmitter<any> = new EventEmitter<any>(); |
| 92 | + |
| 93 | + /** |
| 94 | + * Fired when video pauses. |
| 95 | + * @type {EventEmitter<any>} |
| 96 | + */ |
| 97 | + @Output() |
| 98 | + paused: EventEmitter<any> = new EventEmitter<any>(); |
| 99 | + |
| 100 | + /** |
| 101 | + * |
| 102 | + Fired when video finishes playing. |
| 103 | + * @type {EventEmitter<any>} |
| 104 | + */ |
| 105 | + @Output() |
| 106 | + finishedPlaying: EventEmitter<any> = new EventEmitter<any>(); |
| 107 | + |
| 108 | + /** |
| 109 | + * Fired when video starts to buffer. |
| 110 | + * @type {EventEmitter<any>} |
| 111 | + */ |
| 112 | + @Output() |
| 113 | + startedBuffering: EventEmitter<any> = new EventEmitter<any>(); |
| 114 | + |
| 115 | + /** |
| 116 | + * Fired when video recovers from buffering. |
| 117 | + * @type {EventEmitter<any>} |
| 118 | + */ |
| 119 | + @Output() |
| 120 | + finishedBuffering: EventEmitter<any> = new EventEmitter<any>(); |
| 121 | + |
| 122 | + /** |
| 123 | + * Fired when an error occurs on the video. |
| 124 | + * @type {EventEmitter<any>} |
| 125 | + */ |
| 126 | + @Output() |
| 127 | + error: EventEmitter<any> = new EventEmitter<any>(); |
| 128 | + |
| 129 | + constructor( |
| 130 | + el: ElementRef, |
| 131 | + rnd: Renderer |
| 132 | + ) { |
| 133 | + super(el, rnd, 'fb-video'); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @hidden |
| 138 | + */ |
| 139 | + ngOnInit() { |
| 140 | + FB.Event.subscribe('xfbml.ready', (msg: any) => { |
| 141 | + if (msg.type === 'video') { |
| 142 | + this._instance = msg.instance; |
| 143 | + console.log(msg); |
| 144 | + // TODO make sure that we getting the right instance, in case we have more than one player |
| 145 | + |
| 146 | + this._instance.subscribe('startedPlaying', (e: any) => this.startedPlaying.emit(e)); |
| 147 | + this._instance.subscribe('paused', (e: any) => this.paused.emit(e)); |
| 148 | + this._instance.subscribe('finishedPlaying', (e: any) => this.finishedPlaying.emit(e)); |
| 149 | + this._instance.subscribe('startedBuffering', (e: any) => this.startedBuffering.emit(e)); |
| 150 | + this._instance.subscribe('finishedBuffering', (e: any) => this.finishedBuffering.emit(e)); |
| 151 | + this._instance.subscribe('error', (e: any) => this.error.emit(e)); |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Plays the video. |
| 158 | + */ |
| 159 | + @FBMLInstanceMethod |
| 160 | + play() {} |
| 161 | + |
| 162 | + /** |
| 163 | + * Pauses the video. |
| 164 | + */ |
| 165 | + @FBMLInstanceMethod |
| 166 | + pause() {} |
| 167 | + |
| 168 | + /** |
| 169 | + * Seeks to specified position. |
| 170 | + * @param seconds {number} |
| 171 | + */ |
| 172 | + @FBMLInstanceMethod |
| 173 | + seek(seconds: number) {} |
| 174 | + |
| 175 | + /** |
| 176 | + * Mute the video. |
| 177 | + */ |
| 178 | + @FBMLInstanceMethod |
| 179 | + mute() {} |
| 180 | + |
| 181 | + /** |
| 182 | + * Unmute the video. |
| 183 | + */ |
| 184 | + @FBMLInstanceMethod |
| 185 | + unmute() {} |
| 186 | + |
| 187 | + /** |
| 188 | + * Returns true if video is muted, false if not. |
| 189 | + */ |
| 190 | + @FBMLInstanceMethod |
| 191 | + isMuted(): boolean { return; } |
| 192 | + |
| 193 | + /** |
| 194 | + * Set the volume |
| 195 | + * @param volume |
| 196 | + */ |
| 197 | + @FBMLInstanceMethod |
| 198 | + setVolume(volume: number) {} |
| 199 | + |
| 200 | + /** |
| 201 | + * Get the volume |
| 202 | + */ |
| 203 | + @FBMLInstanceMethod |
| 204 | + getVolume(): number { return; } |
| 205 | + |
| 206 | + /** |
| 207 | + * Returns the current video time position in seconds |
| 208 | + */ |
| 209 | + @FBMLInstanceMethod |
| 210 | + getCurrentPosition() {} |
| 211 | + |
| 212 | + /** |
| 213 | + * Returns the video duration in seconds |
| 214 | + */ |
| 215 | + @FBMLInstanceMethod |
| 216 | + getDuration() {} |
| 217 | + |
| 218 | +} |
0 commit comments