Station.vue 17 KB

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