Edit.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <modal title="Edit Playlist">
  3. <div slot="body">
  4. <nav class="level">
  5. <div class="level-item has-text-centered">
  6. <div>
  7. <p class="heading">
  8. Total Length
  9. </p>
  10. <p class="title">
  11. {{ totalLength() }}
  12. </p>
  13. </div>
  14. </div>
  15. </nav>
  16. <hr />
  17. <aside class="menu">
  18. <ul class="menu-list">
  19. <li v-for="(song, index) in playlist.songs" :key="index">
  20. <a href="#" target="_blank">{{ song.title }}</a>
  21. <div class="controls">
  22. <a href="#" v-on:click="promoteSong(song.songId)">
  23. <i class="material-icons" v-if="index > 0"
  24. >keyboard_arrow_up</i
  25. >
  26. <i
  27. v-else
  28. class="material-icons"
  29. style="opacity: 0"
  30. >error</i
  31. >
  32. </a>
  33. <a href="#" v-on:click="demoteSong(song.songId)">
  34. <i
  35. v-if="playlist.songs.length - 1 !== index"
  36. class="material-icons"
  37. >keyboard_arrow_down</i
  38. >
  39. <i
  40. v-else
  41. class="material-icons"
  42. style="opacity: 0"
  43. >error</i
  44. >
  45. </a>
  46. <a
  47. href="#"
  48. v-on:click="removeSongFromPlaylist(song.songId)"
  49. >
  50. <i class="material-icons">delete</i>
  51. </a>
  52. </div>
  53. </li>
  54. </ul>
  55. <br />
  56. </aside>
  57. <div class="control is-grouped">
  58. <p class="control is-expanded">
  59. <input
  60. v-model="searchSongQuery"
  61. class="input"
  62. type="text"
  63. placeholder="Search for Song to add"
  64. autofocus
  65. @keyup.enter="searchForSongs()"
  66. />
  67. </p>
  68. <p class="control">
  69. <a class="button is-info" @click="searchForSongs()" href="#"
  70. >Search</a
  71. >
  72. </p>
  73. </div>
  74. <table v-if="songQueryResults.length > 0" class="table">
  75. <tbody>
  76. <tr
  77. v-for="(result, index) in songQueryResults"
  78. :key="index"
  79. >
  80. <td>
  81. <img :src="result.thumbnail" />
  82. </td>
  83. <td>{{ result.title }}</td>
  84. <td>
  85. <a
  86. class="button is-success"
  87. href="#"
  88. @click="addSongToPlaylist(result.id)"
  89. >Add</a
  90. >
  91. </td>
  92. </tr>
  93. </tbody>
  94. </table>
  95. <div class="control is-grouped">
  96. <p class="control is-expanded">
  97. <input
  98. v-model="directSongQuery"
  99. class="input"
  100. type="text"
  101. placeholder="Enter a YouTube id or URL directly"
  102. autofocus
  103. @keyup.enter="addSong()"
  104. />
  105. </p>
  106. <p class="control">
  107. <a class="button is-info" @click="addSong()" href="#"
  108. >Add</a
  109. >
  110. </p>
  111. </div>
  112. <div class="control is-grouped">
  113. <p class="control is-expanded">
  114. <input
  115. v-model="importQuery"
  116. class="input"
  117. type="text"
  118. placeholder="YouTube Playlist URL"
  119. @keyup.enter="importPlaylist(false)"
  120. />
  121. </p>
  122. <p class="control">
  123. <a
  124. class="button is-info"
  125. @click="importPlaylist(true)"
  126. href="#"
  127. >Import music</a
  128. >
  129. </p>
  130. <p class="control">
  131. <a
  132. class="button is-info"
  133. @click="importPlaylist(false)"
  134. href="#"
  135. >Import all</a
  136. >
  137. </p>
  138. </div>
  139. <button class="button is-info" @click="shuffle()">Shuffle</button>
  140. <h5>Edit playlist details:</h5>
  141. <div class="control is-grouped">
  142. <p class="control is-expanded">
  143. <input
  144. v-model="playlist.displayName"
  145. class="input"
  146. type="text"
  147. placeholder="Playlist Display Name"
  148. @keyup.enter="renamePlaylist()"
  149. />
  150. </p>
  151. <p class="control">
  152. <a class="button is-info" @click="renamePlaylist()" href="#"
  153. >Rename</a
  154. >
  155. </p>
  156. </div>
  157. </div>
  158. <div slot="footer">
  159. <a class="button is-danger" v-on:click="removePlaylist()" href="#"
  160. >Remove Playlist</a
  161. >
  162. </div>
  163. </modal>
  164. </template>
  165. <script>
  166. import { mapState, mapActions } from "vuex";
  167. import Toast from "toasters";
  168. import Modal from "../Modal.vue";
  169. import io from "../../../io";
  170. import validation from "../../../validation";
  171. import utils from "../../../js/utils";
  172. export default {
  173. components: { Modal },
  174. data() {
  175. return {
  176. utils,
  177. playlist: { songs: [] },
  178. songQueryResults: [],
  179. searchSongQuery: "",
  180. directSongQuery: "",
  181. importQuery: ""
  182. };
  183. },
  184. computed: mapState("user/playlists", {
  185. editing: state => state.editing
  186. }),
  187. mounted() {
  188. io.getSocket(socket => {
  189. this.socket = socket;
  190. this.socket.emit("playlists.getPlaylist", this.editing, res => {
  191. if (res.status === "success") this.playlist = res.data;
  192. this.playlist.oldId = res.data._id;
  193. });
  194. this.socket.on("event:playlist.addSong", data => {
  195. if (this.playlist._id === data.playlistId)
  196. this.playlist.songs.push(data.song);
  197. });
  198. this.socket.on("event:playlist.removeSong", data => {
  199. if (this.playlist._id === data.playlistId) {
  200. this.playlist.songs.forEach((song, index) => {
  201. if (song.songId === data.songId)
  202. this.playlist.songs.splice(index, 1);
  203. });
  204. }
  205. });
  206. this.socket.on("event:playlist.updateDisplayName", data => {
  207. if (this.playlist._id === data.playlistId)
  208. this.playlist.displayName = data.displayName;
  209. });
  210. this.socket.on("event:playlist.moveSongToBottom", data => {
  211. if (this.playlist._id === data.playlistId) {
  212. let songIndex;
  213. this.playlist.songs.forEach((song, index) => {
  214. if (song.songId === data.songId) songIndex = index;
  215. });
  216. const song = this.playlist.songs.splice(songIndex, 1)[0];
  217. this.playlist.songs.push(song);
  218. }
  219. });
  220. this.socket.on("event:playlist.moveSongToTop", data => {
  221. if (this.playlist._id === data.playlistId) {
  222. let songIndex;
  223. this.playlist.songs.forEach((song, index) => {
  224. if (song.songId === data.songId) songIndex = index;
  225. });
  226. const song = this.playlist.songs.splice(songIndex, 1)[0];
  227. this.playlist.songs.unshift(song);
  228. }
  229. });
  230. });
  231. },
  232. methods: {
  233. totalLength() {
  234. let length = 0;
  235. this.playlist.songs.forEach(song => {
  236. length += song.duration;
  237. });
  238. return this.utils.formatTimeLong(length);
  239. },
  240. searchForSongs() {
  241. let query = this.searchSongQuery;
  242. if (query.indexOf("&index=") !== -1) {
  243. query = query.split("&index=");
  244. query.pop();
  245. query = query.join("");
  246. }
  247. if (query.indexOf("&list=") !== -1) {
  248. query = query.split("&list=");
  249. query.pop();
  250. query = query.join("");
  251. }
  252. this.socket.emit("apis.searchYoutube", query, res => {
  253. if (res.status === "success") {
  254. this.songQueryResults = [];
  255. for (let i = 0; i < res.data.items.length; i += 1) {
  256. this.songQueryResults.push({
  257. id: res.data.items[i].id.videoId,
  258. url: `https://www.youtube.com/watch?v=${this.id}`,
  259. title: res.data.items[i].snippet.title,
  260. thumbnail:
  261. res.data.items[i].snippet.thumbnails.default.url
  262. });
  263. }
  264. } else if (res.status === "error")
  265. new Toast({ content: res.message, timeout: 3000 });
  266. });
  267. },
  268. addSongToPlaylist(id) {
  269. this.socket.emit(
  270. "playlists.addSongToPlaylist",
  271. false,
  272. id,
  273. this.playlist._id,
  274. res => {
  275. new Toast({ content: res.message, timeout: 4000 });
  276. }
  277. );
  278. },
  279. /* eslint-disable prefer-destructuring */
  280. addSong() {
  281. let id = "";
  282. if (this.directSongQuery.length === 11) id = this.directSongQuery;
  283. else {
  284. const match = this.directSongQuery.match("v=([0-9A-Za-z_-]+)");
  285. if (match.length > 0) id = match[1];
  286. }
  287. this.addSongToPlaylist(id);
  288. },
  289. /* eslint-enable prefer-destructuring */
  290. shuffle() {
  291. this.socket.emit("playlists.shuffle", this.playlist._id, res => {
  292. new Toast({ content: res.message, timeout: 4000 });
  293. if (res.status === "success") {
  294. this.playlist = res.data;
  295. }
  296. });
  297. },
  298. importPlaylist(musicOnly) {
  299. new Toast({
  300. content:
  301. "Starting to import your playlist. This can take some time to do.",
  302. timeout: 4000
  303. });
  304. this.socket.emit(
  305. "playlists.addSetToPlaylist",
  306. this.importQuery,
  307. this.playlist._id,
  308. musicOnly,
  309. res => {
  310. new Toast({ content: res.message, timeout: 4000 });
  311. if (res.status === "success") {
  312. new Toast({
  313. content: `Successfully added ${res.stats.songsAddedSuccessfully} songs. Failed to add ${res.stats.songsFailedToAdd} songs.`,
  314. timeout: 4000
  315. });
  316. if (musicOnly) {
  317. new Toast({
  318. content: `${res.stats.songsInPlaylistTotal} of the ${res.stats.videosInPlaylistTotal} videos in the playlist were songs.`,
  319. timeout: 4000
  320. });
  321. }
  322. }
  323. }
  324. );
  325. },
  326. removeSongFromPlaylist(id) {
  327. this.socket.emit(
  328. "playlists.removeSongFromPlaylist",
  329. id,
  330. this.playlist._id,
  331. res => {
  332. new Toast({ content: res.message, timeout: 4000 });
  333. }
  334. );
  335. },
  336. renamePlaylist() {
  337. const { displayName } = this.playlist;
  338. if (!validation.isLength(displayName, 2, 32))
  339. return new Toast({
  340. content:
  341. "Display name must have between 2 and 32 characters.",
  342. timeout: 8000
  343. });
  344. if (!validation.regex.ascii.test(displayName))
  345. return new Toast({
  346. content:
  347. "Invalid display name format. Only ASCII characters are allowed.",
  348. timeout: 8000
  349. });
  350. return this.socket.emit(
  351. "playlists.updateDisplayName",
  352. this.playlist._id,
  353. this.playlist.displayName,
  354. res => {
  355. new Toast({ content: res.message, timeout: 4000 });
  356. }
  357. );
  358. },
  359. removePlaylist() {
  360. this.socket.emit("playlists.remove", this.playlist._id, res => {
  361. new Toast({ content: res.message, timeout: 3000 });
  362. if (res.status === "success") {
  363. this.closeModal({
  364. sector: "station",
  365. modal: "editPlaylist"
  366. });
  367. }
  368. });
  369. },
  370. promoteSong(songId) {
  371. this.socket.emit(
  372. "playlists.moveSongToTop",
  373. this.playlist._id,
  374. songId,
  375. res => {
  376. new Toast({ content: res.message, timeout: 4000 });
  377. }
  378. );
  379. },
  380. demoteSong(songId) {
  381. this.socket.emit(
  382. "playlists.moveSongToBottom",
  383. this.playlist._id,
  384. songId,
  385. res => {
  386. new Toast({ content: res.message, timeout: 4000 });
  387. }
  388. );
  389. },
  390. ...mapActions("modals", ["closeModal"])
  391. }
  392. };
  393. </script>
  394. <style lang="scss" scoped>
  395. @import "styles/global.scss";
  396. .menu {
  397. padding: 0 20px;
  398. }
  399. .menu-list li {
  400. display: flex;
  401. justify-content: space-between;
  402. }
  403. .menu-list a:hover {
  404. color: $black !important;
  405. }
  406. li a {
  407. display: flex;
  408. align-items: center;
  409. }
  410. .controls {
  411. display: flex;
  412. a {
  413. display: flex;
  414. align-items: center;
  415. }
  416. }
  417. .table {
  418. margin-bottom: 0;
  419. }
  420. h5 {
  421. padding: 20px 0;
  422. }
  423. </style>