Move player local storage functions in their own file

pull/681/head
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 './settings-menu-button'
import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
import {
getAverageBandwidth,
getStoredMute,
getStoredVolume, isMobile,
saveAverageBandwidth,
saveMuteInStore,
saveVolumeInStore,
videoFileMaxByResolution,
videoFileMinByResolution
} from './utils'
import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils'
import * as CacheChunkStore from 'cache-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')
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()

View File

@ -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 {

View File

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