index.js 2.7 KB
Newer Older
P
Phil Hughes 已提交
1
import Vue from 'vue';
2
import { mapActions } from 'vuex';
3
import _ from 'underscore';
P
Phil Hughes 已提交
4 5 6 7
import Translate from '~/vue_shared/translate';
import ide from './components/ide.vue';
import store from './stores';
import router from './ide_router';
8
import { parseBoolean } from '../lib/utils/common_utils';
P
Phil Hughes 已提交
9

10 11
Vue.use(Translate);

12 13 14 15 16 17 18
/**
 * Function that receives the default store and returns an extended one.
 * @callback extendStoreCallback
 * @param {Vuex.Store} store
 * @param {Element} el
 */

19 20 21 22 23
/**
 * Initialize the IDE on the given element.
 *
 * @param {Element} el - The element that will contain the IDE.
 * @param {Object} options - Extra options for the IDE (Used by EE).
24 25
 * @param {Component} options.rootComponent -
 *   Component that overrides the root component.
26
 * @param {extendStoreCallback} options.extendStore -
27
 *   Function that receives the default store and returns an extended one.
28 29
 */
export function initIde(el, options = {}) {
P
Phil Hughes 已提交
30 31
  if (!el) return null;

32
  const { rootComponent = ide, extendStore = _.identity } = options;
33

P
Phil Hughes 已提交
34 35
  return new Vue({
    el,
36
    store: extendStore(store, el),
P
Phil Hughes 已提交
37
    router,
P
Phil Hughes 已提交
38
    created() {
39
      this.setEmptyStateSvgs({
P
Phil Hughes 已提交
40 41 42
        emptyStateSvgPath: el.dataset.emptyStateSvgPath,
        noChangesStateSvgPath: el.dataset.noChangesStateSvgPath,
        committedStateSvgPath: el.dataset.committedStateSvgPath,
43
        pipelinesEmptyStateSvgPath: el.dataset.pipelinesEmptyStateSvgPath,
P
Phil Hughes 已提交
44
        promotionSvgPath: el.dataset.promotionSvgPath,
P
Phil Hughes 已提交
45
      });
46 47
      this.setLinks({
        ciHelpPagePath: el.dataset.ciHelpPagePath,
P
Phil Hughes 已提交
48 49 50
        webIDEHelpPagePath: el.dataset.webIdeHelpPagePath,
      });
      this.setInitialData({
51
        clientsidePreviewEnabled: parseBoolean(el.dataset.clientsidePreviewEnabled),
52 53 54
      });
    },
    methods: {
P
Phil Hughes 已提交
55
      ...mapActions(['setEmptyStateSvgs', 'setLinks', 'setInitialData']),
P
Phil Hughes 已提交
56
    },
P
Phil Hughes 已提交
57
    render(createElement) {
58
      return createElement(rootComponent);
P
Phil Hughes 已提交
59 60 61 62
    },
  });
}

63
// tell webpack to load assets from origin so that web workers don't break
64
export function resetServiceWorkersPublicPath() {
65 66 67
  // __webpack_public_path__ is a global variable that can be used to adjust
  // the webpack publicPath setting at runtime.
  // see: https://webpack.js.org/guides/public-path/
68 69 70 71
  const relativeRootPath = (gon && gon.relative_url_root) || '';
  const webpackAssetPath = `${relativeRootPath}/assets/webpack/`;
  __webpack_public_path__ = webpackAssetPath; // eslint-disable-line camelcase
}
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

/**
 * Start the IDE.
 *
 * @param {Objects} options - Extra options for the IDE (Used by EE).
 */
export function startIde(options) {
  document.addEventListener('DOMContentLoaded', () => {
    const ideElement = document.getElementById('ide');
    if (ideElement) {
      resetServiceWorkersPublicPath();
      initIde(ideElement, options);
    }
  });
}