summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/environment_details/pagination.vue
blob: 414610b306a57a55b1d330dbf62172506d327a84 (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
<script>
import { GlKeysetPagination } from '@gitlab/ui';
import { setUrlParams } from '~/lib/utils/url_utility';
import { translations } from './constants';

export default {
  components: {
    GlKeysetPagination,
  },
  props: {
    pageInfo: {
      type: Object,
      required: true,
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  translations,
  computed: {
    previousLink() {
      if (!this.pageInfo || !this.pageInfo.hasPreviousPage) {
        return '';
      }
      return setUrlParams({ before: this.pageInfo.startCursor }, window.location.href, true);
    },
    nextLink() {
      if (!this.pageInfo || !this.pageInfo.hasNextPage) {
        return '';
      }
      return setUrlParams({ after: this.pageInfo.endCursor }, window.location.href, true);
    },
    isPaginationVisible() {
      if (!this.pageInfo) {
        return false;
      }

      return this.pageInfo.hasNextPage || this.pageInfo.hasPreviousPage;
    },
  },
  methods: {
    onPrev(previousCursor) {
      this.$router.push({ query: { before: previousCursor } });
    },
    onNext(nextCursor) {
      this.$router.push({ query: { after: nextCursor } });
    },
    onPaginationClick(event) {
      // this check here is to ensure the proper default behvaior when a user ctrl/cmd + clicks the link
      if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) {
        return;
      }
      event.preventDefault();
    },
  },
};
</script>
<template>
  <div v-if="isPaginationVisible" class="gl--flex-center">
    <gl-keyset-pagination
      v-bind="pageInfo"
      :prev-text="$options.translations.previousPageButtonLabel"
      :next-text="$options.translations.nextPageButtonLabel"
      :prev-button-link="previousLink"
      :next-button-link="nextLink"
      :disabled="disabled"
      @prev="onPrev"
      @next="onNext"
      @click="onPaginationClick"
    />
  </div>
</template>