EditSong.vue 11 KB

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