main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import Vue from "vue";
  2. import VueRouter from "vue-router";
  3. import store from "./store";
  4. import App from "./App.vue";
  5. import io from "./io";
  6. const handleMetadata = attrs => {
  7. document.title = `Musare | ${attrs.title}`;
  8. };
  9. Vue.component("Metadata", {
  10. watch: {
  11. $attrs: {
  12. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  13. handler: attrs => {
  14. handleMetadata(attrs);
  15. },
  16. deep: true,
  17. immediate: true
  18. }
  19. },
  20. render() {
  21. return null;
  22. }
  23. });
  24. Vue.use(VueRouter);
  25. Vue.directive("scroll", {
  26. inserted(el, binding) {
  27. const f = evt => {
  28. clearTimeout(window.scrollDebounceId);
  29. window.scrollDebounceId = setTimeout(() => {
  30. if (binding.value(evt, el)) {
  31. window.removeEventListener("scroll", f);
  32. }
  33. }, 200);
  34. };
  35. window.addEventListener("scroll", f);
  36. }
  37. });
  38. Vue.directive("focus", {
  39. inserted(el) {
  40. window.focusedElementBefore = document.activeElement;
  41. el.focus();
  42. }
  43. });
  44. const router = new VueRouter({
  45. mode: "history",
  46. routes: [
  47. {
  48. path: "/",
  49. component: () => import("./pages/Home/index.vue")
  50. },
  51. {
  52. path: "/404",
  53. alias: ["*"],
  54. component: () => import("./pages/404.vue")
  55. },
  56. {
  57. path: "/terms",
  58. component: () => import("./pages/Terms.vue")
  59. },
  60. {
  61. path: "/privacy",
  62. component: () => import("./pages/Privacy.vue")
  63. },
  64. {
  65. path: "/team",
  66. component: () => import("./pages/Team.vue")
  67. },
  68. {
  69. path: "/news",
  70. component: () => import("./pages/News.vue")
  71. },
  72. {
  73. path: "/about",
  74. component: () => import("./pages/About.vue")
  75. },
  76. {
  77. name: "profile",
  78. path: "/u/:username",
  79. component: () => import("./pages/Profile.vue")
  80. },
  81. {
  82. path: "/settings",
  83. component: () => import("./pages/Settings/index.vue"),
  84. meta: {
  85. loginRequired: true
  86. }
  87. },
  88. {
  89. path: "/reset_password",
  90. component: () => import("./pages/ResetPassword.vue")
  91. },
  92. {
  93. path: "/set_password",
  94. props: { mode: "set" },
  95. component: () => import("./pages/ResetPassword.vue"),
  96. meta: {
  97. loginRequired: true
  98. }
  99. },
  100. {
  101. path: "/login",
  102. component: () => import("./components/modals/Login.vue")
  103. },
  104. {
  105. path: "/register",
  106. component: () => import("./components/modals/Register.vue")
  107. },
  108. {
  109. path: "/admin",
  110. component: () => import("./pages/Admin/index.vue"),
  111. meta: {
  112. adminRequired: true
  113. }
  114. },
  115. {
  116. path: "/admin/:page",
  117. component: () => import("./pages/Admin/index.vue"),
  118. meta: {
  119. adminRequired: true
  120. }
  121. },
  122. {
  123. name: "station",
  124. path: "/:id",
  125. component: () => import("./pages/Station/index.vue")
  126. }
  127. ]
  128. });
  129. lofig.folder = "../config/default.json";
  130. lofig.get("serverDomain").then(serverDomain => {
  131. io.init(serverDomain);
  132. io.getSocket(socket => {
  133. socket.on("ready", (loggedIn, role, username, userId) => {
  134. store.dispatch("user/auth/authData", {
  135. loggedIn,
  136. role,
  137. username,
  138. userId
  139. });
  140. });
  141. socket.on("keep.event:banned", ban =>
  142. store.dispatch("user/auth/banUser", ban)
  143. );
  144. socket.on("event:user.username.changed", username =>
  145. store.dispatch("user/auth/updateUsername", username)
  146. );
  147. socket.on("keep.event:user.preferences.changed", preferences => {
  148. console.log("changed");
  149. store.dispatch(
  150. "user/preferences/changeAutoSkipDisliked",
  151. preferences.autoSkipDisliked
  152. );
  153. store.dispatch(
  154. "user/preferences/changeNightmode",
  155. preferences.nightmode
  156. );
  157. });
  158. });
  159. });
  160. router.beforeEach((to, from, next) => {
  161. if (window.stationInterval) {
  162. clearInterval(window.stationInterval);
  163. window.stationInterval = 0;
  164. }
  165. if (window.socket) io.removeAllListeners();
  166. io.clear();
  167. if (to.meta.loginRequired || to.meta.adminRequired) {
  168. const gotData = () => {
  169. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  170. next({ path: "/login" });
  171. else if (
  172. to.meta.adminRequired &&
  173. store.state.user.auth.role !== "admin"
  174. )
  175. next({ path: "/" });
  176. else next();
  177. };
  178. if (store.state.user.auth.gotData) gotData();
  179. else {
  180. const watcher = store.watch(
  181. state => state.user.auth.gotData,
  182. () => {
  183. watcher();
  184. gotData();
  185. }
  186. );
  187. }
  188. } else next();
  189. });
  190. // eslint-disable-next-line no-new
  191. new Vue({
  192. router,
  193. store,
  194. el: "#root",
  195. render: wrapper => wrapper(App)
  196. });