Station.vue 16 KB

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