main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. const router = new VueRouter({
  39. mode: "history",
  40. routes: [
  41. {
  42. path: "/",
  43. component: () => import("./components/pages/Home.vue")
  44. },
  45. {
  46. path: "*",
  47. component: () => import("./components/404.vue")
  48. },
  49. {
  50. path: "/404",
  51. component: () => import("./components/404.vue")
  52. },
  53. {
  54. path: "/terms",
  55. component: () => import("./components/pages/Terms.vue")
  56. },
  57. {
  58. path: "/privacy",
  59. component: () => import("./components/pages/Privacy.vue")
  60. },
  61. {
  62. path: "/team",
  63. component: () => import("./components/pages/Team.vue")
  64. },
  65. {
  66. path: "/news",
  67. component: () => import("./components/pages/News.vue")
  68. },
  69. {
  70. path: "/about",
  71. component: () => import("./components/pages/About.vue")
  72. },
  73. {
  74. name: "profile",
  75. path: "/u/:username",
  76. component: () => import("./components/User/Show.vue")
  77. },
  78. {
  79. path: "/settings",
  80. component: () => import("./components/User/Settings.vue"),
  81. meta: {
  82. loginRequired: true
  83. }
  84. },
  85. {
  86. path: "/reset_password",
  87. component: () => import("./components/User/ResetPassword.vue")
  88. },
  89. {
  90. path: "/login",
  91. component: () => import("./components/Modals/Login.vue")
  92. },
  93. {
  94. path: "/register",
  95. component: () => import("./components/Modals/Register.vue")
  96. },
  97. {
  98. path: "/admin",
  99. component: () => import("./components/pages/Admin.vue"),
  100. meta: {
  101. adminRequired: true
  102. }
  103. },
  104. {
  105. path: "/admin/:page",
  106. component: () => import("./components/pages/Admin.vue"),
  107. meta: {
  108. adminRequired: true
  109. }
  110. },
  111. {
  112. name: "station",
  113. path: "/:id",
  114. component: () => import("./components/Station/Station.vue")
  115. }
  116. ]
  117. });
  118. lofig.folder = "../config/default.json";
  119. lofig.get("serverDomain").then(serverDomain => {
  120. io.init(serverDomain);
  121. io.getSocket(socket => {
  122. socket.on("ready", (loggedIn, role, username, userId) => {
  123. store.dispatch("user/auth/authData", {
  124. loggedIn,
  125. role,
  126. username,
  127. userId
  128. });
  129. });
  130. socket.on("keep.event:banned", ban => {
  131. store.dispatch("user/auth/banUser", ban);
  132. });
  133. socket.on("event:user.username.changed", username => {
  134. store.dispatch("user/auth/updateUsername", username);
  135. });
  136. });
  137. });
  138. router.beforeEach((to, from, next) => {
  139. if (window.stationInterval) {
  140. clearInterval(window.stationInterval);
  141. window.stationInterval = 0;
  142. }
  143. if (window.socket) io.removeAllListeners();
  144. io.clear();
  145. if (to.meta.loginRequired || to.meta.adminRequired) {
  146. const gotData = () => {
  147. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  148. next({ path: "/login" });
  149. else if (
  150. to.meta.adminRequired &&
  151. store.state.user.auth.role !== "admin"
  152. )
  153. next({ path: "/" });
  154. else next();
  155. };
  156. if (store.state.user.auth.gotData) gotData();
  157. else {
  158. const watcher = store.watch(
  159. state => state.user.auth.gotData,
  160. () => {
  161. watcher();
  162. gotData();
  163. }
  164. );
  165. }
  166. } else next();
  167. });
  168. // eslint-disable-next-line no-new
  169. new Vue({
  170. router,
  171. store,
  172. el: "#root",
  173. render: wrapper => wrapper(App)
  174. });