Station.vue 16 KB

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