aircox-radiocampus/aircox/static/aircox/streamer.js
2020-05-26 21:27:58 +02:00

1 line
6.0 KiB
JavaScript

!function(t){function e(e){for(var r,a,u=e[0],o=e[1],c=e[2],h=0,d=[];h<u.length;h++)a=u[h],Object.prototype.hasOwnProperty.call(s,a)&&s[a]&&d.push(s[a][0]),s[a]=0;for(r in o)Object.prototype.hasOwnProperty.call(o,r)&&(t[r]=o[r]);for(l&&l(e);d.length;)d.shift()();return i.push.apply(i,c||[]),n()}function n(){for(var t,e=0;e<i.length;e++){for(var n=i[e],r=!0,u=1;u<n.length;u++){var o=n[u];0!==s[o]&&(r=!1)}r&&(i.splice(e--,1),t=a(a.s=n[0]))}return t}var r={},s={3:0},i=[];function a(e){if(r[e])return r[e].exports;var n=r[e]={i:e,l:!1,exports:{}};return t[e].call(n.exports,n,n.exports,a),n.l=!0,n.exports}a.m=t,a.c=r,a.d=function(t,e,n){a.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},a.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},a.t=function(t,e){if(1&e&&(t=a(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(a.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)a.d(n,r,function(e){return t[e]}.bind(null,r));return n},a.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return a.d(e,"a",e),e},a.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},a.p="";var u=window.webpackJsonp=window.webpackJsonp||[],o=u.push.bind(u);u.push=e,u=u.slice();for(var c=0;c<u.length;c++)e(u[c]);var l=o;i.push([28,0]),n()}({1:function(t,e,n){"use strict";function r(t,...e){return setTimeout((...e)=>{!document.hidden&&t(...e)},...e)}function s(t,...e){return setInterval((...e)=>{!document.hidden&&t(...e)},...e)}n.d(e,"b",(function(){return r})),n.d(e,"a",(function(){return s}))},28:function(t,e,n){"use strict";n.r(e);var r=n(0),s=n(12),i=n.n(s);n(3);var a=null;function u(){return null===a&&(a=function(t){if(document.cookie&&""!==document.cookie){const e=document.cookie.split(";").find(e=>e.trim().startsWith(t+"="));return e?decodeURIComponent(e.split("=")[1]):null}return null}("csrftoken")),a}class o{constructor(t,{url:e=null}={}){this.commit(t)}static getId(t){return t.id}static getOptions(t){return{headers:{"Content-Type":"application/json",Accept:"application/json","X-CSRFToken":u()},...t}}static fetch(t,e=null,n=null){return e=this.getOptions(e),fetch(t,e).then(t=>t.json()).then(e=>new this(d,{url:t,...n}))}static fetchAll(t,e=null,n=null){return e=this.getOptions(e),fetch(t,e).then(t=>t.json()).then(e=>(e instanceof Array||(e=e.results),e=e.map(e=>new this(e,{baseUrl:t,...n}))))}fetch(t){return t=this.constructor.getOptions(t),fetch(this.url,t).then(t=>t.json()).then(t=>this.commit(t))}action(t,e,n=!1){e=this.constructor.getOptions(e);const r=fetch(this.url+t,e);return n?r.then(t=>t.json()).then(t=>{this.commit(t),this.data}):r}commit(t){this.id=this.constructor.getId(t),this.url=t.url_,r.a.set(this,"data",t)}asChild(t,e,n=""){return new t(e,{baseUrl:`${this.url}${n}/`})}getChildOf(t,e){const n=this.data[t].findIndex(t=>t.id=e);return-1==n?null:this.data[t][n]}static updateList(t=[],e=[],...n){return t.reduce((t,r)=>{const s=this.getId(r);let[i,a]=[e.findIndex(t=>t.id==s),null];return-1!=i?(e[i].commit(r),t.push(e[i])):t.push(new this(r,...n)),t},[])}}class c extends o{get name(){return this.data.name}static getId(t){return t.pk}}var l=n(1);class h extends o{get queues(){return this.data?this.data.queues:[]}get playlists(){return this.data?this.data.playlists:[]}get sources(){return[...this.queues,...this.playlists]}get source(){return this.sources.find(t=>t.id==this.data.source)}commit(t){this.data||(this.data={id:t.id,playlists:[],queues:[]}),t.playlists=f.updateList(t.playlists,this.playlists,{streamer:this}),t.queues=g.updateList(t.queues,this.queues,{streamer:this}),super.commit(t)}}class p extends o{static getId(t){return t.rid}}class m extends o{constructor(t,{streamer:e=null,...n}={}){super(t,n),this.streamer=e,Object(l.a)(()=>this.tick(),1e3)}get isQueue(){return!1}get isPlaylist(){return!1}get isPlaying(){return"playing"==this.data.status}get isPaused(){return"paused"==this.data.status}get remainingString(){if(!this.remaining)return"00:00";const t=Math.floor(this.remaining%60),e=Math.floor(this.remaining/60);return String(e).padStart(2,"0")+":"+String(t).padStart(2,"0")}sync(){return this.action("sync/",{method:"POST"},!0)}skip(){return this.action("skip/",{method:"POST"},!0)}restart(){return this.action("restart/",{method:"POST"},!0)}seek(t){return this.action("seek/",{method:"POST",body:JSON.stringify({count:t})},!0)}tick(){if(!this.data.remaining||!this.isPlaying)return;const t=(Date.now()-this.commitDate)/1e3;r.a.set(this,"remaining",this.data.remaining-t)}commit(t){t.air_time&&(t.air_time=new Date(t.air_time)),this.commitDate=Date.now(),super.commit(t),r.a.set(this,"remaining",t.remaining)}}class f extends m{get isPlaylist(){return!0}}class g extends m{get isQueue(){return!0}get queue(){return this.data&&this.data.queue}commit(t){t.queue=p.updateList(t.queue,this.queue),super.commit(t)}push(t){return this.action("push/",{method:"POST",body:JSON.stringify({sound_id:parseInt(t)})},!0)}}r.a.use(i.a),window.aircox.appConfig={data:()=>({streamer:null,streamers:[],fetchInterval:null,Sound:c}),computed:{apiUrl(){return this.$el&&this.$el.dataset.apiUrl},sources(){return(this.streamer?this.streamer.sources:[]).filter(t=>t.data)}},methods:{fetchStreamers(){h.fetchAll(this.apiUrl,null).then(t=>{r.a.set(this,"streamers",t),r.a.set(this,"streamer",t?t[0]:null)})}},mounted(){this.fetchStreamers(),this.fetchInterval=Object(l.a)(()=>this.streamer&&this.streamer.fetch(),5e3)},destroyed(){null!==this.fetchInterval&&clearInterval(this.fetchInterval)}}},3:function(t,e,n){"use strict";n.d(e,"a",(function(){return a}));var r=n(0);const s={el:"#app",delimiters:["[[","]]"]};var i={};function a(t=null){return new Promise((function(e,n){window.addEventListener("load",(function(){try{t=function(t){return t instanceof Function&&(t=t()),t=null==t?i:t,{...s,...t}}(t);if(!document.querySelector(t.el))return void n("Error: missing element "+t.el);e(new r.a(t))}catch(t){n(t)}}))}))}}});