-
Notifications
You must be signed in to change notification settings - Fork 0
/
popcorn.min.js
38 lines (37 loc) · 15.3 KB
/
popcorn.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* popcorn.js version af05609
* http://popcornjs.org
*
* Copyright 2011, Mozilla Foundation
* Licensed under the MIT license
*/
(function(r,o){var w=Array.prototype,x=Object.prototype,y=w.forEach,D=w.slice,z=x.hasOwnProperty,A=x.toString,E=/^(#([\w\-\_\.]+))$/,u=[],B=false,s={events:{hash:{},apis:{}}},b=function(a,c){return new b.p.init(a,c||null)};b.instances=[];b.instanceIds={};b.removeInstance=function(a){if(b.instances.length){b.instances.splice(b.instanceIds[a.id],1);delete b.instanceIds[a.id];return b.instances}};b.addInstance=function(a){var c=b.instances.length,d=a.media.id&&a.media.id;a.id=!(d in b.instanceIds)&&
d||"__popcorn"+c;b.instanceIds[a.id]=c;b.instances.push(a);return b.instances};b.getInstanceById=function(a){return b.instances[b.instanceIds[a]]};b.removeInstanceById=function(a){return b.removeInstance(b.instances[b.instanceIds[a]])};b.p=b.prototype={init:function(a,c){var d;if(typeof a==="function")if(o.readyState==="interactive"||o.readyState==="complete")a(o,b);else{u.push(a);if(!B){B=true;var f=function(){o.removeEventListener("DOMContentLoaded",f,false);for(var g=0,j=u.length;g<j;g++)u[g].call(o,
b);u=null};o.addEventListener("DOMContentLoaded",f,false)}}else{this.media=(d=E.exec(a))&&d.length&&d[2]?o.getElementById(d[2]):a;this[this.media.nodeName&&this.media.nodeName.toLowerCase()||"video"]=this.media;b.addInstance(this);this.options=c||{};this.data={disabled:[],events:{},hooks:{},history:[],trackRefs:{},trackEvents:{byStart:[{start:-1,end:-1}],byEnd:[{start:-1,end:-1}],startIndex:0,endIndex:0,previousUpdateTime:0}};var e=function(g){if(g.media.readyState>=2){var j=g.media.duration;j=j!=
j?Number.MAX_VALUE:j+1;b.addTrackEvent(g,{start:j,end:j});g.media.addEventListener("timeupdate",function(k){var l=this.currentTime,m=g.data.trackEvents.previousUpdateTime,h=g.data.trackEvents,i=h.byEnd,n=h.byStart;if(m<l){for(;i[h.endIndex]&&i[h.endIndex].end<=l;)if(!i[h.endIndex]._natives||g[i[h.endIndex]._natives.type]){if(i[h.endIndex]._running===true){i[h.endIndex]._running=false;i[h.endIndex]._natives.end.call(g,k,i[h.endIndex])}h.endIndex++}else{b.removeTrackEvent(g,i[h.endIndex]._id);return}for(;n[h.startIndex]&&
n[h.startIndex].start<=l;)if(!n[h.startIndex]._natives||g[n[h.startIndex]._natives.type]){if(n[h.startIndex].end>l&&n[h.startIndex]._running===false&&g.data.disabled.indexOf(n[h.startIndex]._natives.type)===-1){n[h.startIndex]._running=true;n[h.startIndex]._natives.start.call(g,k,n[h.startIndex])}h.startIndex++}else{b.removeTrackEvent(g,n[h.startIndex]._id);return}}else if(m>l){for(;n[h.startIndex]&&n[h.startIndex].start>l;)if(!n[h.startIndex]._natives||g[n[h.startIndex]._natives.type]){if(n[h.startIndex]._running===
true){n[h.startIndex]._running=false;n[h.startIndex]._natives.end.call(g,k,n[h.startIndex])}h.startIndex--}else{b.removeTrackEvent(g,n[h.startIndex]._id);return}for(;i[h.endIndex]&&i[h.endIndex].end>l;)if(!i[h.endIndex]._natives||g[i[h.endIndex]._natives.type]){if(i[h.endIndex].start<=l&&i[h.endIndex]._running===false&&g.data.disabled.indexOf(i[h.endIndex]._natives.type)===-1){i[h.endIndex]._running=true;i[h.endIndex]._natives.start.call(g,k,i[h.endIndex])}h.endIndex--}else{b.removeTrackEvent(g,i[h.endIndex]._id);
return}}h.previousUpdateTime=l},false)}else r.setTimeout(function(){e(g)},1)};e(this);return this}}};b.p.init.prototype=b.p;b.forEach=function(a,c,d){if(!a||!c)return{};d=d||this;var f,e;if(y&&a.forEach===y)return a.forEach(c,d);if(A.call(a)==="[object NodeList]"){f=0;for(e=a.length;f<e;f++)c.call(d,a[f],f,a);return a}for(f in a)z.call(a,f)&&c.call(d,a[f],f,a);return a};b.extend=function(a){var c=D.call(arguments,1);b.forEach(c,function(d){for(var f in d)a[f]=d[f]});return a};b.extend(b,{error:function(a){throw Error(a);
},guid:function(a){b.guid.counter++;return(a?a:"")+(+new Date+b.guid.counter)},sizeOf:function(a){var c=0,d;for(d in a)c++;return c},isArray:Array.isArray||function(a){return A.call(a)==="[object Array]"},nop:function(){},position:function(a){a=a.getBoundingClientRect();var c={},d=o.documentElement,f=o.body,e,g,j;e=d.clientTop||f.clientTop||0;g=d.clientLeft||f.clientLeft||0;j=r.pageYOffset&&d.scrollTop||f.scrollTop;d=r.pageXOffset&&d.scrollLeft||f.scrollLeft;e=Math.ceil(a.top+j-e);g=Math.ceil(a.left+
d-g);for(var k in a)c[k]=Math.round(a[k]);return b.extend({},c,{top:e,left:g})},disable:function(a,c){var d=a.data.disabled;d.indexOf(c)===-1&&d.push(c);return a},enable:function(a,c){var d=a.data.disabled,f=d.indexOf(c);f>-1&&d.splice(f,1);return a}});b.guid.counter=1;b.extend(b.p,function(){var a={};b.forEach("load play pause currentTime playbackRate mute volume duration".split(/\s+/g),function(c){a[c]=function(d){if(typeof this.media[c]==="function"){this.media[c]();return this}if(d!==false&&d!==
null&&typeof d!=="undefined"){this.media[c]=d;return this}return this.media[c]}});return a}());b.forEach("enable disable".split(" "),function(a){b.p[a]=function(c){return b[a](this,c)}});b.extend(b.p,{roundTime:function(){return-~this.media.currentTime},exec:function(a,c){b.addTrackEvent(this,{start:a,end:a+1,_running:false,_natives:{start:c||b.nop,end:b.nop,type:"exec"}});return this},position:function(){return b.position(this.media)},toggle:function(a){return b[this.data.disabled.indexOf(a)>-1?
"enable":"disable"](this,a)},defaults:function(a,c){if(b.isArray(a)){b.forEach(a,function(d){for(var f in d)this.defaults(f,d[f])},this);return this}if(!this.options.defaults)this.options.defaults={};this.options.defaults[a]||(this.options.defaults[a]={});b.extend(this.options.defaults[a],c);return this}});b.Events={UIEvents:"blur focus focusin focusout load resize scroll unload",MouseEvents:"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick",Events:"loadstart progress suspend emptied stalled play pause loadedmetadata loadeddata waiting playing canplay canplaythrough seeking seeked timeupdate ended ratechange durationchange volumechange"};
b.Events.Natives=b.Events.UIEvents+" "+b.Events.MouseEvents+" "+b.Events.Events;s.events.apiTypes=["UIEvents","MouseEvents","Events"];(function(a,c){for(var d=s.events.apiTypes,f=a.Natives.split(/\s+/g),e=0,g=f.length;e<g;e++)c.hash[f[e]]=true;d.forEach(function(j){c.apis[j]={};for(var k=a[j].split(/\s+/g),l=k.length,m=0;m<l;m++)c.apis[j][k[m]]=true})})(b.Events,s.events);b.events={isNative:function(a){return!!s.events.hash[a]},getInterface:function(a){if(!b.events.isNative(a))return false;var c=
s.events,d=c.apiTypes;c=c.apis;for(var f=0,e=d.length,g,j;f<e;f++){j=d[f];if(c[j][a]){g=j;break}}return g},all:b.Events.Natives.split(/\s+/g),fn:{trigger:function(a,c){var d;if(this.data.events[a]&&b.sizeOf(this.data.events[a])){if(d=b.events.getInterface(a)){d=o.createEvent(d);d.initEvent(a,true,true,r,1);this.media.dispatchEvent(d);return this}b.forEach(this.data.events[a],function(f){f.call(this,c)},this)}return this},listen:function(a,c){var d=this,f=true,e=b.events.hooks[a],g;if(!this.data.events[a]){this.data.events[a]=
{};f=false}if(e){e.add&&e.add.call(this);if(e.bind)a=e.bind;if(e.handler){g=c;c=function(j){e.handler.call(d,j,g)}}f=true;if(!this.data.events[a]){this.data.events[a]={};f=false}}this.data.events[a][c.name||c.toString()+b.guid()]=c;!f&&b.events.all.indexOf(a)>-1&&this.media.addEventListener(a,function(j){b.forEach(d.data.events[a],function(k){typeof k==="function"&&k.call(d,j)})},false);return this},unlisten:function(a,c){if(this.data.events[a]&&this.data.events[a][c]){delete this.data.events[a][c];
return this}this.data.events[a]=null;return this}},hooks:{canplayall:{bind:"canplaythrough",add:function(){this.data.hooks.canplayall={fired:false}},handler:function(a,c){if(!this.data.hooks.canplayall.fired){c.call(this,a);this.data.hooks.canplayall.fired=true}}}}};b.forEach(["trigger","listen","unlisten"],function(a){b.p[a]=b.events.fn[a]});b.protect={natives:"load play pause currentTime playbackRate mute volume duration removePlugin roundTime trigger listen unlisten exec".toLowerCase().split(/\s+/)};
b.addTrackEvent=function(a,c){if(c&&c._natives&&c._natives.type&&a.options.defaults&&a.options.defaults[c._natives.type])c=b.extend({},a.options.defaults[c._natives.type],c);if(c._natives){c._id=!c.id?b.guid(c._natives.type):c.id;a.data.history.push(c._id)}c.start=b.util.toSeconds(c.start,a.options.framerate);c.end=b.util.toSeconds(c.end,a.options.framerate);var d=a.data.trackEvents.byStart,f=a.data.trackEvents.byEnd,e;for(e=d.length-1;e>=0;e--)if(c.start>=d[e].start){d.splice(e+1,0,c);break}for(e=
f.length-1;e>=0;e--)if(c.end>f[e].end){f.splice(e+1,0,c);break}c._id&&b.addTrackEvent.ref(a,c)};b.addTrackEvent.ref=function(a,c){a.data.trackRefs[c._id]=c;return a};b.removeTrackEvent=function(a,c){var d=a.data.history.length,f=0,e=[],g=[],j=[];b.forEach(a.data.trackEvents.byStart,function(l,m){if(!l._id){e.push(a.data.trackEvents.byStart[m]);g.push(a.data.trackEvents.byEnd[m])}if(l._id){if(l._id!==c){e.push(a.data.trackEvents.byStart[m]);g.push(a.data.trackEvents.byEnd[m])}if(l._id===c){f=m;l._natives._teardown&&
l._natives._teardown.call(a,l)}}});f<=a.data.trackEvents.startIndex&&a.data.trackEvents.startIndex--;f<=a.data.trackEvents.endIndex&&a.data.trackEvents.endIndex--;a.data.trackEvents.byStart=e;a.data.trackEvents.byEnd=g;for(var k=0;k<d;k++)a.data.history[k]!==c&&j.push(a.data.history[k]);a.data.history=j;b.removeTrackEvent.ref(a,c)};b.removeTrackEvent.ref=function(a,c){delete a.data.trackRefs[c];return a};b.getTrackEvents=function(a){var c=[];a=a.data.trackEvents.byStart;for(var d=a.length,f=0,e;f<
d;f++){e=a[f];e._id&&c.push(e)}return c};b.getTrackEvents.ref=function(a){return a.data.trackRefs};b.getTrackEvent=function(a,c){return a.data.trackRefs[c]};b.getTrackEvent.ref=function(a,c){return a.data.trackRefs[c]};b.getLastTrackEventId=function(a){return a.data.history[a.data.history.length-1]};b.extend(b.p,{getTrackEvents:function(){return b.getTrackEvents.call(null,this)},getTrackEvent:function(a){return b.getTrackEvent.call(null,this,a)},getLastTrackEventId:function(){return b.getLastTrackEventId.call(null,
this)},removeTrackEvent:function(a){b.removeTrackEvent.call(null,this,a);return this},removePlugin:function(a){b.removePlugin.call(null,this,a);return this}});b.manifest={};b.registry=[];b.registryByName={};b.plugin=function(a,c,d){if(b.protect.natives.indexOf(a.toLowerCase())>=0)b.error("'"+a+"' is a protected function name");else{var f=["start","end"],e={},g=typeof c==="function",j=["_setup","_teardown","start","end"],k=function(h,i){h=h||b.nop;i=i||b.nop;return function(){h.apply(this,arguments);
i.apply(this,arguments)}};d||(d=c.manifest||{});j.forEach(function(h){c[h]=c[h]||b.nop});var l=function(h,i){if(!i)return this;var n=i._natives={},C="",p,t;b.extend(n,h);i._natives.type=a;i._running=false;p=this.options.defaults&&this.options.defaults[i._natives&&i._natives.type];i.compose=i.compose&&i.compose.split(" ")||[];i.effect=i.effect&&i.effect.split(" ")||[];i.compose=i.compose.concat(i.effect);i.compose.forEach(function(v){C=b.compositions[v]||{};j.forEach(function(q){n[q]=k(n[q],C[q])})});
i._natives.manifest=d;if(!("start"in i))i.start=0;if(!("end"in i))i.end=this.duration()||Number.MAX_VALUE;t=p?b.extend({},p,i):i;if(!t.target){p="options"in d&&d.options;t.target=p&&"target"in p&&p.target}i._natives._setup&&i._natives._setup.call(this,t);b.addTrackEvent(this,b.extend(t,i));b.forEach(h,function(v,q){q!=="type"&&f.indexOf(q)===-1&&this.listen(q,v)},this);return this};if(d||"manifest"in c)b.manifest[a]=d||c.manifest;e[a]=function(h){return l.call(this,g?c.call(this,h):c,h)};b.extend(b.p,
e);var m={fn:e[a],definition:c,base:c,parents:[],name:a};b.registry.push(b.extend(e,m,{type:a}));b.registryByName[a]=m;return e}};b.removePlugin=function(a,c){if(!c){c=a;a=b.p;if(b.protect.natives.indexOf(c.toLowerCase())>=0){b.error("'"+c+"' is a protected function name");return}var d=b.registry.length,f;for(f=0;f<d;f++)if(b.registry[f].name===c){b.registry.splice(f,1);delete b.registryByName[c];delete a[c];return}}d=a.data.trackEvents.byStart;f=a.data.trackEvents.byEnd;var e,g;e=0;for(g=d.length;e<
g;e++)if(d[e]&&d[e]._natives&&d[e]._natives.type===c&&f[e]&&f[e]._natives&&f[e]._natives.type===c){d[e]._natives._teardown&&d[e]._natives._teardown.call(a,d[e]);d.splice(e,1);f.splice(e,1);e--;g--;if(a.data.trackEvents.startIndex<=e){a.data.trackEvents.startIndex--;a.data.trackEvents.endIndex--}}};b.compositions={};b.compose=function(a,c,d){b.manifest[a]=d||c.manifest||{};b.compositions[a]=c};b.plugin.effect=b.effect=b.compose;b.parsers={};b.parser=function(a,c,d){if(b.protect.natives.indexOf(a.toLowerCase())>=
0)b.error("'"+a+"' is a protected function name");else{if(typeof c==="function"&&!d){d=c;c=""}if(!(typeof d!=="function"||typeof c!=="string")){var f={};f[a]=function(e,g){if(!e)return this;var j=this;b.xhr({url:e,dataType:c,success:function(k){var l,m,h=0;k=d(k).data||[];if(l=k.length){for(;h<l;h++){m=k[h];for(var i in m)z.call(m,i)&&j[i]&&j[i](m[i])}g&&g()}}});return this};b.extend(b.p,f);return f}}};var F=/\?/,G={url:"",data:"",dataType:"",success:b.nop,type:"GET",async:true,xhr:function(){return new r.XMLHttpRequest}};
b.xhr=function(a){a.dataType=a.dataType&&a.dataType.toLowerCase()||null;if(a.dataType&&(a.dataType==="jsonp"||a.dataType==="script"))b.xhr.getJSONP(a.url,a.success,a.dataType==="script");else{a=b.extend({},G,a);a.ajax=a.xhr();if(a.ajax){if(a.type==="GET"&&a.data){a.url+=(F.test(a.url)?"&":"?")+a.data;a.data=null}a.ajax.open(a.type,a.url,a.async);a.ajax.send(a.data||null);return b.xhr.httpData(a)}}};b.xhr.httpData=function(a){var c,d=null;a.ajax.onreadystatechange=function(){if(a.ajax.readyState===
4){try{d=JSON.parse(a.ajax.responseText)}catch(f){}c={xml:a.ajax.responseXML,text:a.ajax.responseText,json:d};if(a.dataType)c=c[a.dataType];a.success.call(a.ajax,c)}};return c};b.xhr.getJSONP=function(a,c,d){if(d)if(o.querySelectorAll('script[src="'+a+'"]').length){c&&c(true);return}var f=o.head||o.getElementsByTagName("head")[0]||o.documentElement,e=o.createElement("script"),g=a.split("?")[1],j=false,k=[],l,m;if(g&&!d)k=g.split("&");if(k.length)m=k[k.length-1].split("=");l=k.length?m[1]?m[1]:m[0]:
"jsonp";if(!g&&!d)a+="?callback="+l;if(l&&!d){if(window[l])l=b.guid(l);window[l]=function(h){c&&c(h);j=true};a=a.replace(m.join("="),m[0]+"="+l)}e.onload=e.onreadystatechange=function(){if(!e.readyState||/loaded|complete/.test(e.readyState)){d&&c&&c();if(j){delete window[l];f.removeChild(e)}}};e.src=a;f.insertBefore(e,f.firstChild)};b.getJSONP=b.xhr.getJSONP;b.getScript=b.xhr.getScript=function(a,c){return b.xhr.getJSONP(a,c,true)};b.util={toSeconds:function(a,c){var d=/^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/;
if(typeof a==="number")return a;else if(typeof a==="string")d.test(a)||b.error("Invalid time format");else b.error("Invalid time format");d=a.split(":");var f=d.length-1,e=d[f];if(e.indexOf(";")>-1){e=e.split(";");var g=0;if(c&&typeof c==="number")g=parseFloat(e[1],10)/c;d[f]=parseInt(e[0],10)+g}if(d.length===1)return parseFloat(d[0],10);else if(d.length===2)return parseInt(d[0],10)*60+parseFloat(d[1],10);else if(d.length===3)return parseInt(d[0],10)*3600+parseInt(d[1],10)*60+parseFloat(d[2],10)}};
r.Popcorn=b;o.addEventListener("DOMContentLoaded",function(){var a=o.querySelectorAll("[data-timeline-sources]");b.forEach(a,function(c,d){var f=a[d],e,g,j;if(!f.id)f.id=b.guid("__popcorn");if(f.nodeType&&f.nodeType===1){j=b("#"+f.id);e=(f.getAttribute("data-timeline-sources")||"").split(",");e[0]&&b.forEach(e,function(k){g=k.split("!");if(g.length===1){g=k.split(".");g[0]="parse"+g[g.length-1].toUpperCase();g[1]=k}e[0]&&j[g[0]]&&j[g[0]](g[1])});j.autoplay&&j.play()}})},false)})(window,window.document);