UNPKG

@brightcove/player-loader

Version:

An asynchronous script loader for the Brightcove Player.

3 lines (2 loc) 8.69 kB
/*! @name @brightcove/player-loader @version 1.8.1 @license Apache-2.0 */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).brightcovePlayerLoader=t()}(this,function(){"use strict";function e(){return(e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var t=function(){if("undefined"!=typeof window)return window;var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};return e.window&&e.window.window===e.window?e.window:e},n=function(){if("undefined"!=typeof document)return document;var e=t();if(e&&e.document)return e.document;throw new Error("document is not available in this environment")},r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o=["catalogSearch","catalogSequence"],i=["adConfigId","applicationId","catalogSearch","catalogSequence","playlistId","playlistVideoId","videoId"],a=function(e){return Object.keys(e).filter(function(e){return-1!==i.indexOf(e)}).reduce(function(t,n){var r=function(e,t){if(e&&void 0!==e[t]){if("string"!=typeof e[t]&&-1!==o.indexOf(t))try{return encodeURIComponent(JSON.stringify(e[t]))}catch(e){return}return encodeURIComponent(String(e[t]).trim())||void 0}}(e,n);return void 0!==r&&(t+=t?"&":"?",t+=encodeURIComponent(n)+"="+r),t},"")},d=function(e){var t=e.accountId,n=e.base,o=void 0===n?"https://players.brightcove.net":n,i=e.playerId,d=void 0===i?"default":i,c=e.embedId,l=void 0===c?"default":c,u=e.iframe,f=void 0!==u&&u,s=e.minified,p=void 0===s||s,y=e.queryParams,m=void 0===y?null:y,v="";f?v+="html":(p&&(v+="min."),v+="js"),"/"===o.charAt(o.length-1)&&(o=o.substring(0,o.length-1));var b="";return f&&m&&"object"===(void 0===m?"undefined":r(m))&&(b=a(m)),o+"/"+(t=encodeURIComponent(t))+"/"+(d=encodeURIComponent(d))+"_"+(l=encodeURIComponent(l))+"/index."+v+b};d.VERSION="1.2.0";var c={embedId:"default",embedType:"in-page",playerId:"default",Promise:t().Promise,refNodeInsert:"append"},l="video",u="video-js",f="in-page",s="iframe",p="append",y="prepend",m="before",v="after",b="replace",E=["catalogSearch","catalogSequence"],h="https://players.brightcove.net/",g=function(e){if(e.playerUrl)return e.playerUrl;var t=e.accountId,n=e.playerId,r=e.embedId,o=e.embedOptions,i=e.embedType===s;return d({accountId:t,playerId:n,embedId:r,iframe:i,base:h,minified:!o||!o.unminified,queryParams:e})},I=function(){return h},w=function(e){h=e},N=function(e){return Boolean(e&&1===e.nodeType)},O=function(e){return Boolean(N(e)&&e.parentNode)},_=function(t,r,o){return r?function(e,t){if(!e.pip)return t;var r=n().createElement("div");return r.classList.add("vjs-pip-container"),r.appendChild(t),r}(r,function(t,r,o){if(!r.responsive)return o;var i=n();o.style.position="absolute",o.style.top="0px",o.style.right="0px",o.style.bottom="0px",o.style.left="0px",o.style.width="100%",o.style.height="100%";var a=e({aspectRatio:"16:9",iframeHorizontalPlaylist:!1,maxWidth:"100%"},r.responsive),d=a.aspectRatio.split(":").map(Number),c=i.createElement("div"),l=d[1]/d[0]*100;t===s&&a.iframeHorizontalPlaylist&&(l*=1.25),c.style.paddingTop=l+"%",c.appendChild(o);var u=i.createElement("div");return u.style.position="relative",u.style.display="block",u.style.maxWidth=a.maxWidth,u.appendChild(c),u}(t,r,o)):o},R=function(e){return function(e,t){var r=e.refNode,o=e.refNodeInsert,i=r.parentNode,a=_(e.embedType,e.embedOptions,t);if(o===m?i.insertBefore(a,r):o===v?i.insertBefore(a,r.nextElementSibling||null):o===b?i.replaceChild(a,r):o===y?r.insertBefore(a,r.firstChild||null):r.appendChild(a),e.embedOptions&&e.embedOptions.playlist){var d=n(),c=e.embedOptions.playlist.legacy?"ul":"div",l=d.createElement(c);l.classList.add("vjs-playlist"),t.parentNode.insertBefore(l,t.nextElementSibling||null)}return e.refNode=null,t}(e,function(e,t){if("function"!=typeof e.onEmbedCreated)return t;var n=e.onEmbedCreated(t);return N(n)?n:t}(e,e.embedType===s?function(e){var t=n().createElement("iframe");return t.setAttribute("allow","autoplay;encrypted-media;fullscreen"),t.setAttribute("allowfullscreen","allowfullscreen"),t.src=g(e),t}(e):function(e){var t=e.embedOptions,r=n(),o={adConfigId:"data-ad-config-id",applicationId:"data-application-id",catalogSearch:"data-catalog-search",catalogSequence:"data-catalog-sequence",deliveryConfigId:"data-delivery-config-id",playlistId:"data-playlist-id",playlistVideoId:"data-playlist-video-id",poster:"poster",videoId:"data-video-id"},i=t&&t.tagName||u,a=r.createElement(i);return Object.keys(o).filter(function(t){return e[t]}).forEach(function(t){var n;if("string"!=typeof e[t]&&-1!==E.indexOf(t))try{n=JSON.stringify(e[t])}catch(e){return}else n=String(e[t]).trim();a.setAttribute(o[t],n)}),a.setAttribute("controls","controls"),a.classList.add("video-js"),a}(e)))},S=new(t().Map),C=function(e){return(e.accountId||"*")+"_"+e.playerId+"_"+e.embedId},P=function(){S.clear()},A=function(e){S.forEach(e)},T=function(e){return S.has(C(e))},j=function(e){S.set(C(e),e.accountId?g(e):"")},x=t(),D=/^([A-Za-z0-9]+)_([A-Za-z0-9]+)$/,B=function(){return x.bc?Object.keys(x.bc).filter(function(e){return D.test(e)}):[]},U=function(e){e&&Object.keys(e.players).forEach(function(t){var n=e.players[t];n&&n.dispose()})},q=function(){B().forEach(function(e){var t=e.match(D),n={playerId:t[1],embedId:t[2]};T(n)||j(n)})},F=function(){A(function(e,t){if(e){var r=n();Array.prototype.slice.call(r.querySelectorAll('script[src="'+e+'"]')).forEach(function(e){return e.parentNode.removeChild(e)})}}),P(),U(x.videojs),B().forEach(function(e){return U(x.bc[e].videojs)}),Object.keys(x).filter(function(e){return/^videojs/i.test(e)||/^(bc)$/.test(e)}).forEach(function(e){delete x[e]})},M=t();q();var L=function(e){return"function"==typeof e},k=function(e){var t,n=e.accountId,r=e.embedOptions,o=e.embedType,i=e.options,a=e.refNode,d=e.refNodeInsert;if(!n)throw new Error("accountId is required");if(!O(a))throw new Error("refNode must resolve to a node attached to the DOM");if(!function(e){return e===f||e===s}(o))throw new Error("embedType is missing or invalid");if(o===s&&i)throw new Error("cannot use options with an iframe embed");if(r&&void 0!==r.tagName&&((t=r.tagName)!==u&&t!==l))throw new Error('embedOptions.tagName is invalid (value: "'+r.tagName+'")');if(r&&r.responsive&&r.responsive.aspectRatio&&!/^\d+\:\d+$/.test(r.responsive.aspectRatio))throw new Error('embedOptions.responsive.aspectRatio must be in the "n:n" format (value: "'+r.responsive.aspectRatio+'")');if(!function(e){return e===p||e===y||e===m||e===v||e===b}(d))throw new Error("refNodeInsert is missing or invalid")},V=function(e,t,n,r){var o,i=e.embedId,a=e.playerId,d=M.bc[a+"_"+i]||M.bc;if(!d)return r(new Error("missing bc function for "+a));j(e);try{(o=d(t,e.options)).bcinfo&&(o.bcinfo.PLAYER_LOADER=!0)}catch(t){var c="Could not initialize the Brightcove Player.";return e.embedOptions.tagName===u&&(c+=' You are attempting to embed using a "video-js" element. Please ensure that your Player is v6.11.0 or newer in order to support this embed type. Alternatively, pass `"video"` for `embedOptions.tagName`.'),r(new Error(c))}n({type:f,ref:o})},z=function(e,t,r){e.refNode=function(e){return O(e)?e:"string"==typeof e?n().querySelector(e):null}(e.refNode),k(e);var o=e.refNode,i=e.refNodeInsert,a=o.parentNode,d=R(e);if(e.embedType!==s){if(T(e))return V(e,d,t,r);var c=n().createElement("script");c.onload=function(){return V(e,d,t,r)},c.onerror=function(){r(new Error("player script could not be downloaded"))},c.async=!0,c.charset="utf-8",c.src=g(e),i===b?a.appendChild(c):o.appendChild(c)}else t({type:s,ref:d})},Y=function(t){var n=e({},c,t),r=n.Promise,o=n.onSuccess,i=n.onFailure;return!L(r)||L(o)||L(i)?z(n,L(o)?o:function(){},L(i)?i:function(e){throw e}):new r(function(e,t){return z(n,e,t)})},G=function(e,t){Object.defineProperty(Y,e,{configurable:!1,enumerable:!0,value:t,writable:!1})};return G("getBaseUrl",function(){return I()}),G("setBaseUrl",function(e){w(e)}),G("getUrl",function(e){return g(e)}),G("reset",function(){return F()}),[["EMBED_TAG_NAME_VIDEO",l],["EMBED_TAG_NAME_VIDEOJS",u],["EMBED_TYPE_IN_PAGE",f],["EMBED_TYPE_IFRAME",s],["REF_NODE_INSERT_APPEND",p],["REF_NODE_INSERT_PREPEND",y],["REF_NODE_INSERT_BEFORE",m],["REF_NODE_INSERT_AFTER",v],["REF_NODE_INSERT_REPLACE",b],["VERSION","1.8.1"]].forEach(function(e){G(e[0],e[1])}),Y});