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.

221 lines
7.3 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'
  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 expected 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, { events: '' })
  122. Vue.use(Vuetify, {
  123. rtl: siteConfig.rtl
  124. })
  125. Vue.use(VueMoment, { moment })
  126. Vue.use(Vuescroll)
  127. Vue.use(VueTour)
  128. Vue.prototype.Velocity = Velocity
  129. // ====================================
  130. // Register Vue Components
  131. // ====================================
  132. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  133. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  134. Vue.component('history', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  135. Vue.component('page-source', () => import(/* webpackChunkName: "source" */ './components/source.vue'))
  136. Vue.component('loader', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/loader.vue'))
  137. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  138. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  139. Vue.component('notify', () => import(/* webpackMode: "eager" */ './components/common/notify.vue'))
  140. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  141. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  142. Vue.component('register', () => import(/* webpackChunkName: "register" */ './components/register.vue'))
  143. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  144. Vue.component('search-results', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/search-results.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. })
  170. // ----------------------------------
  171. // Dispatch boot ready
  172. // ----------------------------------
  173. window.boot.notify('vue')
  174. // ====================================
  175. // Load theme-specific code
  176. // ====================================
  177. import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/js/app.js')
  178. // ====================================
  179. // Load Icons
  180. // ====================================
  181. // import(/* webpackChunkName: "icons" */ './svg/icons.svg').then(icons => {
  182. // document.body.insertAdjacentHTML('beforeend', icons.default)
  183. // })
  184. }
  185. window.boot.onDOMReady(bootstrap)