main.js 3.9 KB

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