client-app.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 { split } from 'apollo-link'
  11. import { createHttpLink } from 'apollo-link-http'
  12. import { WebSocketLink } from 'apollo-link-ws'
  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 Hammer from 'hammerjs'
  19. import moment from 'moment'
  20. import VueMoment from 'vue-moment'
  21. import VueTour from 'vue-tour'
  22. import VueTreeNavigation from 'vue-tree-navigation'
  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. // ====================================
  42. // Initialize Apollo Client (GraphQL)
  43. // ====================================
  44. const graphQLEndpoint = window.location.protocol + '//' + window.location.host + '/graphql'
  45. const graphQLWSEndpoint = ((window.location.protocol === 'https:') ? 'wss:' : 'ws:') + '//' + window.location.host + '/graphql-subscriptions'
  46. const graphQLLink = createPersistedQueryLink().concat(
  47. createHttpLink({
  48. includeExtensions: true,
  49. uri: graphQLEndpoint,
  50. credentials: 'include',
  51. fetch: (uri, options) => {
  52. // Strip __typename fields from variables
  53. let body = JSON.parse(options.body)
  54. // body = body.map(bd => {
  55. // return ({
  56. // ...bd,
  57. // variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  58. // })
  59. // })
  60. body = {
  61. ...body,
  62. variables: JSON.parse(JSON.stringify(body.variables), (key, value) => { return key === '__typename' ? undefined : value })
  63. }
  64. options.body = JSON.stringify(body)
  65. // Inject authentication token
  66. const jwtToken = Cookies.get('jwt')
  67. if (jwtToken) {
  68. options.headers.Authorization = `Bearer ${jwtToken}`
  69. }
  70. return fetch(uri, options)
  71. }
  72. })
  73. )
  74. const graphQLWSLink = new WebSocketLink({
  75. uri: graphQLWSEndpoint,
  76. options: {
  77. reconnect: true,
  78. lazy: true
  79. }
  80. })
  81. window.graphQL = new ApolloClient({
  82. link: split(({ query }) => {
  83. const { kind, operation } = getMainDefinition(query)
  84. return kind === 'OperationDefinition' && operation === 'subscription'
  85. }, graphQLWSLink, graphQLLink),
  86. cache: new InMemoryCache(),
  87. connectToDevTools: (process.env.node_env === 'development')
  88. })
  89. // ====================================
  90. // Initialize Vue Modules
  91. // ====================================
  92. Vue.config.productionTip = false
  93. Vue.use(VueRouter)
  94. Vue.use(VueApollo)
  95. Vue.use(VueClipboards)
  96. Vue.use(VueSimpleBreakpoints)
  97. Vue.use(localization.VueI18Next)
  98. Vue.use(helpers)
  99. Vue.use(VeeValidate, { events: '' })
  100. Vue.use(Vuetify)
  101. Vue.use(VueMoment, { moment })
  102. Vue.use(VueTour)
  103. Vue.use(VueTreeNavigation)
  104. Vue.prototype.Velocity = Velocity
  105. // ====================================
  106. // Register Vue Components
  107. // ====================================
  108. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  109. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  110. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  111. Vue.component('nav-footer', () => import(/* webpackMode: "eager" */ './components/common/nav-footer.vue'))
  112. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  113. Vue.component('nav-sidebar', () => import(/* webpackMode: "eager" */ './components/common/nav-sidebar.vue'))
  114. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/app.vue'))
  115. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  116. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  117. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  118. let bootstrap = () => {
  119. // ====================================
  120. // Notifications
  121. // ====================================
  122. window.addEventListener('beforeunload', () => {
  123. store.dispatch('startLoading')
  124. })
  125. const apolloProvider = new VueApollo({
  126. defaultClient: window.graphQL
  127. })
  128. // ====================================
  129. // Bootstrap Vue
  130. // ====================================
  131. const i18n = localization.init()
  132. window.WIKI = new Vue({
  133. el: '#root',
  134. components: {},
  135. mixins: [helpers],
  136. apolloProvider,
  137. store,
  138. i18n
  139. })
  140. // ----------------------------------
  141. // Dispatch boot ready
  142. // ----------------------------------
  143. window.boot.notify('vue')
  144. // ====================================
  145. // Load Icons
  146. // ====================================
  147. // import(/* webpackChunkName: "icons" */ './svg/icons.svg').then(icons => {
  148. // document.body.insertAdjacentHTML('beforeend', icons.default)
  149. // })
  150. }
  151. window.boot.onDOMReady(bootstrap)