Station.vue 13 KB

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