main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 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. Vue.filter('formatDate', function (value, format = '', join = ' ') {
  61. if(typeof value !== 'object') {
  62. value = new Date(value)
  63. }
  64. let sinceNow = new Date(new Date() - value)
  65. //Add leading zero if under 10
  66. function lz(num) {
  67. if(num < 10) {
  68. return '0' + num;
  69. } else {
  70. return '' + num;
  71. }
  72. }
  73. function p(word, num) {
  74. if(num === 1) {
  75. return word
  76. } else {
  77. return word + 's'
  78. }
  79. }
  80. //2 minutes
  81. if(sinceNow <= 1000*60*2) {
  82. return 'Just now'
  83. } else if(sinceNow <= 1000*60*60) {
  84. return sinceNow.getMinutes() + ' minutes ago'
  85. } else if(sinceNow <= 1000*60*60*24) {
  86. let hours = sinceNow.getHours()
  87. return hours + ' ' + p('hour', hours) + ' ago'
  88. } else if(sinceNow <= 1000*60*60*24*2) {
  89. let days = Math.floor(sinceNow / (1000*60*60*24))
  90. return days + ' ' + p('day', days) + ' ago at ' + value.toTimeString().slice(0, 5)
  91. } else {
  92. return (
  93. lz(value.getDate()) + '/' +
  94. lz(value.getMonth() + 1) + '/' +
  95. value.getUTCFullYear()
  96. );
  97. }
  98. });
  99. Vue.filter('stripTags', function (value) {
  100. let div = document.createElement('div')
  101. div.innerHTML = value
  102. return div.textContent
  103. });
  104. Vue.filter('truncate', function (value, length) {
  105. if(value.length <= length) {
  106. return value
  107. } else {
  108. return value.slice(0, length) + '...'
  109. }
  110. });
  111. Vue.filter('pluralize', function(number, value) {
  112. if(number === 1) {
  113. return value
  114. } else {
  115. return value + 's'
  116. }
  117. })
  118. let Root = new Vue({
  119. el: '#app',
  120. template: '<App/>',
  121. store,
  122. components: { App },
  123. router
  124. })
  125. let cookieDict = document.cookie
  126. .split(';')
  127. .map(a => a.split('=').map(a => a.trim()) )
  128. .map(a => {
  129. let k = a[0], v = a[1]
  130. return { [k] : v }
  131. })
  132. .reduce((combinedObj, o) => {
  133. let key = Object.keys(o)[0]
  134. combinedObj[key] = o[key]
  135. return combinedObj
  136. }, {})
  137. if(cookieDict.username) Root.$store.commit('setUsername', cookieDict.username)
  138. if(cookieDict.admin === 'false') {
  139. Root.$store.commit('setAdmin', false)
  140. } else if(cookieDict.admin === 'true') {
  141. Root.$store.commit('setAdmin', true)
  142. }