main.js 4.1 KB

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