main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 REQUIRED_CONFIG_VERSION = 1;
  7. const handleMetadata = attrs => {
  8. document.title = `Musare | ${attrs.title}`;
  9. };
  10. Vue.component("Metadata", {
  11. watch: {
  12. $attrs: {
  13. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  14. handler: attrs => {
  15. handleMetadata(attrs);
  16. },
  17. deep: true,
  18. immediate: true
  19. }
  20. },
  21. render() {
  22. return null;
  23. }
  24. });
  25. Vue.use(VueRouter);
  26. Vue.directive("scroll", {
  27. inserted(el, binding) {
  28. const f = evt => {
  29. clearTimeout(window.scrollDebounceId);
  30. window.scrollDebounceId = setTimeout(() => {
  31. if (binding.value(evt, el)) {
  32. window.removeEventListener("scroll", f);
  33. }
  34. }, 200);
  35. };
  36. window.addEventListener("scroll", f);
  37. }
  38. });
  39. Vue.directive("focus", {
  40. inserted(el) {
  41. window.focusedElementBefore = document.activeElement;
  42. el.focus();
  43. }
  44. });
  45. const router = new VueRouter({
  46. mode: "history",
  47. routes: [
  48. {
  49. path: "/",
  50. component: () => import("./pages/Home.vue")
  51. },
  52. {
  53. path: "/404",
  54. alias: ["*"],
  55. component: () => import("./pages/404.vue")
  56. },
  57. {
  58. path: "/terms",
  59. component: () => import("./pages/Terms.vue")
  60. },
  61. {
  62. path: "/privacy",
  63. component: () => import("./pages/Privacy.vue")
  64. },
  65. {
  66. path: "/team",
  67. component: () => import("./pages/Team.vue")
  68. },
  69. {
  70. path: "/news",
  71. component: () => import("./pages/News.vue")
  72. },
  73. {
  74. path: "/about",
  75. component: () => import("./pages/About.vue")
  76. },
  77. {
  78. name: "profile",
  79. path: "/u/:username",
  80. component: () => import("./pages/Profile/index.vue")
  81. },
  82. {
  83. path: "/settings",
  84. component: () => import("./pages/Settings/index.vue"),
  85. meta: {
  86. loginRequired: true
  87. }
  88. },
  89. {
  90. path: "/reset_password",
  91. component: () => import("./pages/ResetPassword.vue")
  92. },
  93. {
  94. path: "/set_password",
  95. props: { mode: "set" },
  96. component: () => import("./pages/ResetPassword.vue"),
  97. meta: {
  98. loginRequired: true
  99. }
  100. },
  101. {
  102. path: "/login",
  103. component: () => import("./components/modals/Login.vue")
  104. },
  105. {
  106. path: "/register",
  107. component: () => import("./components/modals/Register.vue")
  108. },
  109. {
  110. path: "/admin",
  111. component: () => import("./pages/Admin/index.vue"),
  112. meta: {
  113. adminRequired: true
  114. }
  115. },
  116. {
  117. path: "/admin/:page",
  118. component: () => import("./pages/Admin/index.vue"),
  119. meta: {
  120. adminRequired: true
  121. }
  122. },
  123. {
  124. name: "station",
  125. path: "/:id",
  126. component: () => import("./pages/Station/index.vue")
  127. }
  128. ]
  129. });
  130. lofig.folder = "../config/default.json";
  131. lofig.fetchConfig().then(config => {
  132. const { configVersion, skipConfigVersionCheck } = config;
  133. if (configVersion !== REQUIRED_CONFIG_VERSION && !skipConfigVersionCheck) {
  134. // eslint-disable-next-line no-alert
  135. alert(
  136. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  137. );
  138. window.stop();
  139. }
  140. const { serverDomain } = config;
  141. io.init(serverDomain);
  142. io.getSocket(socket => {
  143. socket.on("ready", (loggedIn, role, username, userId) => {
  144. store.dispatch("user/auth/authData", {
  145. loggedIn,
  146. role,
  147. username,
  148. userId
  149. });
  150. });
  151. socket.on("keep.event:banned", ban =>
  152. store.dispatch("user/auth/banUser", ban)
  153. );
  154. socket.on("event:user.username.changed", username =>
  155. store.dispatch("user/auth/updateUsername", username)
  156. );
  157. socket.on("keep.event:user.preferences.changed", preferences => {
  158. store.dispatch(
  159. "user/preferences/changeAutoSkipDisliked",
  160. preferences.autoSkipDisliked
  161. );
  162. store.dispatch(
  163. "user/preferences/changeNightmode",
  164. preferences.nightmode
  165. );
  166. store.dispatch(
  167. "user/preferences/changeActivityLogPublic",
  168. preferences.activityLogPublic
  169. );
  170. });
  171. });
  172. });
  173. router.beforeEach((to, from, next) => {
  174. if (window.stationInterval) {
  175. clearInterval(window.stationInterval);
  176. window.stationInterval = 0;
  177. }
  178. if (window.socket) io.removeAllListeners();
  179. io.clear();
  180. if (to.meta.loginRequired || to.meta.adminRequired) {
  181. const gotData = () => {
  182. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  183. next({ path: "/login" });
  184. else if (
  185. to.meta.adminRequired &&
  186. store.state.user.auth.role !== "admin"
  187. )
  188. next({ path: "/" });
  189. else next();
  190. };
  191. if (store.state.user.auth.gotData) gotData();
  192. else {
  193. const watcher = store.watch(
  194. state => state.user.auth.gotData,
  195. () => {
  196. watcher();
  197. gotData();
  198. }
  199. );
  200. }
  201. } else next();
  202. });
  203. Vue.directive("click-outside", {
  204. bind(element, binding) {
  205. window.handleOutsideClick = event => {
  206. if (!(element === event.target || element.contains(event.target))) {
  207. binding.value();
  208. }
  209. };
  210. document.body.addEventListener("click", window.handleOutsideClick);
  211. },
  212. unbind() {
  213. document.body.removeEventListener("click", window.handleOutsideClick);
  214. }
  215. });
  216. // eslint-disable-next-line no-new
  217. new Vue({
  218. router,
  219. store,
  220. el: "#root",
  221. render: wrapper => wrapper(App)
  222. });