Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vue_router.js « vue3compat « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 006ed920ef051d9f4dc7fab4dcefd157fc51d157 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import Vue from 'vue';
import {
  createRouter,
  createMemoryHistory,
  createWebHistory,
  createWebHashHistory,
} from 'vue-router-vue3';

const mode = (value, options) => {
  if (!value) return null;
  let history;
  // eslint-disable-next-line default-case
  switch (value) {
    case 'history':
      history = createWebHistory(options.base);
      break;
    case 'hash':
      history = createWebHashHistory();
      break;
    case 'abstract':
      history = createMemoryHistory();
      break;
  }
  return { history };
};

const base = () => null;

const toNewCatchAllPath = (path) => {
  if (path === '*') return '/:pathMatch(.*)*';
  return path;
};

const routes = (value) => {
  if (!value) return null;
  const newRoutes = value.reduce(function handleRoutes(acc, route) {
    const newRoute = {
      ...route,
      path: toNewCatchAllPath(route.path),
    };
    if (route.children) {
      newRoute.children = route.children.reduce(handleRoutes, []);
    }
    acc.push(newRoute);
    return acc;
  }, []);
  return { routes: newRoutes };
};

const scrollBehavior = (value) => {
  return {
    scrollBehavior(...args) {
      const { x, y, left, top } = value(...args);
      return { left: x || left, top: y || top };
    },
  };
};

const transformers = {
  mode,
  base,
  routes,
  scrollBehavior,
};

const transformOptions = (options) => {
  const defaultConfig = {
    routes: null,
    history: createWebHashHistory(),
  };
  return Object.keys(options).reduce((acc, key) => {
    const value = options[key];
    if (key in transformers) {
      Object.assign(acc, transformers[key](value, options));
    } else {
      acc[key] = value;
    }
    return acc;
  }, defaultConfig);
};

const installed = new WeakMap();

export default class VueRouterCompat {
  constructor(options) {
    // eslint-disable-next-line no-constructor-return
    return createRouter(transformOptions(options));
  }

  static install() {
    Vue.mixin({
      beforeCreate() {
        const { app } = this.$.appContext;
        const { router } = this.$options;
        if (router && !installed.get(app)?.has(router)) {
          if (!installed.has(app)) {
            installed.set(app, new WeakSet());
          }
          installed.get(app).add(router);
          this.$.appContext.app.use(this.$options.router);
        }
      },
    });
  }
}