2
0

client-app.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* global siteConfig */
  2. import Vue from 'vue'
  3. import VueRouter from 'vue-router'
  4. import VueClipboards from 'vue-clipboards'
  5. import { ApolloClient } from 'apollo-client'
  6. import { BatchHttpLink } from 'apollo-link-batch-http'
  7. import { ApolloLink } from 'apollo-link'
  8. import { ErrorLink } from 'apollo-link-error'
  9. import { InMemoryCache } from 'apollo-cache-inmemory'
  10. import VueApollo from 'vue-apollo'
  11. import Vuetify from 'vuetify/lib'
  12. import Velocity from 'velocity-animate'
  13. import Vuescroll from 'vuescroll/dist/vuescroll-native'
  14. import Hammer from 'hammerjs'
  15. import moment from 'moment-timezone'
  16. import VueMoment from 'vue-moment'
  17. import store from './store'
  18. import Cookies from 'js-cookie'
  19. // ====================================
  20. // Load Modules
  21. // ====================================
  22. import boot from './modules/boot'
  23. import localization from './modules/localization'
  24. // ====================================
  25. // Load Helpers
  26. // ====================================
  27. import helpers from './helpers'
  28. // ====================================
  29. // Initialize Global Vars
  30. // ====================================
  31. window.WIKI = null
  32. window.boot = boot
  33. window.Hammer = Hammer
  34. moment.locale(siteConfig.lang)
  35. store.commit('user/REFRESH_AUTH')
  36. // ====================================
  37. // Initialize Apollo Client (GraphQL)
  38. // ====================================
  39. const graphQLEndpoint = window.location.protocol + '//' + window.location.host + '/_graphql'
  40. const graphQLLink = ApolloLink.from([
  41. new ErrorLink(({ graphQLErrors, networkError }) => {
  42. if (graphQLErrors) {
  43. let isAuthError = false
  44. graphQLErrors.map(({ message, locations, path }) => {
  45. if (message === `Forbidden`) {
  46. isAuthError = true
  47. }
  48. console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
  49. })
  50. store.commit('showNotification', {
  51. style: 'red',
  52. message: isAuthError ? `You are not authorized to access this resource.` : `An unexpected error occurred.`,
  53. icon: 'alert'
  54. })
  55. }
  56. if (networkError) {
  57. console.error(networkError)
  58. store.commit('showNotification', {
  59. style: 'red',
  60. message: `Network Error: ${networkError.message}`,
  61. icon: 'alert'
  62. })
  63. }
  64. }),
  65. new BatchHttpLink({
  66. includeExtensions: true,
  67. uri: graphQLEndpoint,
  68. credentials: 'include',
  69. fetch: async (uri, options) => {
  70. // Strip __typename fields from variables
  71. let body = JSON.parse(options.body)
  72. body = body.map(bd => {
  73. return ({
  74. ...bd,
  75. variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  76. })
  77. })
  78. options.body = JSON.stringify(body)
  79. // Inject authentication token
  80. const jwtToken = Cookies.get('jwt')
  81. if (jwtToken) {
  82. options.headers.Authorization = `Bearer ${jwtToken}`
  83. }
  84. const resp = await fetch(uri, options)
  85. // Handle renewed JWT
  86. const newJWT = resp.headers.get('new-jwt')
  87. if (newJWT) {
  88. Cookies.set('jwt', newJWT, { expires: 365 })
  89. }
  90. return resp
  91. }
  92. })
  93. ])
  94. window.graphQL = new ApolloClient({
  95. link: graphQLLink,
  96. cache: new InMemoryCache(),
  97. connectToDevTools: (process.env.node_env === 'development')
  98. })
  99. // ====================================
  100. // Initialize Vue Modules
  101. // ====================================
  102. Vue.config.productionTip = false
  103. Vue.use(VueRouter)
  104. Vue.use(VueApollo)
  105. Vue.use(VueClipboards)
  106. Vue.use(localization.VueI18Next)
  107. Vue.use(helpers)
  108. Vue.use(Vuetify)
  109. Vue.use(VueMoment, { moment })
  110. Vue.use(Vuescroll)
  111. Vue.prototype.Velocity = Velocity
  112. // ====================================
  113. // Register Vue Components
  114. // ====================================
  115. Vue.component('Comments', () => import(/* webpackChunkName: "comments" */ './components/comments.vue'))
  116. Vue.component('Editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  117. Vue.component('History', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  118. Vue.component('Loader', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/loader.vue'))
  119. Vue.component('NavHeader', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  120. Vue.component('NewPage', () => import(/* webpackChunkName: "new-page" */ './components/new-page.vue'))
  121. Vue.component('Notify', () => import(/* webpackMode: "eager" */ './components/common/notify.vue'))
  122. Vue.component('NotFound', () => import(/* webpackChunkName: "not-found" */ './components/not-found.vue'))
  123. Vue.component('PageSelector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  124. Vue.component('PageSource', () => import(/* webpackChunkName: "source" */ './components/source.vue'))
  125. Vue.component('SearchResults', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/search-results.vue'))
  126. Vue.component('SocialSharing', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/social-sharing.vue'))
  127. Vue.component('Tags', () => import(/* webpackChunkName: "tags" */ './components/tags.vue'))
  128. Vue.component('Unauthorized', () => import(/* webpackChunkName: "unauthorized" */ './components/unauthorized.vue'))
  129. Vue.component('VCardChin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  130. Vue.component('VCardInfo', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-info.vue'))
  131. Vue.component('NavFooter', () => import(/* webpackChunkName: "theme" */ './themes/' + siteConfig.theme + '/components/nav-footer.vue'))
  132. Vue.component('Page', () => import(/* webpackChunkName: "theme" */ './themes/' + siteConfig.theme + '/components/page.vue'))
  133. let bootstrap = () => {
  134. // ====================================
  135. // Notifications
  136. // ====================================
  137. window.addEventListener('beforeunload', () => {
  138. store.dispatch('startLoading')
  139. })
  140. const apolloProvider = new VueApollo({
  141. defaultClient: window.graphQL
  142. })
  143. // ====================================
  144. // Bootstrap Vue
  145. // ====================================
  146. const i18n = localization.init()
  147. let darkModeEnabled = siteConfig.darkMode
  148. if ((store.get('user/appearance') || '').length > 0) {
  149. darkModeEnabled = (store.get('user/appearance') === 'dark')
  150. }
  151. window.WIKI = new Vue({
  152. el: '#root',
  153. components: {},
  154. mixins: [helpers],
  155. apolloProvider,
  156. store,
  157. i18n,
  158. vuetify: new Vuetify({
  159. rtl: siteConfig.rtl,
  160. theme: {
  161. dark: darkModeEnabled
  162. }
  163. }),
  164. mounted () {
  165. this.$moment.locale(siteConfig.lang)
  166. if ((store.get('user/dateFormat') || '').length > 0) {
  167. this.$moment.updateLocale(this.$moment.locale(), {
  168. longDateFormat: {
  169. 'L': store.get('user/dateFormat')
  170. }
  171. })
  172. }
  173. if ((store.get('user/timezone') || '').length > 0) {
  174. this.$moment.tz.setDefault(store.get('user/timezone'))
  175. }
  176. }
  177. })
  178. // ----------------------------------
  179. // Dispatch boot ready
  180. // ----------------------------------
  181. window.boot.notify('vue')
  182. }
  183. window.boot.onDOMReady(bootstrap)