-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoEvents.min.js
4 lines (4 loc) · 3.14 KB
/
VideoEvents.min.js
1
2
3
4
/**
* @author Aaron Clinger - https://github.com/aaronclinger/videoevents.js
*/
!function(e){"use strict";var n=function(e){if(e===""+e)switch(e=e.toLowerCase()){case"play":case"pause":case"end":case"progress":return{type:e,value:null};default:if("%"===e.slice(-1)&&!1!==(e=t(e.slice(0,-1)))&&e>=0&&e<=100)return{type:"percent",value:e/100}}return!1!==(e=t(e))&&{type:"time",value:e}},t=function(e){return(e=Number(e))+0===e&&e};function r(e,n,t){var r,a,u,i,s={before:!0};s.getType=function(){return u},s.getValue=function(){return a},s.getCallback=function(){return r},s.isOnce=function(){return i},s.trigger=function(e,n,t,i){var s={sender:e,type:u,time:n,percent:t,duration:i};switch(u){case"percent":s.value=100*a+"%";break;case"time":s.value=a}r(s)},s.equals=function(e,n){var t=a===e.getValue()&&u===e.getType();return t&&n&&(t=r===e.getCallback()),t};return function(e,n,t){return u=e.type,a=e.value,r=n,i=!!t,s}(e,n,t)}e.VideoEvents=function(e){var t,a={},u=[],i=null,s=!1,o=!1,c=!1;a.on=function(e,n){return f(e,n,!1),a},a.once=function(e,n){return f(e,n,!0),a},a.off=function(e,t){var i,s,o,c=u.length;if(e){if(!1!==(s=n(e)))for(o=new r(s,t),i=!!t;c--&&(!u[c].equals(o,i)||(u.splice(c,1),!i)););}else u=[];return a},a.getPlayer=function(){return t},a.destroy=function(){t&&(s?(t.removeEventListener("onReady",d),t.removeEventListener("onStateChange",v),null!==i&&(clearInterval(i),i=null)):c?(t.off("play",y),t.off("pause",m),t.off("ended",h),t.off("timeupdate",E)):o&&(t.removeEventListener("play",b),t.removeEventListener("pause",b),t.removeEventListener("ended",b),t.removeEventListener("timeupdate",b)),u=[],t=null)};var f=function(e,t,a){var i,s,o=n(e);if(!1!==o){for(i=new r(o,t,a),s=u.length;s--;)if(u[s].equals(i,!0))return;u.push(i)}},l=function(e,n,t,r){for(var i,s,o,c,f=-1;++f<u.length;){if(i=!1,e===(o=u[f]).getType())switch(e){case"play":case"pause":case"end":case"progress":i=!0}else if("progress"===e)switch(o.getType()){case"time":case"percent":c=o.getValue(),"time"===o.getType()?(s=n,c<0&&(c=Math.max(0,r+c))):s=t,c>s?o.before=!0:o.before&&c<=s&&(o.before=!1,i=!0)}i&&(o.trigger(a,n,t,r),o.isOnce()&&u.splice(f--,1))}},p=function(e){return e=isNaN(e)?0:e,e=Math.min(1,Math.max(0,e)),Math.round(1e3*e)/1e3},d=function(){t.addEventListener("onStateChange",v)},v=function(e){switch(e.data){case 0:g("end");break;case 1:g("play");break;case 2:g("pause")}},g=function(e){var n=t.getCurrentTime(),r=t.getDuration(),a=p(n/r);switch(e){case"play":null===i&&(i=setInterval((function(){g("progress")}),250));break;case"pause":case"end":clearInterval(i),i=null}l(e,n,a,r)},y=function(e){L("play",e)},m=function(e){L("pause",e)},h=function(e){L("end",e)},E=function(e){L("progress",e)},L=function(e,n){l(e,n.seconds,p(n.percent),n.duration)},b=function(e){var n=e.type,t=e.target,r=t.currentTime,a=t.duration;"timeupdate"===n&&(n="progress"),l(n,r,p(r/a),a)};return function(e){c=!!(t=e).on,o=e instanceof Element,(s=!o&&!!t.addEventListener)?t.addEventListener("onReady",d):c?(t.on("play",y),t.on("pause",m),t.on("ended",h),t.on("timeupdate",E)):o&&(t.addEventListener("play",b),t.addEventListener("pause",b),t.addEventListener("ended",b),t.addEventListener("timeupdate",b))}(e),a}}(window);