Skip to content

Commit

Permalink
@dmlap switched global options back to an object at videojs.options. c…
Browse files Browse the repository at this point in the history
…loses #2461
  • Loading branch information
dmlap authored and heff committed Aug 12, 2015
1 parent 4fd59c8 commit fecf3a0
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 161 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ CHANGELOG
* @misteroneill pass vtt.js option to tech ([view](https://github.com/videojs/video.js/pull/2448))
* @forbesjo updated the sauce labs config and browser versions ([view](https://github.com/videojs/video.js/pull/2450))
* @mmcc made sure controls respect muted attribute ([view](https://github.com/videojs/video.js/pull/2408))
* @dmlap switched global options back to an object at videojs.options ([view](https://github.com/videojs/video.js/pull/2461))

--------------------

Expand Down
54 changes: 0 additions & 54 deletions src/js/global-options.js

This file was deleted.

53 changes: 44 additions & 9 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { createTimeRange } from './utils/time-ranges.js';
import { bufferedPercent } from './utils/buffer.js';
import FullscreenApi from './fullscreen-api.js';
import MediaError from './media-error.js';
import globalOptions from './global-options.js';
import safeParseTuple from 'safe-json-parse/tuple';
import assign from 'object.assign';
import mergeOptions from './utils/merge-options.js';
Expand Down Expand Up @@ -91,7 +90,6 @@ class Player extends Component {
// Run base component initializing with new options
super(null, options, ready);


// if the global option object was accidentally blown away by
// someone, bail early with an informative error
if (!this.options_ ||
Expand All @@ -108,7 +106,7 @@ class Player extends Component {
this.tagAttributes = tag && Dom.getElAttributes(tag);

// Update current language
this.language(options.language || globalOptions.language);
this.language(this.options_.language);

// Update Supported Languages
if (options.languages) {
Expand All @@ -120,7 +118,7 @@ class Player extends Component {
});
this.languages_ = languagesToLower;
} else {
this.languages_ = globalOptions.languages;
this.languages_ = Player.prototype.options_.languages;
}

// Cache for video property values.
Expand Down Expand Up @@ -151,7 +149,7 @@ class Player extends Component {
// as well so they don't need to reach back into the player for options later.
// We also need to do another copy of this.options_ so we don't end up with
// an infinite loop.
let playerOptionsCopy = mergeOptions({}, this.options_);
let playerOptionsCopy = mergeOptions(this.options_);

// Load plugins
if (options.plugins) {
Expand Down Expand Up @@ -2403,7 +2401,7 @@ class Player extends Component {
* @method languages
*/
languages() {
return mergeOptions(globalOptions.languages, this.languages_);
return mergeOptions(Player.prototype.options_.languages, this.languages_);
}

/**
Expand Down Expand Up @@ -2488,17 +2486,54 @@ class Player extends Component {
*/
Player.players = {};

let navigator = window.navigator;
/*
* Player instance options, surfaced using options
* options = Player.prototype.options_
* Make changes in options, not here.
* All options should use string keys so they avoid
* renaming by closure compiler
*
* @type {Object}
* @private
*/
Player.prototype.options_ = globalOptions;
Player.prototype.options_ = {
// Default order of fallback technology
techOrder: ['html5','flash'],
// techOrder: ['flash','html5'],

html5: {},
flash: {},

// defaultVolume: 0.85,
defaultVolume: 0.00, // The freakin seaguls are driving me crazy!

// default inactivity timeout
inactivityTimeout: 2000,

// default playback rates
playbackRates: [],
// Add playback rate selection by adding rates
// 'playbackRates': [0.5, 1, 1.5, 2],

// Included control sets
children: {
mediaLoader: {},
posterImage: {},
textTrackDisplay: {},
loadingSpinner: {},
bigPlayButton: {},
controlBar: {},
errorDisplay: {},
textTrackSettings: {}
},

language: document.getElementsByTagName('html')[0].getAttribute('lang') || navigator.languages && navigator.languages[0] || navigator.userLanguage || navigator.language || 'en',

// locales and their language translations
languages: {},

// Default message to show when a video cannot be played.
notSupportedMessage: 'No compatible source was found for this video.'
};

/**
* Fired when the player has initial duration and dimension information
Expand Down
74 changes: 43 additions & 31 deletions src/js/utils/merge-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,50 @@ function isPlain(obj) {
}

/**
* Merge two options objects, recursively merging **only* * plain object
* properties. Previously `deepMerge`.
* Merge customizer. video.js simply overwrites non-simple objects
* (like arrays) instead of attempting to overlay them.
* @see https://lodash.com/docs#merge
*/
const customizer = function(destination, source) {
// If we're not working with a plain object, copy the value as is
// If source is an array, for instance, it will replace destination
if (!isPlain(source)) {
return source;
}

// If the new value is a plain object but the first object value is not
// we need to create a new object for the first object to merge with.
// This makes it consistent with how merge() works by default
// and also protects from later changes the to first object affecting
// the second object's values.
if (!isPlain(destination)) {
return mergeOptions(source);
}
};

/**
* Merge one or more options objects, recursively merging **only**
* plain object properties. Previously `deepMerge`.
*
* @param {Object} object The destination object
* @param {...Object} source One or more objects to merge into the first
* @returns {Object} The updated first object
* @param {...Object} source One or more objects to merge
* @returns {Object} a new object that is the union of all
* provided objects
* @function mergeOptions
*/
export default function mergeOptions(object={}) {

// Allow for infinite additional object args to merge
Array.prototype.slice.call(arguments, 1).forEach(function(source){

// Recursively merge only plain objects
// All other values will be directly copied
merge(object, source, function(a, b) {

// If we're not working with a plain object, copy the value as is
if (!isPlain(b)) {
return b;
}

// If the new value is a plain object but the first object value is not
// we need to create a new object for the first object to merge with.
// This makes it consistent with how merge() works by default
// and also protects from later changes the to first object affecting
// the second object's values.
if (!isPlain(a)) {
return mergeOptions({}, b);
}
});
});

return object;
export default function mergeOptions() {
// contruct the call dynamically to handle the variable number of
// objects to merge
let args = Array.prototype.slice.call(arguments);

// unshift an empty object into the front of the call as the target
// of the merge
args.unshift({});

// customize conflict resolution to match our historical merge behavior
args.push(customizer);

merge.apply(null, args);

// return the mutated result object
return args[0];
}
46 changes: 9 additions & 37 deletions src/js/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import document from 'global/document';
import * as setup from './setup';
import Component from './component';
import EventTarget from './event-target';
import globalOptions from './global-options.js';
import Player from './player';
import plugin from './plugins.js';
import mergeOptions from '../../src/js/utils/merge-options.js';
Expand Down Expand Up @@ -62,18 +61,18 @@ var videojs = function(id, options, ready){
}

// If a player instance has already been created for this ID return it.
if (Player.players[id]) {
if (videojs.getPlayers()[id]) {

// If options or ready funtion are passed, warn
if (options) {
log.warn(`Player "${id}" is already initialised. Options will not be applied.`);
}

if (ready) {
Player.players[id].ready(ready);
videojs.getPlayers()[id].ready(ready);
}

return Player.players[id];
return videojs.getPlayers()[id];

// Otherwise get element for ID
} else {
Expand Down Expand Up @@ -107,44 +106,17 @@ setup.autoSetupTimeout(1, videojs);
videojs.VERSION = '__VERSION__';

/**
* Get the global options object
* The global options object. These are the settings that take effect
* if no overrides are specified when the player is created.
*
* @return {Object} The global options object
* @mixes videojs
* @method getGlobalOptions
*/
videojs.getGlobalOptions = () => globalOptions;

/**
* For backward compatibility, expose global options.
*
* @deprecated
* @memberOf videojs
* @property {Object|Proxy} options
*/
videojs.options = createDeprecationProxy(globalOptions, {
get: 'Access to videojs.options is deprecated; use videojs.getGlobalOptions instead',
set: 'Modification of videojs.options is deprecated; use videojs.setGlobalOptions instead'
});

/**
* Set options that will apply to every player
* ```js
* videojs.setGlobalOptions({
* autoplay: true
* });
* videojs.options.autoplay = true
* // -> all players will autoplay by default
* ```
* NOTE: This will do a deep merge with the new options,
* not overwrite the entire global options object.
*
* @return {Object} The updated global options object
* @mixes videojs
* @method setGlobalOptions
* @type {Object}
*/
videojs.setGlobalOptions = function(newOptions) {
return mergeOptions(globalOptions, newOptions);
};
videojs.options = Player.prototype.options_;

/**
* Get an object with the currently created players, keyed by player ID
Expand Down Expand Up @@ -377,7 +349,7 @@ videojs.plugin = plugin;
*/
videojs.addLanguage = function(code, data){
code = ('' + code).toLowerCase();
return merge(globalOptions.languages, { [code]: data })[code];
return merge(videojs.options.languages, { [code]: data })[code];
};

/**
Expand Down
Loading

0 comments on commit fecf3a0

Please sign in to comment.