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';
P
Phil Hughes 已提交
7
import { convertPermissionToBoolean } 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;

24 25
  const {
    extraInitialData = () => ({}),
26
    rootComponent = ide,
27 28
  } = options;

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

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

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