EditSong.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <template>
  2. <div>
  3. <modal title='Edit Song'>
  4. <div slot='body'>
  5. <span class="tag is-info is-medium reports" v-if="reports > 0">
  6. {{ reports }}
  7. <span v-if="reports > 1">&nbsp;Reports&nbsp;</span>
  8. <span v-else>&nbsp;Report&nbsp;</span>
  9. </span>
  10. <h5 class='has-text-centered'>Video Preview</h5>
  11. <div class='video-container'>
  12. <div id='player'></div>
  13. <div class="controls">
  14. <form action="#" class="column is-7-desktop is-4-mobile">
  15. <p style="margin-top: 0; position: relative;">
  16. <input type="range" id="volumeSlider" min="0" max="100" class="active" v-on:change="changeVolume()" v-on:input="changeVolume()">
  17. </p>
  18. </form>
  19. <p class='control has-addons'>
  20. <button class='button' @click='settings("pause")' v-if='!video.paused'>
  21. <i class='material-icons'>pause</i>
  22. </button>
  23. <button class='button' @click='settings("play")' v-if='video.paused'>
  24. <i class='material-icons'>play_arrow</i>
  25. </button>
  26. <button class='button' @click='settings("stop")'>
  27. <i class='material-icons'>stop</i>
  28. </button>
  29. <button class='button' @click='settings("skipToLast10Secs")'>
  30. <i class='material-icons'>fast_forward</i>
  31. </button>
  32. </p>
  33. </div>
  34. </div>
  35. <h5 class='has-text-centered'>Thumbnail Preview</h5>
  36. <img class='thumbnail-preview' :src='editing.song.thumbnail' onerror="this.src='/assets/notes-transparent.png'">
  37. <div class="control is-horizontal">
  38. <div class="control-label">
  39. <label class="label">Thumbnail URL</label>
  40. </div>
  41. <div class="control">
  42. <input class='input' type='text' v-model='editing.song.thumbnail'>
  43. </div>
  44. </div>
  45. <h5 class='has-text-centered'>Edit Info</h5>
  46. <p class='control'>
  47. <label class='checkbox'>
  48. <input type='checkbox' v-model='editing.song.explicit'>
  49. Explicit
  50. </label>
  51. </p>
  52. <label class='label'>Song ID & Title</label>
  53. <div class="control is-horizontal">
  54. <div class="control is-grouped">
  55. <p class='control is-expanded'>
  56. <input class='input' type='text' v-model='editing.song._id'>
  57. </p>
  58. <p class='control is-expanded'>
  59. <input class='input' type='text' v-model='editing.song.title' autofocus>
  60. </p>
  61. </div>
  62. </div>
  63. <label class='label'>Artists & Genres</label>
  64. <div class='control is-horizontal'>
  65. <div class='control is-grouped artist-genres'>
  66. <div>
  67. <p class='control has-addons'>
  68. <input class='input' id='new-artist' type='text' placeholder='Artist'>
  69. <button class='button is-info' @click='addTag("artists")'>Add Artist</button>
  70. </p>
  71. <span class='tag is-info' v-for='(index, artist) in editing.song.artists' track-by='$index'>
  72. {{ artist }}
  73. <button class='delete is-info' @click='removeTag("artists", index)'></button>
  74. </span>
  75. </div>
  76. <div>
  77. <p class='control has-addons'>
  78. <input class='input' id='new-genre' type='text' placeholder='Genre'>
  79. <button class='button is-info' @click='addTag("genres")'>Add Genre</button>
  80. </p>
  81. <span class='tag is-info' v-for='(index, genre) in editing.song.genres' track-by='$index'>
  82. {{ genre }}
  83. <button class='delete is-info' @click='removeTag("genres", index)'></button>
  84. </span>
  85. </div>
  86. </div>
  87. </div>
  88. <label class='label'>Song Duration</label>
  89. <p class='control'>
  90. <input class='input' type='text' v-model='editing.song.duration'>
  91. </p>
  92. <label class='label'>Skip Duration</label>
  93. <p class='control'>
  94. <input class='input' type='text' v-model='editing.song.skipDuration'>
  95. </p>
  96. </div>
  97. <div slot='footer'>
  98. <button class='button is-success' @click='save(editing.song, false)'>
  99. <i class='material-icons save-changes'>done</i>
  100. <span>&nbsp;Save</span>
  101. </button>
  102. <button class='button is-success' @click='save(editing.song, true)'>
  103. <i class='material-icons save-changes'>done</i>
  104. <span>&nbsp;Save and close</span>
  105. </button>
  106. <button class='button is-danger' @click='$parent.toggleModal()'>
  107. <span>&nbsp;Close</span>
  108. </button>
  109. </div>
  110. </modal>
  111. </div>
  112. </template>
  113. <script>
  114. import io from '../../io';
  115. import { Toast } from 'vue-roaster';
  116. import Modal from './Modal.vue';
  117. export default {
  118. components: { Modal },
  119. data() {
  120. return {
  121. editing: {
  122. index: 0,
  123. song: {},
  124. type: ''
  125. },
  126. reports: 0,
  127. video: {
  128. player: null,
  129. paused: false,
  130. playerReady: false
  131. }
  132. }
  133. },
  134. methods: {
  135. save: function (song, close) {
  136. let _this = this;
  137. this.socket.emit(`${_this.editing.type}.update`, song._id, song, res => {
  138. Toast.methods.addToast(res.message, 4000);
  139. if (res.status === 'success') {
  140. _this.$parent.songs.forEach(lSong => {
  141. if (song._id === lSong._id) {
  142. for (let n in song) {
  143. lSong[n] = song[n];
  144. }
  145. }
  146. });
  147. }
  148. if (close) _this.$parent.toggleModal();
  149. });
  150. },
  151. settings: function (type) {
  152. let _this = this;
  153. switch(type) {
  154. case 'stop':
  155. _this.video.player.stopVideo();
  156. _this.video.paused = true;
  157. break;
  158. case 'pause':
  159. _this.video.player.pauseVideo();
  160. _this.video.paused = true;
  161. break;
  162. case 'play':
  163. _this.video.player.playVideo();
  164. _this.video.paused = false;
  165. break;
  166. case 'skipToLast10Secs':
  167. _this.video.player.seekTo((_this.editing.song.duration - 10) + _this.editing.song.skipDuration);
  168. break;
  169. }
  170. },
  171. changeVolume: function () {
  172. let local = this;
  173. let volume = $("#volumeSlider").val();
  174. localStorage.setItem("volume", volume);
  175. local.video.player.setVolume(volume);
  176. if (volume > 0) local.video.player.unMute();
  177. },
  178. addTag: function (type) {
  179. if (type == 'genres') {
  180. let genre = $('#new-genre').val().toLowerCase().trim();
  181. if (this.editing.song.genres.indexOf(genre) !== -1) return Toast.methods.addToast('Genre already exists', 3000);
  182. if (genre) {
  183. this.editing.song.genres.push(genre);
  184. $('#new-genre').val('');
  185. } else Toast.methods.addToast('Genre cannot be empty', 3000);
  186. } else if (type == 'artists') {
  187. let artist = $('#new-artist').val();
  188. if (this.editing.song.artists.indexOf(artist) !== -1) return Toast.methods.addToast('Artist already exists', 3000);
  189. if ($('#new-artist').val() !== '') {
  190. this.editing.song.artists.push(artist);
  191. $('#new-artist').val('');
  192. } else Toast.methods.addToast('Artist cannot be empty', 3000);
  193. }
  194. },
  195. removeTag: function (type, index) {
  196. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  197. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  198. },
  199. },
  200. ready: function () {
  201. let _this = this;
  202. io.getSocket(socket => {
  203. _this.socket = socket;
  204. });
  205. setInterval(() => {
  206. if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
  207. _this.video.paused = false;
  208. _this.video.player.stopVideo();
  209. }
  210. }, 200);
  211. this.video.player = new YT.Player('player', {
  212. height: 315,
  213. width: 560,
  214. videoId: this.editing.song._id,
  215. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  216. startSeconds: _this.editing.song.skipDuration,
  217. events: {
  218. 'onReady': () => {
  219. let volume = parseInt(localStorage.getItem("volume"));
  220. volume = (typeof volume === "number") ? volume : 20;
  221. _this.video.player.seekTo(_this.editing.song.skipDuration);
  222. _this.video.player.setVolume(volume);
  223. if (volume > 0) _this.video.player.unMute();
  224. _this.playerReady = true;
  225. },
  226. 'onStateChange': event => {
  227. if (event.data === 1) {
  228. _this.video.paused = false;
  229. let youtubeDuration = _this.video.player.getDuration();
  230. youtubeDuration -= _this.editing.song.skipDuration;
  231. if (_this.editing.song.duration > youtubeDuration) {
  232. this.video.player.stopVideo();
  233. _this.video.paused = true;
  234. Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
  235. } else if (_this.editing.song.duration <= 0) {
  236. this.video.player.stopVideo();
  237. _this.video.paused = true;
  238. Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
  239. }
  240. if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
  241. _this.video.player.seekTo(10);
  242. }
  243. } else if (event.data === 2) {
  244. this.video.paused = true;
  245. }
  246. }
  247. }
  248. });
  249. let volume = parseInt(localStorage.getItem("volume"));
  250. volume = (typeof volume === "number") ? volume : 20;
  251. $("#volumeSlider").val(volume);
  252. },
  253. events: {
  254. closeModal: function () {
  255. this.$parent.modals.editSong = false;
  256. this.video.player.stopVideo();
  257. },
  258. editSong: function (song, index, type) {
  259. let _this = this;
  260. this.video.player.loadVideoById(song._id, this.editing.song.skipDuration);
  261. let newSong = {};
  262. for (let n in song) {
  263. newSong[n] = song[n];
  264. }
  265. this.editing = {
  266. index,
  267. song: newSong,
  268. type
  269. };
  270. _this.socket.emit('reports.getReportsForSong', song._id, res => {
  271. if (res.status === 'success') _this.reports = res.data;
  272. });
  273. this.$parent.toggleModal();
  274. },
  275. stopVideo: function () {
  276. this.video.player.stopVideo();
  277. }
  278. }
  279. }
  280. </script>
  281. <style type='scss' scoped>
  282. input[type=range] {
  283. -webkit-appearance: none;
  284. width: 100%;
  285. margin: 7.3px 0;
  286. }
  287. input[type=range]:focus {
  288. outline: none;
  289. }
  290. input[type=range]::-webkit-slider-runnable-track {
  291. width: 100%;
  292. height: 5.2px;
  293. cursor: pointer;
  294. box-shadow: 0;
  295. background: #c2c0c2;
  296. border-radius: 0;
  297. border: 0;
  298. }
  299. input[type=range]::-webkit-slider-thumb {
  300. box-shadow: 0;
  301. border: 0;
  302. height: 19px;
  303. width: 19px;
  304. border-radius: 15px;
  305. background: #03a9f4;
  306. cursor: pointer;
  307. -webkit-appearance: none;
  308. margin-top: -6.5px;
  309. }
  310. input[type=range]::-moz-range-track {
  311. width: 100%;
  312. height: 5.2px;
  313. cursor: pointer;
  314. box-shadow: 0;
  315. background: #c2c0c2;
  316. border-radius: 0;
  317. border: 0;
  318. }
  319. input[type=range]::-moz-range-thumb {
  320. box-shadow: 0;
  321. border: 0;
  322. height: 19px;
  323. width: 19px;
  324. border-radius: 15px;
  325. background: #03a9f4;
  326. cursor: pointer;
  327. -webkit-appearance: none;
  328. margin-top: -6.5px;
  329. }
  330. input[type=range]::-ms-track {
  331. width: 100%;
  332. height: 5.2px;
  333. cursor: pointer;
  334. box-shadow: 0;
  335. background: #c2c0c2;
  336. border-radius: 1.3px;
  337. }
  338. input[type=range]::-ms-fill-lower {
  339. background: #c2c0c2;
  340. border: 0;
  341. border-radius: 0;
  342. box-shadow: 0;
  343. }
  344. input[type=range]::-ms-fill-upper {
  345. background: #c2c0c2;
  346. border: 0;
  347. border-radius: 0;
  348. box-shadow: 0;
  349. }
  350. input[type=range]::-ms-thumb {
  351. box-shadow: 0;
  352. border: 0;
  353. height: 15px;
  354. width: 15px;
  355. border-radius: 15px;
  356. background: #03a9f4;
  357. cursor: pointer;
  358. -webkit-appearance: none;
  359. margin-top: 1.5px;
  360. }
  361. .controls {
  362. display: flex;
  363. flex-direction: column;
  364. align-items: center;
  365. }
  366. .artist-genres {
  367. display: flex;
  368. justify-content: space-between;
  369. }
  370. #volumeSlider { margin-bottom: 15px; }
  371. .has-text-centered { padding: 10px; }
  372. .thumbnail-preview {
  373. display: flex;
  374. margin: 0 auto 25px auto;
  375. max-width: 200px;
  376. width: 100%;
  377. }
  378. .modal-card-body, .modal-card-foot { border-top: 0; }
  379. .label, .checkbox, h5 {
  380. font-weight: normal;
  381. }
  382. .video-container {
  383. display: flex;
  384. flex-direction: column;
  385. align-items: center;
  386. padding: 10px;
  387. iframe { pointer-events: none; }
  388. }
  389. .save-changes { color: #fff; }
  390. .tag:not(:last-child) { margin-right: 5px; }
  391. .reports {
  392. margin: 0 auto;
  393. display: flex;
  394. }
  395. </style>