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.

219 lines
7.4 KiB

  1. /* global siteConfig */
  2. import Vue from 'vue'
  3. import VueRouter from 'vue-router'
  4. import VueClipboards from 'vue-clipboards'
  5. import VeeValidate from 'vee-validate'
  6. import { ApolloClient } from 'apollo-client'
  7. import { BatchHttpLink } from 'apollo-link-batch-http'
  8. import { ApolloLink, split } from 'apollo-link'
  9. import { WebSocketLink } from 'apollo-link-ws'
  10. import { ErrorLink } from 'apollo-link-error'
  11. import { InMemoryCache } from 'apollo-cache-inmemory'
  12. import { getMainDefinition } from 'apollo-utilities'
  13. import VueApollo from 'vue-apollo'
  14. import Vuetify from 'vuetify/lib'
  15. import Velocity from 'velocity-animate'
  16. import Vuescroll from 'vuescroll/dist/vuescroll-native'
  17. import Hammer from 'hammerjs'
  18. import moment from 'moment'
  19. import VueMoment from 'vue-moment'
  20. import VueTour from 'vue-tour'
  21. import store from './store'
  22. import Cookies from 'js-cookie'
  23. // ====================================
  24. // Load Modules
  25. // ====================================
  26. import boot from './modules/boot'
  27. import localization from './modules/localization'
  28. // ====================================
  29. // Load Helpers
  30. // ====================================
  31. import helpers from './helpers'
  32. // ====================================
  33. // Initialize Global Vars
  34. // ====================================
  35. window.WIKI = null
  36. window.boot = boot
  37. window.Hammer = Hammer
  38. moment.locale(siteConfig.lang)
  39. store.commit('user/REFRESH_AUTH')
  40. // ====================================
  41. // Initialize Apollo Client (GraphQL)
  42. // ====================================
  43. const graphQLEndpoint = window.location.protocol + '//' + window.location.host + '/graphql'
  44. const graphQLWSEndpoint = ((window.location.protocol === 'https:') ? 'wss:' : 'ws:') + '//' + window.location.host + '/graphql-subscriptions'
  45. const graphQLLink = ApolloLink.from([
  46. new ErrorLink(({ graphQLErrors, networkError }) => {
  47. if (graphQLErrors) {
  48. graphQLErrors.map(({ message, locations, path }) =>
  49. console.error(
  50. `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  51. )
  52. )
  53. store.commit('showNotification', {
  54. style: 'red',
  55. message: `An unexpected error occured.`,
  56. icon: 'warning'
  57. })
  58. }
  59. if (networkError) {
  60. console.error(networkError)
  61. store.commit('showNotification', {
  62. style: 'red',
  63. message: `Network Error: ${networkError.message}`,
  64. icon: 'error'
  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(VeeValidate, { mode: 'eager' })
  122. Vue.use(Vuetify)
  123. Vue.use(VueMoment, { moment })
  124. Vue.use(Vuescroll)
  125. Vue.use(VueTour)
  126. Vue.prototype.Velocity = Velocity
  127. // ====================================
  128. // Register Vue Components
  129. // ====================================
  130. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  131. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  132. Vue.component('history', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  133. Vue.component('page-source', () => import(/* webpackChunkName: "source" */ './components/source.vue'))
  134. Vue.component('loader', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/loader.vue'))
  135. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  136. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  137. Vue.component('new-page', () => import(/* webpackChunkName: "new-page" */ './components/new-page.vue'))
  138. Vue.component('notify', () => import(/* webpackMode: "eager" */ './components/common/notify.vue'))
  139. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  140. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  141. Vue.component('register', () => import(/* webpackChunkName: "register" */ './components/register.vue'))
  142. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  143. Vue.component('search-results', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/search-results.vue'))
  144. Vue.component('welcome', () => import(/* webpackChunkName: "welcome" */ './components/welcome.vue'))
  145. Vue.component('nav-footer', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-footer.vue'))
  146. Vue.component('nav-sidebar', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-sidebar.vue'))
  147. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/page.vue'))
  148. let bootstrap = () => {
  149. // ====================================
  150. // Notifications
  151. // ====================================
  152. window.addEventListener('beforeunload', () => {
  153. store.dispatch('startLoading')
  154. })
  155. const apolloProvider = new VueApollo({
  156. defaultClient: window.graphQL
  157. })
  158. // ====================================
  159. // Bootstrap Vue
  160. // ====================================
  161. const i18n = localization.init()
  162. window.WIKI = new Vue({
  163. el: '#root',
  164. components: {},
  165. mixins: [helpers],
  166. apolloProvider,
  167. store,
  168. i18n,
  169. vuetify: new Vuetify({
  170. rtl: siteConfig.rtl,
  171. theme: {
  172. dark: siteConfig.darkMode
  173. }
  174. })
  175. })
  176. // ----------------------------------
  177. // Dispatch boot ready
  178. // ----------------------------------
  179. window.boot.notify('vue')
  180. // ====================================
  181. // Load theme-specific code
  182. // ====================================
  183. import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/js/app.js')
  184. }
  185. window.boot.onDOMReady(bootstrap)