var crossbrand = crossbrand || {}; crossbrand.media = crossbrand.media || {}; /** The video player classes contained within the media module control the Crossbrand Video Player. @author [Matt Przybylski](mailto:mprzybylski@sapient.com) @module media @submodule video @main media **/ crossbrand.media.Loader = (function($, crossbrand, window, document, undefined) { 'use strict'; /* ----------------------------------------------------------------------------- *\ Events \* ----------------------------------------------------------------------------- */ /** All events associated with the Loader. @property Loader.Events @type {object} @default (see Events tab) @static **/ Loader.Events = { /** Fired when the loader successfully loads the player files. @event Loader.Events.ON_SUCCESS **/ ON_SUCCESS: 'success.crossbrand.media.loader', /** Fired when the loader fails to load the player files. @event Loader.Events.ON_ERROR **/ ON_ERROR: 'error.crossbrand.media.loader' }; /* ----------------------------------------------------------------------------- *\ Private Properties \* ----------------------------------------------------------------------------- */ /** The default settings of the loader. @property defaults @type {object} @default (see Constructor) @private **/ var defaults = { version: 'current', forceDebug: false }; /* ----------------------------------------------------------------------------- *\ Constructor \* ----------------------------------------------------------------------------- */ /** The Loader is aimed at helping load the appropriate player JavaScript files easily. You don't have to include a bunch of script tags on your page and you can load a specific version. **You can also load the "debug" version (original, unminified files) by passing a query string of *?dev* to your page URL**. @class Loader @constructor @namespace media @example var $vid = $('#crossbrand-video-player-1'), playerLoader = new crossbrand.media.Loader(), vidPlayer = null; $(window).on(crossbrand.media.Loader.Events.ON_SUCCESS, function(evt) { vidPlayer = new crossbrand.media.Video({ container: $vid, src: 'm_jAhIO2Gqo' }); }); $(window).on(crossbrand.media.Loader.Events.ON_ERROR, function(evt) { throw new Error('crossbrand.media.Loader: Could not load the player.'); }); @param {object} options The settings to initialize the loader with @param {string} [options.version='current'] The version of the player to load. You can pass in the numerical version **('1.0.0', '1.0.1', etc. or omit this to get the latest and greatest ('current'))** @param {boolean} [options.forceDebug=false] Whether to force load the debug version no matter what @return {Loader} An instance of the Loader object **/ function Loader(options) { this.settings = $.extend({}, defaults, options); this.jwPath = 'jwplayer/jwplayer.js'; this.playerPath = 'crossbrand/media/video/'; this.path = '/crossbrand/video-player/v1.1.3/js/'; this._init(); } /* ----------------------------------------------------------------------------- *\ Private Methods \* ----------------------------------------------------------------------------- */ /** Checks for the query string and loads the appropriate version of the player. @method _init @private @return {null} **/ Loader.prototype._init = function() { // if there is no hash just load the production version unless forceDebug is true if (window.location.search === '') { (this.settings.forceDebug) ? this._loadDebug() : this._loadProduction(); return; } // split the hash and check if it contains dev, if so load debug version var hashes = window.location.search.split('?')[1].split('&'), len = hashes.length, i = 0, parts = []; for (i; i < len; i++) { parts = hashes[i].split('='); if (parts[0] === 'dev') { this._loadDebug(); return; } } // only runs if there was a query string and it didn't contain dev (this.settings.forceDebug) ? this._loadDebug() : this._loadProduction(); }; /** Loads the debug version of the player (meaning all player files unminified) to aid in testing. @method _loadDebug @private @return {null} **/ Loader.prototype._loadDebug = function() { $.when( $.ajax(this.path + this.jwPath), $.ajax(this.path + this.playerPath + 'abstract-component.js'), $.ajax(this.path + this.playerPath + 'player.js'), $.ajax(this.path + this.playerPath + 'youtube-ios-player.js'), $.ajax(this.path + this.playerPath + 'endcard.js'), $.ajax(this.path + this.playerPath + 'playlist.js'), $.ajax(this.path + this.playerPath + 'video.js')) .then(this._handleLoaded, this._handleError); }; /** Loads the production version of the player (meaning concatenated and minified). @method _loadProduction @private @return {null} **/ Loader.prototype._loadProduction = function() { // commented because mutiple player in same page in not getting initalized if initalization is in on sucess event /*if (window.crossbrand && window.crossbrand.media && window.crossbrand.media.Video) { this._handleLoaded(); } else {*/ $.getScript(this.path + 'video.min.js') .done(this._handleLoaded) .fail(this._handleError); /*}*/ }; /* ----------------------------------------------------------------------------- *\ Event Handlers \* ----------------------------------------------------------------------------- */ /** Event handler for when the player files are loaded successfully. @method _handleLoaded @private @param {object} evt The event object dispatched by the associated event @return {null} **/ Loader.prototype._handleLoaded = function(evt) { $(window).trigger($.Event(Loader.Events.ON_SUCCESS)); }; /** Event handler for when loading of the player files fails. @method _handleError @private @param {object} evt The event object dispatched by the associated event @return {null} **/ Loader.prototype._handleError = function(evt) { $(window).trigger($.Event(Loader.Events.ON_ERROR)); }; return Loader; })(jQuery, crossbrand, window, document);