Station.vue 16 KB

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