Station.vue 12 KB

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