From 7ccbfad6adf721799869a9b826778199116f63ed Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 17 May 2015 17:23:09 +1000 Subject: [PATCH] New API methods (fixes #77), Fix for non strict mode (fixes #78) --- dist/plyr.js | 2 +- docs/error.html | 2 +- docs/index.html | 10 +++---- package.json | 2 +- readme.md | 18 +++++++++--- src/js/plyr.js | 78 ++++++++++++++++++++++++++++++++++++++++++++----- 6 files changed, 92 insertions(+), 20 deletions(-) diff --git a/dist/plyr.js b/dist/plyr.js index 6d709798..8236e978 100644 --- a/dist/plyr.js +++ b/dist/plyr.js @@ -1 +1 @@ -!function(e){"use strict";function t(){var e=["
","
","","","","0% played","","","0% buffered","","
",""];return a(T.controls,"restart")&&e.push(""),a(T.controls,"rewind")&&e.push(""),a(T.controls,"play")&&e.push("",""),a(T.controls,"fast-forward")&&e.push(""),a(T.controls,"current-time")&&e.push("","Current time","00:00",""),a(T.controls,"duration")&&e.push("","Duration","00:00",""),e.push("",""),a(T.controls,"mute")&&e.push("",""),a(T.controls,"volume")&&e.push("",""),a(T.controls,"captions")&&e.push("",""),a(T.controls,"fullscreen")&&e.push(""),e.push("","
"),e.join("")}function n(e,t){T.debug&&window.console&&console[t?"error":"log"](e)}function r(){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)),{name:s,version:o,ios:/(iPad|iPhone|iPod)/g.test(navigator.platform)}}function s(e,t){var n=e.media;if("video"==e.type)switch(t){case"video/webm":return!(!n.canPlayType||!n.canPlayType('video/webm; codecs="vp8, vorbis"').replace(/no/,""));case"video/mp4":return!(!n.canPlayType||!n.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/,""));case"video/ogg":return!(!n.canPlayType||!n.canPlayType('video/ogg; codecs="theora"').replace(/no/,""))}else if("audio"==e.type)switch(t){case"audio/mpeg":return!(!n.canPlayType||!n.canPlayType("audio/mpeg;").replace(/no/,""));case"audio/ogg":return!(!n.canPlayType||!n.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/,""));case"audio/wav":return!(!n.canPlayType||!n.canPlayType('audio/wav; codecs="1"').replace(/no/,""))}return!1}function a(e,t){return Array.prototype.indexOf&&-1!=e.indexOf(t)}function o(e,t,n){return e.replace(new RegExp(t.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1"),"g"),n)}function i(e,t){e.length||(e=[e]);for(var n=e.length-1;n>=0;n--){var r=n>0?t.cloneNode(!0):t,s=e[n],a=s.parentNode,o=s.nextSibling;r.appendChild(s),o?a.insertBefore(r,o):a.appendChild(r)}}function l(e){e.parentNode.removeChild(e)}function c(e,t){e.insertBefore(t,e.firstChild)}function u(e,t){for(var n in t)e.setAttribute(n,t[n])}function p(e,t,n){if(e)if(e.classList)e.classList[n?"add":"remove"](t);else{var r=(" "+e.className+" ").replace(/\s+/g," ").replace(" "+t+" ","");e.className=r+(n?" "+t:"")}}function d(e,t,n,r){if(t=t.split(" "),e instanceof NodeList)for(var s=0;sn;n++){if(e.prefix=t[n],"undefined"!=typeof document[e.prefix+"CancelFullScreen"]){e.supportsFullScreen=!0;break}if("undefined"!=typeof document.msExitFullscreen&&document.msFullscreenEnabled){e.prefix="ms",e.supportsFullScreen=!0;break}}return"webkit"===e.prefix&&navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)&&(e.supportsFullScreen=!1),e.supportsFullScreen&&(e.fullScreenEventName="ms"==e.prefix?"MSFullscreenChange":e.prefix+"fullscreenchange",e.isFullScreen=function(e){switch("undefined"==typeof e&&(e=document),this.prefix){case"":return document.fullscreenElement==e;case"moz":return document.mozFullScreenElement==e;default:return document[this.prefix+"FullscreenElement"]==e}},e.requestFullScreen=function(e){return""===this.prefix?e.requestFullScreen():e[this.prefix+("ms"==this.prefix?"RequestFullscreen":"RequestFullScreen")]("webkit"===this.prefix?e.ALLOW_KEYBOARD_INPUT:null)},e.cancelFullScreen=function(){return""===this.prefix?document.cancelFullScreen():document[this.prefix+("ms"==this.prefix?"ExitFullscreen":"CancelFullScreen")]()},e.element=function(){return""===this.prefix?document.fullscreenElement:document[this.prefix+"FullscreenElement"]}),e}function k(){var e={supported:function(){try{return"localStorage"in window&&null!==window.localStorage}catch(e){return!1}}()};return e}function x(a){function d(e){if(!st.usingTextTracks&&"video"===st.type&&st.supported.full){for(st.subcount=0,e="number"==typeof e?e:st.media.currentTime;F(st.captions[st.subcount][0])st.captions.length-1){st.subcount=st.captions.length-1;break}st.media.currentTime.toFixed(1)>=x(st.captions[st.subcount][0])&&st.media.currentTime.toFixed(1)<=F(st.captions[st.subcount][0])?(st.currentCaption=st.captions[st.subcount][1],st.captionsContainer.innerHTML=st.currentCaption):st.captionsContainer.innerHTML=""}}function b(){st.buttons.captions&&(p(st.container,T.classes.captions.enabled,!0),T.captions.defaultActive&&(p(st.container,T.classes.captions.active,!0),st.buttons.captions.checked=!0))}function x(e){var t=[];return t=e.split(" --> "),S(t[0])}function F(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 C(e){return st.container.querySelectorAll(e)}function E(e){return C(e)[0]}function A(){try{return window.self!==window.top}catch(e){return!0}}function N(){var e=T.html;if(n("Injecting custom controls."),e||(e=t()),e=o(e,"{seektime}",T.seekTime),e=o(e,"{id}",Math.floor(1e4*Math.random())),st.container.insertAdjacentHTML("beforeend",e),T.tooltips)for(var r=C(T.selectors.labels),s=r.length-1;s>=0;s--){var a=r[s];p(a,T.classes.hidden,!1),p(a,T.classes.tooltip,!0)}}function P(){try{return st.controls=E(T.selectors.controls),st.buttons={},st.buttons.seek=E(T.selectors.buttons.seek),st.buttons.play=E(T.selectors.buttons.play),st.buttons.pause=E(T.selectors.buttons.pause),st.buttons.restart=E(T.selectors.buttons.restart),st.buttons.rewind=E(T.selectors.buttons.rewind),st.buttons.forward=E(T.selectors.buttons.forward),st.buttons.fullscreen=E(T.selectors.buttons.fullscreen),st.buttons.mute=E(T.selectors.buttons.mute),st.buttons.captions=E(T.selectors.buttons.captions),st.checkboxes=C("[type='checkbox']"),st.progress={},st.progress.container=E(T.selectors.progress.container),st.progress.buffer={},st.progress.buffer.bar=E(T.selectors.progress.buffer),st.progress.buffer.text=st.progress.buffer.bar&&st.progress.buffer.bar.getElementsByTagName("span")[0],st.progress.played={},st.progress.played.bar=E(T.selectors.progress.played),st.progress.played.text=st.progress.played.bar&&st.progress.played.bar.getElementsByTagName("span")[0],st.volume=E(T.selectors.buttons.volume),st.duration=E(T.selectors.duration),st.currentTime=E(T.selectors.currentTime),st.seekTime=C(T.selectors.seekTime),!0}catch(e){return n("It looks like there's a problem with your controls html. Bailing.",!0),st.media.setAttribute("controls",""),!1}}function I(){if(st.buttons.play){var e=st.buttons.play.innerText||"Play";"undefined"!=typeof T.title&&T.title.length&&(e+=", "+T.title),st.buttons.play.setAttribute("aria-label",e)}}function M(){if(!st.media)return n("No audio or video element found!",!0),!1;if(st.supported.full&&(st.media.removeAttribute("controls"),p(st.container,T.classes[st.type],!0),p(st.container,T.classes.stopped,null===st.media.getAttribute("autoplay")),st.browser.ios&&p(st.container,"ios",!0),"video"===st.type)){var e=document.createElement("div");e.setAttribute("class",T.classes.videoWrapper),i(st.media,e),st.videoContainer=e}null!==st.media.getAttribute("autoplay")&&q()}function L(){if("video"===st.type){st.videoContainer.insertAdjacentHTML("afterbegin","
"),st.captionsContainer=E(T.selectors.captions),st.usingTextTracks=!1,st.media.textTracks&&(st.usingTextTracks=!0);for(var e,t="",r=st.media.childNodes,s=0;s=31||"Safari"===st.browser.name&&st.browser.version>=7)&&(n("Detected IE 10/11 or Firefox 31+ or Safari 7+."),st.usingTextTracks=!1),st.usingTextTracks){n("TextTracks supported.");for(var i=0;i=7){n("Safari 7+ detected; removing track from DOM."),a=st.media.getElementsByTagName("track");for(var u=0;ut?t=0:t>st.media.duration&&(t=st.media.duration);try{st.media.currentTime=t.toFixed(1)}catch(r){}n("Seeking to "+st.media.currentTime+" seconds"),d(t)}function B(){p(st.container,T.classes.playing,!st.media.paused),p(st.container,T.classes.stopped,st.media.paused)}function D(e){var t=w.supportsFullScreen;e&&e.type===w.fullScreenEventName?st.isFullscreen=w.isFullScreen(st.container):t?(w.isFullScreen(st.container)?w.cancelFullScreen():w.requestFullScreen(st.container),st.isFullscreen=w.isFullScreen(st.container)):(st.isFullscreen=!st.isFullscreen,st.isFullscreen?(f(document,"keyup",W),document.body.style.overflow="hidden"):(m(document,"keyup",W),document.body.style.overflow="")),p(st.container,T.classes.fullscreen.active,st.isFullscreen),st.isFullscreen&&p(st.controls,T.classes.hover,!1)}function W(e){27===(e.which||e.charCode||e.keyCode)&&st.isFullscreen&&D()}function z(e){st.volume&&("undefined"==typeof e&&(e=T.storage.enabled&&k().supported?window.localStorage[T.storage.key]||T.volume:T.volume),e>10&&(e=10),st.supported.full&&(st.volume.value=e),st.media.volume=parseFloat(e/10),_(),T.storage.enabled&&k().supported&&window.localStorage.setItem(T.storage.key,e))}function U(e){"undefined"==typeof e&&(e=!st.media.muted),st.supported.full&&(st.buttons.mute.checked=e),st.media.muted=e,_()}function X(e){st.supported.full&&st.buttons.captions&&("undefined"==typeof e&&(e=-1===st.container.className.indexOf(T.classes.captions.active),st.buttons.captions.checked=e),p(st.container,T.classes.captions.active,e))}function _(){p(st.container,T.classes.muted,0===st.media.volume||st.media.muted)}function J(e){var t="waiting"===e.type;clearTimeout(st.loadingTimer),st.loadingTimer=setTimeout(function(){p(st.container,T.classes.loading,t)},t?250:0)}function $(e){var t=st.progress.played.bar,n=st.progress.played.text,r=0;if(e)switch(e.type){case"timeupdate":case"seeking":r=g(st.media.currentTime,st.media.duration),"timeupdate"==e.type&&st.buttons.seek&&(st.buttons.seek.value=r);break;case"change":case"input":r=e.target.value;break;case"playing":case"progress":t=st.progress.buffer.bar,n=st.progress.buffer.text,r=function(){var e=st.media.buffered;return e.length?g(e.end(0),st.media.duration):0}()}t&&(t.value=r),n&&(n.innerHTML=r)}function K(e,t){if(t){st.secs=parseInt(e%60),st.mins=parseInt(e/60%60),st.hours=parseInt(e/60/60%60);var n=parseInt(st.media.duration/60/60%60)>0;st.secs=("0"+st.secs).slice(-2),st.mins=("0"+st.mins).slice(-2),t.innerHTML=(n?st.hours+":":"")+st.mins+":"+st.secs}}function Y(){var e=st.media.duration||0;!st.duration&&T.displayDuration&&st.media.paused&&K(e,st.currentTime),st.duration&&K(e,st.duration)}function G(e){K(st.media.currentTime,st.currentTime),$(e)}function Q(){for(var e=st.media.querySelectorAll("source"),t=e.length-1;t>=0;t--)l(e[t]);st.media.removeAttribute("src")}function Z(e){if(e.src){var t=document.createElement("source");u(t,e),c(st.media,t)}}function et(e){if(H(),j(),Q(),"string"==typeof e)st.media.setAttribute("src",e);else if(e.constructor===Array)for(var t in e)Z(e[t]);st.supported.full&&(G(),B()),st.media.load(),null!==st.media.getAttribute("autoplay")&&q()}function tt(e){"video"===st.type&&st.media.setAttribute("poster",e)}function nt(){var e="IE"==st.browser.name?"change":"input";f(st.buttons.play,"click",function(){q(),setTimeout(function(){st.buttons.pause.focus()},100)}),f(st.buttons.pause,"click",function(){H(),setTimeout(function(){st.buttons.play.focus()},100)}),f(st.buttons.restart,"click",j),f(st.buttons.rewind,"click",V),f(st.buttons.forward,"click",R),f(st.buttons.seek,e,j),f(st.volume,e,function(){z(this.value)}),f(st.buttons.mute,"change",function(){U(this.checked)}),f(st.buttons.fullscreen,"click",D),w.supportsFullScreen&&f(document,w.fullScreenEventName,D),f(st.media,"timeupdate seeking",G),f(st.media,"timeupdate",d),f(st.media,"loadedmetadata",Y),f(st.buttons.captions,"change",function(){X(this.checked)}),f(st.media,"ended",function(){"video"===st.type&&(st.captionsContainer.innerHTML=""),B()}),f(st.media,"progress",$),f(st.media,"playing",$),f(st.media,"volumechange",_),f(st.media,"play pause",B),f(st.media,"waiting canplay seeked",J),f(st.checkboxes,"keyup",v),"video"===st.type&&T.click&&f(st.videoContainer,"click",function(){st.media.paused?y(st.buttons.play,"click"):st.media.ended?(j(),y(st.buttons.play,"click")):y(st.buttons.pause,"click")}),T.fullscreen.hideControls&&f(st.controls,"mouseenter mouseleave",function(e){p(st.controls,T.classes.hover,"mouseenter"===e.type)})}function rt(){if(w=h(),st.browser=r(),st.media=st.container.querySelectorAll("audio, video")[0],st.type=st.media.tagName.toLowerCase(),st.supported=e.supported(st.type),!st.supported.basic)return!1;if(n(st.browser.name+" "+st.browser.version),M(),st.supported.full){if(N(),!P())return!1;T.displayDuration&&Y(),I(),L(),z(),O(),nt()}return!0}var st=this;return st.container=a,rt()?{media:st.media,play:q,pause:H,restart:j,rewind:V,forward:R,seek:j,source:et,poster:tt,setVolume:z,toggleMute:U,toggleCaptions:X,toggleFullscreen:D,isFullscreen:function(){return st.isFullscreen||!1},support:function(e){return s(st,e)}}:{}}var w,T,F={enabled:!0,debug:!1,seekTime:10,volume:5,click:!0,tooltips:!1,displayDuration:!0,selectors:{container:".player",controls:".player-controls",labels:"[data-player] .sr-only, label .sr-only",buttons:{seek:"[data-player='seek']",play:"[data-player='play']",pause:"[data-player='pause']",restart:"[data-player='restart']",rewind:"[data-player='rewind']",forward:"[data-player='fast-forward']",mute:"[data-player='mute']",volume:"[data-player='volume']",captions:"[data-player='captions']",fullscreen:"[data-player='fullscreen']"},progress:{container:".player-progress",buffer:".player-progress-buffer",played:".player-progress-played"},captions:".player-captions",currentTime:".player-current-time",duration:".player-duration"},classes:{video:"player-video",videoWrapper:"player-video-wrapper",audio:"player-audio",stopped:"stopped",playing:"playing",muted:"muted",loading:"loading",tooltip:"player-tooltip",hidden:"sr-only",hover:"hover",captions:{enabled:"captions-enabled",active:"captions-active"},fullscreen:{enabled:"fullscreen-enabled",active:"fullscreen-active",hideControls:"fullscreen-hide-controls"}},captions:{defaultActive:!1},fullscreen:{enabled:!0,fallback:!0,hideControls:!0},storage:{enabled:!0,key:"plyr_volume"},controls:["restart","rewind","play","fast-forward","current-time","duration","mute","volume","captions","fullscreen"],onSetup:function(){}};e.supported=function(e){var t,n,s=r(),a="IE"===s.name&&s.version<=9,o=/iPhone|iPod/i.test(navigator.userAgent),i=!!document.createElement("audio").canPlayType,l=!!document.createElement("video").canPlayType;switch(e){case"video":t=l,n=t&&!a&&!o;break;case"audio":t=i,n=t&&!a;break;default:t=i&&l,n=t&&!a}return{basic:t,full:n}},e.setup=function(t){if(T=b(F,t),!T.enabled||!e.supported().basic)return!1;for(var n=document.querySelectorAll(T.selectors.container),r=[],s=n.length-1;s>=0;s--){var a=n[s];if("undefined"==typeof a.plyr){var o=new x(a);a.plyr=Object.keys(o).length?o:!1,T.onSetup.apply(a.plyr)}r.push(a.plyr)}return r}}(this.plyr=this.plyr||{}); \ No newline at end of file +!function(e){"use strict";function t(){var e=["
","
","","","","0% played","","","0% buffered","","
",""];return a(F.controls,"restart")&&e.push(""),a(F.controls,"rewind")&&e.push(""),a(F.controls,"play")&&e.push("",""),a(F.controls,"fast-forward")&&e.push(""),a(F.controls,"current-time")&&e.push("","Current time","00:00",""),a(F.controls,"duration")&&e.push("","Duration","00:00",""),e.push("",""),a(F.controls,"mute")&&e.push("",""),a(F.controls,"volume")&&e.push("",""),a(F.controls,"captions")&&e.push("",""),a(F.controls,"fullscreen")&&e.push(""),e.push("","
"),e.join("")}function n(e,t){F.debug&&window.console&&console[t?"error":"log"](e)}function r(){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)),{name:s,version:o,ios:/(iPad|iPhone|iPod)/g.test(navigator.platform)}}function s(e,t){var n=e.media;if("video"==e.type)switch(t){case"video/webm":return!(!n.canPlayType||!n.canPlayType('video/webm; codecs="vp8, vorbis"').replace(/no/,""));case"video/mp4":return!(!n.canPlayType||!n.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/,""));case"video/ogg":return!(!n.canPlayType||!n.canPlayType('video/ogg; codecs="theora"').replace(/no/,""))}else if("audio"==e.type)switch(t){case"audio/mpeg":return!(!n.canPlayType||!n.canPlayType("audio/mpeg;").replace(/no/,""));case"audio/ogg":return!(!n.canPlayType||!n.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/,""));case"audio/wav":return!(!n.canPlayType||!n.canPlayType('audio/wav; codecs="1"').replace(/no/,""))}return!1}function a(e,t){return Array.prototype.indexOf&&-1!=e.indexOf(t)}function o(e,t,n){return e.replace(new RegExp(t.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1"),"g"),n)}function i(e,t){e.length||(e=[e]);for(var n=e.length-1;n>=0;n--){var r=n>0?t.cloneNode(!0):t,s=e[n],a=s.parentNode,o=s.nextSibling;r.appendChild(s),o?a.insertBefore(r,o):a.appendChild(r)}}function l(e){for(var t=e.parentNode;e.firstChild;)t.insertBefore(e.firstChild,e);t.removeChild(e)}function c(e){e.parentNode.removeChild(e)}function u(e,t){e.insertBefore(t,e.firstChild)}function p(e,t){for(var n in t)e.setAttribute(n,t[n])}function d(e,t,n){if(e)if(e.classList)e.classList[n?"add":"remove"](t);else{var r=(" "+e.className+" ").replace(/\s+/g," ").replace(" "+t+" ","");e.className=r+(n?" "+t:"")}}function f(e,t,n,r){var s=t.split(" ");if(e instanceof NodeList)for(var a=0;an;n++){if(e.prefix=t[n],"undefined"!=typeof document[e.prefix+"CancelFullScreen"]){e.supportsFullScreen=!0;break}if("undefined"!=typeof document.msExitFullscreen&&document.msFullscreenEnabled){e.prefix="ms",e.supportsFullScreen=!0;break}}return"webkit"===e.prefix&&navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)&&(e.supportsFullScreen=!1),e.supportsFullScreen&&(e.fullScreenEventName="ms"==e.prefix?"MSFullscreenChange":e.prefix+"fullscreenchange",e.isFullScreen=function(e){switch("undefined"==typeof e&&(e=document),this.prefix){case"":return document.fullscreenElement==e;case"moz":return document.mozFullScreenElement==e;default:return document[this.prefix+"FullscreenElement"]==e}},e.requestFullScreen=function(e){return""===this.prefix?e.requestFullScreen():e[this.prefix+("ms"==this.prefix?"RequestFullscreen":"RequestFullScreen")]("webkit"===this.prefix?e.ALLOW_KEYBOARD_INPUT:null)},e.cancelFullScreen=function(){return""===this.prefix?document.cancelFullScreen():document[this.prefix+("ms"==this.prefix?"ExitFullscreen":"CancelFullScreen")]()},e.element=function(){return""===this.prefix?document.fullscreenElement:document[this.prefix+"FullscreenElement"]}),e}function x(){var e={supported:function(){try{return"localStorage"in window&&null!==window.localStorage}catch(e){return!1}}()};return e}function w(a){function f(e){if(!ot.usingTextTracks&&"video"===ot.type&&ot.supported.full){for(ot.subcount=0,e="number"==typeof e?e:ot.media.currentTime;S(ot.captions[ot.subcount][0])ot.captions.length-1){ot.subcount=ot.captions.length-1;break}ot.media.currentTime.toFixed(1)>=w(ot.captions[ot.subcount][0])&&ot.media.currentTime.toFixed(1)<=S(ot.captions[ot.subcount][0])?(ot.currentCaption=ot.captions[ot.subcount][1],ot.captionsContainer.innerHTML=ot.currentCaption):ot.captionsContainer.innerHTML=""}}function h(){ot.buttons.captions&&(d(ot.container,F.classes.captions.enabled,!0),F.captions.defaultActive&&(d(ot.container,F.classes.captions.active,!0),ot.buttons.captions.checked=!0))}function w(e){var t=[];return t=e.split(" --> "),C(t[0])}function S(e){var t=[];return t=e.split(" --> "),C(t[1])}function C(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 E(e){return ot.container.querySelectorAll(e)}function A(e){return E(e)[0]}function N(){try{return window.self!==window.top}catch(e){return!0}}function P(){var e=F.html;if(n("Injecting custom controls."),e||(e=t()),e=o(e,"{seektime}",F.seekTime),e=o(e,"{id}",Math.floor(1e4*Math.random())),ot.container.insertAdjacentHTML("beforeend",e),F.tooltips)for(var r=E(F.selectors.labels),s=r.length-1;s>=0;s--){var a=r[s];d(a,F.classes.hidden,!1),d(a,F.classes.tooltip,!0)}}function I(){try{return ot.controls=A(F.selectors.controls),ot.buttons={},ot.buttons.seek=A(F.selectors.buttons.seek),ot.buttons.play=A(F.selectors.buttons.play),ot.buttons.pause=A(F.selectors.buttons.pause),ot.buttons.restart=A(F.selectors.buttons.restart),ot.buttons.rewind=A(F.selectors.buttons.rewind),ot.buttons.forward=A(F.selectors.buttons.forward),ot.buttons.fullscreen=A(F.selectors.buttons.fullscreen),ot.buttons.mute=A(F.selectors.buttons.mute),ot.buttons.captions=A(F.selectors.buttons.captions),ot.checkboxes=E("[type='checkbox']"),ot.progress={},ot.progress.container=A(F.selectors.progress.container),ot.progress.buffer={},ot.progress.buffer.bar=A(F.selectors.progress.buffer),ot.progress.buffer.text=ot.progress.buffer.bar&&ot.progress.buffer.bar.getElementsByTagName("span")[0],ot.progress.played={},ot.progress.played.bar=A(F.selectors.progress.played),ot.progress.played.text=ot.progress.played.bar&&ot.progress.played.bar.getElementsByTagName("span")[0],ot.volume=A(F.selectors.buttons.volume),ot.duration=A(F.selectors.duration),ot.currentTime=A(F.selectors.currentTime),ot.seekTime=E(F.selectors.seekTime),!0}catch(e){return n("It looks like there's a problem with your controls html. Bailing.",!0),ot.media.setAttribute("controls",""),!1}}function M(){if(ot.buttons.play){var e=ot.buttons.play.innerText||"Play";"undefined"!=typeof F.title&&F.title.length&&(e+=", "+F.title),ot.buttons.play.setAttribute("aria-label",e)}}function L(){if(!ot.media)return n("No audio or video element found!",!0),!1;if(ot.supported.full&&(ot.media.removeAttribute("controls"),d(ot.container,F.classes[ot.type],!0),d(ot.container,F.classes.stopped,null===ot.media.getAttribute("autoplay")),ot.browser.ios&&d(ot.container,"ios",!0),"video"===ot.type)){var e=document.createElement("div");e.setAttribute("class",F.classes.videoWrapper),i(ot.media,e),ot.videoContainer=e}null!==ot.media.getAttribute("autoplay")&&H()}function O(){if("video"===ot.type){ot.videoContainer.insertAdjacentHTML("afterbegin","
"),ot.captionsContainer=A(F.selectors.captions),ot.usingTextTracks=!1,ot.media.textTracks&&(ot.usingTextTracks=!0);for(var e,t="",r=ot.media.childNodes,s=0;s=31||"Safari"===ot.browser.name&&ot.browser.version>=7)&&(n("Detected IE 10/11 or Firefox 31+ or Safari 7+."),ot.usingTextTracks=!1),ot.usingTextTracks){n("TextTracks supported.");for(var i=0;i=7){n("Safari 7+ detected; removing track from DOM."),a=ot.media.getElementsByTagName("track");for(var u=0;ut?t=0:t>ot.media.duration&&(t=ot.media.duration);try{ot.media.currentTime=t.toFixed(1)}catch(r){}n("Seeking to "+ot.media.currentTime+" seconds"),f(t)}function D(){d(ot.container,F.classes.playing,!ot.media.paused),d(ot.container,F.classes.stopped,ot.media.paused)}function W(e){var t=T.supportsFullScreen;e&&e.type===T.fullScreenEventName?ot.isFullscreen=T.isFullScreen(ot.container):t?(T.isFullScreen(ot.container)?T.cancelFullScreen():T.requestFullScreen(ot.container),ot.isFullscreen=T.isFullScreen(ot.container)):(ot.isFullscreen=!ot.isFullscreen,ot.isFullscreen?(m(document,"keyup",z),document.body.style.overflow="hidden"):(y(document,"keyup",z),document.body.style.overflow="")),d(ot.container,F.classes.fullscreen.active,ot.isFullscreen),ot.isFullscreen&&d(ot.controls,F.classes.hover,!1)}function z(e){27===(e.which||e.charCode||e.keyCode)&&ot.isFullscreen&&W()}function U(e){ot.volume&&("undefined"==typeof e&&(e=F.storage.enabled&&x().supported?window.localStorage[F.storage.key]||F.volume:F.volume),e>10&&(e=10),ot.supported.full&&(ot.volume.value=e),ot.media.volume=parseFloat(e/10),J(),F.storage.enabled&&x().supported&&window.localStorage.setItem(F.storage.key,e))}function X(e){"undefined"==typeof e&&(e=!ot.media.muted),ot.supported.full&&(ot.buttons.mute.checked=e),ot.media.muted=e,J()}function _(e){ot.supported.full&&ot.buttons.captions&&("undefined"==typeof e&&(e=-1===ot.container.className.indexOf(F.classes.captions.active),ot.buttons.captions.checked=e),d(ot.container,F.classes.captions.active,e))}function J(){d(ot.container,F.classes.muted,0===ot.media.volume||ot.media.muted)}function $(e){var t="waiting"===e.type;clearTimeout(ot.loadingTimer),ot.loadingTimer=setTimeout(function(){d(ot.container,F.classes.loading,t)},t?250:0)}function K(e){var t=ot.progress.played.bar,n=ot.progress.played.text,r=0;if(e)switch(e.type){case"timeupdate":case"seeking":r=g(ot.media.currentTime,ot.media.duration),"timeupdate"==e.type&&ot.buttons.seek&&(ot.buttons.seek.value=r);break;case"change":case"input":r=e.target.value;break;case"playing":case"progress":t=ot.progress.buffer.bar,n=ot.progress.buffer.text,r=function(){var e=ot.media.buffered;return e.length?g(e.end(0),ot.media.duration):0}()}t&&(t.value=r),n&&(n.innerHTML=r)}function Y(e,t){if(t){ot.secs=parseInt(e%60),ot.mins=parseInt(e/60%60),ot.hours=parseInt(e/60/60%60);var n=parseInt(ot.media.duration/60/60%60)>0;ot.secs=("0"+ot.secs).slice(-2),ot.mins=("0"+ot.mins).slice(-2),t.innerHTML=(n?ot.hours+":":"")+ot.mins+":"+ot.secs}}function G(){var e=ot.media.duration||0;!ot.duration&&F.displayDuration&&ot.media.paused&&Y(e,ot.currentTime),ot.duration&&Y(e,ot.duration)}function Q(e){Y(ot.media.currentTime,ot.currentTime),K(e)}function Z(){for(var e=ot.media.querySelectorAll("source"),t=e.length-1;t>=0;t--)c(e[t]);ot.media.removeAttribute("src")}function et(e){if(e.src){var t=document.createElement("source");p(t,e),u(ot.media,t)}}function tt(e){if(V(),j(),Z(),"string"==typeof e)ot.media.setAttribute("src",e);else if(e.constructor===Array)for(var t in e)et(e[t]);ot.supported.full&&(Q(),D()),ot.media.load(),null!==ot.media.getAttribute("autoplay")&&H()}function nt(e){"video"===ot.type&&ot.media.setAttribute("poster",e)}function rt(){var e="IE"==ot.browser.name?"change":"input";m(ot.buttons.play,"click",function(){H(),setTimeout(function(){ot.buttons.pause.focus()},100)}),m(ot.buttons.pause,"click",function(){V(),setTimeout(function(){ot.buttons.play.focus()},100)}),m(ot.buttons.restart,"click",j),m(ot.buttons.rewind,"click",B),m(ot.buttons.forward,"click",R),m(ot.buttons.seek,e,j),m(ot.volume,e,function(){U(this.value)}),m(ot.buttons.mute,"change",function(){X(this.checked)}),m(ot.buttons.fullscreen,"click",W),T.supportsFullScreen&&m(document,T.fullScreenEventName,W),m(ot.media,"timeupdate seeking",Q),m(ot.media,"timeupdate",f),m(ot.media,"loadedmetadata",G),m(ot.buttons.captions,"change",function(){_(this.checked)}),m(ot.media,"ended",function(){"video"===ot.type&&(ot.captionsContainer.innerHTML=""),D()}),m(ot.media,"progress",K),m(ot.media,"playing",K),m(ot.media,"volumechange",J),m(ot.media,"play pause",D),m(ot.media,"waiting canplay seeked",$),m(ot.checkboxes,"keyup",b),"video"===ot.type&&F.click&&m(ot.videoContainer,"click",function(){ot.media.paused?v(ot.buttons.play,"click"):ot.media.ended?(j(),v(ot.buttons.play,"click")):v(ot.buttons.pause,"click")}),F.fullscreen.hideControls&&m(ot.controls,"mouseenter mouseleave",function(e){d(ot.controls,F.classes.hover,"mouseenter"===e.type)})}function st(){if(!ot.init)return null;c(A(F.selectors.controls)),"video"===ot.type&&(c(A(F.selectors.captions)),l(ot.videoContainer)),ot.media.setAttribute("controls","");var e=ot.media.cloneNode(!0);ot.media.parentNode.replaceChild(e,ot.media),ot.init=!1}function at(){if(ot.init)return null;if(T=k(),ot.browser=r(),ot.media=ot.container.querySelectorAll("audio, video")[0],ot.type=ot.media.tagName.toLowerCase(),ot.supported=e.supported(ot.type),!ot.supported.basic)return!1;if(n(ot.browser.name+" "+ot.browser.version),L(),ot.supported.full){if(P(),!I())return!1;F.displayDuration&&G(),M(),O(),U(),q(),rt()}ot.init=!0}var ot=this;return ot.container=a,at(),ot.init?{media:ot.media,play:H,pause:V,restart:j,rewind:B,forward:R,seek:j,source:tt,poster:nt,setVolume:U,toggleMute:X,toggleCaptions:_,toggleFullscreen:W,isFullscreen:function(){return ot.isFullscreen||!1},support:function(e){return s(ot,e)},destroy:st,restore:at}:{}}var T,F,S={enabled:!0,debug:!1,seekTime:10,volume:5,click:!0,tooltips:!1,displayDuration:!0,selectors:{container:".player",controls:".player-controls",labels:"[data-player] .sr-only, label .sr-only",buttons:{seek:"[data-player='seek']",play:"[data-player='play']",pause:"[data-player='pause']",restart:"[data-player='restart']",rewind:"[data-player='rewind']",forward:"[data-player='fast-forward']",mute:"[data-player='mute']",volume:"[data-player='volume']",captions:"[data-player='captions']",fullscreen:"[data-player='fullscreen']"},progress:{container:".player-progress",buffer:".player-progress-buffer",played:".player-progress-played"},captions:".player-captions",currentTime:".player-current-time",duration:".player-duration"},classes:{video:"player-video",videoWrapper:"player-video-wrapper",audio:"player-audio",stopped:"stopped",playing:"playing",muted:"muted",loading:"loading",tooltip:"player-tooltip",hidden:"sr-only",hover:"hover",captions:{enabled:"captions-enabled",active:"captions-active"},fullscreen:{enabled:"fullscreen-enabled",active:"fullscreen-active",hideControls:"fullscreen-hide-controls"}},captions:{defaultActive:!1},fullscreen:{enabled:!0,fallback:!0,hideControls:!0},storage:{enabled:!0,key:"plyr_volume"},controls:["restart","rewind","play","fast-forward","current-time","duration","mute","volume","captions","fullscreen"],onSetup:function(){}};e.supported=function(e){var t,n,s=r(),a="IE"===s.name&&s.version<=9,o=/iPhone|iPod/i.test(navigator.userAgent),i=!!document.createElement("audio").canPlayType,l=!!document.createElement("video").canPlayType;switch(e){case"video":t=l,n=t&&!a&&!o;break;case"audio":t=i,n=t&&!a;break;default:t=i&&l,n=t&&!a}return{basic:t,full:n}},e.setup=function(t){if(F=h(S,t),!F.enabled||!e.supported().basic)return!1;for(var n=document.querySelectorAll(F.selectors.container),r=[],s=n.length-1;s>=0;s--){var a=n[s];if("undefined"==typeof a.plyr){var o=new w(a);a.plyr=Object.keys(o).length?o:!1,F.onSetup.apply(a.plyr)}r.push(a.plyr)}return r}}(this.plyr=this.plyr||{}); \ No newline at end of file diff --git a/docs/error.html b/docs/error.html index 63cd0f0b..6393d28f 100644 --- a/docs/error.html +++ b/docs/error.html @@ -6,7 +6,7 @@ - +
diff --git a/docs/index.html b/docs/index.html index 0bc8e349..aa5a8859 100644 --- a/docs/index.html +++ b/docs/index.html @@ -8,10 +8,10 @@ - + - +
@@ -79,13 +79,13 @@ b.insertBefore(c, b.childNodes[0]); } } - })(document, "https://cdn.plyr.io/1.1.5/sprite.svg"); + })(document, "https://cdn.plyr.io/1.1.6/sprite.svg"); - + - + diff --git a/package.json b/package.json index 192df16d..f5595373 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plyr", - "version": "1.1.5", + "version": "1.1.6", "description": "A simple HTML5 media player using custom controls", "homepage": "http://plyr.io", "main": "gulpfile.js", diff --git a/readme.md b/readme.md index 6c6ada04..332655d9 100644 --- a/readme.md +++ b/readme.md @@ -37,7 +37,7 @@ If you have any cool ideas or features, please let me know by [creating an issue Check `docs/index.html` and `docs/dist/docs.js` for an example setup. -**Heads up**, the example `index.html` file needs to be served from a webserver (such as Apache, Nginx, IIS or similar) unless you change the file sources to include http or https. e.g. change `//cdn.plyr.io/1.1.5/plyr.js` to `https://cdn.plyr.io/1.1.5/plyr.js` +**Heads up**, the example `index.html` file needs to be served from a webserver (such as Apache, Nginx, IIS or similar) unless you change the file sources to include http or https. e.g. change `//cdn.plyr.io/1.1.6/plyr.js` to `https://cdn.plyr.io/1.1.6/plyr.js` ### Bower If bower is your thang, you can grab Plyr using: @@ -57,11 +57,11 @@ More info is on [npm](https://www.npmjs.com/package/ember-cli-plyr) and [GitHub] If you want to use our CDN, you can use the following: ```html - - + + ``` -You can also access the `sprite.svg` file at `https://cdn.plyr.io/1.1.5/sprite.svg`. +You can also access the `sprite.svg` file at `https://cdn.plyr.io/1.1.6/sprite.svg`. ### CSS If you want to use the default css, add the `plyr.css` file from /dist into your head, or even better use `plyr.less` or `plyr.sass` file included in `/src` in your build to save a request. @@ -346,6 +346,16 @@ Here's a list of the methods supported: String Set the poster url. This is supported for the video element only. + + destroy() + — + Destroys the plyr UI and any media event listeners, effectively restoring to the previous state before setup() was called. + + + restore() + — + Reverses the effects of the destroy() method, restoring the UI and listeners. + diff --git a/src/js/plyr.js b/src/js/plyr.js index bf4b3e44..59847333 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v1.1.5 +// plyr.js v1.1.6 // https://github.com/selz/plyr // License: The MIT License (MIT) // ========================================================================== @@ -366,12 +366,28 @@ // append it to the parent. if (sibling) { parent.insertBefore(child, sibling); - } else { + } + else { parent.appendChild(child); } } } + // Unwrap an element + // http://plainjs.com/javascript/manipulation/unwrap-a-dom-element-35/ + function _unwrap(wrapper) { + // Get the element's parent node + var parent = wrapper.parentNode; + + // Move all children out of the element + while (wrapper.firstChild) { + parent.insertBefore(wrapper.firstChild, wrapper); + } + + // Remove the empty element + parent.removeChild(wrapper); + } + // Remove an element function _remove(element) { element.parentNode.removeChild(element); @@ -404,7 +420,7 @@ // Toggle event function _toggleHandler(element, events, callback, toggle) { - events = events.split(" "); + var eventList = events.split(" "); // If a nodelist is passed, call itself on each node if(element instanceof NodeList) { @@ -417,8 +433,8 @@ } // If a single node is passed, bind the event listener - for (var i = 0; i < events.length; i++) { - element[toggle ? "addEventListener" : "removeEventListener"](events[i], callback, false); + for (var i = 0; i < eventList.length; i++) { + element[toggle ? "addEventListener" : "removeEventListener"](eventList[i], callback, false); } } @@ -1512,7 +1528,47 @@ } } + // Destroy an instance + function _destroy() { + // Bail if the element is not initialized + if(!player.init) { + return null; + } + + // Event listeners are removed when elements are removed + // http://stackoverflow.com/questions/12528049/if-a-dom-element-is-removed-are-its-listeners-also-removed-from-memory + + // Remove controls + _remove(_getElement(config.selectors.controls)); + + // If video, we need to remove some more + if(player.type === "video") { + // Remove captions + _remove(_getElement(config.selectors.captions)); + + // Remove video wrapper + _unwrap(player.videoContainer); + } + + // Restore native video controls + player.media.setAttribute("controls", ""); + + // Clone the media element to remove listeners + // http://stackoverflow.com/questions/19469881/javascript-remove-all-event-listeners-of-specific-type + var clone = player.media.cloneNode(true); + player.media.parentNode.replaceChild(clone, player.media); + + // Remove init flag + player.init = false; + } + + // Setup a player function _init() { + // Bail if the element is initialized + if(player.init) { + return null; + } + // Setup the fullscreen api fullscreen = _fullscreen(); @@ -1571,10 +1627,14 @@ } // Successful setup - return true; + player.init = true; } - if(!_init()) { + // Initialize instance + _init(); + + // If init failed, return an empty object + if(!player.init) { return {}; } @@ -1593,7 +1653,9 @@ toggleCaptions: _toggleCaptions, toggleFullscreen: _toggleFullscreen, isFullscreen: function() { return player.isFullscreen || false; }, - support: function(mimeType) { return _supportMime(player, mimeType); } + support: function(mimeType) { return _supportMime(player, mimeType); }, + destroy: _destroy, + restore: _init } }