QueueSongs.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. <p style="margin-top: 0; position: relative;">
  59. <input type="range" id="volumeSlider" min="0" max="100" class="active" v-on:change="changeVolume()" v-on:input="changeVolume()">
  60. </p>
  61. <h5 class='has-text-centered'>Thumbnail Preview</h5>
  62. <img class='thumbnail-preview' :src='editing.song.thumbnail'>
  63. <label class='label'>Thumbnail URL</label>
  64. <p class='control'>
  65. <input class='input' type='text' v-model='editing.song.thumbnail'>
  66. </p>
  67. <h5 class='has-text-centered'>Edit Info</h5>
  68. <p class='control'>
  69. <label class='checkbox'>
  70. <input type='checkbox' v-model='editing.song.explicit'>
  71. Explicit
  72. </label>
  73. </p>
  74. <label class='label'>Song ID</label>
  75. <p class='control'>
  76. <input class='input' type='text' v-model='editing.song._id'>
  77. </p>
  78. <label class='label'>Song Title</label>
  79. <p class='control'>
  80. <input class='input' type='text' v-model='editing.song.title'>
  81. </p>
  82. <div class='control is-horizontal'>
  83. <div class='control is-grouped'>
  84. <div>
  85. <p class='control has-addons'>
  86. <input class='input' id='new-artist' type='text' placeholder='Artist'>
  87. <a class='button is-info' @click='addTag("artists")'>Add Artist</a>
  88. </p>
  89. <span class='tag is-info' v-for='(index, artist) in editing.song.artists' track-by='$index'>
  90. {{ artist }}
  91. <button class='delete is-info' @click='removeTag("artists", index)'></button>
  92. </span>
  93. </div>
  94. <div>
  95. <p class='control has-addons'>
  96. <input class='input' id='new-genre' type='text' placeholder='Genre'>
  97. <a class='button is-info' @click='addTag("genres")'>Add Genre</a>
  98. </p>
  99. <span class='tag is-info' v-for='(index, genre) in editing.song.genres' track-by='$index'>
  100. {{ genre }}
  101. <button class='delete is-info' @click='removeTag("genres", index)'></button>
  102. </span>
  103. </div>
  104. </div>
  105. </div>
  106. <label class='label'>Song Duration</label>
  107. <p class='control'>
  108. <input class='input' type='text' v-model='editing.song.duration'>
  109. </p>
  110. <label class='label'>Skip Duration</label>
  111. <p class='control'>
  112. <input class='input' type='text' v-model='editing.song.skipDuration'>
  113. </p>
  114. </section>
  115. <footer class='modal-card-foot'>
  116. <a class='button is-success' @click='save(editing.song)'>
  117. <i class='material-icons save-changes'>done</i>
  118. <span>&nbsp;Save</span>
  119. </a>
  120. <a class='button is-danger' @click='cancel()'>
  121. <span>&nbspCancel</span>
  122. </a>
  123. </footer>
  124. </div>
  125. </div>
  126. </template>
  127. <script>
  128. import { Toast } from 'vue-roaster';
  129. export default {
  130. data() {
  131. return {
  132. songs: [],
  133. isEditActive: false,
  134. editing: {
  135. index: 0,
  136. song: {}
  137. },
  138. video: {
  139. player: null,
  140. paused: false,
  141. settings: function (type) {
  142. switch(type) {
  143. case 'stop':
  144. this.player.stopVideo();
  145. this.paused = true;
  146. break;
  147. case 'pause':
  148. this.player.pauseVideo();
  149. this.paused = true;
  150. break;
  151. case 'play':
  152. this.player.playVideo();
  153. this.paused = false;
  154. break;
  155. case 'skipToLast10Secs':
  156. this.player.seekTo(this.player.getDuration() - 10);
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. },
  163. methods: {
  164. changeVolume: function() {
  165. let local = this;
  166. let volume = $("#volumeSlider").val();
  167. localStorage.setItem("volume", volume);
  168. local.video.player.setVolume(volume);
  169. if (volume > 0) local.player.unMute();
  170. },
  171. toggleModal: function () {
  172. this.isEditActive = !this.isEditActive;
  173. this.video.settings('stop');
  174. },
  175. addTag: function (type) {
  176. if (type == 'genres') {
  177. for (let z = 0; z < this.editing.song.genres.length; z++) {
  178. if (this.editing.song.genres[z] == $('#new-genre').val()) return Toast.methods.addToast('Genre already exists', 3000);
  179. }
  180. if ($('#new-genre').val() !== '') this.editing.song.genres.push($('#new-genre').val());
  181. else Toast.methods.addToast('Genre cannot be empty', 3000);
  182. } else if (type == 'artists') {
  183. for (let z = 0; z < this.editing.song.artists.length; z++) {
  184. if (this.editing.song.artists[z] == $('#new-artist').val()) return Toast.methods.addToast('Artist already exists', 3000);
  185. }
  186. if ($('#new-artist').val() !== '') this.editing.song.artists.push($('#new-artist').val());
  187. else Toast.methods.addToast('Artist cannot be empty', 3000);
  188. }
  189. },
  190. removeTag: function (type, index) {
  191. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  192. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  193. },
  194. edit: function (song, index) {
  195. this.editing = { index, song };
  196. this.video.player.loadVideoById(song._id);
  197. this.isEditActive = true;
  198. },
  199. save: function (song) {
  200. let _this = this;
  201. this.socket.emit('queueSongs.update', song._id, song, function (res) {
  202. if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
  203. _this.toggleModal();
  204. });
  205. },
  206. cancel: function () {
  207. let _this = this;
  208. _this.toggleModal();
  209. },
  210. add: function (song) {
  211. this.socket.emit('songs.add', song, res => {
  212. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  213. });
  214. },
  215. remove: function (id, index) {
  216. this.songs.splice(index, 1);
  217. this.socket.emit('queueSongs.remove', id, res => {
  218. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  219. });
  220. }
  221. },
  222. ready: function () {
  223. let _this = this;
  224. let socketInterval = setInterval(() => {
  225. if (!!_this.$parent.$parent.socket) {
  226. _this.socket = _this.$parent.$parent.socket;
  227. _this.socket.emit('queueSongs.index', data => {
  228. _this.songs = data;
  229. });
  230. clearInterval(socketInterval);
  231. }
  232. }, 100);
  233. this.video.player = new YT.Player('player', {
  234. height: 315,
  235. width: 560,
  236. videoId: this.editing.song._id,
  237. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  238. events: {
  239. 'onReady': () => {
  240. let volume = parseInt(localStorage.getItem("volume"));
  241. volume = (typeof volume === "number") ? volume : 20;
  242. _this.video.player.setVolume(volume);
  243. if (volume > 0) _this.video.player.unMute();
  244. },
  245. 'onStateChange': event => {
  246. if (event.data == 1) {
  247. let youtubeDuration = _this.video.player.getDuration();
  248. youtubeDuration -= _this.editing.song.skipDuration;
  249. if (_this.editing.song.duration > youtubeDuration) this.stopVideo();
  250. }
  251. }
  252. }
  253. });
  254. let volume = parseInt(localStorage.getItem("volume"));
  255. volume = (typeof volume === "number") ? volume : 20;
  256. $("#volumeSlider").val(volume);
  257. }
  258. }
  259. </script>
  260. <style lang='scss' scoped>
  261. body { font-family: 'Roboto', sans-serif; }
  262. .thumbnail-preview {
  263. display: flex;
  264. margin: 0 auto;
  265. padding: 10px 0 20px 0;
  266. }
  267. .modal-card-body, .modal-card-foot { border-top: 0; }
  268. .label, .checkbox, h5 {
  269. font-weight: normal;
  270. }
  271. .video-container {
  272. display: flex;
  273. flex-direction: column;
  274. align-items: center;
  275. padding: 10px;
  276. iframe {
  277. pointer-events: none;
  278. }
  279. }
  280. .save-changes { color: #fff; }
  281. .song-thumbnail {
  282. display: block;
  283. max-width: 50px;
  284. margin: 0 auto;
  285. }
  286. td { vertical-align: middle; }
  287. .tag:not(:last-child) { margin-right: 5px; }
  288. </style>