Skip to content

Commit

Permalink
Merge pull request #5307 from avalonmediasystem/conditional_local_sto…
Browse files Browse the repository at this point in the history
…rage

LocalStorage is not available inside an iframe unless users change a setting to allow it
  • Loading branch information
cjcolvar authored Aug 16, 2023
2 parents 6364985 + f515a3a commit a868aae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
34 changes: 25 additions & 9 deletions app/assets/javascripts/media_player_wrapper/avalon_player_new.es6
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class MEJSPlayer {
this.mejsUtility = new MEJSUtility();
this.mejsTimeRailHelper = new MEJSTimeRailHelper();
this.mejsMarkersHelper = new MEJSMarkersHelper();
this.localStorage = window.localStorage;
if (typeof window.localStorage !== "undefined") {
this.localStorage = window.localStorage;
} else {
this.localStorage = false;
}
this.canvasIndex = 0;

// Unpack player configuration object for the new player.
Expand Down Expand Up @@ -184,6 +188,8 @@ class MEJSPlayer {
* @return {void}
*/
handleVolumeChange() {
if (!this.localStorage) return;

this.localStorage.setItem('startVolume', this.mediaElement.volume)
}

Expand All @@ -194,6 +200,8 @@ class MEJSPlayer {
* @return {void}
*/
handleCaptionsChange() {
if (!this.localStorage) return;

let srclang = currentPlayer.selectedTrack === null ? '' : currentPlayer.selectedTrack.srclang;
this.localStorage.setItem('captions', srclang)
}
Expand Down Expand Up @@ -291,7 +299,9 @@ class MEJSPlayer {
// Quality selector is turned off in mobile devices
if(!mejs.Features.isAndroid) {
// Set defaultQuality in player options before building the quality feature
this.player.options.defaultQuality = this.localStorage.getItem('quality');
if (!this.localStorage) {
this.player.options.defaultQuality = this.localStorage.getItem('quality');
}

// Build quality
this.player.buildquality(this.player, null, null, this.mediaElement);
Expand Down Expand Up @@ -365,6 +375,8 @@ class MEJSPlayer {
* @returns {void}
*/
toggleCaptions() {
if (!this.localStorage) return;

if (this.mediaType==="video" && this.player.options.toggleCaptionsButtonWhenOnlyOne) {
if (this.localStorage.getItem('captions') !== '' && this.player.tracks && this.player.tracks.length===1) {
this.player.setTrack(this.player.tracks[0].trackId, (typeof keyboard !== 'undefined'));
Expand Down Expand Up @@ -601,13 +613,17 @@ class MEJSPlayer {
*/
initializePlayer() {
let currentStreamInfo = this.currentStreamInfo;
// Set default quality value in localStorage
this.localStorage.setItem('quality', this.defaultQuality);

// Interval in seconds to jump forward and backward in media
let jumpInterval = 5;

// Set default quality value in localStorage
this.localStorage.setItem('quality', this.defaultQuality);
let startVolume = 1.0;
let startLanguage = '';
if (!this.localStorage) {
startVolume = this.localStorage.getItem('startVolume') || 1.0;
startLanguage = this.localStorage.getItem('captions') || '';
// Set default quality value in localStorage
this.localStorage.setItem('quality', this.defaultQuality);
}

// Mediaelement default root level configuration
let defaults = {
Expand All @@ -621,8 +637,8 @@ class MEJSPlayer {
qualityText: 'Stream Quality',
defaultQuality: this.defaultQuality,
toggleCaptionsButtonWhenOnlyOne: true,
startVolume: this.localStorage.getItem('startVolume') || 1.0,
startLanguage: this.localStorage.getItem('captions') || '',
startVolume: startVolume,
startLanguage: startLanguage,
// jump forward and backward when player is not focused
defaultSeekBackwardInterval: function() { return jumpInterval },
defaultSeekForwardInterval: function() { return jumpInterval }
Expand Down
10 changes: 9 additions & 1 deletion app/assets/javascripts/supplemental_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ $('.supplemental-file-form')
$row.find('.visible-inline').addClass('alert');
});

/* Store collapsed section ids in localStorage */
/* Store collapsed section ids in localStorage if available */
$('button[id^=edit_section').on('click', (e) => {
if (typeof window.localStorage === "undefined") {
return;
}

// Active sections
var activeSections = JSON.parse(localStorage.getItem('activeSections')) || [];

Expand All @@ -123,6 +127,10 @@ $('button[id^=edit_section').on('click', (e) => {

/* On page reload; collapse sections which were collapsed previously */
$(document).ready(function () {
if (typeof window.localStorage === "undefined") {
return;
}

var activeSections = JSON.parse(localStorage.getItem('activeSections')) || [];

// Collapse active sections on page
Expand Down

0 comments on commit a868aae

Please sign in to comment.