EditSong.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <template>
  2. <div>
  3. <modal title='Edit Song'>
  4. <div slot='body'>
  5. <h5 class='has-text-centered'>Video Preview</h5>
  6. <div class='video-container'>
  7. <div id='player'></div>
  8. <div class="controls">
  9. <form action="#">
  10. <p style="margin-top: 0; position: relative;">
  11. <input type="range" id="volumeSlider" min="0" max="100" class="active" v-on:change="changeVolume()" v-on:input="changeVolume()">
  12. </p>
  13. </form>
  14. <p class='control has-addons'>
  15. <button class='button' @click='settings("pause")' v-if='!video.paused'>
  16. <i class='material-icons'>pause</i>
  17. </button>
  18. <button class='button' @click='settings("play")' v-if='video.paused'>
  19. <i class='material-icons'>play_arrow</i>
  20. </button>
  21. <button class='button' @click='settings("stop")'>
  22. <i class='material-icons'>stop</i>
  23. </button>
  24. <button class='button' @click='settings("skipToLast10Secs")'>
  25. <i class='material-icons'>fast_forward</i>
  26. </button>
  27. </p>
  28. </div>
  29. </div>
  30. <h5 class='has-text-centered'>Thumbnail Preview</h5>
  31. <img class='thumbnail-preview' :src='editing.song.thumbnail' onerror="this.src='/assets/notes-transparent.png'">
  32. <div class="control is-horizontal">
  33. <div class="control-label">
  34. <label class="label">Thumbnail URL</label>
  35. </div>
  36. <div class="control">
  37. <input class='input' type='text' v-model='editing.song.thumbnail'>
  38. </div>
  39. </div>
  40. <h5 class='has-text-centered'>Edit Information</h5>
  41. <p class='control'>
  42. <label class='checkbox'>
  43. <input type='checkbox' v-model='editing.song.explicit'>
  44. Explicit
  45. </label>
  46. </p>
  47. <label class='label'>Song ID & Title</label>
  48. <div class="control is-horizontal">
  49. <div class="control is-grouped">
  50. <p class='control is-expanded'>
  51. <input class='input' type='text' v-model='editing.song.songId'>
  52. </p>
  53. <p class='control is-expanded'>
  54. <input class='input' type='text' v-model='editing.song.title' autofocus>
  55. </p>
  56. </div>
  57. </div>
  58. <label class='label'>Artists & Genres</label>
  59. <div class='control is-horizontal'>
  60. <div class='control is-grouped artist-genres'>
  61. <div>
  62. <p class='control has-addons'>
  63. <input class='input' id='new-artist' type='text' placeholder='Artist'>
  64. <button class='button is-info' @click='addTag("artists")'>Add Artist</button>
  65. </p>
  66. <span class='tag is-info' v-for='(index, artist) in editing.song.artists' track-by='$index'>
  67. {{ artist }}
  68. <button class='delete is-info' @click='removeTag("artists", index)'></button>
  69. </span>
  70. </div>
  71. <div>
  72. <p class='control has-addons'>
  73. <input class='input' id='new-genre' type='text' placeholder='Genre'>
  74. <button class='button is-info' @click='addTag("genres")'>Add Genre</button>
  75. </p>
  76. <span class='tag is-info' v-for='(index, genre) in editing.song.genres' track-by='$index'>
  77. {{ genre }}
  78. <button class='delete is-info' @click='removeTag("genres", index)'></button>
  79. </span>
  80. </div>
  81. </div>
  82. </div>
  83. <label class='label'>Song Duration</label>
  84. <p class='control'>
  85. <input class='input' type='text' v-model='editing.song.duration'>
  86. </p>
  87. <label class='label'>Skip Duration</label>
  88. <p class='control'>
  89. <input class='input' type='text' v-model='editing.song.skipDuration'>
  90. </p>
  91. <article class="message">
  92. <div class="message-body">
  93. <span class="reports-length">
  94. {{ reports.length }}
  95. <span v-if="reports.length > 1 || reports.length <= 0">&nbsp;Reports</span>
  96. <span v-else>&nbsp;Report</span>
  97. </span>
  98. <div v-for='report in reports'>
  99. <a :href='`/admin/reports?id=${report}`' class='report-link'>Report - {{ report }}</a>
  100. </div>
  101. </div>
  102. </article>
  103. <hr />
  104. <h5 class='has-text-centered'>Spotify Information</h5>
  105. <label class='label'>Song title</label>
  106. <p class='control'>
  107. <input class='input' type='text' v-model='spotify.title'>
  108. </p>
  109. <label class='label'>Song artist (1 artist full name)</label>
  110. <p class='control'>
  111. <input class='input' type='text' v-model='spotify.artist'>
  112. </p>
  113. <button class='button is-success' @click='getSpotifySongs()'>
  114. Get Spotify songs
  115. </button>
  116. <hr />
  117. <article class="media" v-for='song in spotify.songs'>
  118. <figure class="media-left">
  119. <p class="image is-64x64">
  120. <img :src="song.thumbnail" onerror="this.src='/assets/notes-transparent.png'">
  121. </p>
  122. </figure>
  123. <div class="media-content">
  124. <div class="content">
  125. <p>
  126. <strong>{{song.title}}</strong>
  127. <br />
  128. <small>Artists: {{song.artists}}</small>, <small>Duration: {{song.duration}}</small>, <small>Explicit: {{song.explicit}}</small>
  129. <br />
  130. <small>Thumbnail: {{song.thumbnail}}</small>
  131. </p>
  132. </div>
  133. </div>
  134. </article>
  135. </div>
  136. <div slot='footer'>
  137. <button class='button is-success' @click='save(editing.song, false)'>
  138. <i class='material-icons save-changes'>done</i>
  139. <span>&nbsp;Save</span>
  140. </button>
  141. <button class='button is-success' @click='save(editing.song, true)'>
  142. <i class='material-icons save-changes'>done</i>
  143. <span>&nbsp;Save and close</span>
  144. </button>
  145. <button class='button is-danger' @click='$parent.toggleModal()'>
  146. <span>&nbsp;Close</span>
  147. </button>
  148. </div>
  149. </modal>
  150. </div>
  151. </template>
  152. <script>
  153. import io from '../../io';
  154. import { Toast } from 'vue-roaster';
  155. import Modal from './Modal.vue';
  156. export default {
  157. components: { Modal },
  158. data() {
  159. return {
  160. editing: {
  161. index: 0,
  162. song: {},
  163. type: ''
  164. },
  165. reports: 0,
  166. video: {
  167. player: null,
  168. paused: false,
  169. playerReady: false
  170. },
  171. spotify: {
  172. title: '',
  173. artist: '',
  174. songs: []
  175. }
  176. }
  177. },
  178. methods: {
  179. save: function (song, close) {
  180. let _this = this;
  181. this.socket.emit(`${_this.editing.type}.update`, song._id, song, res => {
  182. Toast.methods.addToast(res.message, 4000);
  183. if (res.status === 'success') {
  184. _this.$parent.songs.forEach(lSong => {
  185. if (song._id === lSong._id) {
  186. for (let n in song) {
  187. lSong[n] = song[n];
  188. }
  189. }
  190. });
  191. }
  192. if (close) _this.$parent.toggleModal();
  193. });
  194. },
  195. settings: function (type) {
  196. let _this = this;
  197. switch(type) {
  198. case 'stop':
  199. _this.video.player.stopVideo();
  200. _this.video.paused = true;
  201. break;
  202. case 'pause':
  203. _this.video.player.pauseVideo();
  204. _this.video.paused = true;
  205. break;
  206. case 'play':
  207. _this.video.player.playVideo();
  208. _this.video.paused = false;
  209. break;
  210. case 'skipToLast10Secs':
  211. _this.video.player.seekTo((_this.editing.song.duration - 10) + _this.editing.song.skipDuration);
  212. break;
  213. }
  214. },
  215. changeVolume: function () {
  216. let local = this;
  217. let volume = $("#volumeSlider").val();
  218. localStorage.setItem("volume", volume);
  219. local.video.player.setVolume(volume);
  220. if (volume > 0) local.video.player.unMute();
  221. },
  222. addTag: function (type) {
  223. if (type == 'genres') {
  224. let genre = $('#new-genre').val().toLowerCase().trim();
  225. if (this.editing.song.genres.indexOf(genre) !== -1) return Toast.methods.addToast('Genre already exists', 3000);
  226. if (genre) {
  227. this.editing.song.genres.push(genre);
  228. $('#new-genre').val('');
  229. } else Toast.methods.addToast('Genre cannot be empty', 3000);
  230. } else if (type == 'artists') {
  231. let artist = $('#new-artist').val();
  232. if (this.editing.song.artists.indexOf(artist) !== -1) return Toast.methods.addToast('Artist already exists', 3000);
  233. if ($('#new-artist').val() !== '') {
  234. this.editing.song.artists.push(artist);
  235. $('#new-artist').val('');
  236. } else Toast.methods.addToast('Artist cannot be empty', 3000);
  237. }
  238. },
  239. removeTag: function (type, index) {
  240. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  241. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  242. },
  243. getSpotifySongs: function() {
  244. this.socket.emit('apis.getSpotifySongs', this.spotify.title, this.spotify.artist, (res) => {
  245. if (res.status === 'success') {
  246. this.spotify.songs = res.songs;
  247. }
  248. });
  249. }
  250. },
  251. ready: function () {
  252. let _this = this;
  253. io.getSocket(socket => {
  254. _this.socket = socket;
  255. });
  256. setInterval(() => {
  257. if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
  258. _this.video.paused = false;
  259. _this.video.player.stopVideo();
  260. }
  261. }, 200);
  262. this.video.player = new YT.Player('player', {
  263. height: 315,
  264. width: 560,
  265. videoId: this.editing.song.songId,
  266. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  267. startSeconds: _this.editing.song.skipDuration,
  268. events: {
  269. 'onReady': () => {
  270. let volume = parseInt(localStorage.getItem("volume"));
  271. volume = (typeof volume === "number") ? volume : 20;
  272. _this.video.player.seekTo(_this.editing.song.skipDuration);
  273. _this.video.player.setVolume(volume);
  274. if (volume > 0) _this.video.player.unMute();
  275. _this.playerReady = true;
  276. },
  277. 'onStateChange': event => {
  278. if (event.data === 1) {
  279. _this.video.paused = false;
  280. let youtubeDuration = _this.video.player.getDuration();
  281. youtubeDuration -= _this.editing.song.skipDuration;
  282. if (_this.editing.song.duration > youtubeDuration) {
  283. this.video.player.stopVideo();
  284. _this.video.paused = true;
  285. Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
  286. } else if (_this.editing.song.duration <= 0) {
  287. this.video.player.stopVideo();
  288. _this.video.paused = true;
  289. Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
  290. }
  291. if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
  292. _this.video.player.seekTo(10);
  293. }
  294. } else if (event.data === 2) {
  295. this.video.paused = true;
  296. }
  297. }
  298. }
  299. });
  300. let volume = parseInt(localStorage.getItem("volume"));
  301. volume = (typeof volume === "number") ? volume : 20;
  302. $("#volumeSlider").val(volume);
  303. },
  304. events: {
  305. closeModal: function () {
  306. this.$parent.modals.editSong = false;
  307. this.video.player.stopVideo();
  308. },
  309. editSong: function (song, index, type) {
  310. let _this = this;
  311. this.video.player.loadVideoById(song.songId, this.editing.song.skipDuration);
  312. let newSong = {};
  313. for (let n in song) {
  314. newSong[n] = song[n];
  315. }
  316. this.editing = {
  317. index,
  318. song: newSong,
  319. type
  320. };
  321. _this.socket.emit('reports.getReportsForSong', song.songId, res => {
  322. if (res.status === 'success') _this.reports = res.data;
  323. });
  324. this.$parent.toggleModal();
  325. },
  326. stopVideo: function () {
  327. this.video.player.stopVideo();
  328. }
  329. }
  330. }
  331. </script>
  332. <style type='scss' scoped>
  333. input[type=range] {
  334. -webkit-appearance: none;
  335. width: 100%;
  336. margin: 7.3px 0;
  337. }
  338. input[type=range]:focus {
  339. outline: none;
  340. }
  341. input[type=range]::-webkit-slider-runnable-track {
  342. width: 100%;
  343. height: 5.2px;
  344. cursor: pointer;
  345. box-shadow: 0;
  346. background: #c2c0c2;
  347. border-radius: 0;
  348. border: 0;
  349. }
  350. input[type=range]::-webkit-slider-thumb {
  351. box-shadow: 0;
  352. border: 0;
  353. height: 19px;
  354. width: 19px;
  355. border-radius: 15px;
  356. background: #03a9f4;
  357. cursor: pointer;
  358. -webkit-appearance: none;
  359. margin-top: -6.5px;
  360. }
  361. input[type=range]::-moz-range-track {
  362. width: 100%;
  363. height: 5.2px;
  364. cursor: pointer;
  365. box-shadow: 0;
  366. background: #c2c0c2;
  367. border-radius: 0;
  368. border: 0;
  369. }
  370. input[type=range]::-moz-range-thumb {
  371. box-shadow: 0;
  372. border: 0;
  373. height: 19px;
  374. width: 19px;
  375. border-radius: 15px;
  376. background: #03a9f4;
  377. cursor: pointer;
  378. -webkit-appearance: none;
  379. margin-top: -6.5px;
  380. }
  381. input[type=range]::-ms-track {
  382. width: 100%;
  383. height: 5.2px;
  384. cursor: pointer;
  385. box-shadow: 0;
  386. background: #c2c0c2;
  387. border-radius: 1.3px;
  388. }
  389. input[type=range]::-ms-fill-lower {
  390. background: #c2c0c2;
  391. border: 0;
  392. border-radius: 0;
  393. box-shadow: 0;
  394. }
  395. input[type=range]::-ms-fill-upper {
  396. background: #c2c0c2;
  397. border: 0;
  398. border-radius: 0;
  399. box-shadow: 0;
  400. }
  401. input[type=range]::-ms-thumb {
  402. box-shadow: 0;
  403. border: 0;
  404. height: 15px;
  405. width: 15px;
  406. border-radius: 15px;
  407. background: #03a9f4;
  408. cursor: pointer;
  409. -webkit-appearance: none;
  410. margin-top: 1.5px;
  411. }
  412. .controls {
  413. display: flex;
  414. flex-direction: column;
  415. align-items: center;
  416. }
  417. .artist-genres {
  418. display: flex;
  419. justify-content: space-between;
  420. }
  421. #volumeSlider { margin-bottom: 15px; }
  422. .has-text-centered { padding: 10px; }
  423. .thumbnail-preview {
  424. display: flex;
  425. margin: 0 auto 25px auto;
  426. max-width: 200px;
  427. width: 100%;
  428. }
  429. .modal-card-body, .modal-card-foot { border-top: 0; }
  430. .label, .checkbox, h5 {
  431. font-weight: normal;
  432. }
  433. .video-container {
  434. display: flex;
  435. flex-direction: column;
  436. align-items: center;
  437. padding: 10px;
  438. iframe { pointer-events: none; }
  439. }
  440. .save-changes { color: #fff; }
  441. .tag:not(:last-child) { margin-right: 5px; }
  442. .reports-length {
  443. color: #03A9F4;
  444. font-weight: bold;
  445. display: flex;
  446. justify-content: center;
  447. }
  448. .report-link {
  449. color: #000;
  450. }
  451. </style>