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

9 10
Vue.use(Translate);

11 12 13 14 15 16 17
/**
 * 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).
 * @param {(e:Element) => Object} options.extraInitialData -
 *   Function that returns extra properties to seed initial data.
18 19
 * @param {Component} options.rootComponent -
 *   Component that overrides the root component.
20 21
 */
export function initIde(el, options = {}) {
P
Phil Hughes 已提交
22 23
  if (!el) return null;

M
Mike Greiling 已提交
24
  const { extraInitialData = () => ({}), rootComponent = ide } = options;
25

P
Phil Hughes 已提交
26 27 28 29
  return new Vue({
    el,
    store,
    router,
P
Phil Hughes 已提交
30
    created() {
31
      this.setEmptyStateSvgs({
P
Phil Hughes 已提交
32 33 34
        emptyStateSvgPath: el.dataset.emptyStateSvgPath,
        noChangesStateSvgPath: el.dataset.noChangesStateSvgPath,
        committedStateSvgPath: el.dataset.committedStateSvgPath,
35
        pipelinesEmptyStateSvgPath: el.dataset.pipelinesEmptyStateSvgPath,
P
Phil Hughes 已提交
36
        promotionSvgPath: el.dataset.promotionSvgPath,
P
Phil Hughes 已提交
37
      });
38 39
      this.setLinks({
        ciHelpPagePath: el.dataset.ciHelpPagePath,
P
Phil Hughes 已提交
40 41 42
        webIDEHelpPagePath: el.dataset.webIdeHelpPagePath,
      });
      this.setInitialData({
43
        clientsidePreviewEnabled: parseBoolean(el.dataset.clientsidePreviewEnabled),
44
        ...extraInitialData(el),
45 46 47
      });
    },
    methods: {
P
Phil Hughes 已提交
48
      ...mapActions(['setEmptyStateSvgs', 'setLinks', 'setInitialData']),
P
Phil Hughes 已提交
49
    },
P
Phil Hughes 已提交
50
    render(createElement) {
51
      return createElement(rootComponent);
P
Phil Hughes 已提交
52 53 54 55
    },
  });
}

56
// tell webpack to load assets from origin so that web workers don't break
57
export function resetServiceWorkersPublicPath() {
58 59 60
  // __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/
61 62 63 64
  const relativeRootPath = (gon && gon.relative_url_root) || '';
  const webpackAssetPath = `${relativeRootPath}/assets/webpack/`;
  __webpack_public_path__ = webpackAssetPath; // eslint-disable-line camelcase
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

/**
 * 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);
    }
  });
}