main.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.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/index.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. socket.on("keep.event:user.preferences.changed", preferences => {
  148. store.dispatch(
  149. "user/preferences/changeAutoSkipDisliked",
  150. preferences.autoSkipDisliked
  151. );
  152. store.dispatch(
  153. "user/preferences/changeNightmode",
  154. preferences.nightmode
  155. );
  156. store.dispatch(
  157. "user/preferences/changeActivityLogPublic",
  158. preferences.activityLogPublic
  159. );
  160. });
  161. });
  162. });
  163. router.beforeEach((to, from, next) => {
  164. if (window.stationInterval) {
  165. clearInterval(window.stationInterval);
  166. window.stationInterval = 0;
  167. }
  168. if (window.socket) io.removeAllListeners();
  169. io.clear();
  170. if (to.meta.loginRequired || to.meta.adminRequired) {
  171. const gotData = () => {
  172. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  173. next({ path: "/login" });
  174. else if (
  175. to.meta.adminRequired &&
  176. store.state.user.auth.role !== "admin"
  177. )
  178. next({ path: "/" });
  179. else next();
  180. };
  181. if (store.state.user.auth.gotData) gotData();
  182. else {
  183. const watcher = store.watch(
  184. state => state.user.auth.gotData,
  185. () => {
  186. watcher();
  187. gotData();
  188. }
  189. );
  190. }
  191. } else next();
  192. });
  193. Vue.directive("click-outside", {
  194. bind(element, binding) {
  195. window.handleOutsideClick = event => {
  196. if (!(element === event.target || element.contains(event.target))) {
  197. binding.value();
  198. }
  199. };
  200. document.body.addEventListener("click", window.handleOutsideClick);
  201. },
  202. unbind() {
  203. document.body.removeEventListener("click", window.handleOutsideClick);
  204. }
  205. });
  206. // eslint-disable-next-line no-new
  207. new Vue({
  208. router,
  209. store,
  210. el: "#root",
  211. render: wrapper => wrapper(App)
  212. });