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.

213 lines
6.7 KiB

  1. /* global siteConfig */
  2. import Vue from 'vue'
  3. import VueRouter from 'vue-router'
  4. import VueClipboards from 'vue-clipboards'
  5. import VueSimpleBreakpoints from 'vue-simple-breakpoints'
  6. import VeeValidate from 'vee-validate'
  7. import { ApolloClient } from 'apollo-client'
  8. import { createPersistedQueryLink } from 'apollo-link-persisted-queries'
  9. import { BatchHttpLink } from 'apollo-link-batch-http'
  10. import { ApolloLink, split } from 'apollo-link'
  11. // import { createHttpLink } from 'apollo-link-http'
  12. import { WebSocketLink } from 'apollo-link-ws'
  13. import { ErrorLink } from 'apollo-link-error'
  14. import { InMemoryCache } from 'apollo-cache-inmemory'
  15. import { getMainDefinition } from 'apollo-utilities'
  16. import VueApollo from 'vue-apollo'
  17. import Vuetify from 'vuetify'
  18. import Velocity from 'velocity-animate'
  19. import Hammer from 'hammerjs'
  20. import moment from 'moment'
  21. import VueMoment from 'vue-moment'
  22. import VueTour from 'vue-tour'
  23. import VueTreeNavigation from 'vue-tree-navigation'
  24. import store from './store'
  25. import Cookies from 'js-cookie'
  26. // ====================================
  27. // Load Modules
  28. // ====================================
  29. import boot from './modules/boot'
  30. import localization from './modules/localization'
  31. // ====================================
  32. // Load Helpers
  33. // ====================================
  34. import helpers from './helpers'
  35. // ====================================
  36. // Initialize Global Vars
  37. // ====================================
  38. window.WIKI = null
  39. window.boot = boot
  40. window.Hammer = Hammer
  41. moment.locale(siteConfig.lang)
  42. // ====================================
  43. // Initialize Apollo Client (GraphQL)
  44. // ====================================
  45. const graphQLEndpoint = window.location.protocol + '//' + window.location.host + '/graphql'
  46. const graphQLWSEndpoint = ((window.location.protocol === 'https:') ? 'wss:' : 'ws:') + '//' + window.location.host + '/graphql-subscriptions'
  47. const graphQLLink = ApolloLink.from([
  48. new ErrorLink(({ graphQLErrors, networkError }) => {
  49. if (graphQLErrors) {
  50. graphQLErrors.map(({ message, locations, path }) =>
  51. console.error(
  52. `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  53. )
  54. )
  55. store.commit('showNotification', {
  56. style: 'red',
  57. message: `An expected error occured.`,
  58. icon: 'warning'
  59. })
  60. }
  61. if (networkError) {
  62. console.error(networkError)
  63. store.commit('showNotification', {
  64. style: 'red',
  65. message: `Network Error: ${networkError.message}`,
  66. icon: 'error'
  67. })
  68. }
  69. }),
  70. createPersistedQueryLink(),
  71. new BatchHttpLink({
  72. includeExtensions: true,
  73. uri: graphQLEndpoint,
  74. credentials: 'include',
  75. fetch: async (uri, options) => {
  76. // Strip __typename fields from variables
  77. let body = JSON.parse(options.body)
  78. body = body.map(bd => {
  79. return ({
  80. ...bd,
  81. variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  82. })
  83. })
  84. // body = {
  85. // ...body,
  86. // variables: JSON.parse(JSON.stringify(body.variables), (key, value) => { return key === '__typename' ? undefined : value })
  87. // }
  88. options.body = JSON.stringify(body)
  89. // Inject authentication token
  90. const jwtToken = Cookies.get('jwt')
  91. if (jwtToken) {
  92. options.headers.Authorization = `Bearer ${jwtToken}`
  93. }
  94. const resp = await fetch(uri, options)
  95. // Handle renewed JWT
  96. const newJWT = resp.headers.get('new-jwt')
  97. if (newJWT) {
  98. Cookies.set('jwt', newJWT, { expires: 365 })
  99. }
  100. return resp
  101. }
  102. })
  103. ])
  104. const graphQLWSLink = new WebSocketLink({
  105. uri: graphQLWSEndpoint,
  106. options: {
  107. reconnect: true,
  108. lazy: true
  109. }
  110. })
  111. window.graphQL = new ApolloClient({
  112. link: split(({ query }) => {
  113. const { kind, operation } = getMainDefinition(query)
  114. return kind === 'OperationDefinition' && operation === 'subscription'
  115. }, graphQLWSLink, graphQLLink),
  116. cache: new InMemoryCache(),
  117. connectToDevTools: (process.env.node_env === 'development')
  118. })
  119. // ====================================
  120. // Initialize Vue Modules
  121. // ====================================
  122. Vue.config.productionTip = false
  123. Vue.use(VueRouter)
  124. Vue.use(VueApollo)
  125. Vue.use(VueClipboards)
  126. Vue.use(VueSimpleBreakpoints)
  127. Vue.use(localization.VueI18Next)
  128. Vue.use(helpers)
  129. Vue.use(VeeValidate, { events: '' })
  130. Vue.use(Vuetify)
  131. Vue.use(VueMoment, { moment })
  132. Vue.use(VueTour)
  133. Vue.use(VueTreeNavigation)
  134. Vue.prototype.Velocity = Velocity
  135. // ====================================
  136. // Register Vue Components
  137. // ====================================
  138. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  139. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  140. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  141. Vue.component('nav-footer', () => import(/* webpackMode: "eager" */ './components/common/nav-footer.vue'))
  142. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  143. Vue.component('nav-sidebar', () => import(/* webpackMode: "eager" */ './components/common/nav-sidebar.vue'))
  144. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/app.vue'))
  145. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  146. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  147. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.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 Icons
  176. // ====================================
  177. // import(/* webpackChunkName: "icons" */ './svg/icons.svg').then(icons => {
  178. // document.body.insertAdjacentHTML('beforeend', icons.default)
  179. // })
  180. }
  181. window.boot.onDOMReady(bootstrap)