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.

225 lines
7.5 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 { createPersistedQueryLink } from 'apollo-link-persisted-queries'
  8. import { BatchHttpLink } from 'apollo-link-batch-http'
  9. import { ApolloLink, split } from 'apollo-link'
  10. // import { createHttpLink } from 'apollo-link-http'
  11. import { WebSocketLink } from 'apollo-link-ws'
  12. import { ErrorLink } from 'apollo-link-error'
  13. import { InMemoryCache } from 'apollo-cache-inmemory'
  14. import { getMainDefinition } from 'apollo-utilities'
  15. import VueApollo from 'vue-apollo'
  16. import Vuetify from 'vuetify'
  17. import Velocity from 'velocity-animate'
  18. import Vuescroll from 'vuescroll/dist/vuescroll-native'
  19. import Hammer from 'hammerjs'
  20. import moment from 'moment'
  21. import VueMoment from 'vue-moment'
  22. import VueTour from 'vue-tour'
  23. import store from './store'
  24. import Cookies from 'js-cookie'
  25. // ====================================
  26. // Load Modules
  27. // ====================================
  28. import boot from './modules/boot'
  29. import localization from './modules/localization'
  30. // ====================================
  31. // Load Helpers
  32. // ====================================
  33. import helpers from './helpers'
  34. // ====================================
  35. // Initialize Global Vars
  36. // ====================================
  37. window.WIKI = null
  38. window.boot = boot
  39. window.Hammer = Hammer
  40. moment.locale(siteConfig.lang)
  41. store.commit('user/REFRESH_AUTH')
  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(localization.VueI18Next)
  127. Vue.use(helpers)
  128. Vue.use(VeeValidate, { events: '' })
  129. Vue.use(Vuetify)
  130. Vue.use(VueMoment, { moment })
  131. Vue.use(Vuescroll)
  132. Vue.use(VueTour)
  133. Vue.prototype.Velocity = Velocity
  134. // ====================================
  135. // Register Vue Components
  136. // ====================================
  137. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  138. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  139. Vue.component('history', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  140. Vue.component('page-source', () => import(/* webpackChunkName: "source" */ './components/source.vue'))
  141. Vue.component('loader', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/loader.vue'))
  142. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  143. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  144. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  145. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  146. Vue.component('register', () => import(/* webpackChunkName: "register" */ './components/register.vue'))
  147. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  148. Vue.component('search-results', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/search-results.vue'))
  149. Vue.component('nav-footer', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-footer.vue'))
  150. Vue.component('nav-sidebar', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-sidebar.vue'))
  151. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/page.vue'))
  152. let bootstrap = () => {
  153. // ====================================
  154. // Notifications
  155. // ====================================
  156. window.addEventListener('beforeunload', () => {
  157. store.dispatch('startLoading')
  158. })
  159. const apolloProvider = new VueApollo({
  160. defaultClient: window.graphQL
  161. })
  162. // ====================================
  163. // Bootstrap Vue
  164. // ====================================
  165. const i18n = localization.init()
  166. window.WIKI = new Vue({
  167. el: '#root',
  168. components: {},
  169. mixins: [helpers],
  170. apolloProvider,
  171. store,
  172. i18n
  173. })
  174. // ----------------------------------
  175. // Dispatch boot ready
  176. // ----------------------------------
  177. window.boot.notify('vue')
  178. // ====================================
  179. // Load theme-specific code
  180. // ====================================
  181. import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/js/app.js')
  182. // ====================================
  183. // Load Icons
  184. // ====================================
  185. // import(/* webpackChunkName: "icons" */ './svg/icons.svg').then(icons => {
  186. // document.body.insertAdjacentHTML('beforeend', icons.default)
  187. // })
  188. }
  189. window.boot.onDOMReady(bootstrap)