main.js 3.6 KB

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