Edit.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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="songQuery"
  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="importQuery"
  99. class="input"
  100. type="text"
  101. placeholder="YouTube Playlist URL"
  102. @keyup.enter="importPlaylist()"
  103. />
  104. </p>
  105. <p class="control">
  106. <a class="button is-info" @click="importPlaylist()" href="#"
  107. >Import</a
  108. >
  109. </p>
  110. </div>
  111. <h5>Edit playlist details:</h5>
  112. <div class="control is-grouped">
  113. <p class="control is-expanded">
  114. <input
  115. v-model="playlist.displayName"
  116. class="input"
  117. type="text"
  118. placeholder="Playlist Display Name"
  119. @keyup.enter="renamePlaylist()"
  120. />
  121. </p>
  122. <p class="control">
  123. <a class="button is-info" @click="renamePlaylist()" href="#"
  124. >Rename</a
  125. >
  126. </p>
  127. </div>
  128. </div>
  129. <div slot="footer">
  130. <a class="button is-danger" v-on:click="removePlaylist()" href="#"
  131. >Remove Playlist</a
  132. >
  133. </div>
  134. </modal>
  135. </template>
  136. <script>
  137. import { mapState, mapActions } from "vuex";
  138. import Toast from "toasters";
  139. import Modal from "../Modal.vue";
  140. import io from "../../../io";
  141. import validation from "../../../validation";
  142. export default {
  143. components: { Modal },
  144. data() {
  145. return {
  146. playlist: { songs: [] },
  147. songQueryResults: [],
  148. songQuery: "",
  149. importQuery: ""
  150. };
  151. },
  152. computed: mapState("user/playlists", {
  153. editing: state => state.editing
  154. }),
  155. mounted() {
  156. io.getSocket(socket => {
  157. this.socket = socket;
  158. this.socket.emit("playlists.getPlaylist", this.editing, res => {
  159. if (res.status === "success") this.playlist = res.data;
  160. this.playlist.oldId = res.data._id;
  161. });
  162. this.socket.on("event:playlist.addSong", data => {
  163. if (this.playlist._id === data.playlistId)
  164. this.playlist.songs.push(data.song);
  165. });
  166. this.socket.on("event:playlist.removeSong", data => {
  167. if (this.playlist._id === data.playlistId) {
  168. this.playlist.songs.forEach((song, index) => {
  169. if (song.songId === data.songId)
  170. this.playlist.songs.splice(index, 1);
  171. });
  172. }
  173. });
  174. this.socket.on("event:playlist.updateDisplayName", data => {
  175. if (this.playlist._id === data.playlistId)
  176. this.playlist.displayName = data.displayName;
  177. });
  178. this.socket.on("event:playlist.moveSongToBottom", data => {
  179. if (this.playlist._id === data.playlistId) {
  180. let songIndex;
  181. this.playlist.songs.forEach((song, index) => {
  182. if (song.songId === data.songId) songIndex = index;
  183. });
  184. const song = this.playlist.songs.splice(songIndex, 1)[0];
  185. this.playlist.songs.push(song);
  186. }
  187. });
  188. this.socket.on("event:playlist.moveSongToTop", data => {
  189. if (this.playlist._id === data.playlistId) {
  190. let songIndex;
  191. this.playlist.songs.forEach((song, index) => {
  192. if (song.songId === data.songId) songIndex = index;
  193. });
  194. const song = this.playlist.songs.splice(songIndex, 1)[0];
  195. this.playlist.songs.unshift(song);
  196. }
  197. });
  198. });
  199. },
  200. methods: {
  201. formatTime(duration) {
  202. if (duration <= 0) return "0 seconds";
  203. const hours = Math.floor(duration / (60 * 60));
  204. const formatHours = () => {
  205. if (hours > 0) {
  206. if (hours > 1) {
  207. if (hours < 10) return `0${hours} hours `;
  208. return `${hours} hours `;
  209. }
  210. return `0${hours} hour `;
  211. }
  212. return "";
  213. };
  214. const minutes = Math.floor((duration - hours) / 60);
  215. const formatMinutes = () => {
  216. if (minutes > 0) {
  217. if (minutes > 1) {
  218. if (minutes < 10) return `0${minutes} minutes `;
  219. return `${minutes} minutes `;
  220. }
  221. return `0${minutes} minute `;
  222. }
  223. return "";
  224. };
  225. const seconds = Math.floor(
  226. duration - hours * 60 * 60 - minutes * 60
  227. );
  228. const formatSeconds = () => {
  229. if (seconds > 0) {
  230. if (seconds > 1) {
  231. if (seconds < 10) return `0${seconds} seconds `;
  232. return `${seconds} seconds `;
  233. }
  234. return `0${seconds} second `;
  235. }
  236. return "";
  237. };
  238. return formatHours() + formatMinutes() + formatSeconds();
  239. },
  240. totalLength() {
  241. let length = 0;
  242. this.playlist.songs.forEach(song => {
  243. length += song.duration;
  244. });
  245. return this.formatTime(length);
  246. },
  247. searchForSongs() {
  248. let query = this.songQuery;
  249. if (query.indexOf("&index=") !== -1) {
  250. query = query.split("&index=");
  251. query.pop();
  252. query = query.join("");
  253. }
  254. if (query.indexOf("&list=") !== -1) {
  255. query = query.split("&list=");
  256. query.pop();
  257. query = query.join("");
  258. }
  259. this.socket.emit("apis.searchYoutube", query, res => {
  260. if (res.status === "success") {
  261. this.songQueryResults = [];
  262. for (let i = 0; i < res.data.items.length; i += 1) {
  263. this.songQueryResults.push({
  264. id: res.data.items[i].id.videoId,
  265. url: `https://www.youtube.com/watch?v=${this.id}`,
  266. title: res.data.items[i].snippet.title,
  267. thumbnail:
  268. res.data.items[i].snippet.thumbnails.default.url
  269. });
  270. }
  271. } else if (res.status === "error")
  272. new Toast({ content: res.message, timeout: 3000 });
  273. });
  274. },
  275. addSongToPlaylist(id) {
  276. this.socket.emit(
  277. "playlists.addSongToPlaylist",
  278. id,
  279. this.playlist._id,
  280. res => {
  281. new Toast({ content: res.message, timeout: 4000 });
  282. }
  283. );
  284. },
  285. importPlaylist() {
  286. new Toast({
  287. content:
  288. "Starting to import your playlist. This can take some time to do.",
  289. timeout: 4000
  290. });
  291. this.socket.emit(
  292. "playlists.addSetToPlaylist",
  293. this.importQuery,
  294. this.playlist._id,
  295. res => {
  296. if (res.status === "success")
  297. this.playlist.songs = res.data;
  298. new Toast({ content: res.message, timeout: 4000 });
  299. }
  300. );
  301. },
  302. removeSongFromPlaylist(id) {
  303. this.socket.emit(
  304. "playlists.removeSongFromPlaylist",
  305. id,
  306. this.playlist._id,
  307. res => {
  308. new Toast({ content: res.message, timeout: 4000 });
  309. }
  310. );
  311. },
  312. renamePlaylist() {
  313. const { displayName } = this.playlist;
  314. if (!validation.isLength(displayName, 2, 32))
  315. return new Toast({
  316. content:
  317. "Display name must have between 2 and 32 characters.",
  318. timeout: 8000
  319. });
  320. if (!validation.regex.ascii.test(displayName))
  321. return new Toast({
  322. content:
  323. "Invalid display name format. Only ASCII characters are allowed.",
  324. timeout: 8000
  325. });
  326. return this.socket.emit(
  327. "playlists.updateDisplayName",
  328. this.playlist._id,
  329. this.playlist.displayName,
  330. res => {
  331. new Toast({ content: res.message, timeout: 4000 });
  332. }
  333. );
  334. },
  335. removePlaylist() {
  336. this.socket.emit("playlists.remove", this.playlist._id, res => {
  337. new Toast({ content: res.message, timeout: 3000 });
  338. if (res.status === "success") {
  339. this.closeModal();
  340. }
  341. });
  342. },
  343. promoteSong(songId) {
  344. this.socket.emit(
  345. "playlists.moveSongToTop",
  346. this.playlist._id,
  347. songId,
  348. res => {
  349. new Toast({ content: res.message, timeout: 4000 });
  350. }
  351. );
  352. },
  353. demoteSong(songId) {
  354. this.socket.emit(
  355. "playlists.moveSongToBottom",
  356. this.playlist._id,
  357. songId,
  358. res => {
  359. new Toast({ content: res.message, timeout: 4000 });
  360. }
  361. );
  362. },
  363. ...mapActions("modals", ["closeModal"])
  364. }
  365. };
  366. </script>
  367. <style lang="scss" scoped>
  368. @import "styles/global.scss";
  369. .menu {
  370. padding: 0 20px;
  371. }
  372. .menu-list li {
  373. display: flex;
  374. justify-content: space-between;
  375. }
  376. .menu-list a:hover {
  377. color: $black !important;
  378. }
  379. li a {
  380. display: flex;
  381. align-items: center;
  382. }
  383. .controls {
  384. display: flex;
  385. a {
  386. display: flex;
  387. align-items: center;
  388. }
  389. }
  390. .table {
  391. margin-bottom: 0;
  392. }
  393. h5 {
  394. padding: 20px 0;
  395. }
  396. </style>