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.

269 lines
8.6 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 { createUploadLink } from 'apollo-upload-client'
  11. import { ErrorLink } from 'apollo-link-error'
  12. import { InMemoryCache } from 'apollo-cache-inmemory'
  13. import { getMainDefinition } from 'apollo-utilities'
  14. import VueApollo from 'vue-apollo'
  15. import Vuetify from 'vuetify'
  16. import Velocity from 'velocity-animate'
  17. import Vuescroll from 'vuescroll/dist/vuescroll-native'
  18. import Hammer from 'hammerjs'
  19. import moment from 'moment'
  20. import VueMoment from 'vue-moment'
  21. import VueTour from 'vue-tour'
  22. import store from './store'
  23. import Cookies from 'js-cookie'
  24. // ====================================
  25. // Load Modules
  26. // ====================================
  27. import boot from './modules/boot'
  28. import localization from './modules/localization'
  29. // ====================================
  30. // Load Helpers
  31. // ====================================
  32. import helpers from './helpers'
  33. // ====================================
  34. // Initialize Global Vars
  35. // ====================================
  36. window.WIKI = null
  37. window.boot = boot
  38. window.Hammer = Hammer
  39. moment.locale(siteConfig.lang)
  40. store.commit('user/REFRESH_AUTH')
  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 graphQLFetch = async (uri, options) => {
  47. // Strip __typename fields from variables
  48. let body = JSON.parse(options.body)
  49. body = body.map(bd => {
  50. return ({
  51. ...bd,
  52. variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  53. })
  54. })
  55. options.body = JSON.stringify(body)
  56. // Inject authentication token
  57. const jwtToken = Cookies.get('jwt')
  58. if (jwtToken) {
  59. options.headers.Authorization = `Bearer ${jwtToken}`
  60. }
  61. const resp = await fetch(uri, options)
  62. // Handle renewed JWT
  63. const newJWT = resp.headers.get('new-jwt')
  64. if (newJWT) {
  65. Cookies.set('jwt', newJWT, { expires: 365 })
  66. }
  67. return resp
  68. }
  69. const graphQLLink = ApolloLink.from([
  70. new ErrorLink(({ graphQLErrors, networkError }) => {
  71. if (graphQLErrors) {
  72. graphQLErrors.map(({ message, locations, path }) =>
  73. console.error(
  74. `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  75. )
  76. )
  77. store.commit('showNotification', {
  78. style: 'red',
  79. message: `An expected error occured.`,
  80. icon: 'warning'
  81. })
  82. }
  83. if (networkError) {
  84. console.error(networkError)
  85. store.commit('showNotification', {
  86. style: 'red',
  87. message: `Network Error: ${networkError.message}`,
  88. icon: 'error'
  89. })
  90. }
  91. }),
  92. new BatchHttpLink({
  93. includeExtensions: true,
  94. uri: graphQLEndpoint,
  95. credentials: 'include',
  96. fetch: async (uri, options) => {
  97. // Strip __typename fields from variables
  98. let body = JSON.parse(options.body)
  99. body = body.map(bd => {
  100. return ({
  101. ...bd,
  102. variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  103. })
  104. })
  105. options.body = JSON.stringify(body)
  106. // Inject authentication token
  107. const jwtToken = Cookies.get('jwt')
  108. if (jwtToken) {
  109. options.headers.Authorization = `Bearer ${jwtToken}`
  110. }
  111. const resp = await fetch(uri, options)
  112. // Handle renewed JWT
  113. const newJWT = resp.headers.get('new-jwt')
  114. if (newJWT) {
  115. Cookies.set('jwt', newJWT, { expires: 365 })
  116. }
  117. return resp
  118. }
  119. })
  120. ])
  121. const graphQLUploadLink = createUploadLink({
  122. includeExtensions: true,
  123. uri: graphQLEndpoint,
  124. credentials: 'include',
  125. fetch: async (uri, options) => {
  126. // Inject authentication token
  127. const jwtToken = Cookies.get('jwt')
  128. if (jwtToken) {
  129. options.headers.Authorization = `Bearer ${jwtToken}`
  130. }
  131. const resp = await fetch(uri, options)
  132. // Handle renewed JWT
  133. const newJWT = resp.headers.get('new-jwt')
  134. if (newJWT) {
  135. Cookies.set('jwt', newJWT, { expires: 365 })
  136. }
  137. return resp
  138. }
  139. })
  140. const graphQLWSLink = new WebSocketLink({
  141. uri: graphQLWSEndpoint,
  142. options: {
  143. reconnect: true,
  144. lazy: true
  145. }
  146. })
  147. window.graphQL = new ApolloClient({
  148. link: split(({ query }) => {
  149. const { kind, operation } = getMainDefinition(query)
  150. return kind === 'OperationDefinition' && operation === 'subscription'
  151. }, graphQLWSLink, split(operation => operation.getContext().hasUpload, graphQLUploadLink, graphQLLink)),
  152. cache: new InMemoryCache(),
  153. connectToDevTools: (process.env.node_env === 'development')
  154. })
  155. // ====================================
  156. // Initialize Vue Modules
  157. // ====================================
  158. Vue.config.productionTip = false
  159. Vue.use(VueRouter)
  160. Vue.use(VueApollo)
  161. Vue.use(VueClipboards)
  162. Vue.use(localization.VueI18Next)
  163. Vue.use(helpers)
  164. Vue.use(VeeValidate, { events: '' })
  165. Vue.use(Vuetify)
  166. Vue.use(VueMoment, { moment })
  167. Vue.use(Vuescroll)
  168. Vue.use(VueTour)
  169. Vue.prototype.Velocity = Velocity
  170. // ====================================
  171. // Register Vue Components
  172. // ====================================
  173. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  174. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  175. Vue.component('history', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  176. Vue.component('page-source', () => import(/* webpackChunkName: "source" */ './components/source.vue'))
  177. Vue.component('loader', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/loader.vue'))
  178. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  179. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  180. Vue.component('notify', () => import(/* webpackMode: "eager" */ './components/common/notify.vue'))
  181. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  182. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  183. Vue.component('register', () => import(/* webpackChunkName: "register" */ './components/register.vue'))
  184. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  185. Vue.component('search-results', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/search-results.vue'))
  186. Vue.component('nav-footer', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-footer.vue'))
  187. Vue.component('nav-sidebar', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-sidebar.vue'))
  188. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/page.vue'))
  189. let bootstrap = () => {
  190. // ====================================
  191. // Notifications
  192. // ====================================
  193. window.addEventListener('beforeunload', () => {
  194. store.dispatch('startLoading')
  195. })
  196. const apolloProvider = new VueApollo({
  197. defaultClient: window.graphQL
  198. })
  199. // ====================================
  200. // Bootstrap Vue
  201. // ====================================
  202. const i18n = localization.init()
  203. window.WIKI = new Vue({
  204. el: '#root',
  205. components: {},
  206. mixins: [helpers],
  207. apolloProvider,
  208. store,
  209. i18n
  210. })
  211. // ----------------------------------
  212. // Dispatch boot ready
  213. // ----------------------------------
  214. window.boot.notify('vue')
  215. // ====================================
  216. // Load theme-specific code
  217. // ====================================
  218. import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/js/app.js')
  219. // ====================================
  220. // Load Icons
  221. // ====================================
  222. // import(/* webpackChunkName: "icons" */ './svg/icons.svg').then(icons => {
  223. // document.body.insertAdjacentHTML('beforeend', icons.default)
  224. // })
  225. }
  226. window.boot.onDOMReady(bootstrap)