Edit.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. id,
  272. this.playlist._id,
  273. res => {
  274. new Toast({ content: res.message, timeout: 4000 });
  275. }
  276. );
  277. },
  278. /* eslint-disable prefer-destructuring */
  279. addSong() {
  280. let id = "";
  281. if (this.directSongQuery.length === 11) id = this.directSongQuery;
  282. else {
  283. const match = this.directSongQuery.match("v=([0-9A-Za-z_-]+)");
  284. if (match.length > 0) id = match[1];
  285. }
  286. this.addSongToPlaylist(id);
  287. },
  288. /* eslint-enable prefer-destructuring */
  289. shuffle() {
  290. this.socket.emit("playlists.shuffle", this.playlist._id, res => {
  291. new Toast({ content: res.message, timeout: 4000 });
  292. if (res.status === "success") {
  293. this.playlist = res.data;
  294. }
  295. });
  296. },
  297. importPlaylist(musicOnly) {
  298. new Toast({
  299. content:
  300. "Starting to import your playlist. This can take some time to do.",
  301. timeout: 4000
  302. });
  303. this.socket.emit(
  304. "playlists.addSetToPlaylist",
  305. this.importQuery,
  306. this.playlist._id,
  307. musicOnly,
  308. res => {
  309. new Toast({ content: res.message, timeout: 4000 });
  310. if (res.status === "success") {
  311. new Toast({
  312. content: `Successfully added ${res.stats.songsAddedSuccessfully} songs. Failed to add ${res.stats.songsFailedToAdd} songs.`,
  313. timeout: 4000
  314. });
  315. if (musicOnly) {
  316. new Toast({
  317. content: `${res.stats.songsInPlaylistTotal} of the ${res.stats.videosInPlaylistTotal} videos in the playlist were songs.`,
  318. timeout: 4000
  319. });
  320. }
  321. }
  322. }
  323. );
  324. },
  325. removeSongFromPlaylist(id) {
  326. this.socket.emit(
  327. "playlists.removeSongFromPlaylist",
  328. id,
  329. this.playlist._id,
  330. res => {
  331. new Toast({ content: res.message, timeout: 4000 });
  332. }
  333. );
  334. },
  335. renamePlaylist() {
  336. const { displayName } = this.playlist;
  337. if (!validation.isLength(displayName, 2, 32))
  338. return new Toast({
  339. content:
  340. "Display name must have between 2 and 32 characters.",
  341. timeout: 8000
  342. });
  343. if (!validation.regex.ascii.test(displayName))
  344. return new Toast({
  345. content:
  346. "Invalid display name format. Only ASCII characters are allowed.",
  347. timeout: 8000
  348. });
  349. return this.socket.emit(
  350. "playlists.updateDisplayName",
  351. this.playlist._id,
  352. this.playlist.displayName,
  353. res => {
  354. new Toast({ content: res.message, timeout: 4000 });
  355. }
  356. );
  357. },
  358. removePlaylist() {
  359. this.socket.emit("playlists.remove", this.playlist._id, res => {
  360. new Toast({ content: res.message, timeout: 3000 });
  361. if (res.status === "success") {
  362. this.closeModal({
  363. sector: "station",
  364. modal: "editPlaylist"
  365. });
  366. }
  367. });
  368. },
  369. promoteSong(songId) {
  370. this.socket.emit(
  371. "playlists.moveSongToTop",
  372. this.playlist._id,
  373. songId,
  374. res => {
  375. new Toast({ content: res.message, timeout: 4000 });
  376. }
  377. );
  378. },
  379. demoteSong(songId) {
  380. this.socket.emit(
  381. "playlists.moveSongToBottom",
  382. this.playlist._id,
  383. songId,
  384. res => {
  385. new Toast({ content: res.message, timeout: 4000 });
  386. }
  387. );
  388. },
  389. ...mapActions("modals", ["closeModal"])
  390. }
  391. };
  392. </script>
  393. <style lang="scss" scoped>
  394. @import "styles/global.scss";
  395. .menu {
  396. padding: 0 20px;
  397. }
  398. .menu-list li {
  399. display: flex;
  400. justify-content: space-between;
  401. }
  402. .menu-list a:hover {
  403. color: $black !important;
  404. }
  405. li a {
  406. display: flex;
  407. align-items: center;
  408. }
  409. .controls {
  410. display: flex;
  411. a {
  412. display: flex;
  413. align-items: center;
  414. }
  415. }
  416. .table {
  417. margin-bottom: 0;
  418. }
  419. h5 {
  420. padding: 20px 0;
  421. }
  422. </style>