EditSong.vue 18 KB

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