QueueSongs.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <div class='columns is-mobile'>
  3. <div class='column is-8-desktop is-offset-2-desktop is-12-mobile'>
  4. <table class='table is-striped'>
  5. <thead>
  6. <tr>
  7. <td>Thumbnail</td>
  8. <td>Title</td>
  9. <td>YouTube ID</td>
  10. <td>Artists</td>
  11. <td>Genres</td>
  12. <td>Requested By</td>
  13. <td>Options</td>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <tr v-for='(index, song) in songs' track-by='$index'>
  18. <td>
  19. <img class='song-thumbnail' :src='song.thumbnail' onerror="this.src='/assets/notes.png'">
  20. </td>
  21. <td>
  22. <strong>{{ song.title }}</strong>
  23. </td>
  24. <td>{{ song._id }}</td>
  25. <td>{{ song.artists.join(', ') }}</td>
  26. <td>{{ song.genres.join(', ') }}</td>
  27. <td>{{ song.requestedBy }}</td>
  28. <td>
  29. <a class='button is-primary' @click='edit(song, index)'>Edit</a>
  30. <a class='button is-success' @click='add(song)'>Add</a>
  31. <a class='button is-danger' @click='remove(song._id, index)'>Remove</a>
  32. </td>
  33. </tr>
  34. </tbody>
  35. </table>
  36. </div>
  37. </div>
  38. <div class='modal' :class="{ 'is-active': isEditActive }">
  39. <div class='modal-background'></div>
  40. <div class='modal-card'>
  41. <section class='modal-card-body'>
  42. <h5 class='has-text-centered'>Video Preview</h5>
  43. <div class='video-container'>
  44. <div id='player'></div>
  45. <p class='control has-addons'>
  46. <a class='button'>
  47. <i class='material-icons' @click='video.settings("pause")' v-if='!video.paused'>pause</i>
  48. <i class='material-icons' @click='video.settings("play")' v-else>play_arrow</i>
  49. </a>
  50. <a class='button' @click='video.settings("stop")'>
  51. <i class='material-icons'>stop</i>
  52. </a>
  53. <a class='button' @click='video.settings("skipToLast10Secs")'>
  54. <i class='material-icons'>fast_forward</i>
  55. </a>
  56. </p>
  57. </div>
  58. <h5 class='has-text-centered'>Thumbnail Preview</h5>
  59. <img class='thumbnail-preview' :src='editing.song.thumbnail'>
  60. <label class='label'>Thumbnail URL</label>
  61. <p class='control'>
  62. <input class='input' type='text' v-model='editing.song.thumbnail'>
  63. </p>
  64. <h5 class='has-text-centered'>Edit Info</h5>
  65. <p class='control'>
  66. <label class='checkbox'>
  67. <input type='checkbox' v-model='editing.song.explicit'>
  68. Explicit
  69. </label>
  70. </p>
  71. <label class='label'>Song ID</label>
  72. <p class='control'>
  73. <input class='input' type='text' v-model='editing.song._id'>
  74. </p>
  75. <label class='label'>Song Title</label>
  76. <p class='control'>
  77. <input class='input' type='text' v-model='editing.song.title'>
  78. </p>
  79. <div class='control is-horizontal'>
  80. <div class='control is-grouped'>
  81. <div>
  82. <p class='control has-addons'>
  83. <input class='input' id='new-artist' type='text' placeholder='Artist'>
  84. <a class='button is-info' @click='addTag("artists")'>Add Artist</a>
  85. </p>
  86. <span class='tag is-info' v-for='(index, artist) in editing.song.artists' track-by='$index'>
  87. {{ artist }}
  88. <button class='delete is-info' @click='removeTag("artists", index)'></button>
  89. </span>
  90. </div>
  91. <div>
  92. <p class='control has-addons'>
  93. <input class='input' id='new-genre' type='text' placeholder='Genre'>
  94. <a class='button is-info' @click='addTag("genres")'>Add Genre</a>
  95. </p>
  96. <span class='tag is-info' v-for='(index, genre) in editing.song.genres' track-by='$index'>
  97. {{ genre }}
  98. <button class='delete is-info' @click='removeTag("genres", index)'></button>
  99. </span>
  100. </div>
  101. </div>
  102. </div>
  103. <label class='label'>Song Duration</label>
  104. <p class='control'>
  105. <input class='input' type='text' v-model='editing.song.duration'>
  106. </p>
  107. <label class='label'>Skip Duration</label>
  108. <p class='control'>
  109. <input class='input' type='text' v-model='editing.song.skipDuration'>
  110. </p>
  111. </section>
  112. <footer class='modal-card-foot'>
  113. <a class='button is-success' @click='save(editing.song)'>
  114. <i class='material-icons save-changes'>done</i>
  115. <span>&nbsp;Save</span>
  116. </a>
  117. </footer>
  118. </div>
  119. </div>
  120. </template>
  121. <script>
  122. import { Toast } from 'vue-roaster';
  123. export default {
  124. data() {
  125. return {
  126. songs: [],
  127. isEditActive: false,
  128. editing: {
  129. index: 0,
  130. song: {}
  131. },
  132. video: {
  133. player: null,
  134. paused: false,
  135. settings: function (type) {
  136. switch(type) {
  137. case 'stop':
  138. this.player.stopVideo();
  139. this.paused = true;
  140. break;
  141. case 'pause':
  142. this.player.pauseVideo();
  143. this.paused = true;
  144. break;
  145. case 'play':
  146. this.player.playVideo();
  147. this.paused = false;
  148. break;
  149. case 'skipToLast10Secs':
  150. this.player.seekTo(this.player.getDuration() - 10);
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. },
  157. methods: {
  158. toggleModal: function () {
  159. this.isEditActive = !this.isEditActive;
  160. this.video.settings('stop');
  161. },
  162. addTag: function (type) {
  163. if (type == 'genres') {
  164. for (let z = 0; z < this.editing.song.genres.length; z++) {
  165. if (this.editing.song.genres[z] == $('#new-genre').val()) return Toast.methods.addToast('Genre already exists', 3000);
  166. }
  167. if ($('#new-genre').val() !== '') this.editing.song.genres.push($('#new-genre').val());
  168. else Toast.methods.addToast('Genre cannot be empty', 3000);
  169. } else if (type == 'artists') {
  170. for (let z = 0; z < this.editing.song.artists.length; z++) {
  171. if (this.editing.song.artists[z] == $('#new-artist').val()) return Toast.methods.addToast('Artist already exists', 3000);
  172. }
  173. if ($('#new-artist').val() !== '') this.editing.song.artists.push($('#new-artist').val());
  174. else Toast.methods.addToast('Artist cannot be empty', 3000);
  175. }
  176. },
  177. removeTag: function (type, index) {
  178. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  179. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  180. },
  181. edit: function (song, index) {
  182. this.editing = { index, song };
  183. this.video.player.loadVideoById(song._id);
  184. this.isEditActive = true;
  185. },
  186. save: function (song) {
  187. let _this = this;
  188. this.socket.emit('queueSongs.update', song._id, song, function (res) {
  189. if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
  190. _this.toggleModal();
  191. });
  192. },
  193. add: function (song) {
  194. this.socket.emit('queueSongs.remove', song._id, res => {
  195. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  196. });
  197. this.socket.emit('users.findBySession', res => {
  198. if (res.status == 'success') {
  199. song.acceptedBy = res.data.username;
  200. this.socket.emit('songs.add', song, res => {
  201. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  202. });
  203. }
  204. });
  205. },
  206. remove: function (id, index) {
  207. this.songs.splice(index, 1);
  208. this.socket.emit('queueSongs.remove', id, res => {
  209. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  210. });
  211. }
  212. },
  213. ready: function () {
  214. let _this = this;
  215. let socketInterval = setInterval(() => {
  216. if (!!_this.$parent.$parent.socket) {
  217. _this.socket = _this.$parent.$parent.socket;
  218. _this.socket.emit('queueSongs.index', data => {
  219. _this.songs = data;
  220. });
  221. clearInterval(socketInterval);
  222. }
  223. }, 100);
  224. this.video.player = new YT.Player('player', {
  225. height: 315,
  226. width: 560,
  227. videoId: this.editing.song._id,
  228. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  229. events: {
  230. 'onStateChange': event => {
  231. if (event.data == 1) {
  232. let youtubeDuration = _this.video.player.getDuration();
  233. youtubeDuration -= _this.editing.song.skipDuration;
  234. if (_this.editing.song.duration > youtubeDuration) this.stopVideo();
  235. }
  236. }
  237. }
  238. });
  239. }
  240. }
  241. </script>
  242. <style lang='scss' scoped>
  243. body { font-family: 'Roboto', sans-serif; }
  244. .thumbnail-preview {
  245. display: flex;
  246. margin: 0 auto;
  247. padding: 10px 0 20px 0;
  248. }
  249. .modal-card-body, .modal-card-foot { border-top: 0; }
  250. .label, .checkbox, h5 {
  251. font-weight: normal;
  252. }
  253. .video-container {
  254. display: flex;
  255. flex-direction: column;
  256. align-items: center;
  257. padding: 10px;
  258. iframe {
  259. pointer-events: none;
  260. }
  261. }
  262. .save-changes { color: #fff; }
  263. .song-thumbnail {
  264. display: block;
  265. max-width: 50px;
  266. margin: 0 auto;
  267. }
  268. td { vertical-align: middle; }
  269. .tag:not(:last-child) { margin-right: 5px; }
  270. </style>