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

versions_menu.vue « components « default « frontend « content - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f29e7af53cbfe3d0c65aed9b1b182a997c7a9ecd (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
<script>
import { GlDropdown, GlDropdownItem, GlDropdownDivider } from '@gitlab/ui';
import { getVersions } from '../../services/fetch_versions';
import { isGitLabHosted } from '../environment';

export default {
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownDivider,
  },
  data() {
    return {
      versions: {},
      activeVersion: '',
    };
  },
  async created() {
    // Only build this menu if this is a GitLab-hosted copy of the site.
    // Self-hosted Docs will only contain a single version.
    if (isGitLabHosted()) {
      try {
        this.versions = await getVersions();
        this.activeVersion = this.getActiveVersion(this.versions);
      } catch (err) {
        console.error(`Failed to fetch versions.json: ${err}`); // eslint-disable-line no-console
      }
    }
  },
  methods: {
    getVersionPath(versionNumber) {
      let path = window.location.pathname;

      // If we're viewing an older version, drop its version prefix when creating links.
      if (this.activeVersion !== this.versions.next) {
        const pathArr = window.location.pathname.split('/').filter((n) => n);
        pathArr.shift();
        path = `/${pathArr.join('/')}`;
      }

      if (versionNumber) {
        path = `/${versionNumber}${path}`;
      }
      return path;
    },
    getActiveVersion(versions) {
      let activeVersion = versions.next;

      // Check if the first item in the URL path is a valid version.
      // If so, that should be the active menu item.
      const versionPath = window.location.pathname.split('/')[1];

      Object.keys(versions).forEach((key) => {
        if (
          versions[key] === versionPath ||
          (versions[key].constructor === Array && versions[key].includes(versionPath))
        ) {
          activeVersion = versionPath;
        }
      });
      return activeVersion;
    },
  },
};
</script>

<template>
  <gl-dropdown
    v-if="versions.next"
    :text="activeVersion"
    class="mb-2 mb-md-0 mr-md-3 ml-md-3 d-flex"
    data-testid="versions-menu"
  >
    <gl-dropdown-item :href="getVersionPath()">
      <span data-testid="next-version">{{ versions.next }}</span> (not yet released)
    </gl-dropdown-item>
    <gl-dropdown-divider />

    <gl-dropdown-item :href="getVersionPath(versions.current)">
      {{ versions.current }} (recently released)
    </gl-dropdown-item>
    <gl-dropdown-item v-for="v in versions.last_minor" :key="v" :href="getVersionPath(v)">
      {{ v }}
    </gl-dropdown-item>
    <gl-dropdown-divider />

    <gl-dropdown-item v-for="v in versions.last_major" :key="v" :href="getVersionPath(v)">
      {{ v }}
    </gl-dropdown-item>
    <gl-dropdown-divider />

    <gl-dropdown-item href="/archives">Archives</gl-dropdown-item>
  </gl-dropdown>
</template>