plyr/dist/js/simple-media.js
2015-02-15 11:03:06 +11:00

1 line
11 KiB
JavaScript

!function(e){"use strict";function t(){var e,t,n,r=navigator.userAgent,s=navigator.appName,a=""+parseFloat(navigator.appVersion),o=parseInt(navigator.appVersion,10);return-1!==navigator.appVersion.indexOf("Windows NT")&&-1!==navigator.appVersion.indexOf("rv:11")?(s="IE",a="11;"):-1!==(t=r.indexOf("MSIE"))?(s="IE",a=r.substring(t+5)):-1!==(t=r.indexOf("Chrome"))?(s="Chrome",a=r.substring(t+7)):-1!==(t=r.indexOf("Safari"))?(s="Safari",a=r.substring(t+7),-1!==(t=r.indexOf("Version"))&&(a=r.substring(t+8))):-1!==(t=r.indexOf("Firefox"))?(s="Firefox",a=r.substring(t+8)):(e=r.lastIndexOf(" ")+1)<(t=r.lastIndexOf("/"))&&(s=r.substring(e,t),a=r.substring(t+1),s.toLowerCase()==s.toUpperCase()&&(s=navigator.appName)),-1!==(n=a.indexOf(";"))&&(a=a.substring(0,n)),-1!==(n=a.indexOf(" "))&&(a=a.substring(0,n)),o=parseInt(""+a,10),isNaN(o)&&(a=""+parseFloat(navigator.appVersion),o=parseInt(navigator.appVersion,10)),[s,o]}function n(e){var t=[];return t=e.split(" --> "),s(t[0])}function r(e){var t=[];return t=e.split(" --> "),s(t[1])}function s(e){if(null===e||void 0===e)return 0;var t,n=[],r=[];return n=e.split(","),r=n[0].split(":"),t=Math.floor(60*r[0]*60)+Math.floor(60*r[1])+Math.floor(r[2])}function a(e){for(e.subcount=0;r(e.captions[e.subcount][0])<e.media.currentTime.toFixed(1);)if(e.subcount++,e.subcount>e.captions.length-1){e.subcount=e.captions.length-1;break}}function o(e){L.captions.default&&(e.container.className+=" "+L.classes.captions,e.buttons.captions.setAttribute("checked","checked"))}function i(e,t,n){return e.replace(new RegExp(t.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1"),"g"),n)}function c(e){var t=N.isFullScreen()?{x:0,y:0}:l(e.currentTarget);return{x:e.clientX-t.x,y:e.clientY-t.y}}function l(e){for(var t=0,n=0;e;)t+=e.offsetLeft-e.scrollLeft+e.clientLeft,n+=e.offsetTop-e.scrollTop+e.clientTop,e=e.offsetParent;return{x:t,y:n}}function u(e,t){for(var n in t)t[n]&&t[n].constructor&&t[n].constructor===Object?(e[n]=e[n]||{},u(e[n],t[n])):e[n]=t[n];return e}function d(e){var t,n=F[e]||[],r=n.length;for(t=0;r>t;t++)n[t].apply(E.media,[])}function p(){var e={supportsFullScreen:!1,isFullScreen:function(){return!1},requestFullScreen:function(){},cancelFullScreen:function(){},fullScreenEventName:"",element:null,prefix:""},t="webkit moz o ms khtml".split(" ");if("undefined"!=typeof document.cancelFullScreen)e.supportsFullScreen=!0;else for(var n=0,r=t.length;r>n;n++)if(e.prefix=t[n],"undefined"!=typeof document[e.prefix+"CancelFullScreen"]){e.supportsFullScreen=!0;break}return"webkit"===e.prefix&&navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)&&(e.supportsFullScreen=!1),e.supportsFullScreen&&(e.fullScreenEventName=e.prefix+"fullscreenchange",e.isFullScreen=function(){switch(this.prefix){case"":return document.fullScreen;case"webkit":return document.webkitIsFullScreen;default:return document[this.prefix+"FullScreen"]}},e.requestFullScreen=function(e){return""===this.prefix?e.requestFullScreen():e[this.prefix+"RequestFullScreen"]("webkit"===this.prefix?e.ALLOW_KEYBOARD_INPUT:null)},e.cancelFullScreen=function(){return""===this.prefix?document.cancelFullScreen():document[this.prefix+"CancelFullScreen"]()},e.element=function(){return""===this.prefix?document.fullscreenElement:document[this.prefix+"FullscreenElement"]}),e}function f(){L.debug&&console.log("Injecting custom controls");var e=L.html;e=i(e,"{aria-label}",L.playAriaLabel),e=i(e,"{id}",E.random),E.container.insertAdjacentHTML("beforeend",e)}function m(e){return E.container.querySelectorAll(e)}function b(e){return m(e)[0]}function g(){E.controls=b(L.selectors.controls),E.buttons={},E.buttons.play=b(L.selectors.buttons.play),E.buttons.pause=b(L.selectors.buttons.pause),E.buttons.restart=b(L.selectors.buttons.restart),E.buttons.rewind=b(L.selectors.buttons.rewind),E.buttons.forward=b(L.selectors.buttons.forward),E.buttons.mute=b(L.selectors.buttons.mute),E.buttons.volume=b(L.selectors.buttons.volume),E.buttons.captions=b(L.selectors.buttons.captions),E.buttons.fullscreen=b(L.selectors.buttons.fullscreen),E.progress={},E.progress.bar=b(L.selectors.progress),E.progress.text=E.progress.bar.getElementsByTagName("span")[0],E.duration=b(L.selectors.duration),E.seekTime=m(L.selectors.seekTime)}function v(){E.media.play(),E.container.className=E.container.className.replace(L.classes.stopped,L.classes.playing)}function x(){E.media.pause(),E.container.className=E.container.className.replace(L.classes.playing,L.classes.stopped)}function T(){E.media.currentTime=0,E.isTextTracks||(E.subcount=0),v()}function h(){return E.media=E.container.querySelectorAll("audio, video")[0],E.media?(null===E.media.getAttribute("autoplay")&&(E.container.className+=" "+L.classes.stopped),E.media.removeAttribute("controls"),E.type="video"==E.media.tagName.toLowerCase()?"video":"audio",void 0):(console.warn("No audio or video element found!"),!1)}function k(){if("video"==E.type){E.videoContainer.insertAdjacentHTML("afterbegin","<div class='"+L.selectors.captions.replace(".","")+"'></div>"),E.captionsContainer=b(L.selectors.captions),E.isTextTracks=!1,E.media.textTracks&&(E.isTextTracks=!0);for(var e,t="",s=E.media.childNodes,a=0;a<s.length;a++)"track"===s[a].nodeName.toLowerCase()&&(e=s[a].getAttribute("kind"),"captions"===e&&(t=s[a].getAttribute("src")));if(E.captionExists=!0,""===t?(E.captionExists=!1,L.debug&&console.log("No caption track found.")):L.debug&&console.log("Caption track found; URI: "+t),E.captionExists){var i,c,l={};if("IE"===E.browserName&&10===E.browserMajorVersion||"IE"===E.browserName&&11===E.browserMajorVersion||"Firefox"===E.browserName&&E.browserMajorVersion>=31||"Safari"===E.browserName&&E.browserMajorVersion>=7)for(L.debug&&console.log("Detected IE 10/11 or Firefox 31+ or Safari 7+"),E.isTextTracks=!1,l={},i=E.media.textTracks,c=0;c<i.length;c++)l=E.media.textTracks[c],l.mode="hidden";if(E.isTextTracks)for(L.debug&&console.log("textTracks supported"),o(E),l={},i=E.media.textTracks,c=0;c<i.length;c++)l=E.media.textTracks[c],l.mode="hidden","captions"===l.kind&&l.addEventListener("cuechange",function(){this.activeCues[0]&&this.activeCues[0].hasOwnProperty("text")&&(E.captionsContainer.innerHTML=this.activeCues[0].text)},!1);else if(L.debug&&console.log("textTracks not supported so rendering captions manually"),o(E),E.currentCaption="",E.subcount=0,E.captions=[],E.media.addEventListener("timeupdate",function(){E.media.currentTime.toFixed(1)>n(E.captions[E.subcount][0])&&E.media.currentTime.toFixed(1)<r(E.captions[E.subcount][0])&&(E.currentCaption=E.captions[E.subcount][1]),E.media.currentTime.toFixed(1)>r(E.captions[E.subcount][0])&&E.subcount<E.captions.length-1&&E.subcount++,E.captionsContainer.innerHTML=E.currentCaption},!1),""!==t){var u;window.XMLHttpRequest?u=new XMLHttpRequest:window.ActiveXObject&&(u=new ActiveXObject("Microsoft.XMLHTTP")),u.onreadystatechange=function(){if(4===u.readyState)if(200===u.status){L.debug&&console.log("xhr = 200"),E.captions=[];var e,t=[],n=u.responseText;t=n.split("\n\n");for(var r=0;r<t.length;r++)e=t[r],E.captions[r]=[],E.captions[r]=e.split("\n");E.captions.shift(),L.debug&&console.log("Successfully loaded the caption file via ajax.")}else L.debug&&console.log("There was a problem loading the caption file via ajax.")},u.open("get",t,!0),u.send()}"Safari"===E.browserName&&7===E.browserMajorVersion&&(console.log("Safari 7 detected; removing track from DOM"),i=E.media.getElementsByTagName("track"),E.media.removeChild(i[0]))}else E.container.className=E.container.className.replace(L.classes.captions,"")}}function w(){E.seekTime[0].innerHTML=L.seekInterval,E.seekTime[1].innerHTML=L.seekInterval}function y(){E.buttons.fullscreen.addEventListener("click",function(){N.isFullScreen()?N.cancelFullScreen():N.requestFullScreen(E.container)},!1),E.videoContainer.addEventListener("click",function(){E.media.paused?v():E.media.ended?T():x()},!1),E.buttons.play.addEventListener("click",function(){v(),E.buttons.pause.focus()},!1),E.buttons.pause.addEventListener("click",function(){x(),E.buttons.play.focus()},!1),E.buttons.restart.addEventListener("click",T,!1),E.buttons.rewind.addEventListener("click",function(){var e=E.media.currentTime-L.seekInterval;E.media.currentTime=0>e?0:e,E.isTextTracks||a(E)},!1),E.buttons.forward.addEventListener("click",function(){var e=E.media.currentTime+L.seekInterval;E.media.currentTime=e>E.media.duration?E.media.duration:e,E.isTextTracks||a(E)},!1),E.buttons.volume.addEventListener("change",function(){E.media.volume=parseFloat(this.value/10)},!1),E.buttons.mute.addEventListener("click",function(){E.media.muted===!0?(E.media.muted=!1,E.container.className=E.container.className.replace(L.classes.muted,"")):(E.media.muted=!0,E.container.className+=" "+L.classes.muted)},!1),E.media.addEventListener("timeupdate",function(){E.secs=parseInt(E.media.currentTime%60),E.mins=parseInt(E.media.currentTime/60%60),E.secs=("0"+E.secs).slice(-2),E.mins=("0"+E.mins).slice(-2),E.duration.innerHTML=E.mins+":"+E.secs},!1),E.media.addEventListener("timeupdate",function(){E.percent=100/E.media.duration*E.media.currentTime,E.percent>0&&(E.progress.bar.value=E.percent,E.progress.text.innerHTML=E.percent)},!1),E.progress.bar.addEventListener("click",function(e){E.pos=c(e).x/this.offsetWidth,E.media.currentTime=E.pos*E.media.duration,E.isTextTracks||a(E)}),E.buttons.captions.addEventListener("click",function(){this.checked?E.container.className+=" "+L.classes.captions:E.container.className=E.container.className.replace(L.classes.captions,"")},!1),E.media.addEventListener("ended",function(){E.captionsContainer.innerHTML="",E.container.className=E.container.className.replace(L.classes.playing,L.classes.stopped)})}function S(e){E.container=e,E.videoContainer=b(L.selectors.videoContainer),h(),E.random=Math.floor(1e4*Math.random()),f(),g(),k(),w(),y()}var N,L,F={},E={},M={debug:!1,seekInterval:10,selectors:{container:".player",videoContainer:".player-video",controls:".player-controls",buttons:{play:"[data-player='play']",pause:"[data-player='pause']",restart:"[data-player='restart']",rewind:"[data-player='restart']",forward:"[data-player='fast-forward']",mute:"[data-player='mute']",volume:"[data-player='volume']",captions:"[data-player='captions']",fullscreen:"[data-player='fullscreen']"},progress:".player-progress",captions:".player-captions",duration:".player-duration",seekTime:".player-seek-time"},classes:{stopped:"stopped",playing:"playing",muted:"muted",captions:"captions"},captions:{"default":!0}};e.on=function(e,t){F[e]||(F[e]=[]),F[e].push(t)},e.setup=function(e){if(L=u(M,e),N=p(),E.browserInfo=t(),E.browserName=E.browserInfo[0],E.browserMajorVersion=E.browserInfo[1],L.debug&&(console.log(L),console.log("fullscreen support: "+N.supportsFullScreen),console.log(E.browserName+" "+E.browserMajorVersion)),"IE"===E.browserName&&(8===E.browserMajorVersion||9===E.browserMajorVersion))return console.warn("Browser not suppported."),!1;if(E.isSmartphoneOrTablet=/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent),E.isSmartphoneOrTablet)return console.warn("Browser not suppported."),!1;L.playAriaLabel="undefined"!=typeof L.title&&L.title.length?"Play "+L.title:"Play";for(var n=document.querySelectorAll(L.selectors.container),r=n.length-1;r>=0;r--)S(n[r]);d("setup")}}(this.simpleMedia=this.simpleMedia||{});