-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathavp_client.min.js
1 lines (1 loc) · 3.55 KB
/
avp_client.min.js
1
function ApiVideoPlayer(t,e){return this.element="string"==typeof t?document.getElementById(t):t,this.url=avp.client.findUrl(this.element),this.isInitialized=!1,this.emit=function(t){"loaded"===t.data.type&&this.init();var e=document.createEvent("Event");for(key in e.initEvent(t.data.type,!0,!0),e.data=t.data,this.element.dispatchEvent(e),t.data.values)avp.client.api.setAttribute(this,key,t.data.values[key])},this.exec=function(t){this.isInitialized?this.element.contentWindow.postMessage(t,"*"):avp.client.queue.add(t)},this.init=function(){this.isInitialized=!0,avp.client.listener.init(this),avp.client.api.init(this)},avp.client.api.setMethods(this),avp.client.api.setDefaultAttributes(this),avp.client.registry.add(this),"function"==typeof e&&e(this.element),this.element}var avp=avp||{};avp.client={findUrl:function(t){var e=null;return t instanceof ApiVideoPlayer?e=t.url:avp.client.listener.isValidEvent(t)?e=t.data.url:avp.client.api.isValidCommand(t)?e=t.url:void 0!==t.src&&(e=t.src),null===e?null:(e=e.split("//")).pop()}},avp.client.api={methods:["play","pause","toggle","seek","mute","setVolume","chapter","subtitle"],attributes:["volume","currentTime","duration"],setMethods:function(l){for(var t=0;t<this.methods.length;t++)!function(e,t){var i=e.element;if(void 0===i[t]){var n=t;t.match(/^set[A-Z]/)&&(n=t.substr(3).toLowerCase()),i[t]=function(t){e.exec({url:avp.client.findUrl(l),type:n,value:void 0!==t?t:null})}}}(l,this.methods[t])},setDefaultAttributes:function(t){for(var e=0;e<this.attributes.length;e++)this.setAttribute(t,this.attributes[e],0)},setAttribute:function(t,e,i){if(this.isAttributeSupported(e)){var n=t.element;n[e]=i}},isAttributeSupported:function(t){for(var e=0;e<this.attributes.length;e++)if(t==this.attributes[e])return!0;return!1},init:function(t){avp.client.queue.consume(t,"command",function(t){avp.client.registry.get(avp.client.findUrl(t)).exec(t)})},isValidCommand:function(t){return!(null===t||void 0===t.url||void 0===t.type||void 0===t.value)}},avp.client.listener={start:function(){window.addEventListener("message",this.listen)},stop:function(){window.removeEventListener("message",this.listen)},listen:function(t){if(avp.client.listener.isValidEvent(t)){var e=avp.client.findUrl(t);avp.client.registry.has(e)?avp.client.registry.get(e).emit(t):avp.client.queue.add(t)}},init:function(t){avp.client.queue.consume(t,"event",function(t){avp.client.registry.get(avp.client.findUrl(t)).emit(t)})},isValidEvent:function(t){return!(null===t||void 0===t.data||void 0===t.data.url||void 0===t.data.type)}},avp.client.listener.start(),avp.client.queue={queue:{},add:function(t){var e=this.findType(t);void 0===this.queue[e]&&(this.queue[e]=[]);var i=avp.client.findUrl(t);void 0===this.queue[e][i]&&(this.queue[e][i]=[]),this.queue[e][i].push(t)},list:function(t,e){if(void 0===this.queue[e])return[];var i=avp.client.findUrl(t);return void 0===this.queue[e][i]?[]:this.queue[e][i]},count:function(t,e){return this.list(t,e).length},isEmpty:function(t,e){return this.count(t,e)<=0},fetch:function(t,e){return this.queue[e][avp.client.findUrl(t)].shift()},consume:function(t,e,i){for(;!this.isEmpty(t,e);){var n=this.fetch(t,e);i(n)}},findType:function(t){return avp.client.listener.isValidEvent(t)?"event":avp.client.api.isValidCommand(t)?"command":"unknown"}},avp.client.registry={players:[],add:function(t){this.players.push(t)},has:function(t){for(var e=0;e<this.players.length;e++)if(t==avp.client.findUrl(this.players[e]))return!0;return!1},get:function(t){for(var e=0;e<this.players.length;e++)if(t==avp.client.findUrl(this.players[e]))return this.players[e];return null}};