Handle subtitles in player
[oweals/peertube.git] / client / src / assets / player / utils.ts
index 487b3a1be055c9114d4a872dc4465c2126ab049b..c27e630e58dd39610a68eec3e17584181abbf2a7 100644 (file)
@@ -1,3 +1,5 @@
+import { VideoFile } from '../../../../shared/models/videos'
+
 function toTitleCase (str: string) {
   return str.charAt(0).toUpperCase() + str.slice(1)
 }
@@ -17,49 +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 saveVolumeInStore (value: number) {
-  return setLocalStorage('volume', value.toString())
-}
-
-function saveMuteInStore (value: boolean) {
-  return setLocalStorage('mute', value.toString())
-}
-
-function saveAverageBandwidth (value: number) {
-  return setLocalStorage('average-bandwidth', value.toString())
-}
-
 function isMobile () {
   return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
 }
@@ -78,6 +37,7 @@ function buildVideoLink (time?: number) {
 
 function buildVideoEmbed (embedUrl: string) {
   return '<iframe width="560" height="315" ' +
+    'sandbox="allow-same-origin allow-scripts" ' +
     'src="' + embedUrl + '" ' +
     'frameborder="0" allowfullscreen>' +
     '</iframe>'
@@ -95,35 +55,37 @@ function copyToClipboard (text: string) {
   document.body.removeChild(el)
 }
 
+function videoFileMaxByResolution (files: VideoFile[]) {
+  let max = files[0]
+
+  for (let i = 1; i < files.length; i++) {
+    const file = files[i]
+    if (max.resolution.id < file.resolution.id) max = file
+  }
+
+  return max
+}
+
+function videoFileMinByResolution (files: VideoFile[]) {
+  let min = files[0]
+
+  for (let i = 1; i < files.length; i++) {
+    const file = files[i]
+    if (min.resolution.id > file.resolution.id) min = file
+  }
+
+  return min
+}
+
+// ---------------------------------------------------------------------------
+
 export {
   toTitleCase,
   buildVideoLink,
-  getStoredVolume,
-  saveVolumeInStore,
-  saveAverageBandwidth,
-  getAverageBandwidth,
-  saveMuteInStore,
   buildVideoEmbed,
-  getStoredMute,
+  videoFileMaxByResolution,
+  videoFileMinByResolution,
   copyToClipboard,
   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 */ }
-}