main.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. });
  148. });
  149. router.beforeEach((to, from, next) => {
  150. if (window.stationInterval) {
  151. clearInterval(window.stationInterval);
  152. window.stationInterval = 0;
  153. }
  154. if (window.socket) io.removeAllListeners();
  155. io.clear();
  156. if (to.meta.loginRequired || to.meta.adminRequired) {
  157. const gotData = () => {
  158. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  159. next({ path: "/login" });
  160. else if (
  161. to.meta.adminRequired &&
  162. store.state.user.auth.role !== "admin"
  163. )
  164. next({ path: "/" });
  165. else next();
  166. };
  167. if (store.state.user.auth.gotData) gotData();
  168. else {
  169. const watcher = store.watch(
  170. state => state.user.auth.gotData,
  171. () => {
  172. watcher();
  173. gotData();
  174. }
  175. );
  176. }
  177. } else next();
  178. });
  179. // eslint-disable-next-line no-new
  180. new Vue({
  181. router,
  182. store,
  183. el: "#root",
  184. render: wrapper => wrapper(App)
  185. });