main.js 4.8 KB

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