main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import Vue from "vue";
  2. import VueTippy, { TippyComponent } from "vue-tippy";
  3. import VueRouter from "vue-router";
  4. import ws from "@/ws";
  5. import store from "./store";
  6. import App from "./App.vue";
  7. const REQUIRED_CONFIG_VERSION = 5;
  8. const handleMetadata = attrs => {
  9. document.title = `Musare | ${attrs.title}`;
  10. };
  11. Vue.use(VueTippy, {
  12. directive: "tippy", // => v-tippy
  13. flipDuration: 0,
  14. touch: false,
  15. popperOptions: {
  16. modifiers: {
  17. preventOverflow: {
  18. enabled: true
  19. }
  20. }
  21. },
  22. allowHTML: true,
  23. animation: "scale",
  24. theme: "dark",
  25. arrow: true
  26. });
  27. Vue.component("Tippy", TippyComponent);
  28. Vue.component("Metadata", {
  29. watch: {
  30. $attrs: {
  31. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  32. handler: attrs => {
  33. handleMetadata(attrs);
  34. },
  35. deep: true,
  36. immediate: true
  37. }
  38. },
  39. render() {
  40. return null;
  41. }
  42. });
  43. Vue.use(VueRouter);
  44. Vue.directive("scroll", {
  45. inserted(el, binding) {
  46. const f = evt => {
  47. clearTimeout(window.scrollDebounceId);
  48. window.scrollDebounceId = setTimeout(() => {
  49. if (binding.value(evt, el)) {
  50. window.removeEventListener("scroll", f);
  51. }
  52. }, 200);
  53. };
  54. window.addEventListener("scroll", f);
  55. }
  56. });
  57. Vue.directive("focus", {
  58. inserted(el) {
  59. window.focusedElementBefore = document.activeElement;
  60. el.focus();
  61. }
  62. });
  63. const router = new VueRouter({
  64. mode: "history",
  65. routes: [
  66. {
  67. path: "/",
  68. component: () => import("@/pages/Home.vue")
  69. },
  70. {
  71. path: "/404",
  72. alias: ["*"],
  73. component: () => import("@/pages/404.vue")
  74. },
  75. {
  76. path: "/terms",
  77. component: () => import("@/pages/Terms.vue")
  78. },
  79. {
  80. path: "/privacy",
  81. component: () => import("@/pages/Privacy.vue")
  82. },
  83. {
  84. path: "/team",
  85. component: () => import("@/pages/Team.vue")
  86. },
  87. {
  88. path: "/news",
  89. component: () => import("@/pages/News.vue")
  90. },
  91. {
  92. path: "/about",
  93. component: () => import("@/pages/About.vue")
  94. },
  95. {
  96. name: "profile",
  97. path: "/u/:username",
  98. component: () => import("@/pages/Profile/index.vue")
  99. },
  100. {
  101. path: "/settings",
  102. component: () => import("@/pages/Settings/index.vue"),
  103. meta: {
  104. loginRequired: true
  105. }
  106. },
  107. {
  108. path: "/reset_password",
  109. component: () => import("@/pages/ResetPassword.vue")
  110. },
  111. {
  112. path: "/set_password",
  113. props: { mode: "set" },
  114. component: () => import("@/pages/ResetPassword.vue"),
  115. meta: {
  116. loginRequired: true
  117. }
  118. },
  119. {
  120. path: "/login",
  121. component: () => import("@/components/modals/Login.vue"),
  122. meta: {
  123. guestsOnly: true
  124. }
  125. },
  126. {
  127. path: "/register",
  128. component: () => import("@/components/modals/Register.vue"),
  129. meta: {
  130. guestsOnly: true
  131. }
  132. },
  133. {
  134. path: "/admin",
  135. component: () => import("@/pages/Admin/index.vue"),
  136. meta: {
  137. adminRequired: true
  138. }
  139. },
  140. {
  141. path: "/admin/:page",
  142. component: () => import("@/pages//Admin/index.vue"),
  143. meta: {
  144. adminRequired: true
  145. }
  146. },
  147. {
  148. name: "station",
  149. path: "/:id",
  150. component: () => import("@/pages//Station/index.vue")
  151. }
  152. ]
  153. });
  154. lofig.folder = "../config/default.json";
  155. (async () => {
  156. lofig.fetchConfig().then(config => {
  157. const { configVersion, skipConfigVersionCheck } = config;
  158. if (
  159. configVersion !== REQUIRED_CONFIG_VERSION &&
  160. !skipConfigVersionCheck
  161. ) {
  162. // eslint-disable-next-line no-alert
  163. alert(
  164. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  165. );
  166. window.stop();
  167. }
  168. });
  169. const websocketsDomain = await lofig.get("websocketsDomain");
  170. ws.init(websocketsDomain);
  171. ws.socket.on("ready", res => {
  172. const { loggedIn, role, username, userId, email } = res.data;
  173. store.dispatch("user/auth/authData", {
  174. loggedIn,
  175. role,
  176. username,
  177. email,
  178. userId
  179. });
  180. });
  181. ws.socket.on("keep.event:user.banned", res =>
  182. store.dispatch("user/auth/banUser", res.data.ban)
  183. );
  184. ws.socket.on("event:user.username.updated", res =>
  185. store.dispatch("user/auth/updateUsername", res.data.username)
  186. );
  187. ws.socket.on("keep.event:user.preferences.updated", res => {
  188. const { preferences } = res.data;
  189. store.dispatch(
  190. "user/preferences/changeAutoSkipDisliked",
  191. preferences.autoSkipDisliked
  192. );
  193. store.dispatch(
  194. "user/preferences/changeNightmode",
  195. preferences.nightmode
  196. );
  197. store.dispatch(
  198. "user/preferences/changeActivityLogPublic",
  199. preferences.activityLogPublic
  200. );
  201. store.dispatch(
  202. "user/preferences/changeAnonymousSongRequests",
  203. preferences.anonymousSongRequests
  204. );
  205. store.dispatch(
  206. "user/preferences/changeActivityWatch",
  207. preferences.activityWatch
  208. );
  209. });
  210. router.beforeEach((to, from, next) => {
  211. if (window.stationInterval) {
  212. clearInterval(window.stationInterval);
  213. window.stationInterval = 0;
  214. }
  215. if (ws.socket) {
  216. ws.clearCallbacks();
  217. ws.destroyListeners();
  218. }
  219. if (
  220. to.meta.loginRequired ||
  221. to.meta.adminRequired ||
  222. to.meta.guestsOnly
  223. ) {
  224. const gotData = () => {
  225. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  226. next({ path: "/login" });
  227. else if (
  228. to.meta.adminRequired &&
  229. store.state.user.auth.role !== "admin"
  230. )
  231. next({ path: "/" });
  232. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  233. next({ path: "/" });
  234. else next();
  235. };
  236. if (store.state.user.auth.gotData) gotData();
  237. else {
  238. const watcher = store.watch(
  239. state => state.user.auth.gotData,
  240. () => {
  241. watcher();
  242. gotData();
  243. }
  244. );
  245. }
  246. } else next();
  247. });
  248. Vue.directive("click-outside", {
  249. bind(element, binding) {
  250. window.handleOutsideClick = event => {
  251. if (
  252. !(
  253. element === event.target ||
  254. element.contains(event.target)
  255. )
  256. ) {
  257. binding.value();
  258. }
  259. };
  260. document.body.addEventListener("click", window.handleOutsideClick);
  261. },
  262. unbind() {
  263. document.body.removeEventListener(
  264. "click",
  265. window.handleOutsideClick
  266. );
  267. }
  268. });
  269. // eslint-disable-next-line no-new
  270. new Vue({
  271. router,
  272. store,
  273. el: "#root",
  274. render: wrapper => wrapper(App)
  275. });
  276. })();