Edit.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 } from "vuex";
  138. import { Toast } from "vue-roaster";
  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: function() {
  156. let _this = this;
  157. io.getSocket(socket => {
  158. _this.socket = socket;
  159. _this.socket.emit("playlists.getPlaylist", _this.editing, res => {
  160. if (res.status === "success") _this.playlist = res.data;
  161. _this.playlist.oldId = res.data._id;
  162. });
  163. _this.socket.on("event:playlist.addSong", data => {
  164. if (_this.playlist._id === data.playlistId)
  165. _this.playlist.songs.push(data.song);
  166. });
  167. _this.socket.on("event:playlist.removeSong", data => {
  168. if (_this.playlist._id === data.playlistId) {
  169. _this.playlist.songs.forEach((song, index) => {
  170. if (song.songId === data.songId)
  171. _this.playlist.songs.splice(index, 1);
  172. });
  173. }
  174. });
  175. _this.socket.on("event:playlist.updateDisplayName", data => {
  176. if (_this.playlist._id === data.playlistId)
  177. _this.playlist.displayName = data.displayName;
  178. });
  179. _this.socket.on("event:playlist.moveSongToBottom", data => {
  180. if (_this.playlist._id === data.playlistId) {
  181. let songIndex;
  182. _this.playlist.songs.forEach((song, index) => {
  183. if (song.songId === data.songId) songIndex = index;
  184. });
  185. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  186. _this.playlist.songs.push(song);
  187. }
  188. });
  189. _this.socket.on("event:playlist.moveSongToTop", data => {
  190. if (_this.playlist._id === data.playlistId) {
  191. let songIndex;
  192. _this.playlist.songs.forEach((song, index) => {
  193. if (song.songId === data.songId) songIndex = index;
  194. });
  195. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  196. _this.playlist.songs.unshift(song);
  197. }
  198. });
  199. });
  200. },
  201. methods: {
  202. formatTime: function(length) {
  203. let duration = moment.duration(length, "seconds");
  204. function getHours() {
  205. return Math.floor(duration.asHours());
  206. }
  207. if (length <= 0) return "0 seconds";
  208. else
  209. return (
  210. (getHours() > 0
  211. ? getHours() > 1
  212. ? getHours() < 10
  213. ? "0" + getHours() + " hours "
  214. : getHours() + " hours "
  215. : "0" + getHours() + " hour "
  216. : "") +
  217. (duration.minutes() > 0
  218. ? duration.minutes() > 1
  219. ? duration.minutes() < 10
  220. ? "0" + duration.minutes() + " minutes "
  221. : duration.minutes() + " minutes "
  222. : "0" + duration.minutes() + " minute "
  223. : "") +
  224. (duration.seconds() > 0
  225. ? duration.seconds() > 1
  226. ? duration.seconds() < 10
  227. ? "0" + duration.seconds() + " seconds "
  228. : duration.seconds() + " seconds "
  229. : "0" + duration.seconds() + " second "
  230. : "")
  231. );
  232. },
  233. totalLength: function() {
  234. let length = 0;
  235. this.playlist.songs.forEach(song => {
  236. length += song.duration;
  237. });
  238. return this.formatTime(length);
  239. },
  240. searchForSongs: function() {
  241. let _this = this;
  242. let query = _this.songQuery;
  243. if (query.indexOf("&index=") !== -1) {
  244. query = query.split("&index=");
  245. query.pop();
  246. query = query.join("");
  247. }
  248. if (query.indexOf("&list=") !== -1) {
  249. query = query.split("&list=");
  250. query.pop();
  251. query = query.join("");
  252. }
  253. _this.socket.emit("apis.searchYoutube", query, res => {
  254. if (res.status == "success") {
  255. _this.songQueryResults = [];
  256. for (let i = 0; i < res.data.items.length; i++) {
  257. _this.songQueryResults.push({
  258. id: res.data.items[i].id.videoId,
  259. url: `https://www.youtube.com/watch?v=${this.id}`,
  260. title: res.data.items[i].snippet.title,
  261. thumbnail:
  262. res.data.items[i].snippet.thumbnails.default.url
  263. });
  264. }
  265. } else if (res.status === "error")
  266. Toast.methods.addToast(res.message, 3000);
  267. });
  268. },
  269. addSongToPlaylist: function(id) {
  270. let _this = this;
  271. _this.socket.emit(
  272. "playlists.addSongToPlaylist",
  273. id,
  274. _this.playlist._id,
  275. res => {
  276. Toast.methods.addToast(res.message, 4000);
  277. }
  278. );
  279. },
  280. importPlaylist: function() {
  281. let _this = this;
  282. Toast.methods.addToast(
  283. "Starting to import your playlist. This can take some time to do.",
  284. 4000
  285. );
  286. this.socket.emit(
  287. "playlists.addSetToPlaylist",
  288. _this.importQuery,
  289. _this.playlist._id,
  290. res => {
  291. if (res.status === "success")
  292. _this.playlist.songs = res.data;
  293. Toast.methods.addToast(res.message, 4000);
  294. }
  295. );
  296. },
  297. removeSongFromPlaylist: function(id) {
  298. let _this = this;
  299. this.socket.emit(
  300. "playlists.removeSongFromPlaylist",
  301. id,
  302. _this.playlist._id,
  303. res => {
  304. Toast.methods.addToast(res.message, 4000);
  305. }
  306. );
  307. },
  308. renamePlaylist: function() {
  309. const displayName = this.playlist.displayName;
  310. if (!validation.isLength(displayName, 2, 32))
  311. return Toast.methods.addToast(
  312. "Display name must have between 2 and 32 characters.",
  313. 8000
  314. );
  315. if (!validation.regex.azAZ09_.test(displayName))
  316. return Toast.methods.addToast(
  317. "Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.",
  318. 8000
  319. );
  320. this.socket.emit(
  321. "playlists.updateDisplayName",
  322. this.playlist._id,
  323. this.playlist.displayName,
  324. res => {
  325. Toast.methods.addToast(res.message, 4000);
  326. }
  327. );
  328. },
  329. removePlaylist: function() {
  330. let _this = this;
  331. _this.socket.emit("playlists.remove", _this.playlist._id, res => {
  332. Toast.methods.addToast(res.message, 3000);
  333. if (res.status === "success") {
  334. _this.$parent.modals.editPlaylist = !_this.$parent.modals
  335. .editPlaylist;
  336. }
  337. });
  338. },
  339. promoteSong: function(songId) {
  340. let _this = this;
  341. _this.socket.emit(
  342. "playlists.moveSongToTop",
  343. _this.playlist._id,
  344. songId,
  345. res => {
  346. Toast.methods.addToast(res.message, 4000);
  347. }
  348. );
  349. },
  350. demoteSong: function(songId) {
  351. let _this = this;
  352. _this.socket.emit(
  353. "playlists.moveSongToBottom",
  354. _this.playlist._id,
  355. songId,
  356. res => {
  357. Toast.methods.addToast(res.message, 4000);
  358. }
  359. );
  360. }
  361. }
  362. };
  363. </script>
  364. <style lang="scss" scoped>
  365. .menu {
  366. padding: 0 20px;
  367. }
  368. .menu-list li {
  369. display: flex;
  370. justify-content: space-between;
  371. }
  372. .menu-list a:hover {
  373. color: #000 !important;
  374. }
  375. li a {
  376. display: flex;
  377. align-items: center;
  378. }
  379. .controls {
  380. display: flex;
  381. a {
  382. display: flex;
  383. align-items: center;
  384. }
  385. }
  386. .table {
  387. margin-bottom: 0;
  388. }
  389. h5 {
  390. padding: 20px 0;
  391. }
  392. </style>