From ddf4d45b76f61d2df75736b8e8fb3e89f25dd60c Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 14 Sep 2020 13:42:06 +0200 Subject: [PATCH] Grid view fix for Safari --- dist/spotify-card.js | 4 ++-- src/const.ts | 2 +- src/spotify-card.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/spotify-card.js b/dist/spotify-card.js index 887e956..de152a1 100644 --- a/dist/spotify-card.js +++ b/dist/spotify-card.js @@ -438,7 +438,7 @@ found at http://polymer.github.io/PATENTS.txt paper-slider { width: auto; } - `}};e([J({type:Object})],je.prototype,"hass",void 0),e([K()],je.prototype,"config",void 0),e([K()],je.prototype,"_toggle",void 0),e([K()],je.prototype,"chromecast_devices",void 0),je=e([B("spotify-card-editor")],je);class Re{constructor(e){this.state_ttl=4e3,this.last_state_update_time=0,this.loading=!1,this.parent=e}is_loading(){return setTimeout(this.set_loading_off,100),this.loading}set_loading_off(){this.loading=!1}is_loaded(){return void 0!==this.parent.playlists.length}getPlaybackOptions(e){return{uri:e,force_playback:"playing"==this.parent.getSpotifyEntityState(),random_song:this.parent.config.always_play_random_song||!1,account:this.parent.config.account}}playUri(e){const t=this.getCurrentPlayer();if(!t){const t=this.parent.config.default_device;if(t){const i=this.parent.devices.filter(e=>e.name==t);if(i.length>0)return this.playUriOnConnectDevice(i[0].id,e);{const i=this.parent.chromecast_devices.filter(e=>e.friendly_name==t);if(i.length>0)return this.playUriOnCastDevice(i[0].friendly_name,e);throw new Error("Could not find default_device: "+t)}}throw new Error("No active device nor default device in settings")}this.playUriOnConnectDevice(t.id,e)}transferPlaybackToCastDevice(e){this.parent.hass.callService("spotcast","start",{device_name:e,force_playback:!0,account:this.parent.config.account})}transferPlaybackToConnectDevice(e){this.parent.hass.callService("spotcast","start",{spotify_device_id:e,force_playback:!0,account:this.parent.config.account})}playUriOnCastDevice(e,t){const i=Object.assign(Object.assign({},this.getPlaybackOptions(t)),{device_name:e});this.parent.hass.callService("spotcast","start",i)}playUriOnConnectDevice(e,t){const i=Object.assign(Object.assign({},this.getPlaybackOptions(t)),{spotify_device_id:e});this.parent.hass.callService("spotcast","start",i)}async updateState(){if(!((new Date).getTime()-this.last_state_update_time{let t=[];function i(i,s){e=s?i:Object.assign(Object.assign({},e),i);let n=t;for(let t=0;t(t.push(e),()=>{!function(e){let i=[];for(let s=0;s{if(e[t])return e[t];let n,a=0,r=Fe();const o=()=>i(e).then(e=>r.setState(e,!0)),l=()=>o().catch(t=>{if(e.connected)throw t});return e[t]={get state(){return r.state},refresh:o,subscribe(t){a++,1===a&&(s&&(n=s(e,r)),e.addEventListener("ready",l),l());const i=r.subscribe(t);return void 0!==r.state&&setTimeout(()=>t(r.state),0),()=>{i(),a--,a||(n&&n.then(e=>{e()}),e.removeEventListener("ready",o))}}},e[t]},Ie=e=>e.sendMessagePromise({type:"get_states"});function We(e,t){if(void 0===e)return null;const{domain:i,service:s}=t.data;return{[i]:Object.assign({},e[i],{[s]:{description:"",fields:{}}})}}function qe(e,t){if(void 0===e)return null;const{domain:i,service:s}=t.data,n=e[i];if(!n||!(s in n))return null;const a={};return Object.keys(n).forEach(e=>{e!==s&&(a[e]=n[e])}),{[i]:a}}const Ge=e=>e.sendMessagePromise({type:"get_services"}),Be=(e,t)=>Promise.all([e.subscribeEvents(t.action(We),"service_registered"),e.subscribeEvents(t.action(qe),"service_removed")]).then(e=>()=>e.forEach(e=>e()));async function Ze(e){const t=await Ie(e),i={};for(let e=0;ee.subscribeEvents(e=>function(e,t){const i=e.state;if(void 0===i)return;const{entity_id:s,new_state:n}=t.data;if(n)e.setState({[n.entity_id]:n});else{const t=Object.assign({},i);delete t[s],e.setState(t,!0)}}(t,e),"state_changed"),Ke=(e,t)=>(e=>Ye(e,"_ent",Ze,Je))(e).subscribe(t);var Qe;function Xe(e,t){if(!t||!oe(t)&&!oe(e)&&e.length!=t.length)return!0;for(const i in e)if(e[i].id!=t[i].id)return!0;return!1}function et(e,t){return!t||(e.state!=t.state||e.attributes.shuffle!=t.attributes.shuffle)}console.info(`%c SPOTIFY-CARD \n%c ${Le("common.version")} 2.2.2 `,"color: orange; font-weight: bold; background: black","color: white; font-weight: bold; background: dimgray"),window.customCards=window.customCards||[],window.customCards.push({type:"spotify-card",name:"Spotify Card",description:Le("common.description"),preview:!0});let tt=Qe=class extends se{constructor(){super(),this.playlists=[],this.devices=[],this.chromecast_devices=[],this._spotify_installed=!1,this._fetch_time_out=0,this.spotcast_connector=new Re(this)}static async getConfigElement(){return document.createElement("spotify-card-editor")}static getStubConfig(){return{}}setConfig(e){let t="";if(e.playlist_type&&!Object.values(ae).includes(e.playlist_type.toLowerCase())&&(t="playlist_type"),e.display_style&&!Object.values(ne).includes(e.display_style.toLowerCase())&&(t="display_style"),e.show_error||""!=t)throw new Error(Le("common.invalid_configuration")+": "+t);this.config=e}connectedCallback(){super.connectedCallback(),this.doSubscribeEntities(),this.updateSpotcast()}doSubscribeEntities(){var e;(null===(e=this.hass)||void 0===e?void 0:e.connection)&&!this._unsubscribe_entitites&&this.isConnected&&(this._unsubscribe_entitites=Ke(this.hass.connection,e=>this.entitiesUpdated(e)))}entitiesUpdated(e){let t=!1;for(const i in e)i.startsWith("media_player")&&((i.startsWith("media_player.spotify")||i==this.config.spotify_entity)&&(this._spotify_installed=!0,this._spotify_state=e[i]),t=!0);t&&!document.hidden&&this.updateSpotcast()}updateSpotcast(){this._fetch_time_out&&clearTimeout(this._fetch_time_out),this._fetch_time_out=setTimeout(async()=>{this.hass&&this.isSpotcastInstalled()&&!this.spotcast_connector.is_loading()&&(await this.spotcast_connector.updateState(),await this.spotcast_connector.fetchPlaylists())},500)}disconnectedCallback(){super.disconnectedCallback(),this._unsubscribe_entitites&&(this._unsubscribe_entitites(),this._unsubscribe_entitites=void 0)}updated(e){super.updated(e),this.updateComplete.then(()=>{for(const e of this.renderRoot.querySelectorAll("[data-spotify-image-url]")){const t=new Image;t.onload=function(t){var i;null===(i=e.firstElementChild)||void 0===i||i.replaceWith(t.srcElement)},e.dataset.spotifyImageUrl&&(t.src=e.dataset.spotifyImageUrl)}})}getDisplayStyle(){var e;return"grid"==(null===(e=this.config.display_style)||void 0===e?void 0:e.toLowerCase())?ne.Grid:ne.List}getPlayingState(){var e,t;return null!==(t="playing"==(null===(e=this._spotify_state)||void 0===e?void 0:e.state))&&void 0!==t&&t}getShuffleState(){var e,t;return null!==(t=null===(e=this.player)||void 0===e?void 0:e.shuffle_state)&&void 0!==t&&t}getSpotifyEntityState(){return this._spotify_state?this._spotify_state.state:""}isSpotcastInstalled(){var e,t;return!(!(null===(e=this.hass)||void 0===e?void 0:e.connection)||void 0===(t=this.hass.connection,Ye(t,"_srv",Ge,Be)).state.spotcast)}checkIfAllowedToShow(e){var t,i;const s=null!==(i=null===(t=this.config.filter_devices)||void 0===t?void 0:t.map(e=>new RegExp(e+"$")))&&void 0!==i?i:[];for(const t of s)if(t.test("name"in e?e.name:e.friendly_name))return!1;return!0}getDefaultDevice(){let[e,t]=this.getFilteredDevices();if(e=e.filter(e=>e.name==this.config.default_device),t=t.filter(e=>e.friendly_name==this.config.default_device),e.length>0||t.length>0)return this.config.default_device}getFilteredDevices(){return[this.devices.filter(this.checkIfAllowedToShow,this),this.chromecast_devices.filter(this.checkIfAllowedToShow,this)]}getPlaylists(){return this.playlists}isThisPlaylistPlaying(e){var t;return(null===(t=this._spotify_state)||void 0===t?void 0:t.attributes.media_playlist)===e.name}startUri(e,t){const i="loading",s=e.srcElement;"div"==(null==s?void 0:s.localName)?s.children[1].classList.add(i):"svg"==(null==s?void 0:s.localName)?s.parentElement.classList.add(i):"path"==(null==s?void 0:s.localName)?s.parentElement.parentElement.classList.add(i):"img"==(null==s?void 0:s.localName)?s.nextElementSibling.classList.add(i):"p"==(null==s?void 0:s.localName)?s.parentElement.children[1].classList.add(i):console.log(s),this.spotcast_connector.playUri(t)}onShuffleSelect(){var e,t;"playing"==(null===(e=this._spotify_state)||void 0===e?void 0:e.state)&&this.hass.callService("media_player","shuffle_set",{entity_id:this._spotify_state.entity_id,shuffle:!(null===(t=this.player)||void 0===t?void 0:t.shuffle_state)})}handlePlayPauseEvent(e,t){e.stopPropagation(),this._spotify_state&&this.hass.callService("media_player",t,{entity_id:this._spotify_state.entity_id})}spotifyDeviceSelected(e){if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToConnectDevice(e.id);const t=this.playlists[0];console.log("spotifyDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnConnectDevice(e.id,t.uri)}chromecastDeviceSelected(e){if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToCastDevice(e.friendly_name);const t=this.playlists[0];console.log("chromecastDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnCastDevice(e.friendly_name,t.uri)}getCurrentPlayer(){return this.spotcast_connector.getCurrentPlayer()}render(){var e,t,i;let s=H``;this.isSpotcastInstalled()||(s=this.showWarning(Le("common.show_missing_spotcast")));let n=H`
Loading...
`;if(this.spotcast_connector.is_loaded())switch(this.getDisplayStyle()){case ne.Grid:n=this.generateGridView();break;default:n=this.generateListView()}const a=H`