main.js 3.8 KB

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