Edit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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">{{ totalLength() }}</p>
  9. </div>
  10. </div>
  11. </nav>
  12. <hr />
  13. <aside class="menu" v-if="playlist.songs && playlist.songs.length > 0">
  14. <ul class="menu-list">
  15. <li v-for="(song, index) in playlist.songs" :key="index">
  16. <a href="#" target="_blank">{{ song.title }}</a>
  17. <div class="controls">
  18. <a href="#" v-on:click="promoteSong(song.songId)">
  19. <i class="material-icons" v-if="$index > 0">keyboard_arrow_up</i>
  20. <i class="material-icons" style="opacity: 0" v-else>error</i>
  21. </a>
  22. <a href="#" v-on:click="demoteSong(song.songId)">
  23. <i
  24. class="material-icons"
  25. v-if="playlist.songs.length - 1 !== $index"
  26. >keyboard_arrow_down</i>
  27. <i class="material-icons" style="opacity: 0" v-else>error</i>
  28. </a>
  29. <a href="#" v-on:click="removeSongFromPlaylist(song.songId)">
  30. <i class="material-icons">delete</i>
  31. </a>
  32. </div>
  33. </li>
  34. </ul>
  35. <br />
  36. </aside>
  37. <div class="control is-grouped">
  38. <p class="control is-expanded">
  39. <input
  40. class="input"
  41. type="text"
  42. placeholder="Search for Song to add"
  43. v-model="songQuery"
  44. autofocus
  45. @keyup.enter="searchForSongs()"
  46. />
  47. </p>
  48. <p class="control">
  49. <a class="button is-info" v-on:click="searchForSongs()" href="#">Search</a>
  50. </p>
  51. </div>
  52. <table class="table" v-if="songQueryResults.length > 0">
  53. <tbody>
  54. <tr v-for="(result, index) in songQueryResults" :key="index">
  55. <td>
  56. <img :src="result.thumbnail" />
  57. </td>
  58. <td>{{ result.title }}</td>
  59. <td>
  60. <a class="button is-success" v-on:click="addSongToPlaylist(result.id)" href="#">Add</a>
  61. </td>
  62. </tr>
  63. </tbody>
  64. </table>
  65. <div class="control is-grouped">
  66. <p class="control is-expanded">
  67. <input
  68. class="input"
  69. type="text"
  70. placeholder="YouTube Playlist URL"
  71. v-model="importQuery"
  72. @keyup.enter="importPlaylist()"
  73. />
  74. </p>
  75. <p class="control">
  76. <a class="button is-info" v-on:click="importPlaylist()" href="#">Import</a>
  77. </p>
  78. </div>
  79. <h5>Edit playlist details:</h5>
  80. <div class="control is-grouped">
  81. <p class="control is-expanded">
  82. <input
  83. class="input"
  84. type="text"
  85. placeholder="Playlist Display Name"
  86. v-model="playlist.displayName"
  87. @keyup.enter="renamePlaylist()"
  88. />
  89. </p>
  90. <p class="control">
  91. <a class="button is-info" v-on:click="renamePlaylist()" href="#">Rename</a>
  92. </p>
  93. </div>
  94. </div>
  95. <div slot="footer">
  96. <a class="button is-danger" v-on:click="removePlaylist()" href="#">Remove Playlist</a>
  97. </div>
  98. </modal>
  99. </template>
  100. <script>
  101. import { mapState } from "vuex";
  102. import { Toast } from "vue-roaster";
  103. import Modal from "../Modal.vue";
  104. import io from "../../../io";
  105. import validation from "../../../validation";
  106. export default {
  107. components: { Modal },
  108. data() {
  109. return {
  110. playlist: { songs: [] },
  111. songQueryResults: [],
  112. songQuery: "",
  113. importQuery: ""
  114. };
  115. },
  116. computed: mapState("user/playlists", {
  117. editing: state => state.editing
  118. }),
  119. methods: {
  120. formatTime: function(length) {
  121. let duration = moment.duration(length, "seconds");
  122. function getHours() {
  123. return Math.floor(duration.asHours());
  124. }
  125. if (length <= 0) return "0 seconds";
  126. else
  127. return (
  128. (getHours() > 0
  129. ? getHours() > 1
  130. ? getHours() < 10
  131. ? "0" + getHours() + " hours "
  132. : getHours() + " hours "
  133. : "0" + getHours() + " hour "
  134. : "") +
  135. (duration.minutes() > 0
  136. ? duration.minutes() > 1
  137. ? duration.minutes() < 10
  138. ? "0" + duration.minutes() + " minutes "
  139. : duration.minutes() + " minutes "
  140. : "0" + duration.minutes() + " minute "
  141. : "") +
  142. (duration.seconds() > 0
  143. ? duration.seconds() > 1
  144. ? duration.seconds() < 10
  145. ? "0" + duration.seconds() + " seconds "
  146. : duration.seconds() + " seconds "
  147. : "0" + duration.seconds() + " second "
  148. : "")
  149. );
  150. },
  151. totalLength: function() {
  152. let length = 0;
  153. this.playlist.songs.forEach(song => {
  154. length += song.duration;
  155. });
  156. return this.formatTime(length);
  157. },
  158. searchForSongs: function() {
  159. let _this = this;
  160. let query = _this.songQuery;
  161. if (query.indexOf("&index=") !== -1) {
  162. query = query.split("&index=");
  163. query.pop();
  164. query = query.join("");
  165. }
  166. if (query.indexOf("&list=") !== -1) {
  167. query = query.split("&list=");
  168. query.pop();
  169. query = query.join("");
  170. }
  171. _this.socket.emit("apis.searchYoutube", query, res => {
  172. if (res.status == "success") {
  173. _this.songQueryResults = [];
  174. for (let i = 0; i < res.data.items.length; i++) {
  175. _this.songQueryResults.push({
  176. id: res.data.items[i].id.videoId,
  177. url: `https://www.youtube.com/watch?v=${this.id}`,
  178. title: res.data.items[i].snippet.title,
  179. thumbnail: res.data.items[i].snippet.thumbnails.default.url
  180. });
  181. }
  182. } else if (res.status === "error")
  183. Toast.methods.addToast(res.message, 3000);
  184. });
  185. },
  186. addSongToPlaylist: function(id) {
  187. let _this = this;
  188. _this.socket.emit(
  189. "playlists.addSongToPlaylist",
  190. id,
  191. _this.playlist._id,
  192. res => {
  193. Toast.methods.addToast(res.message, 4000);
  194. }
  195. );
  196. },
  197. importPlaylist: function() {
  198. let _this = this;
  199. Toast.methods.addToast(
  200. "Starting to import your playlist. This can take some time to do.",
  201. 4000
  202. );
  203. this.socket.emit(
  204. "playlists.addSetToPlaylist",
  205. _this.importQuery,
  206. _this.playlist._id,
  207. res => {
  208. if (res.status === "success") _this.playlist.songs = res.data;
  209. Toast.methods.addToast(res.message, 4000);
  210. }
  211. );
  212. },
  213. removeSongFromPlaylist: function(id) {
  214. let _this = this;
  215. this.socket.emit(
  216. "playlists.removeSongFromPlaylist",
  217. id,
  218. _this.playlist._id,
  219. res => {
  220. Toast.methods.addToast(res.message, 4000);
  221. }
  222. );
  223. },
  224. renamePlaylist: function() {
  225. const displayName = this.playlist.displayName;
  226. if (!validation.isLength(displayName, 2, 32))
  227. return Toast.methods.addToast(
  228. "Display name must have between 2 and 32 characters.",
  229. 8000
  230. );
  231. if (!validation.regex.azAZ09_.test(displayName))
  232. return Toast.methods.addToast(
  233. "Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.",
  234. 8000
  235. );
  236. this.socket.emit(
  237. "playlists.updateDisplayName",
  238. this.playlist._id,
  239. this.playlist.displayName,
  240. res => {
  241. Toast.methods.addToast(res.message, 4000);
  242. }
  243. );
  244. },
  245. removePlaylist: function() {
  246. let _this = this;
  247. _this.socket.emit("playlists.remove", _this.playlist._id, res => {
  248. Toast.methods.addToast(res.message, 3000);
  249. if (res.status === "success") {
  250. _this.$parent.modals.editPlaylist = !_this.$parent.modals
  251. .editPlaylist;
  252. }
  253. });
  254. },
  255. promoteSong: function(songId) {
  256. let _this = this;
  257. _this.socket.emit(
  258. "playlists.moveSongToTop",
  259. _this.playlist._id,
  260. songId,
  261. res => {
  262. Toast.methods.addToast(res.message, 4000);
  263. }
  264. );
  265. },
  266. demoteSong: function(songId) {
  267. let _this = this;
  268. _this.socket.emit(
  269. "playlists.moveSongToBottom",
  270. _this.playlist._id,
  271. songId,
  272. res => {
  273. Toast.methods.addToast(res.message, 4000);
  274. }
  275. );
  276. }
  277. },
  278. mounted: function() {
  279. let _this = this;
  280. io.getSocket(socket => {
  281. _this.socket = socket;
  282. _this.socket.emit("playlists.getPlaylist", _this.editing, res => {
  283. if (res.status === "success") _this.playlist = res.data;
  284. _this.playlist.oldId = res.data._id;
  285. });
  286. _this.socket.on("event:playlist.addSong", data => {
  287. if (_this.playlist._id === data.playlistId)
  288. _this.playlist.songs.push(data.song);
  289. });
  290. _this.socket.on("event:playlist.removeSong", data => {
  291. if (_this.playlist._id === data.playlistId) {
  292. _this.playlist.songs.forEach((song, index) => {
  293. if (song.songId === data.songId)
  294. _this.playlist.songs.splice(index, 1);
  295. });
  296. }
  297. });
  298. _this.socket.on("event:playlist.updateDisplayName", data => {
  299. if (_this.playlist._id === data.playlistId)
  300. _this.playlist.displayName = data.displayName;
  301. });
  302. _this.socket.on("event:playlist.moveSongToBottom", data => {
  303. if (_this.playlist._id === data.playlistId) {
  304. let songIndex;
  305. _this.playlist.songs.forEach((song, index) => {
  306. if (song.songId === data.songId) songIndex = index;
  307. });
  308. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  309. _this.playlist.songs.push(song);
  310. }
  311. });
  312. _this.socket.on("event:playlist.moveSongToTop", data => {
  313. if (_this.playlist._id === data.playlistId) {
  314. let songIndex;
  315. _this.playlist.songs.forEach((song, index) => {
  316. if (song.songId === data.songId) songIndex = index;
  317. });
  318. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  319. _this.playlist.songs.unshift(song);
  320. }
  321. });
  322. });
  323. }
  324. };
  325. </script>
  326. <style lang='scss' scoped>
  327. .menu {
  328. padding: 0 20px;
  329. }
  330. .menu-list li {
  331. display: flex;
  332. justify-content: space-between;
  333. }
  334. .menu-list a:hover {
  335. color: #000 !important;
  336. }
  337. li a {
  338. display: flex;
  339. align-items: center;
  340. }
  341. .controls {
  342. display: flex;
  343. a {
  344. display: flex;
  345. align-items: center;
  346. }
  347. }
  348. .table {
  349. margin-bottom: 0;
  350. }
  351. h5 {
  352. padding: 20px 0;
  353. }
  354. </style>