main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. meta: {
  94. loginRequired: true
  95. }
  96. },
  97. {
  98. path: "/set_password",
  99. props: { mode: "set" },
  100. component: () => import("./pages/ResetPassword.vue"),
  101. meta: {
  102. loginRequired: true
  103. }
  104. },
  105. {
  106. path: "/login",
  107. component: () => import("./components/modals/Login.vue")
  108. },
  109. {
  110. path: "/register",
  111. component: () => import("./components/modals/Register.vue")
  112. },
  113. {
  114. path: "/admin",
  115. component: () => import("./pages/Admin/index.vue"),
  116. meta: {
  117. adminRequired: true
  118. }
  119. },
  120. {
  121. path: "/admin/:page",
  122. component: () => import("./pages/Admin/index.vue"),
  123. meta: {
  124. adminRequired: true
  125. }
  126. },
  127. {
  128. name: "station",
  129. path: "/:id",
  130. component: () => import("./pages/Station/index.vue")
  131. }
  132. ]
  133. });
  134. lofig.folder = "../config/default.json";
  135. lofig.get("serverDomain").then(serverDomain => {
  136. io.init(serverDomain);
  137. io.getSocket(socket => {
  138. socket.on("ready", (loggedIn, role, username, userId) => {
  139. store.dispatch("user/auth/authData", {
  140. loggedIn,
  141. role,
  142. username,
  143. userId
  144. });
  145. });
  146. socket.on("keep.event:banned", ban => {
  147. store.dispatch("user/auth/banUser", ban);
  148. });
  149. socket.on("event:user.username.changed", username => {
  150. store.dispatch("user/auth/updateUsername", username);
  151. });
  152. });
  153. });
  154. router.beforeEach((to, from, next) => {
  155. if (window.stationInterval) {
  156. clearInterval(window.stationInterval);
  157. window.stationInterval = 0;
  158. }
  159. if (window.socket) io.removeAllListeners();
  160. io.clear();
  161. if (to.meta.loginRequired || to.meta.adminRequired) {
  162. const gotData = () => {
  163. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  164. next({ path: "/login" });
  165. else if (
  166. to.meta.adminRequired &&
  167. store.state.user.auth.role !== "admin"
  168. )
  169. next({ path: "/" });
  170. else next();
  171. };
  172. if (store.state.user.auth.gotData) gotData();
  173. else {
  174. const watcher = store.watch(
  175. state => state.user.auth.gotData,
  176. () => {
  177. watcher();
  178. gotData();
  179. }
  180. );
  181. }
  182. } else next();
  183. });
  184. // eslint-disable-next-line no-new
  185. new Vue({
  186. router,
  187. store,
  188. el: "#root",
  189. render: wrapper => wrapper(App)
  190. });