From 9df52d660feb722404be00a50f3c8a612bec1c15 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 17 Aug 2021 14:42:53 +0200 Subject: Migrate client to eslint --- client/src/standalone/player/definitions.ts | 2 +- client/src/standalone/player/events.ts | 14 ++++----- client/src/standalone/player/player.ts | 31 +++++++++++-------- client/src/standalone/videos/embed-api.ts | 19 ++++++------ client/src/standalone/videos/embed.ts | 47 +++++++++++++++-------------- client/src/standalone/videos/test-embed.ts | 20 ++++++------ 6 files changed, 71 insertions(+), 62 deletions(-) (limited to 'client/src/standalone') diff --git a/client/src/standalone/player/definitions.ts b/client/src/standalone/player/definitions.ts index cc5203ed5..495f1a98c 100644 --- a/client/src/standalone/player/definitions.ts +++ b/client/src/standalone/player/definitions.ts @@ -21,5 +21,5 @@ export type PeerTubeTextTrack = { id: string label: string src: string - mode: 'showing' | 'disabled' + mode: TextTrackMode } diff --git a/client/src/standalone/player/events.ts b/client/src/standalone/player/events.ts index 28a13c727..7a8e9dbec 100644 --- a/client/src/standalone/player/events.ts +++ b/client/src/standalone/player/events.ts @@ -1,7 +1,7 @@ import { EventHandler } from './definitions' interface PlayerEventRegistrar { - registrations: Function[] + registrations: EventHandler[] } interface PlayerEventRegistrationMap { @@ -20,28 +20,28 @@ export class EventRegistrar { public registerTypes (names: string[]) { for (const name of names) { - this.eventRegistrations[ name ] = { registrations: [] } + this.eventRegistrations[name] = { registrations: [] } } } public fire (name: string, event: T) { - this.eventRegistrations[ name ].registrations.forEach(x => x(event)) + this.eventRegistrations[name].registrations.forEach(x => x(event)) } public addListener (name: string, handler: EventHandler) { - if (!this.eventRegistrations[ name ]) { + if (!this.eventRegistrations[name]) { console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`) return false } - this.eventRegistrations[ name ].registrations.push(handler) + this.eventRegistrations[name].registrations.push(handler) return true } public removeListener (name: string, handler: EventHandler) { - if (!this.eventRegistrations[ name ]) return false + if (!this.eventRegistrations[name]) return false - this.eventRegistrations[ name ].registrations = this.eventRegistrations[ name ].registrations.filter(x => x === handler) + this.eventRegistrations[name].registrations = this.eventRegistrations[name].registrations.filter(x => x === handler) return true } diff --git a/client/src/standalone/player/player.ts b/client/src/standalone/player/player.ts index 9776fda12..bbe37a42b 100644 --- a/client/src/standalone/player/player.ts +++ b/client/src/standalone/player/player.ts @@ -17,7 +17,7 @@ const PASSTHROUGH_EVENTS = [ */ export class PeerTubePlayer { - private eventRegistrar: EventRegistrar = new EventRegistrar() + private readonly eventRegistrar: EventRegistrar = new EventRegistrar() private channel: Channel.MessagingChannel private readyPromise: Promise @@ -31,8 +31,8 @@ export class PeerTubePlayer { * @param scope */ constructor ( - private embedElement: HTMLIFrameElement, - private scope?: string + private readonly embedElement: HTMLIFrameElement, + private readonly scope?: string ) { this.eventRegistrar.registerTypes(PASSTHROUGH_EVENTS) @@ -90,6 +90,7 @@ export class PeerTubePlayer { /** * Tell the embed to change the audio volume + * * @param value A number from 0 to 1 */ async setVolume (value: number) { @@ -98,14 +99,16 @@ export class PeerTubePlayer { /** * Get the current volume level in the embed. + * * @param value A number from 0 to 1 */ async getVolume (): Promise { - return this.sendMessage('getVolume') + return this.sendMessage('getVolume') } /** * Tell the embed to change the current caption + * * @param value Caption id */ async setCaption (value: string) { @@ -116,11 +119,12 @@ export class PeerTubePlayer { * Get video captions */ async getCaptions (): Promise { - return this.sendMessage('getCaptions') + return this.sendMessage('getCaptions') } /** * Tell the embed to seek to a specific position (in seconds) + * * @param seconds */ async seek (seconds: number) { @@ -143,21 +147,21 @@ export class PeerTubePlayer { * resolutions change. */ async getResolutions (): Promise { - return this.sendMessage('getResolutions') + return this.sendMessage('getResolutions') } /** * Retrieve a list of available playback rates. */ async getPlaybackRates (): Promise { - return this.sendMessage('getPlaybackRates') + return this.sendMessage('getPlaybackRates') } /** * Get the current playback rate. Defaults to 1 (1x playback rate). */ async getPlaybackRate (): Promise { - return this.sendMessage('getPlaybackRate') + return this.sendMessage('getPlaybackRate') } /** @@ -188,7 +192,7 @@ export class PeerTubePlayer { * Get video position currently played (starts from 1) */ async getCurrentPosition () { - return this.sendMessage('getCurrentPosition') + return this.sendMessage('getCurrentPosition') } private constructChannel () { @@ -201,8 +205,8 @@ export class PeerTubePlayer { } private prepareToBeReady () { - let readyResolve: Function - let readyReject: Function + let readyResolve: () => void + let readyReject: () => void this.readyPromise = new Promise((res, rej) => { readyResolve = res @@ -219,7 +223,8 @@ export class PeerTubePlayer { private sendMessage (method: string, params?: TIn): Promise { return new Promise((resolve, reject) => { this.channel.call({ - method, params, + method, + params, success: result => resolve(result), error: error => reject(error) }) @@ -228,4 +233,4 @@ export class PeerTubePlayer { } // put it on the window as well as the export -(window[ 'PeerTubePlayer' ] as any) = PeerTubePlayer +(window['PeerTubePlayer'] as any) = PeerTubePlayer diff --git a/client/src/standalone/videos/embed-api.ts b/client/src/standalone/videos/embed-api.ts index 75174f2f8..b5c9da431 100644 --- a/client/src/standalone/videos/embed-api.ts +++ b/client/src/standalone/videos/embed-api.ts @@ -13,7 +13,8 @@ export class PeerTubeEmbedApi { private isReady = false private resolutions: PeerTubeResolution[] = [] - constructor (private embed: PeerTubeEmbed) { + constructor (private readonly embed: PeerTubeEmbed) { + } initialize () { @@ -45,7 +46,7 @@ export class PeerTubeEmbedApi { channel.bind('getResolutions', (txn, params) => this.resolutions) channel.bind('getCaptions', (txn, params) => this.getCaptions()) - channel.bind('setCaption', (txn, id) => this.setCaption(id)), + channel.bind('setCaption', (txn, id) => this.setCaption(id)) channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate)) channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate()) @@ -79,14 +80,12 @@ export class PeerTubeEmbedApi { } private getCaptions (): PeerTubeTextTrack[] { - return this.embed.player.textTracks().tracks_.map(t => { - return { - id: t.id, - src: t.src, - label: t.label, - mode: t.mode as any - } - }) + return this.embed.player.textTracks().tracks_.map(t => ({ + id: t.id, + src: t.src, + label: t.label, + mode: t.mode + })) } private setCaption (id: string) { diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index c9a4e541c..dad717108 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -64,17 +64,11 @@ export class PeerTubeEmbed { private playlistElements: VideoPlaylistElement[] private currentPlaylistElement: VideoPlaylistElement - private wrapperElement: HTMLElement + private readonly wrapperElement: HTMLElement private pluginsManager: PluginsManager - static async main () { - const videoContainerId = 'video-wrapper' - const embed = new PeerTubeEmbed(videoContainerId) - await embed.init() - } - - constructor (private videoWrapperId: string) { + constructor (private readonly videoWrapperId: string) { this.wrapperElement = document.getElementById(this.videoWrapperId) try { @@ -84,6 +78,12 @@ export class PeerTubeEmbed { } } + static async main () { + const videoContainerId = 'video-wrapper' + const embed = new PeerTubeEmbed(videoContainerId) + await embed.init() + } + getVideoUrl (id: string) { return window.location.origin + '/api/v1/videos/' + id } @@ -316,7 +316,7 @@ export class PeerTubeEmbed { while (total > elements.length && i < 10) { const result = await this.loadPlaylistElements(playlistId, elements.length) - const json = await result.json() as ResultList + const json = await result.json() total = json.total elements = elements.concat(json.data) @@ -469,7 +469,7 @@ export class PeerTubeEmbed { // Issue when we parsed config from HTML, fallback to API if (!this.config) { this.config = await this.refreshFetch('/api/v1/config') - .then(res => res.json()) + .then(res => res.json()) } const videoInfoPromise = videoResponse.json() @@ -506,7 +506,7 @@ export class PeerTubeEmbed { this.currentPlaylistElement = videoPlaylistElement this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid) - .catch(err => console.error(err)) + .catch(err => console.error(err)) } } : undefined @@ -542,7 +542,9 @@ export class PeerTubeEmbed { isLive: videoInfo.isLive, playerElement: this.playerElement, - onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, + onPlayerElementChange: (element: HTMLVideoElement) => { + this.playerElement = element + }, videoDuration: videoInfo.duration, enableHotkeys: true, @@ -577,10 +579,13 @@ export class PeerTubeEmbed { }) } - this.player = await PeertubePlayerManager.initialize(this.mode, options, (player: videojs.Player) => this.player = player) + this.player = await PeertubePlayerManager.initialize(this.mode, options, (player: videojs.Player) => { + this.player = player + }) + this.player.on('customError', (event: any, data: any) => this.handleError(data.err, serverTranslations)) - window[ 'videojsPlayer' ] = this.player + window['videojsPlayer'] = this.player this.buildCSS() @@ -656,7 +661,7 @@ export class PeerTubeEmbed { this.player.dispose() this.playerElement = null this.displayError('This video is not available because the remote instance is not responding.', translations) - return + } } @@ -694,9 +699,9 @@ export class PeerTubeEmbed { private async buildCaptions (serverTranslations: any, captionsResponse: Response): Promise { if (captionsResponse.ok) { - const { data } = (await captionsResponse.json()) as ResultList + const { data } = await captionsResponse.json() - return data.map(c => ({ + return data.map((c: VideoCaption) => ({ label: peertubeTranslate(c.language.label, serverTranslations), language: c.language.id, src: window.location.origin + c.captionPath @@ -733,7 +738,7 @@ export class PeerTubeEmbed { private getResourceId () { const urlParts = window.location.pathname.split('/') - return urlParts[ urlParts.length - 1 ] + return urlParts[urlParts.length - 1] } private isPlaylistEmbed () { @@ -751,7 +756,7 @@ export class PeerTubeEmbed { } private buildPeerTubeHelpers (translations?: { [ id: string ]: string }): RegisterClientHelpers { - function unimplemented (): any { + const unimplemented = () => { throw new Error('This helper is not implemented in embed.') } @@ -780,9 +785,7 @@ export class PeerTubeEmbed { enhancedMarkdownToHTML: unimplemented }, - translate: (value: string) => { - return Promise.resolve(peertubeTranslate(value, translations)) - } + translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations)) } } } diff --git a/client/src/standalone/videos/test-embed.ts b/client/src/standalone/videos/test-embed.ts index 6e035c0c9..066b3e024 100644 --- a/client/src/standalone/videos/test-embed.ts +++ b/client/src/standalone/videos/test-embed.ts @@ -4,11 +4,11 @@ import { PeerTubePlayer } from '../player/player' window.addEventListener('load', async () => { const urlParts = window.location.href.split('/') - const lastPart = urlParts[ urlParts.length - 1 ] + const lastPart = urlParts[urlParts.length - 1] const isPlaylist = window.location.pathname.startsWith('/video-playlists/') - const elementId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[ 0 ] + const elementId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[0] const iframe = document.createElement('iframe') iframe.src = isPlaylist @@ -18,14 +18,14 @@ window.addEventListener('load', async () => { const mainElement = document.querySelector('#host') mainElement.appendChild(iframe) - console.log(`Document finished loading.`) + console.log('Document finished loading.') const player = new PeerTubePlayer(document.querySelector('iframe')) - window[ 'player' ] = player + window['player'] = player - console.log(`Awaiting player ready...`) + console.log('Awaiting player ready...') await player.ready - console.log(`Player is ready.`) + console.log('Player is ready.') const monitoredEvents = [ 'pause', @@ -39,7 +39,9 @@ window.addEventListener('load', async () => { console.log(`PLAYER: now listening for event '${e}'`) player.getCurrentPosition() - .then(position => document.getElementById('playlist-position').innerHTML = position + '') + .then(position => { + document.getElementById('playlist-position').innerHTML = position + '' + }) }) let playbackRates: number[] = [] @@ -105,7 +107,7 @@ window.addEventListener('load', async () => { updateCaptions() - const updateResolutions = ((resolutions: PeerTubeResolution[]) => { + const updateResolutions = (resolutions: PeerTubeResolution[]) => { const resolutionListEl = document.querySelector('#resolution-list') resolutionListEl.innerHTML = '' @@ -126,7 +128,7 @@ window.addEventListener('load', async () => { resolutionListEl.appendChild(itemEl) } }) - }) + } player.getResolutions().then( resolutions => updateResolutions(resolutions)) -- cgit v1.2.3