You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.7 KiB

  1. /* global siteConfig */
  2. import Vue from 'vue'
  3. import VueRouter from 'vue-router'
  4. import VueClipboards from 'vue-clipboards'
  5. import { ApolloClient } from 'apollo-client'
  6. import { BatchHttpLink } from 'apollo-link-batch-http'
  7. import { ApolloLink, split } from 'apollo-link'
  8. import { WebSocketLink } from 'apollo-link-ws'
  9. import { ErrorLink } from 'apollo-link-error'
  10. import { InMemoryCache } from 'apollo-cache-inmemory'
  11. import { getMainDefinition } from 'apollo-utilities'
  12. import VueApollo from 'vue-apollo'
  13. import Vuetify from 'vuetify/lib'
  14. import Velocity from 'velocity-animate'
  15. import Vuescroll from 'vuescroll/dist/vuescroll-native'
  16. import Hammer from 'hammerjs'
  17. import moment from 'moment'
  18. import VueMoment from 'vue-moment'
  19. import store from './store'
  20. import Cookies from 'js-cookie'
  21. // ====================================
  22. // Load Modules
  23. // ====================================
  24. import boot from './modules/boot'
  25. import localization from './modules/localization'
  26. // ====================================
  27. // Load Helpers
  28. // ====================================
  29. import helpers from './helpers'
  30. // ====================================
  31. // Initialize Global Vars
  32. // ====================================
  33. window.WIKI = null
  34. window.boot = boot
  35. window.Hammer = Hammer
  36. moment.locale(siteConfig.lang)
  37. store.commit('user/REFRESH_AUTH')
  38. // ====================================
  39. // Initialize Apollo Client (GraphQL)
  40. // ====================================
  41. const graphQLEndpoint = window.location.protocol + '//' + window.location.host + '/graphql'
  42. const graphQLWSEndpoint = ((window.location.protocol === 'https:') ? 'wss:' : 'ws:') + '//' + window.location.host + '/graphql-subscriptions'
  43. const graphQLLink = ApolloLink.from([
  44. new ErrorLink(({ graphQLErrors, networkError }) => {
  45. if (graphQLErrors) {
  46. let isAuthError = false
  47. graphQLErrors.map(({ message, locations, path }) => {
  48. if (message === `Forbidden`) {
  49. isAuthError = true
  50. }
  51. console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
  52. })
  53. store.commit('showNotification', {
  54. style: 'red',
  55. message: isAuthError ? `You are not authorized to access this resource.` : `An unexpected error occured.`,
  56. icon: 'alert'
  57. })
  58. }
  59. if (networkError) {
  60. console.error(networkError)
  61. store.commit('showNotification', {
  62. style: 'red',
  63. message: `Network Error: ${networkError.message}`,
  64. icon: 'alert'
  65. })
  66. }
  67. }),
  68. new BatchHttpLink({
  69. includeExtensions: true,
  70. uri: graphQLEndpoint,
  71. credentials: 'include',
  72. fetch: async (uri, options) => {
  73. // Strip __typename fields from variables
  74. let body = JSON.parse(options.body)
  75. body = body.map(bd => {
  76. return ({
  77. ...bd,
  78. variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  79. })
  80. })
  81. options.body = JSON.stringify(body)
  82. // Inject authentication token
  83. const jwtToken = Cookies.get('jwt')
  84. if (jwtToken) {
  85. options.headers.Authorization = `Bearer ${jwtToken}`
  86. }
  87. const resp = await fetch(uri, options)
  88. // Handle renewed JWT
  89. const newJWT = resp.headers.get('new-jwt')
  90. if (newJWT) {
  91. Cookies.set('jwt', newJWT, { expires: 365 })
  92. }
  93. return resp
  94. }
  95. })
  96. ])
  97. const graphQLWSLink = new WebSocketLink({
  98. uri: graphQLWSEndpoint,
  99. options: {
  100. reconnect: true,
  101. lazy: true
  102. }
  103. })
  104. window.graphQL = new ApolloClient({
  105. link: split(({ query }) => {
  106. const { kind, operation } = getMainDefinition(query)
  107. return kind === 'OperationDefinition' && operation === 'subscription'
  108. }, graphQLWSLink, graphQLLink),
  109. cache: new InMemoryCache(),
  110. connectToDevTools: (process.env.node_env === 'development')
  111. })
  112. // ====================================
  113. // Initialize Vue Modules
  114. // ====================================
  115. Vue.config.productionTip = false
  116. Vue.use(VueRouter)
  117. Vue.use(VueApollo)
  118. Vue.use(VueClipboards)
  119. Vue.use(localization.VueI18Next)
  120. Vue.use(helpers)
  121. Vue.use(Vuetify)
  122. Vue.use(VueMoment, { moment })
  123. Vue.use(Vuescroll)
  124. Vue.prototype.Velocity = Velocity
  125. // ====================================
  126. // Register Vue Components
  127. // ====================================
  128. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  129. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  130. Vue.component('history', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  131. Vue.component('page-source', () => import(/* webpackChunkName: "source" */ './components/source.vue'))
  132. Vue.component('loader', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/loader.vue'))
  133. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  134. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  135. Vue.component('new-page', () => import(/* webpackChunkName: "new-page" */ './components/new-page.vue'))
  136. Vue.component('notify', () => import(/* webpackMode: "eager" */ './components/common/notify.vue'))
  137. Vue.component('not-found', () => import(/* webpackChunkName: "not-found" */ './components/not-found.vue'))
  138. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  139. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  140. Vue.component('register', () => import(/* webpackChunkName: "register" */ './components/register.vue'))
  141. Vue.component('search-results', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/search-results.vue'))
  142. Vue.component('tags', () => import(/* webpackChunkName: "tags" */ './components/tags.vue'))
  143. Vue.component('unauthorized', () => import(/* webpackChunkName: "unauthorized" */ './components/unauthorized.vue'))
  144. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  145. Vue.component('welcome', () => import(/* webpackChunkName: "welcome" */ './components/welcome.vue'))
  146. Vue.component('nav-footer', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-footer.vue'))
  147. Vue.component('nav-sidebar', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-sidebar.vue'))
  148. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/page.vue'))
  149. let bootstrap = () => {
  150. // ====================================
  151. // Notifications
  152. // ====================================
  153. window.addEventListener('beforeunload', () => {
  154. store.dispatch('startLoading')
  155. })
  156. const apolloProvider = new VueApollo({
  157. defaultClient: window.graphQL
  158. })
  159. // ====================================
  160. // Bootstrap Vue
  161. // ====================================
  162. const i18n = localization.init()
  163. window.WIKI = new Vue({
  164. el: '#root',
  165. components: {},
  166. mixins: [helpers],
  167. apolloProvider,
  168. store,
  169. i18n,
  170. vuetify: new Vuetify({
  171. rtl: siteConfig.rtl,
  172. theme: {
  173. dark: siteConfig.darkMode
  174. }
  175. })
  176. })
  177. // ----------------------------------
  178. // Dispatch boot ready
  179. // ----------------------------------
  180. window.boot.notify('vue')
  181. // ====================================
  182. // Load theme-specific code
  183. // ====================================
  184. import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/js/app.js')
  185. }
  186. window.boot.onDOMReady(bootstrap)