1
0
Fork 0

Move player local storage functions in their own file

This commit is contained in:
Chocobozzz 2018-06-18 09:46:58 +02:00
parent f5a2dc48eb
commit 7b3a99d517
No known key found for this signature in database
GPG key ID: 583A612D890159BE
4 changed files with 100 additions and 93 deletions

View file

@ -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 */
}
}

View file

@ -4,18 +4,17 @@ import { VideoFile } from '../../../../shared/models/videos/video.model'
import { renderVideo } from './video-renderer' import { renderVideo } from './video-renderer'
import './settings-menu-button' import './settings-menu-button'
import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
import { import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils'
getAverageBandwidth,
getStoredMute,
getStoredVolume, isMobile,
saveAverageBandwidth,
saveMuteInStore,
saveVolumeInStore,
videoFileMaxByResolution,
videoFileMinByResolution
} from './utils'
import * as CacheChunkStore from 'cache-chunk-store' import * as CacheChunkStore from 'cache-chunk-store'
import { PeertubeChunkStore } from './peertube-chunk-store' import { PeertubeChunkStore } from './peertube-chunk-store'
import {
getAverageBandwidthInStore,
getStoredMute,
getStoredVolume,
saveAverageBandwidth,
saveMuteInStore,
saveVolumeInStore
} from './peertube-player-local-storage'
const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
class PeerTubePlugin extends Plugin { class PeerTubePlugin extends Plugin {
@ -148,7 +147,7 @@ class PeerTubePlugin extends Plugin {
) { ) {
// Automatically choose the adapted video file // Automatically choose the adapted video file
if (videoFile === undefined) { if (videoFile === undefined) {
const savedAverageBandwidth = getAverageBandwidth() const savedAverageBandwidth = getAverageBandwidthInStore()
videoFile = savedAverageBandwidth videoFile = savedAverageBandwidth
? this.getAppropriateFile(savedAverageBandwidth) ? this.getAppropriateFile(savedAverageBandwidth)
: this.pickAverageVideoFile() : this.pickAverageVideoFile()

View file

@ -1,5 +1,6 @@
import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' 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') const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button')
class TheaterButton extends Button { class TheaterButton extends Button {

View file

@ -1,4 +1,3 @@
import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
import { VideoFile } from '../../../../shared/models/videos' import { VideoFile } from '../../../../shared/models/videos'
function toTitleCase (str: string) { function toTitleCase (str: string) {
@ -20,60 +19,6 @@ function bytes (value) {
return [ calc, format.type ] 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 () { function isMobile () {
return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
} }
@ -132,39 +77,15 @@ function videoFileMinByResolution (files: VideoFile[]) {
return min return min
} }
// ---------------------------------------------------------------------------
export { export {
toTitleCase, toTitleCase,
buildVideoLink, buildVideoLink,
getStoredVolume,
saveVolumeInStore,
saveAverageBandwidth,
getAverageBandwidth,
saveMuteInStore,
buildVideoEmbed, buildVideoEmbed,
getStoredMute,
videoFileMaxByResolution, videoFileMaxByResolution,
videoFileMinByResolution, videoFileMinByResolution,
copyToClipboard, copyToClipboard,
getStoredTheater,
saveTheaterInStore,
isMobile, isMobile,
bytes 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 */ }
}