From 7b3a99d51716e404bdea0cef8d1f994aab0e8aac Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 18 Jun 2018 09:46:58 +0200 Subject: Move player local storage functions in their own file --- .../assets/player/peertube-player-local-storage.ts | 86 ++++++++++++++++++++++ .../src/assets/player/peertube-videojs-plugin.ts | 17 ++--- client/src/assets/player/theater-button.ts | 3 +- client/src/assets/player/utils.ts | 83 +-------------------- 4 files changed, 98 insertions(+), 91 deletions(-) create mode 100644 client/src/assets/player/peertube-player-local-storage.ts diff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.ts new file mode 100644 index 000000000..6882f68a6 --- /dev/null +++ b/client/src/assets/player/peertube-player-local-storage.ts @@ -0,0 +1,86 @@ +function getStoredVolume () { + const value = getLocalStorage('volume') + if (value !== null && value !== undefined) { + const valueNumber = parseFloat(value) + if (isNaN(valueNumber)) return undefined + + return valueNumber + } + + return undefined +} + +function getStoredMute () { + const value = getLocalStorage('mute') + if (value !== null && value !== undefined) return value === 'true' + + return undefined +} + +function getStoredTheater () { + const value = getLocalStorage('theater-enabled') + if (value !== null && value !== undefined) return value === 'true' + + return undefined +} + +function saveVolumeInStore (value: number) { + return setLocalStorage('volume', value.toString()) +} + +function saveMuteInStore (value: boolean) { + return setLocalStorage('mute', value.toString()) +} + +function saveTheaterInStore (enabled: boolean) { + return setLocalStorage('theater-enabled', enabled.toString()) +} + +function saveAverageBandwidth (value: number) { + return setLocalStorage('average-bandwidth', value.toString()) +} + +function getAverageBandwidthInStore () { + const value = getLocalStorage('average-bandwidth') + if (value !== null && value !== undefined) { + const valueNumber = parseInt(value, 10) + if (isNaN(valueNumber)) return undefined + + return valueNumber + } + + return undefined +} + + +// --------------------------------------------------------------------------- + +export { + getStoredVolume, + getStoredMute, + getStoredTheater, + saveVolumeInStore, + saveMuteInStore, + saveTheaterInStore, + saveAverageBandwidth, + getAverageBandwidthInStore +} + +// --------------------------------------------------------------------------- + +const KEY_PREFIX = 'peertube-videojs-' + +function getLocalStorage (key: string) { + try { + return localStorage.getItem(KEY_PREFIX + key) + } catch { + return undefined + } +} + +function setLocalStorage (key: string, value: string) { + try { + localStorage.setItem(KEY_PREFIX + key, value) + } catch { /* empty */ + } +} diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index b62dcb829..35a347e99 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts @@ -4,18 +4,17 @@ import { VideoFile } from '../../../../shared/models/videos/video.model' import { renderVideo } from './video-renderer' import './settings-menu-button' import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' +import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils' +import * as CacheChunkStore from 'cache-chunk-store' +import { PeertubeChunkStore } from './peertube-chunk-store' import { - getAverageBandwidth, + getAverageBandwidthInStore, getStoredMute, - getStoredVolume, isMobile, + getStoredVolume, saveAverageBandwidth, saveMuteInStore, - saveVolumeInStore, - videoFileMaxByResolution, - videoFileMinByResolution -} from './utils' -import * as CacheChunkStore from 'cache-chunk-store' -import { PeertubeChunkStore } from './peertube-chunk-store' + saveVolumeInStore +} from './peertube-player-local-storage' const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') class PeerTubePlugin extends Plugin { @@ -148,7 +147,7 @@ class PeerTubePlugin extends Plugin { ) { // Automatically choose the adapted video file if (videoFile === undefined) { - const savedAverageBandwidth = getAverageBandwidth() + const savedAverageBandwidth = getAverageBandwidthInStore() videoFile = savedAverageBandwidth ? this.getAppropriateFile(savedAverageBandwidth) : this.pickAverageVideoFile() diff --git a/client/src/assets/player/theater-button.ts b/client/src/assets/player/theater-button.ts index 1d330e08f..bad737eda 100644 --- a/client/src/assets/player/theater-button.ts +++ b/client/src/assets/player/theater-button.ts @@ -1,5 +1,6 @@ import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' -import { getStoredTheater, saveTheaterInStore } from './utils' +import { saveTheaterInStore } from './peertube-player-local-storage' +import { getStoredTheater } from './peertube-player-local-storage' const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') class TheaterButton extends Button { diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts index 18a6b4dfa..c27e630e5 100644 --- a/client/src/assets/player/utils.ts +++ b/client/src/assets/player/utils.ts @@ -1,4 +1,3 @@ -import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n' import { VideoFile } from '../../../../shared/models/videos' function toTitleCase (str: string) { @@ -20,60 +19,6 @@ function bytes (value) { return [ calc, format.type ] } -function getStoredVolume () { - const value = getLocalStorage('volume') - if (value !== null && value !== undefined) { - const valueNumber = parseFloat(value) - if (isNaN(valueNumber)) return undefined - - return valueNumber - } - - return undefined -} - -function getStoredMute () { - const value = getLocalStorage('mute') - if (value !== null && value !== undefined) return value === 'true' - - return undefined -} - -function getAverageBandwidth () { - const value = getLocalStorage('average-bandwidth') - if (value !== null && value !== undefined) { - const valueNumber = parseInt(value, 10) - if (isNaN(valueNumber)) return undefined - - return valueNumber - } - - return undefined -} - -function getStoredTheater () { - const value = getLocalStorage('theater-enabled') - if (value !== null && value !== undefined) return value === 'true' - - return undefined -} - -function saveVolumeInStore (value: number) { - return setLocalStorage('volume', value.toString()) -} - -function saveMuteInStore (value: boolean) { - return setLocalStorage('mute', value.toString()) -} - -function saveTheaterInStore (enabled: boolean) { - return setLocalStorage('theater-enabled', enabled.toString()) -} - -function saveAverageBandwidth (value: number) { - return setLocalStorage('average-bandwidth', value.toString()) -} - function isMobile () { return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) } @@ -132,39 +77,15 @@ function videoFileMinByResolution (files: VideoFile[]) { return min } +// --------------------------------------------------------------------------- + export { toTitleCase, buildVideoLink, - getStoredVolume, - saveVolumeInStore, - saveAverageBandwidth, - getAverageBandwidth, - saveMuteInStore, buildVideoEmbed, - getStoredMute, videoFileMaxByResolution, videoFileMinByResolution, copyToClipboard, - getStoredTheater, - saveTheaterInStore, isMobile, bytes } - -// --------------------------------------------------------------------------- - -const KEY_PREFIX = 'peertube-videojs-' - -function getLocalStorage (key: string) { - try { - return localStorage.getItem(KEY_PREFIX + key) - } catch { - return undefined - } -} - -function setLocalStorage (key: string, value: string) { - try { - localStorage.setItem(KEY_PREFIX + key, value) - } catch { /* empty */ } -} -- cgit v1.2.3