main.js 4.4 KB

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