Station.vue 16 KB

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