editSong.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* eslint no-param-reassign: 0 */
  2. export default {
  3. namespaced: true,
  4. state: {
  5. video: {
  6. player: null,
  7. paused: true,
  8. playerReady: false,
  9. autoPlayed: false,
  10. currentTime: 0,
  11. playbackRate: 1
  12. },
  13. youtubeId: null,
  14. song: {},
  15. originalSong: {},
  16. reports: [],
  17. tab: "discogs",
  18. newSong: false
  19. },
  20. getters: {},
  21. actions: {
  22. init: ({ commit }, { song }) => commit("editSong", song),
  23. showTab: ({ commit }, tab) => commit("showTab", tab),
  24. editSong: ({ commit }, song) => commit("editSong", song),
  25. setSong: ({ commit }, song) => commit("setSong", song),
  26. updateOriginalSong: ({ commit }, song) =>
  27. commit("updateOriginalSong", song),
  28. resetSong: ({ commit }, youtubeId) => commit("resetSong", youtubeId),
  29. stopVideo: ({ commit }) => commit("stopVideo"),
  30. hardStopVideo: ({ commit }) => commit("hardStopVideo"),
  31. loadVideoById: ({ commit }, id, skipDuration) =>
  32. commit("loadVideoById", id, skipDuration),
  33. pauseVideo: ({ commit }, status) => commit("pauseVideo", status),
  34. getCurrentTime: ({ commit, state }, fixedVal) =>
  35. new Promise(resolve => {
  36. commit("getCurrentTime", fixedVal);
  37. resolve(state.video.currentTime);
  38. }),
  39. updateSongField: ({ commit }, data) => commit("updateSongField", data),
  40. selectDiscogsInfo: ({ commit }, discogsInfo) =>
  41. commit("selectDiscogsInfo", discogsInfo),
  42. updateReports: ({ commit }, reports) =>
  43. commit("updateReports", reports),
  44. resolveReport: ({ commit }, reportId) =>
  45. commit("resolveReport", reportId),
  46. updateYoutubeId: ({ commit }, youtubeId) => {
  47. commit("updateYoutubeId", youtubeId);
  48. commit("loadVideoById", youtubeId, 0);
  49. },
  50. updateTitle: ({ commit }, title) => commit("updateTitle", title),
  51. updateThumbnail: ({ commit }, thumbnail) =>
  52. commit("updateThumbnail", thumbnail),
  53. setPlaybackRate: ({ commit }, rate) => commit("setPlaybackRate", rate)
  54. },
  55. mutations: {
  56. showTab(state, tab) {
  57. state.tab = tab;
  58. },
  59. editSong(state, song) {
  60. state.newSong = !!song.newSong || !song._id;
  61. state.youtubeId = song.youtubeId || null;
  62. state.prefillData = song.prefill ? song.prefill : {};
  63. },
  64. setSong(state, song) {
  65. if (song.discogs === undefined) song.discogs = null;
  66. state.originalSong = JSON.parse(JSON.stringify(song));
  67. state.song = JSON.parse(JSON.stringify(song));
  68. state.newSong = !song._id;
  69. state.youtubeId = song.youtubeId;
  70. },
  71. updateOriginalSong(state, song) {
  72. state.originalSong = JSON.parse(JSON.stringify(song));
  73. },
  74. resetSong(state, youtubeId) {
  75. if (state.youtubeId === youtubeId) state.youtubeId = "";
  76. if (state.song && state.song.youtubeId === youtubeId)
  77. state.song = {};
  78. if (
  79. state.originalSong &&
  80. state.originalSong.youtubeId === youtubeId
  81. )
  82. state.originalSong = {};
  83. },
  84. stopVideo(state) {
  85. if (state.video.player && state.video.player.pauseVideo) {
  86. state.video.player.pauseVideo();
  87. state.video.player.seekTo(0);
  88. }
  89. },
  90. hardStopVideo(state) {
  91. if (state.video.player && state.video.player.stopVideo) {
  92. state.video.player.stopVideo();
  93. }
  94. },
  95. loadVideoById(state, id, skipDuration) {
  96. state.song.duration = -1;
  97. state.video.player.loadVideoById(id, skipDuration);
  98. },
  99. pauseVideo(state, status) {
  100. if (
  101. (state.video.player && state.video.player.pauseVideo) ||
  102. state.video.playVideo
  103. ) {
  104. if (status) state.video.player.pauseVideo();
  105. else state.video.player.playVideo();
  106. }
  107. state.video.paused = status;
  108. },
  109. getCurrentTime(state, fixedVal) {
  110. if (!state.playerReady) state.video.currentTime = 0;
  111. else {
  112. Promise.resolve(state.video.player.getCurrentTime()).then(
  113. time => {
  114. if (fixedVal)
  115. Promise.resolve(time.toFixed(fixedVal)).then(
  116. fixedTime => {
  117. state.video.currentTime = fixedTime;
  118. }
  119. );
  120. else state.video.currentTime = time;
  121. }
  122. );
  123. }
  124. },
  125. updateSongField(state, data) {
  126. state.song[data.field] = data.value;
  127. },
  128. selectDiscogsInfo(state, discogsInfo) {
  129. state.song.discogs = discogsInfo;
  130. },
  131. updateReports(state, reports) {
  132. state.reports = reports;
  133. },
  134. resolveReport(state, reportId) {
  135. state.reports = state.reports.filter(
  136. report => report._id !== reportId
  137. );
  138. },
  139. updateYoutubeId(state, youtubeId) {
  140. state.song.youtubeId = youtubeId;
  141. },
  142. updateTitle(state, title) {
  143. state.song.title = title;
  144. },
  145. updateThumbnail(state, thumbnail) {
  146. state.song.thumbnail = thumbnail;
  147. },
  148. setPlaybackRate(state, rate) {
  149. if (rate) {
  150. state.video.playbackRate = rate;
  151. state.video.player.setPlaybackRate(rate);
  152. } else if (
  153. state.video.player.getPlaybackRate() !== undefined &&
  154. state.video.playbackRate !==
  155. state.video.player.getPlaybackRate()
  156. ) {
  157. state.video.player.setPlaybackRate(state.video.playbackRate);
  158. state.video.playbackRate = state.video.player.getPlaybackRate();
  159. }
  160. }
  161. }
  162. };