summaryrefslogtreecommitdiffstats
path: root/web_src/js/components/VueComponentLoader.js
blob: 6b2a2cbd58f62e589ae9c038e0a0b9bbf274e05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Vue from 'vue';
import {svgs} from '../svg.js';

const vueDelimiters = ['${', '}'];

let vueEnvInited = false;
function initVueEnv() {
  if (vueEnvInited) return;
  vueEnvInited = true;

  const isProd = window.config.IsProd;
  Vue.config.productionTip = false;
  Vue.config.devtools = !isProd;
}

let vueSvgInited = false;
function initVueSvg() {
  if (vueSvgInited) return;
  vueSvgInited = true;

  // register svg icon vue components, e.g. <octicon-repo size="16"/>
  for (const [name, htmlString] of Object.entries(svgs)) {
    const template = htmlString
      .replace(/height="[0-9]+"/, 'v-bind:height="size"')
      .replace(/width="[0-9]+"/, 'v-bind:width="size"');

    Vue.component(name, {
      props: {
        size: {
          type: String,
          default: '16',
        },
      },
      template,
    });
  }
}


function initVueApp(el, opts = {}) {
  if (typeof el === 'string') {
    el = document.querySelector(el);
  }
  if (!el) return null;

  return new Vue(Object.assign({
    el,
    delimiters: vueDelimiters,
  }, opts));
}

export {vueDelimiters, initVueEnv, initVueSvg, initVueApp};