Songs.vue 8.9 KB

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