EditSong.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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
  12. type="range"
  13. id="volumeSlider"
  14. min="0"
  15. max="100"
  16. class="active"
  17. v-on:change="changeVolume()"
  18. v-on:input="changeVolume()"
  19. />
  20. </p>
  21. </form>
  22. <p class="control has-addons">
  23. <button class="button" v-on:click="settings('pause')" v-if="!video.paused">
  24. <i class="material-icons">pause</i>
  25. </button>
  26. <button class="button" v-on:click="settings('play')" v-if="video.paused">
  27. <i class="material-icons">play_arrow</i>
  28. </button>
  29. <button class="button" v-on:click="settings('stop')">
  30. <i class="material-icons">stop</i>
  31. </button>
  32. <button class="button" v-on:click="settings('skipToLast10Secs')">
  33. <i class="material-icons">fast_forward</i>
  34. </button>
  35. </p>
  36. <p>
  37. YouTube: <span>{{youtubeVideoCurrentTime}}</span> / <span>{{youtubeVideoDuration}}</span> {{youtubeVideoNote}}
  38. </p>
  39. </div>
  40. </div>
  41. <h5 class="has-text-centered">Thumbnail Preview</h5>
  42. <img
  43. class="thumbnail-preview"
  44. :src="editing.song.thumbnail"
  45. onerror="this.src='/assets/notes-transparent.png'"
  46. />
  47. <div class="control is-horizontal">
  48. <div class="control-label">
  49. <label class="label">Thumbnail URL</label>
  50. </div>
  51. <div class="control">
  52. <input class="input" type="text" v-model="editing.song.thumbnail" />
  53. </div>
  54. </div>
  55. <h5 class="has-text-centered">Edit Information</h5>
  56. <p class="control">
  57. <label class="checkbox">
  58. <input type="checkbox" v-model="editing.song.explicit" />
  59. Explicit
  60. </label>
  61. </p>
  62. <label class="label">Song ID & Title</label>
  63. <div class="control is-horizontal">
  64. <div class="control is-grouped">
  65. <p class="control is-expanded">
  66. <input class="input" type="text" v-model="editing.song.songId" />
  67. </p>
  68. <p class="control is-expanded">
  69. <input class="input" type="text" v-model="editing.song.title" autofocus />
  70. </p>
  71. </div>
  72. </div>
  73. <label class="label">Artists & Genres</label>
  74. <div class="control is-horizontal">
  75. <div class="control is-grouped artist-genres">
  76. <div>
  77. <p class="control has-addons">
  78. <input class="input" id="new-artist" type="text" placeholder="Artist" />
  79. <button class="button is-info" v-on:click="addTag('artists')">Add Artist</button>
  80. </p>
  81. <span
  82. class="tag is-info"
  83. v-for="(artist, index) in editing.song.artists"
  84. :key="index"
  85. >
  86. {{ artist }}
  87. <button class="delete is-info" v-on:click="removeTag('artists', index)"></button>
  88. </span>
  89. </div>
  90. <div>
  91. <p class="control has-addons">
  92. <input class="input" id="new-genre" type="text" placeholder="Genre" />
  93. <button class="button is-info" v-on:click="addTag('genres')">Add Genre</button>
  94. </p>
  95. <span class="tag is-info" v-for="(genre, index) in editing.song.genres" :key="index">
  96. {{ genre }}
  97. <button class="delete is-info" v-on:click="removeTag('genres', index)"></button>
  98. </span>
  99. </div>
  100. </div>
  101. </div>
  102. <label class="label">Song Duration</label>
  103. <p class="control">
  104. <input class="input" type="text" v-model="editing.song.duration" />
  105. </p>
  106. <label class="label">Skip Duration</label>
  107. <p class="control">
  108. <input class="input" type="text" v-model="editing.song.skipDuration" />
  109. </p>
  110. <article class="message" v-if="editing.type === 'songs'">
  111. <div class="message-body">
  112. <span class="reports-length">
  113. {{ reports.length }}
  114. <span
  115. v-if="reports.length > 1 || reports.length <= 0"
  116. >&nbsp;Reports</span>
  117. <span v-else>&nbsp;Report</span>
  118. </span>
  119. <div v-for="(report, index) in reports" :key="index">
  120. <a :href="`/admin/reports?id=${report}`" class="report-link">Report - {{ report }}</a>
  121. </div>
  122. </div>
  123. </article>
  124. <hr />
  125. <h5 class="has-text-centered">Spotify Information</h5>
  126. <label class="label">Song title</label>
  127. <p class="control">
  128. <input class="input" type="text" v-model="spotify.title" />
  129. </p>
  130. <label class="label">Song artist (1 artist full name)</label>
  131. <p class="control">
  132. <input class="input" type="text" v-model="spotify.artist" />
  133. </p>
  134. <button class="button is-success" v-on:click="getSpotifySongs()">Get Spotify songs</button>
  135. <hr />
  136. <article class="media" v-for="(song, index) in spotify.songs" :key="index">
  137. <figure class="media-left">
  138. <p class="image is-64x64">
  139. <img :src="song.thumbnail" onerror="this.src='/assets/notes-transparent.png'" />
  140. </p>
  141. </figure>
  142. <div class="media-content">
  143. <div class="content">
  144. <p>
  145. <strong>{{song.title}}</strong>
  146. <br />
  147. <small>Artists: {{song.artists}}</small>,
  148. <small>Duration: {{song.duration}}</small>,
  149. <small>Explicit: {{song.explicit}}</small>
  150. <br />
  151. <small>Thumbnail: {{song.thumbnail}}</small>
  152. </p>
  153. </div>
  154. </div>
  155. </article>
  156. </div>
  157. <div slot="footer">
  158. <button class="button is-success" v-on:click="save(editing.song, false)">
  159. <i class="material-icons save-changes">done</i>
  160. <span>&nbsp;Save</span>
  161. </button>
  162. <button class="button is-success" v-on:click="save(editing.song, true)">
  163. <i class="material-icons save-changes">done</i>
  164. <span>&nbsp;Save and close</span>
  165. </button>
  166. <button
  167. class="button is-danger"
  168. v-on:click="toggleModal({ sector: 'admin', modal: 'editSong' })"
  169. >
  170. <span>&nbsp;Close</span>
  171. </button>
  172. </div>
  173. </modal>
  174. </div>
  175. </template>
  176. <script>
  177. import { mapState, mapActions } from "vuex";
  178. import io from "../../io";
  179. import validation from "../../validation";
  180. import { Toast } from "vue-roaster";
  181. import Modal from "./Modal.vue";
  182. export default {
  183. components: { Modal },
  184. data() {
  185. return {
  186. reports: 0,
  187. spotify: {
  188. title: "",
  189. artist: "",
  190. songs: []
  191. },
  192. youtubeVideoDuration: 0.0,
  193. youtubeVideoCurrentTime: 0.0,
  194. youtubeVideoNote: "",
  195. };
  196. },
  197. computed: {
  198. ...mapState("admin/songs", {
  199. video: state => state.video,
  200. editing: state => state.editing
  201. }),
  202. ...mapState("modals", {
  203. modals: state => state.modals.admin
  204. })
  205. },
  206. methods: {
  207. save: function(song, close) {
  208. let _this = this;
  209. if (!song.title)
  210. return Toast.methods.addToast("Please fill in all fields", 8000);
  211. if (!song.thumbnail)
  212. return Toast.methods.addToast("Please fill in all fields", 8000);
  213. // Duration
  214. if (Number(song.skipDuration) + Number(song.duration) > this.youtubeVideoDuration) {
  215. return Toast.methods.addToast(
  216. "Duration can't be higher than the length of the video",
  217. 8000
  218. );
  219. }
  220. // Title
  221. if (!validation.isLength(song.title, 1, 64))
  222. return Toast.methods.addToast(
  223. "Title must have between 1 and 64 characters.",
  224. 8000
  225. );
  226. if (!validation.regex.ascii.test(song.title))
  227. return Toast.methods.addToast(
  228. "Invalid title format. Only ascii characters are allowed.",
  229. 8000
  230. );
  231. // Artists
  232. if (song.artists.length < 1 || song.artists.length > 10)
  233. return Toast.methods.addToast(
  234. "Invalid artists. You must have at least 1 artist and a maximum of 10 artists.",
  235. 8000
  236. );
  237. let error;
  238. song.artists.forEach(artist => {
  239. if (!validation.isLength(artist, 1, 32))
  240. return (error = "Artist must have between 1 and 32 characters.");
  241. if (!validation.regex.ascii.test(artist))
  242. return (error =
  243. "Invalid artist format. Only ascii characters are allowed.");
  244. if (artist === "NONE")
  245. return (error =
  246. 'Invalid artist format. Artists are not allowed to be named "NONE".');
  247. });
  248. if (error) return Toast.methods.addToast(error, 8000);
  249. // Genres
  250. error = undefined;
  251. song.genres.forEach(genre => {
  252. if (!validation.isLength(genre, 1, 16))
  253. return (error = "Genre must have between 1 and 16 characters.");
  254. if (!validation.regex.az09_.test(genre))
  255. return (error =
  256. "Invalid genre format. Only ascii characters are allowed.");
  257. });
  258. if (error) return Toast.methods.addToast(error, 8000);
  259. // Thumbnail
  260. if (!validation.isLength(song.thumbnail, 8, 256))
  261. return Toast.methods.addToast(
  262. "Thumbnail must have between 8 and 256 characters.",
  263. 8000
  264. );
  265. if (song.thumbnail.indexOf("https://") !== 0)
  266. return Toast.methods.addToast(
  267. 'Thumbnail must start with "https://".',
  268. 8000
  269. );
  270. this.socket.emit(`${_this.editing.type}.update`, song._id, song, res => {
  271. Toast.methods.addToast(res.message, 4000);
  272. if (res.status === "success") {
  273. _this.$parent.songs.forEach(lSong => {
  274. if (song._id === lSong._id) {
  275. for (let n in song) {
  276. lSong[n] = song[n];
  277. }
  278. }
  279. });
  280. }
  281. if (close) _this.closeCurrentModal();
  282. });
  283. },
  284. settings: function(type) {
  285. let _this = this;
  286. switch (type) {
  287. case "stop":
  288. _this.stopVideo();
  289. _this.pauseVideo(true);
  290. break;
  291. case "pause":
  292. _this.pauseVideo(true);
  293. break;
  294. case "play":
  295. _this.pauseVideo(false);
  296. break;
  297. case "skipToLast10Secs":
  298. _this.video.player.seekTo(
  299. _this.editing.song.duration - 10 + _this.editing.song.skipDuration
  300. );
  301. break;
  302. }
  303. },
  304. changeVolume: function() {
  305. let local = this;
  306. let volume = $("#volumeSlider").val();
  307. localStorage.setItem("volume", volume);
  308. local.video.player.setVolume(volume);
  309. if (volume > 0) local.video.player.unMute();
  310. },
  311. addTag: function(type) {
  312. if (type == "genres") {
  313. let genre = $("#new-genre")
  314. .val()
  315. .toLowerCase()
  316. .trim();
  317. if (this.editing.song.genres.indexOf(genre) !== -1)
  318. return Toast.methods.addToast("Genre already exists", 3000);
  319. if (genre) {
  320. this.editing.song.genres.push(genre);
  321. $("#new-genre").val("");
  322. } else Toast.methods.addToast("Genre cannot be empty", 3000);
  323. } else if (type == "artists") {
  324. let artist = $("#new-artist").val();
  325. if (this.editing.song.artists.indexOf(artist) !== -1)
  326. return Toast.methods.addToast("Artist already exists", 3000);
  327. if ($("#new-artist").val() !== "") {
  328. this.editing.song.artists.push(artist);
  329. $("#new-artist").val("");
  330. } else Toast.methods.addToast("Artist cannot be empty", 3000);
  331. }
  332. },
  333. removeTag: function(type, index) {
  334. if (type == "genres") this.editing.song.genres.splice(index, 1);
  335. else if (type == "artists") this.editing.song.artists.splice(index, 1);
  336. },
  337. getSpotifySongs: function() {
  338. this.socket.emit(
  339. "apis.getSpotifySongs",
  340. this.spotify.title,
  341. this.spotify.artist,
  342. res => {
  343. if (res.status === "success") {
  344. Toast.methods.addToast(
  345. `Succesfully got ${res.songs.length} song${
  346. res.songs.length !== 1 ? "s" : ""
  347. }.`,
  348. 3000
  349. );
  350. this.spotify.songs = res.songs;
  351. } else
  352. Toast.methods.addToast(`Failed to get songs. ${res.message}`, 3000);
  353. }
  354. );
  355. },
  356. ...mapActions("admin/songs", [
  357. "stopVideo",
  358. "loadVideoById",
  359. "pauseVideo",
  360. "editSong"
  361. ]),
  362. ...mapActions("modals", ["toggleModal", "closeCurrentModal"])
  363. },
  364. mounted: function() {
  365. let _this = this;
  366. // if (this.modals.editSong = false) this.video.player.stopVideo();
  367. // this.loadVideoById(
  368. // this.editing.song.songId,
  369. // this.editing.song.skipDuration
  370. // );
  371. io.getSocket(socket => (_this.socket = socket));
  372. setInterval(() => {
  373. if (
  374. _this.video.paused === false &&
  375. _this.playerReady &&
  376. _this.video.player.getCurrentTime() - _this.editing.song.skipDuration >
  377. _this.editing.song.duration
  378. ) {
  379. _this.video.paused = false;
  380. _this.video.player.stopVideo();
  381. }
  382. if (this.playerReady) this.youtubeVideoCurrentTime = _this.video.player.getCurrentTime().toFixed(3);
  383. }, 200);
  384. this.video.player = new YT.Player("player", {
  385. height: 315,
  386. width: 560,
  387. videoId: this.editing.song.songId,
  388. playerVars: {
  389. controls: 0,
  390. iv_load_policy: 3,
  391. rel: 0,
  392. showinfo: 0,
  393. autoplay: 1
  394. },
  395. startSeconds: _this.editing.song.skipDuration,
  396. events: {
  397. onReady: () => {
  398. let volume = parseInt(localStorage.getItem("volume"));
  399. volume = typeof volume === "number" ? volume : 20;
  400. console.log("Seekto: " + _this.editing.song.skipDuration);
  401. _this.video.player.seekTo(_this.editing.song.skipDuration);
  402. _this.video.player.setVolume(volume);
  403. if (volume > 0) _this.video.player.unMute();
  404. this.youtubeVideoDuration = _this.video.player.getDuration();
  405. this.youtubeVideoNote = "(~)";
  406. _this.playerReady = true;
  407. },
  408. onStateChange: event => {
  409. if (event.data === 1) {
  410. if (!_this.video.autoPlayed) {
  411. _this.video.autoPlayed = true;
  412. return _this.video.player.stopVideo();
  413. }
  414. _this.video.paused = false;
  415. let youtubeDuration = _this.video.player.getDuration();
  416. this.youtubeVideoDuration = youtubeDuration;
  417. this.youtubeVideoNote = "";
  418. youtubeDuration -= _this.editing.song.skipDuration;
  419. if (_this.editing.song.duration > youtubeDuration) {
  420. this.video.player.stopVideo();
  421. _this.video.paused = true;
  422. Toast.methods.addToast(
  423. "Video can't play. Specified duration is bigger than the YouTube song duration.",
  424. 4000
  425. );
  426. } else if (_this.editing.song.duration <= 0) {
  427. this.video.player.stopVideo();
  428. _this.video.paused = true;
  429. Toast.methods.addToast(
  430. "Video can't play. Specified duration has to be more than 0 seconds.",
  431. 4000
  432. );
  433. }
  434. if (
  435. _this.video.player.getCurrentTime() <
  436. _this.editing.song.skipDuration
  437. ) {
  438. _this.video.player.seekTo(_this.editing.song.skipDuration);
  439. }
  440. } else if (event.data === 2) {
  441. this.video.paused = true;
  442. }
  443. }
  444. }
  445. });
  446. let volume = parseInt(localStorage.getItem("volume"));
  447. volume = typeof volume === "number" ? volume : 20;
  448. $("#volumeSlider").val(volume);
  449. }
  450. };
  451. </script>
  452. <style lang='scss' scoped>
  453. input[type="range"] {
  454. -webkit-appearance: none;
  455. width: 100%;
  456. margin: 7.3px 0;
  457. }
  458. input[type="range"]:focus {
  459. outline: none;
  460. }
  461. input[type="range"]::-webkit-slider-runnable-track {
  462. width: 100%;
  463. height: 5.2px;
  464. cursor: pointer;
  465. box-shadow: 0;
  466. background: #c2c0c2;
  467. border-radius: 0;
  468. border: 0;
  469. }
  470. input[type="range"]::-webkit-slider-thumb {
  471. box-shadow: 0;
  472. border: 0;
  473. height: 19px;
  474. width: 19px;
  475. border-radius: 15px;
  476. background: #03a9f4;
  477. cursor: pointer;
  478. -webkit-appearance: none;
  479. margin-top: -6.5px;
  480. }
  481. input[type="range"]::-moz-range-track {
  482. width: 100%;
  483. height: 5.2px;
  484. cursor: pointer;
  485. box-shadow: 0;
  486. background: #c2c0c2;
  487. border-radius: 0;
  488. border: 0;
  489. }
  490. input[type="range"]::-moz-range-thumb {
  491. box-shadow: 0;
  492. border: 0;
  493. height: 19px;
  494. width: 19px;
  495. border-radius: 15px;
  496. background: #03a9f4;
  497. cursor: pointer;
  498. -webkit-appearance: none;
  499. margin-top: -6.5px;
  500. }
  501. input[type="range"]::-ms-track {
  502. width: 100%;
  503. height: 5.2px;
  504. cursor: pointer;
  505. box-shadow: 0;
  506. background: #c2c0c2;
  507. border-radius: 1.3px;
  508. }
  509. input[type="range"]::-ms-fill-lower {
  510. background: #c2c0c2;
  511. border: 0;
  512. border-radius: 0;
  513. box-shadow: 0;
  514. }
  515. input[type="range"]::-ms-fill-upper {
  516. background: #c2c0c2;
  517. border: 0;
  518. border-radius: 0;
  519. box-shadow: 0;
  520. }
  521. input[type="range"]::-ms-thumb {
  522. box-shadow: 0;
  523. border: 0;
  524. height: 15px;
  525. width: 15px;
  526. border-radius: 15px;
  527. background: #03a9f4;
  528. cursor: pointer;
  529. -webkit-appearance: none;
  530. margin-top: 1.5px;
  531. }
  532. .controls {
  533. display: flex;
  534. flex-direction: column;
  535. align-items: center;
  536. }
  537. .artist-genres {
  538. display: flex;
  539. justify-content: space-between;
  540. }
  541. #volumeSlider {
  542. margin-bottom: 15px;
  543. }
  544. .has-text-centered {
  545. padding: 10px;
  546. }
  547. .thumbnail-preview {
  548. display: flex;
  549. margin: 0 auto 25px auto;
  550. max-width: 200px;
  551. width: 100%;
  552. }
  553. .modal-card-body,
  554. .modal-card-foot {
  555. border-top: 0;
  556. }
  557. .label,
  558. .checkbox,
  559. h5 {
  560. font-weight: normal;
  561. }
  562. .video-container {
  563. display: flex;
  564. flex-direction: column;
  565. align-items: center;
  566. padding: 10px;
  567. iframe {
  568. pointer-events: none;
  569. }
  570. }
  571. .save-changes {
  572. color: #fff;
  573. }
  574. .tag:not(:last-child) {
  575. margin-right: 5px;
  576. }
  577. .reports-length {
  578. color: #ff4545;
  579. font-weight: bold;
  580. display: flex;
  581. justify-content: center;
  582. }
  583. .report-link {
  584. color: #000;
  585. }
  586. </style>