main.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import IO from 'socket.io-client'
  2. window.socket = IO()
  3. socket.on('disconnect', () => {
  4. socket.connect('http://localhost:3000', {
  5. reconnection: true,
  6. reconnectionDelay: 1000,
  7. reconnectionDelayMax : 5000,
  8. reconnectionAttempts: Infinity
  9. } );
  10. })
  11. window.onload = () => {
  12. let div = document.createElement('div');
  13. div.innerHTML =
  14. `<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.10.0/styles/default.min.css">`;
  15. document.head.append(...div.children);
  16. }
  17. import Vue from 'vue'
  18. import VueRouter from 'vue-router'
  19. import Vuex from 'vuex'
  20. import axios from 'axios'
  21. import VueAxios from 'vue-axios'
  22. import linkExpander from './assets/js/linkExpander'
  23. import App from './App'
  24. import store from './store/index'
  25. const Index = () => import('./components/routes/Index')
  26. const P = () => import('./components/routes/P')
  27. const Start = () => import('./components/routes/Start')
  28. const Thread = () => import('./components/routes/Thread')
  29. const ThreadNew = () => import('./components/routes/ThreadNew')
  30. const Search = () => import('./components/routes/Search')
  31. const User = () => import('./components/routes/User')
  32. const UserPosts = () => import('./components/routes/UserPosts')
  33. const UserThreads = () => import('./components/routes/UserThreads')
  34. const Settings = () => import('./components/routes/Settings')
  35. const SettingsGeneral = () => import('./components/routes/SettingsGeneral')
  36. const SettingsAccount = () => import('./components/routes/SettingsAccount')
  37. const Admin = () => import('./components/routes/Admin')
  38. const AdminDashboard = () => import('./components/routes/AdminDashboard')
  39. const AdminModerationReports = () => import('./components/routes/AdminModerationReports')
  40. const AdminModerationBannedUsers = () => import('./components/routes/AdminModerationBannedUsers')
  41. const AdminGeneral = () => import('./components/routes/AdminGeneral')
  42. import NotFound from './components/routes/NotFound'
  43. Vue.use(VueRouter)
  44. Vue.use(Vuex)
  45. Vue.use(VueAxios, axios)
  46. Vue.use(linkExpander)
  47. const router = new VueRouter({
  48. routes: [
  49. { path: '/', redirect: '/category/all' },
  50. { path: '/category/:category', component: Index },
  51. { path: '/p/:id', component: P },
  52. { path: '/start', component: Start },
  53. { path: '/thread/:slug/:id', component: Thread },
  54. { path: '/thread/:slug/:id/:post_number', name: 'thread-post', component: Thread },
  55. { path: '/thread/new', component: ThreadNew },
  56. { path: '/search/:q', component: Search },
  57. { path: '/user/:username', redirect: '/user/:username/posts', component: User, children: [
  58. { path: 'posts', component: UserPosts },
  59. { path: 'threads', component: UserThreads }
  60. ] },
  61. { path: '/settings', redirect: '/settings/general', component: Settings, children: [
  62. { path: 'general', component: SettingsGeneral },
  63. { path: 'account', component: SettingsAccount }
  64. ] },
  65. { path: '/admin', redirect: '/admin/dashboard', component: Admin, children: [
  66. { path: 'dashboard', component: AdminDashboard },
  67. { path: 'general', component: AdminGeneral },
  68. { path: 'moderation', redirect: '/admin/moderation/reports' },
  69. { path: 'moderation/reports', component: AdminModerationReports },
  70. { path: 'moderation/bans', component: AdminModerationBannedUsers }
  71. ] },
  72. { path: '*', component: NotFound }
  73. ],
  74. mode: 'history'
  75. })
  76. router.beforeEach((to, from, next) => {
  77. next()
  78. router.app.$store.commit('set404Page', false)
  79. })
  80. Vue.filter('formatDate', function (value, format = '', join = ' ') {
  81. if(typeof value !== 'object') {
  82. value = new Date(value)
  83. }
  84. let sinceNow = new Date(new Date() - value)
  85. //Add leading zero if under 10
  86. function lz(num) {
  87. if(num < 10) {
  88. return '0' + num;
  89. } else {
  90. return '' + num;
  91. }
  92. }
  93. function p(word, num) {
  94. if(num === 1) {
  95. return word
  96. } else {
  97. return word + 's'
  98. }
  99. }
  100. //2 minutes
  101. if(sinceNow <= 1000*60*2) {
  102. return 'Just now'
  103. } else if(sinceNow <= 1000*60*60) {
  104. return sinceNow.getMinutes() + ' minutes ago'
  105. } else if(sinceNow <= 1000*60*60*24) {
  106. let hours = sinceNow.getHours()
  107. return hours + ' ' + p('hour', hours) + ' ago'
  108. } else if(sinceNow <= 1000*60*60*24*2) {
  109. let days = Math.floor(sinceNow / (1000*60*60*24))
  110. return days + ' ' + p('day', days) + ' ago at ' + value.toTimeString().slice(0, 5)
  111. } else {
  112. return (
  113. lz(value.getDate()) + '/' +
  114. lz(value.getMonth() + 1) + '/' +
  115. value.getUTCFullYear()
  116. );
  117. }
  118. });
  119. Vue.filter('stripTags', function (value) {
  120. let div = document.createElement('div')
  121. div.innerHTML = value
  122. return div.textContent
  123. });
  124. Vue.filter('truncate', function (value, length) {
  125. if(value.length <= length) {
  126. return value
  127. } else {
  128. return value.slice(0, length) + '...'
  129. }
  130. });
  131. Vue.filter('pluralize', function(number, value) {
  132. if(number === 1) {
  133. return value
  134. } else {
  135. return value + 's'
  136. }
  137. })
  138. let Root = new Vue({
  139. el: '#app',
  140. template: '<App/>',
  141. store,
  142. components: { App },
  143. router
  144. })
  145. let cookieDict = document.cookie
  146. .split(';')
  147. .map(a => a.split('=').map(a => a.trim()) )
  148. .map(a => {
  149. let k = a[0], v = a[1]
  150. return { [k] : v }
  151. })
  152. .reduce((combinedObj, o) => {
  153. let key = Object.keys(o)[0]
  154. combinedObj[key] = o[key]
  155. return combinedObj
  156. }, {})
  157. if(cookieDict.username) Root.$store.commit('setUsername', cookieDict.username)
  158. if(cookieDict.admin === 'false') {
  159. Root.$store.commit('setAdmin', false)
  160. } else if(cookieDict.admin === 'true') {
  161. Root.$store.commit('setAdmin', true)
  162. }