main.js 3.8 KB

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