EditSong.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. Toast.methods.addToast(`Succesfully got ${res.songs.length} song${(res.songs.length !== 1) ? 's' : ''}.`, 3000);
  247. this.spotify.songs = res.songs;
  248. } else Toast.methods.addToast(`Failed to get songs. ${res.message}`, 3000);
  249. });
  250. }
  251. },
  252. ready: function () {
  253. let _this = this;
  254. io.getSocket(socket => {
  255. _this.socket = socket;
  256. });
  257. setInterval(() => {
  258. if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
  259. _this.video.paused = false;
  260. _this.video.player.stopVideo();
  261. }
  262. }, 200);
  263. this.video.player = new YT.Player('player', {
  264. height: 315,
  265. width: 560,
  266. videoId: this.editing.song.songId,
  267. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  268. startSeconds: _this.editing.song.skipDuration,
  269. events: {
  270. 'onReady': () => {
  271. let volume = parseInt(localStorage.getItem("volume"));
  272. volume = (typeof volume === "number") ? volume : 20;
  273. _this.video.player.seekTo(_this.editing.song.skipDuration);
  274. _this.video.player.setVolume(volume);
  275. if (volume > 0) _this.video.player.unMute();
  276. _this.playerReady = true;
  277. },
  278. 'onStateChange': event => {
  279. if (event.data === 1) {
  280. _this.video.paused = false;
  281. let youtubeDuration = _this.video.player.getDuration();
  282. youtubeDuration -= _this.editing.song.skipDuration;
  283. if (_this.editing.song.duration > youtubeDuration) {
  284. this.video.player.stopVideo();
  285. _this.video.paused = true;
  286. Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
  287. } else if (_this.editing.song.duration <= 0) {
  288. this.video.player.stopVideo();
  289. _this.video.paused = true;
  290. Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
  291. }
  292. if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
  293. _this.video.player.seekTo(10);
  294. }
  295. } else if (event.data === 2) {
  296. this.video.paused = true;
  297. }
  298. }
  299. }
  300. });
  301. let volume = parseInt(localStorage.getItem("volume"));
  302. volume = (typeof volume === "number") ? volume : 20;
  303. $("#volumeSlider").val(volume);
  304. },
  305. events: {
  306. closeModal: function () {
  307. this.$parent.modals.editSong = false;
  308. this.video.player.stopVideo();
  309. },
  310. editSong: function (song, index, type) {
  311. let _this = this;
  312. this.video.player.loadVideoById(song.songId, this.editing.song.skipDuration);
  313. let newSong = {};
  314. for (let n in song) {
  315. newSong[n] = song[n];
  316. }
  317. this.editing = {
  318. index,
  319. song: newSong,
  320. type
  321. };
  322. _this.socket.emit('reports.getReportsForSong', song.songId, res => {
  323. if (res.status === 'success') _this.reports = res.data;
  324. });
  325. this.$parent.toggleModal();
  326. },
  327. stopVideo: function () {
  328. this.video.player.stopVideo();
  329. }
  330. }
  331. }
  332. </script>
  333. <style type='scss' scoped>
  334. input[type=range] {
  335. -webkit-appearance: none;
  336. width: 100%;
  337. margin: 7.3px 0;
  338. }
  339. input[type=range]:focus {
  340. outline: none;
  341. }
  342. input[type=range]::-webkit-slider-runnable-track {
  343. width: 100%;
  344. height: 5.2px;
  345. cursor: pointer;
  346. box-shadow: 0;
  347. background: #c2c0c2;
  348. border-radius: 0;
  349. border: 0;
  350. }
  351. input[type=range]::-webkit-slider-thumb {
  352. box-shadow: 0;
  353. border: 0;
  354. height: 19px;
  355. width: 19px;
  356. border-radius: 15px;
  357. background: #03a9f4;
  358. cursor: pointer;
  359. -webkit-appearance: none;
  360. margin-top: -6.5px;
  361. }
  362. input[type=range]::-moz-range-track {
  363. width: 100%;
  364. height: 5.2px;
  365. cursor: pointer;
  366. box-shadow: 0;
  367. background: #c2c0c2;
  368. border-radius: 0;
  369. border: 0;
  370. }
  371. input[type=range]::-moz-range-thumb {
  372. box-shadow: 0;
  373. border: 0;
  374. height: 19px;
  375. width: 19px;
  376. border-radius: 15px;
  377. background: #03a9f4;
  378. cursor: pointer;
  379. -webkit-appearance: none;
  380. margin-top: -6.5px;
  381. }
  382. input[type=range]::-ms-track {
  383. width: 100%;
  384. height: 5.2px;
  385. cursor: pointer;
  386. box-shadow: 0;
  387. background: #c2c0c2;
  388. border-radius: 1.3px;
  389. }
  390. input[type=range]::-ms-fill-lower {
  391. background: #c2c0c2;
  392. border: 0;
  393. border-radius: 0;
  394. box-shadow: 0;
  395. }
  396. input[type=range]::-ms-fill-upper {
  397. background: #c2c0c2;
  398. border: 0;
  399. border-radius: 0;
  400. box-shadow: 0;
  401. }
  402. input[type=range]::-ms-thumb {
  403. box-shadow: 0;
  404. border: 0;
  405. height: 15px;
  406. width: 15px;
  407. border-radius: 15px;
  408. background: #03a9f4;
  409. cursor: pointer;
  410. -webkit-appearance: none;
  411. margin-top: 1.5px;
  412. }
  413. .controls {
  414. display: flex;
  415. flex-direction: column;
  416. align-items: center;
  417. }
  418. .artist-genres {
  419. display: flex;
  420. justify-content: space-between;
  421. }
  422. #volumeSlider { margin-bottom: 15px; }
  423. .has-text-centered { padding: 10px; }
  424. .thumbnail-preview {
  425. display: flex;
  426. margin: 0 auto 25px auto;
  427. max-width: 200px;
  428. width: 100%;
  429. }
  430. .modal-card-body, .modal-card-foot { border-top: 0; }
  431. .label, .checkbox, h5 {
  432. font-weight: normal;
  433. }
  434. .video-container {
  435. display: flex;
  436. flex-direction: column;
  437. align-items: center;
  438. padding: 10px;
  439. iframe { pointer-events: none; }
  440. }
  441. .save-changes { color: #fff; }
  442. .tag:not(:last-child) { margin-right: 5px; }
  443. .reports-length {
  444. color: #03A9F4;
  445. font-weight: bold;
  446. display: flex;
  447. justify-content: center;
  448. }
  449. .report-link {
  450. color: #000;
  451. }
  452. </style>