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.

226 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 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 Vuescroll from 'vuescroll/dist/vuescroll-native'
  20. import Hammer from 'hammerjs'
  21. import moment from 'moment'
  22. import VueMoment from 'vue-moment'
  23. import VueTour from 'vue-tour'
  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. store.commit('user/REFRESH_AUTH')
  43. // ====================================
  44. // Initialize Apollo Client (GraphQL)
  45. // ====================================
  46. const graphQLEndpoint = window.location.protocol + '//' + window.location.host + '/graphql'
  47. const graphQLWSEndpoint = ((window.location.protocol === 'https:') ? 'wss:' : 'ws:') + '//' + window.location.host + '/graphql-subscriptions'
  48. const graphQLLink = ApolloLink.from([
  49. new ErrorLink(({ graphQLErrors, networkError }) => {
  50. if (graphQLErrors) {
  51. graphQLErrors.map(({ message, locations, path }) =>
  52. console.error(
  53. `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  54. )
  55. )
  56. store.commit('showNotification', {
  57. style: 'red',
  58. message: `An expected error occured.`,
  59. icon: 'warning'
  60. })
  61. }
  62. if (networkError) {
  63. console.error(networkError)
  64. store.commit('showNotification', {
  65. style: 'red',
  66. message: `Network Error: ${networkError.message}`,
  67. icon: 'error'
  68. })
  69. }
  70. }),
  71. // createPersistedQueryLink(),
  72. new BatchHttpLink({
  73. includeExtensions: true,
  74. uri: graphQLEndpoint,
  75. credentials: 'include',
  76. fetch: async (uri, options) => {
  77. // Strip __typename fields from variables
  78. let body = JSON.parse(options.body)
  79. body = body.map(bd => {
  80. return ({
  81. ...bd,
  82. variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  83. })
  84. })
  85. // body = {
  86. // ...body,
  87. // variables: JSON.parse(JSON.stringify(body.variables), (key, value) => { return key === '__typename' ? undefined : value })
  88. // }
  89. options.body = JSON.stringify(body)
  90. // Inject authentication token
  91. const jwtToken = Cookies.get('jwt')
  92. if (jwtToken) {
  93. options.headers.Authorization = `Bearer ${jwtToken}`
  94. }
  95. const resp = await fetch(uri, options)
  96. // Handle renewed JWT
  97. const newJWT = resp.headers.get('new-jwt')
  98. if (newJWT) {
  99. Cookies.set('jwt', newJWT, { expires: 365 })
  100. }
  101. return resp
  102. }
  103. })
  104. ])
  105. const graphQLWSLink = new WebSocketLink({
  106. uri: graphQLWSEndpoint,
  107. options: {
  108. reconnect: true,
  109. lazy: true
  110. }
  111. })
  112. window.graphQL = new ApolloClient({
  113. link: split(({ query }) => {
  114. const { kind, operation } = getMainDefinition(query)
  115. return kind === 'OperationDefinition' && operation === 'subscription'
  116. }, graphQLWSLink, graphQLLink),
  117. cache: new InMemoryCache(),
  118. connectToDevTools: (process.env.node_env === 'development')
  119. })
  120. // ====================================
  121. // Initialize Vue Modules
  122. // ====================================
  123. Vue.config.productionTip = false
  124. Vue.use(VueRouter)
  125. Vue.use(VueApollo)
  126. Vue.use(VueClipboards)
  127. Vue.use(VueSimpleBreakpoints)
  128. Vue.use(localization.VueI18Next)
  129. Vue.use(helpers)
  130. Vue.use(VeeValidate, { events: '' })
  131. Vue.use(Vuetify)
  132. Vue.use(VueMoment, { moment })
  133. Vue.use(Vuescroll)
  134. Vue.use(VueTour)
  135. Vue.prototype.Velocity = Velocity
  136. // ====================================
  137. // Register Vue Components
  138. // ====================================
  139. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  140. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  141. Vue.component('history', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  142. Vue.component('page-source', () => import(/* webpackChunkName: "source" */ './components/source.vue'))
  143. Vue.component('loader', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/loader.vue'))
  144. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  145. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  146. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  147. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  148. Vue.component('register', () => import(/* webpackChunkName: "register" */ './components/register.vue'))
  149. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  150. Vue.component('nav-footer', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-footer.vue'))
  151. Vue.component('nav-sidebar', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-sidebar.vue'))
  152. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/page.vue'))
  153. let bootstrap = () => {
  154. // ====================================
  155. // Notifications
  156. // ====================================
  157. window.addEventListener('beforeunload', () => {
  158. store.dispatch('startLoading')
  159. })
  160. const apolloProvider = new VueApollo({
  161. defaultClient: window.graphQL
  162. })
  163. // ====================================
  164. // Bootstrap Vue
  165. // ====================================
  166. const i18n = localization.init()
  167. window.WIKI = new Vue({
  168. el: '#root',
  169. components: {},
  170. mixins: [helpers],
  171. apolloProvider,
  172. store,
  173. i18n
  174. })
  175. // ----------------------------------
  176. // Dispatch boot ready
  177. // ----------------------------------
  178. window.boot.notify('vue')
  179. // ====================================
  180. // Load theme-specific code
  181. // ====================================
  182. import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/js/app.js')
  183. // ====================================
  184. // Load Icons
  185. // ====================================
  186. // import(/* webpackChunkName: "icons" */ './svg/icons.svg').then(icons => {
  187. // document.body.insertAdjacentHTML('beforeend', icons.default)
  188. // })
  189. }
  190. window.boot.onDOMReady(bootstrap)