Station.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <template>
  2. <station-header></station-header>
  3. <song-queue v-if='modals.addSongToQueue'></song-queue>
  4. <queue-sidebar v-if='sidebars.queue'></queue-sidebar>
  5. <playlist-sidebar v-if='sidebars.playlist'></playlist-sidebar>
  6. <users-sidebar v-if='sidebars.users'></users-sidebar>
  7. <div class="station">
  8. <h1 v-if="noSong" class="noSong">No song is currently playing.</h1>
  9. <div class="columns is-mobile" v-if="!noSong">
  10. <div class="column is-8-desktop is-offset-2-desktop is-12-mobile">
  11. <div class="video-container">
  12. <div id="player"></div>
  13. <div class="seeker-bar-container white" id="preview-progress">
  14. <div class="seeker-bar light-blue" style="width: 0%;"></div>
  15. </div>
  16. </div>
  17. </div>
  18. </div>
  19. <div class="columns is-mobile" v-if="!noSong">
  20. <div class="column is-8-desktop is-offset-2-desktop is-12-mobile">
  21. <div class="columns is-mobile">
  22. <div class="column is-8-desktop is-12-mobile">
  23. <h4 id="time-display">{{timeElapsed}} / {{formatTime(currentSong.duration)}}</h4>
  24. <h3>{{currentSong.title}}</h3>
  25. <h4 class="thin" style="margin-left: 0">{{currentSong.artists}}</h4>
  26. <div class="columns is-mobile">
  27. <form style="margin-top: 12px; margin-bottom: 0;" action="#" class="column is-7-desktop is-4-mobile">
  28. <p style="margin-top: 0; position: relative;">
  29. <input type="range" id="volumeSlider" min="0" max="100" class="active" v-on:change="changeVolume()" v-on:input="changeVolume()">
  30. </p>
  31. </form>
  32. <div class="column is-8-mobile is-5-desktop" style="float: right;">
  33. <ul id="ratings" v-if="currentSong.likes !== -1 && currentSong.dislikes !== -1">
  34. <li id="like" class="right" @click="toggleLike()">
  35. <span class="flow-text">{{currentSong.likes}} </span>
  36. <i id="thumbs_up" class="material-icons grey-text" v-bind:class="{liked: liked}">thumb_up</i>
  37. </li>
  38. <li style="margin-right: 10px;" id="dislike" class="right" @click="toggleDislike()">
  39. <span class="flow-text">{{currentSong.dislikes}} </span>
  40. <i id="thumbs_down" class="material-icons grey-text" v-bind:class="{disliked: disliked}">thumb_down</i>
  41. </li>
  42. </ul>
  43. </div>
  44. </div>
  45. </div>
  46. <div class="column is-4-desktop is-12-mobile">
  47. <img class="image" id="song-thumbnail" style="margin-top: 10px !important" :src="currentSong.thumbnail" alt="Song Thumbnail" />
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. </template>
  54. <script>
  55. import { Toast } from 'vue-roaster';
  56. import SongQueue from '../Modals/AddSongToQueue.vue';
  57. import QueueSidebar from '../Sidebars/Queue.vue';
  58. import PlaylistSidebar from '../Sidebars/Playlist.vue';
  59. import UsersSidebar from '../Sidebars/UsersList.vue';
  60. import StationHeader from './StationHeader.vue';
  61. export default {
  62. data() {
  63. return {
  64. playerReady: false,
  65. currentSong: {},
  66. player: undefined,
  67. timePaused: 0,
  68. paused: false,
  69. timeElapsed: "0:00",
  70. interval: 0,
  71. liked: false,
  72. disliked: false,
  73. modals: {
  74. addSongToQueue: false
  75. },
  76. sidebars: {
  77. queue: false,
  78. users: false,
  79. playlist: false
  80. },
  81. noSong: false
  82. }
  83. },
  84. methods: {
  85. toggleModal: function (type) {
  86. if (type == 'addSongToQueue') this.modals.addSongToQueue = !this.modals.addSongToQueue;
  87. },
  88. youtubeReady: function() {
  89. let local = this;
  90. local.player = new YT.Player("player", {
  91. height: 270,
  92. width: 480,
  93. videoId: local.currentSong._id,
  94. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  95. events: {
  96. 'onReady': function(event) {
  97. local.playerReady = true;
  98. let volume = parseInt(localStorage.getItem("volume"));
  99. volume = (typeof volume === "number") ? volume : 20;
  100. local.player.setVolume(volume);
  101. if (volume > 0) local.player.unMute();
  102. local.playVideo();
  103. },
  104. 'onStateChange': function(event) {
  105. if (event.data === 1 && local.videoLoading === true) {
  106. local.videoLoading = false;
  107. local.player.seekTo(local.getTimeElapsed() / 1000, true);
  108. if (local.paused) local.player.pauseVideo();
  109. }
  110. }
  111. }
  112. });
  113. },
  114. getTimeElapsed: function() {
  115. let local = this;
  116. if (local.currentSong) {
  117. return Date.now() - local.startedAt - local.timePaused;
  118. } else {
  119. return 0;
  120. }
  121. },
  122. playVideo: function() {
  123. let local = this;
  124. if (local.playerReady) {
  125. local.videoLoading = true;
  126. local.player.loadVideoById(local.currentSong._id);
  127. if (local.currentSong.artists) local.currentSong.artists = local.currentSong.artists.join(", ");
  128. if (local.interval !== 0) clearInterval(local.interval);
  129. local.interval = setInterval(function () {
  130. local.resizeSeekerbar();
  131. local.calculateTimeElapsed();
  132. }, 250);
  133. }
  134. },
  135. resizeSeekerbar: function() {
  136. let local = this;
  137. if (!local.paused) {
  138. $(".seeker-bar").width(parseInt(((local.getTimeElapsed() / 1000) / local.currentSong.duration * 100)) + "%");
  139. }
  140. },
  141. formatTime: function(duration) {
  142. let d = moment.duration(duration, 'seconds');
  143. return ((d.hours() > 0) ? (d.hours() < 10 ? ("0" + d.hours() + ":") : (d.hours() + ":")) : "") + (d.minutes() + ":") + (d.seconds() < 10 ? ("0" + d.seconds()) : d.seconds());
  144. },
  145. calculateTimeElapsed: function() {
  146. let local = this;
  147. let currentTime = Date.now();
  148. if (local.currentTime !== undefined && local.paused) {
  149. local.timePaused += (Date.now() - local.currentTime);
  150. local.currentTime = undefined;
  151. }
  152. let duration = (Date.now() - local.startedAt - local.timePaused) / 1000;
  153. let songDuration = local.currentSong.duration;
  154. if (songDuration <= duration) local.player.pauseVideo();
  155. if ((!local.paused) && duration <= songDuration) local.timeElapsed = local.formatTime(duration);
  156. },
  157. changeVolume: function() {
  158. let local = this;
  159. let volume = $("#volumeSlider").val();
  160. localStorage.setItem("volume", volume);
  161. if (local.playerReady) {
  162. local.player.setVolume(volume);
  163. if (volume > 0) local.player.unMute();
  164. }
  165. },
  166. resumeLocalStation: function() {
  167. this.paused = false;
  168. if (!this.noSong) {
  169. if (this.playerReady) {
  170. this.player.seekTo(this.getTimeElapsed() / 1000);
  171. this.player.playVideo();
  172. }
  173. }
  174. },
  175. pauseLocalStation: function() {
  176. this.paused = true;
  177. if (!this.noSong) {
  178. if (this.playerReady) this.player.pauseVideo();
  179. }
  180. },
  181. skipStation: function () {
  182. let _this = this;
  183. _this.socket.emit('stations.forceSkip', _this.stationId, data => {
  184. if (data.status !== 'success') {
  185. Toast.methods.addToast(`Error: ${data.message}`, 8000);
  186. } else {
  187. Toast.methods.addToast('Successfully skipped the station\'s current song.', 4000);
  188. }
  189. });
  190. },
  191. resumeStation: function () {
  192. let _this = this;
  193. _this.socket.emit('stations.resume', _this.stationId, data => {
  194. console.log(data);
  195. if (data.status !== 'success') {
  196. Toast.methods.addToast(`Error: ${data.message}`, 8000);
  197. } else {
  198. Toast.methods.addToast('Successfully resumed the station.', 4000);
  199. }
  200. });
  201. },
  202. pauseStation: function () {
  203. let _this = this;
  204. _this.socket.emit('stations.pause', _this.stationId, data => {
  205. console.log(data);
  206. if (data.status !== 'success') {
  207. Toast.methods.addToast(`Error: ${data.message}`, 8000);
  208. } else {
  209. Toast.methods.addToast('Successfully paused the station.', 4000);
  210. }
  211. });
  212. },
  213. toggleLike: function() {
  214. let _this = this;
  215. if (_this.liked) _this.socket.emit('songs.unlike', _this.currentSong._id, data => {
  216. if (data.status !== 'success') {
  217. Toast.methods.addToast(`Error: ${data.message}`, 8000);
  218. }
  219. }); else _this.socket.emit('songs.like', _this.currentSong._id, data => {
  220. if (data.status !== 'success') {
  221. Toast.methods.addToast(`Error: ${data.message}`, 8000);
  222. }
  223. });
  224. },
  225. toggleDislike: function() {
  226. let _this = this;
  227. if (_this.disliked) return _this.socket.emit('songs.undislike', _this.currentSong._id, data => {
  228. if (data.status !== 'success') {
  229. Toast.methods.addToast(`Error: ${data.message}`, 8000);
  230. }
  231. });
  232. _this.socket.emit('songs.dislike', _this.currentSong._id, data => {
  233. if (data.status !== 'success') {
  234. Toast.methods.addToast(`Error: ${data.message}`, 8000);
  235. }
  236. });
  237. }
  238. },
  239. ready: function() {
  240. let _this = this;
  241. _this.stationId = _this.$route.params.id;
  242. _this.interval = 0;
  243. let socketInterval = setInterval(() => {
  244. if (!!_this.$parent.socket) {
  245. _this.socket = _this.$parent.socket;
  246. _this.socket.emit('stations.join', _this.stationId, data => {
  247. if (data.status === "success") {
  248. _this.currentSong = (data.currentSong) ? data.currentSong : {};
  249. _this.startedAt = data.startedAt;
  250. _this.paused = data.paused;
  251. _this.timePaused = data.timePaused;
  252. if (data.currentSong) {
  253. _this.youtubeReady();
  254. _this.playVideo();
  255. _this.socket.emit('songs.getOwnSongRatings', data.currentSong._id, data => {
  256. if (_this.currentSong._id === data.songId) {
  257. _this.liked = data.liked;
  258. _this.disliked = data.disliked;
  259. }
  260. });
  261. } else {
  262. _this.noSong = true;
  263. }
  264. } else {
  265. //TODO Handle error
  266. }
  267. });
  268. _this.socket.on('event:songs.next', data => {
  269. _this.currentSong = (data.currentSong) ? data.currentSong : {};
  270. _this.startedAt = data.startedAt;
  271. _this.paused = data.paused;
  272. _this.timePaused = data.timePaused;
  273. if (data.currentSong) {
  274. if (!_this.playerReady) {
  275. _this.youtubeReady();
  276. }
  277. _this.playVideo();
  278. _this.socket.emit('songs.getOwnSongRatings', data.currentSong._id, (data) => {
  279. console.log(data);
  280. if (_this.currentSong._id === data.songId) {
  281. _this.liked = data.liked;
  282. _this.disliked = data.disliked;
  283. }
  284. });
  285. } else {
  286. _this.noSong = true;
  287. }
  288. });
  289. _this.socket.on('event:stations.pause', data => {
  290. _this.pauseLocalStation();
  291. });
  292. _this.socket.on('event:stations.resume', data => {
  293. _this.timePaused = data.timePaused;
  294. _this.resumeLocalStation();
  295. });
  296. _this.socket.on('event:song.like', data => {
  297. if (!this.noSong) {
  298. if (data.songId === _this.currentSong._id) {
  299. _this.currentSong.likes++;
  300. if (data.undisliked) _this.currentSong.dislikes--;
  301. }
  302. }
  303. });
  304. _this.socket.on('event:song.dislike', data => {
  305. if (!this.noSong) {
  306. if (data.songId === _this.currentSong._id) {
  307. _this.currentSong.dislikes++;
  308. if (data.unliked) _this.currentSong.likes--;
  309. }
  310. }
  311. });
  312. _this.socket.on('event:song.unlike', data => {
  313. if (!this.noSong) {
  314. if (data.songId === _this.currentSong._id) _this.currentSong.likes--;
  315. }
  316. });
  317. _this.socket.on('event:song.undislike', data => {
  318. if (!this.noSong) {
  319. if (data.songId === _this.currentSong._id) _this.currentSong.dislikes--;
  320. }
  321. });
  322. _this.socket.on('event:song.newRatings', data => {
  323. if (!this.noSong) {
  324. if (data.songId === _this.currentSong._id) {
  325. _this.liked = data.liked;
  326. _this.disliked = data.disliked;
  327. }
  328. }
  329. });
  330. clearInterval(socketInterval);
  331. }
  332. }, 100);
  333. let volume = parseInt(localStorage.getItem("volume"));
  334. volume = (typeof volume === "number") ? volume : 20;
  335. $("#volumeSlider").val(volume);
  336. },
  337. components: { StationHeader, SongQueue, QueueSidebar, PlaylistSidebar, UsersSidebar }
  338. }
  339. </script>
  340. <style lang="scss">
  341. .noSong {
  342. color: #03A9F4;
  343. text-align: center;
  344. }
  345. .slideout {
  346. top: 50px;
  347. height: 100%;
  348. position: fixed;
  349. right: 0;
  350. width: 350px;
  351. background-color: white;
  352. box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
  353. .slideout-header {
  354. text-align: center;
  355. background-color: rgb(3, 169, 244) !important;
  356. margin: 0;
  357. padding-top: 5px;
  358. padding-bottom: 7px;
  359. color: white;
  360. }
  361. .slideout-content {
  362. height: 100%;
  363. }
  364. }
  365. .modal-large {
  366. width: 75%;
  367. }
  368. .station {
  369. flex: 1 0 auto;
  370. padding-top: 0.5vw;
  371. transition: all 0.1s;
  372. margin: 0 auto;
  373. max-width: 1280px;
  374. width: 90%;
  375. @media only screen and (min-width: 993px) {
  376. width: 70%;
  377. }
  378. @media only screen and (min-width: 601px) {
  379. width: 85%;
  380. }
  381. input[type=range] {
  382. -webkit-appearance: none;
  383. width: 100%;
  384. margin: 7.3px 0;
  385. }
  386. input[type=range]:focus {
  387. outline: none;
  388. }
  389. input[type=range]::-webkit-slider-runnable-track {
  390. width: 100%;
  391. height: 5.2px;
  392. cursor: pointer;
  393. box-shadow: 0;
  394. background: #c2c0c2;
  395. border-radius: 0;
  396. border: 0;
  397. }
  398. input[type=range]::-webkit-slider-thumb {
  399. box-shadow: 0;
  400. border: 0;
  401. height: 19px;
  402. width: 19px;
  403. border-radius: 15px;
  404. background: #03a9f4;
  405. cursor: pointer;
  406. -webkit-appearance: none;
  407. margin-top: -6.5px;
  408. }
  409. input[type=range]::-moz-range-track {
  410. width: 100%;
  411. height: 5.2px;
  412. cursor: pointer;
  413. box-shadow: 0;
  414. background: #c2c0c2;
  415. border-radius: 0;
  416. border: 0;
  417. }
  418. input[type=range]::-moz-range-thumb {
  419. box-shadow: 0;
  420. border: 0;
  421. height: 19px;
  422. width: 19px;
  423. border-radius: 15px;
  424. background: #03a9f4;
  425. cursor: pointer;
  426. -webkit-appearance: none;
  427. margin-top: -6.5px;
  428. }
  429. input[type=range]::-ms-track {
  430. width: 100%;
  431. height: 5.2px;
  432. cursor: pointer;
  433. box-shadow: 0;
  434. background: #c2c0c2;
  435. border-radius: 1.3px;
  436. }
  437. input[type=range]::-ms-fill-lower {
  438. background: #c2c0c2;
  439. border: 0;
  440. border-radius: 0;
  441. box-shadow: 0;
  442. }
  443. input[type=range]::-ms-fill-upper {
  444. background: #c2c0c2;
  445. border: 0;
  446. border-radius: 0;
  447. box-shadow: 0;
  448. }
  449. input[type=range]::-ms-thumb {
  450. box-shadow: 0;
  451. border: 0;
  452. height: 15px;
  453. width: 15px;
  454. border-radius: 15px;
  455. background: #03a9f4;
  456. cursor: pointer;
  457. -webkit-appearance: none;
  458. margin-top: 1.5px;
  459. }
  460. .video-container {
  461. position: relative;
  462. padding-bottom: 56.25%;
  463. height: 0;
  464. overflow: hidden;
  465. iframe {
  466. position: absolute;
  467. top: 0;
  468. left: 0;
  469. width: 100%;
  470. height: 100%;
  471. pointer-events: none;
  472. }
  473. }
  474. .video-col {
  475. padding-right: 0.75rem;
  476. padding-left: 0.75rem;
  477. }
  478. }
  479. .room-title {
  480. left: 50%;
  481. -webkit-transform: translateX(-50%);
  482. transform: translateX(-50%);
  483. font-size: 2.1em;
  484. }
  485. #ratings {
  486. span {
  487. font-size: 1.68rem;
  488. }
  489. i {
  490. color: #9e9e9e !important;
  491. cursor: pointer;
  492. transition: 0.1s color;
  493. }
  494. }
  495. #time-display {
  496. margin-top: 30px;
  497. float: right;
  498. }
  499. #thumbs_up:hover, #thumbs_up.liked {
  500. color: #87D37C !important;
  501. }
  502. #thumbs_down:hover, #thumbs_down.disliked {
  503. color: #EC644B !important;
  504. }
  505. #song-thumbnail {
  506. max-width: 100%;
  507. width: 85%;
  508. }
  509. .seeker-bar-container {
  510. position: relative;
  511. height: 5px;
  512. display: block;
  513. width: 100%;
  514. overflow: hidden;
  515. }
  516. .seeker-bar {
  517. top: 0;
  518. left: 0;
  519. bottom: 0;
  520. position: absolute;
  521. }
  522. ul {
  523. list-style: none;
  524. margin: 0;
  525. display: block;
  526. }
  527. h1, h2, h3, h4, h5, h6 {
  528. font-weight: 400;
  529. line-height: 1.1;
  530. }
  531. h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {
  532. font-weight: inherit;
  533. }
  534. h1 {
  535. font-size: 4.2rem;
  536. line-height: 110%;
  537. margin: 2.1rem 0 1.68rem 0;
  538. }
  539. h2 {
  540. font-size: 3.56rem;
  541. line-height: 110%;
  542. margin: 1.78rem 0 1.424rem 0;
  543. }
  544. h3 {
  545. font-size: 2.92rem;
  546. line-height: 110%;
  547. margin: 1.46rem 0 1.168rem 0;
  548. }
  549. h4 {
  550. font-size: 2.28rem;
  551. line-height: 110%;
  552. margin: 1.14rem 0 0.912rem 0;
  553. }
  554. h5 {
  555. font-size: 1.64rem;
  556. line-height: 110%;
  557. margin: 0.82rem 0 0.656rem 0;
  558. }
  559. h6 {
  560. font-size: 1rem;
  561. line-height: 110%;
  562. margin: 0.5rem 0 0.4rem 0;
  563. }
  564. .thin {
  565. font-weight: 200;
  566. }
  567. .left {
  568. float: left !important;
  569. }
  570. .right {
  571. float: right !important;
  572. }
  573. .light-blue {
  574. background-color: #03a9f4 !important;
  575. }
  576. .white {
  577. background-color: #FFFFFF !important;
  578. }
  579. .btn-search {
  580. font-size: 14px;
  581. }
  582. </style>