Edit.vue 10 KB

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