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

runner_instructions_modal.vue « runner_instructions « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 22d9b88fa41447c673d1fa6162c0dce163f389b5 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<script>
import {
  GlAlert,
  GlButton,
  GlModal,
  GlButtonGroup,
  GlDropdown,
  GlDropdownItem,
  GlIcon,
  GlLoadingIcon,
  GlSkeletonLoader,
  GlResizeObserverDirective,
} from '@gitlab/ui';
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
import { __, s__ } from '~/locale';
import getRunnerPlatformsQuery from './graphql/get_runner_platforms.query.graphql';
import { PLATFORM_DOCKER, PLATFORM_KUBERNETES, PLATFORM_AWS } from './constants';

import RunnerCliInstructions from './instructions/runner_cli_instructions.vue';
import RunnerDockerInstructions from './instructions/runner_docker_instructions.vue';
import RunnerKubernetesInstructions from './instructions/runner_kubernetes_instructions.vue';
import RunnerAwsInstructions from './instructions/runner_aws_instructions.vue';

export default {
  components: {
    GlAlert,
    GlButton,
    GlButtonGroup,
    GlDropdown,
    GlDropdownItem,
    GlModal,
    GlIcon,
    GlLoadingIcon,
    GlSkeletonLoader,
    RunnerDockerInstructions,
  },
  directives: {
    GlResizeObserver: GlResizeObserverDirective,
  },
  props: {
    modalId: {
      type: String,
      required: false,
      default: 'runner-instructions-modal',
    },
    registrationToken: {
      type: String,
      required: false,
      default: null,
    },
    defaultPlatformName: {
      type: String,
      required: false,
      default: null,
    },
  },
  apollo: {
    platforms: {
      query: getRunnerPlatformsQuery,
      skip() {
        // Only load instructions once the modal is shown
        return !this.shown;
      },
      update(data) {
        return (
          data?.runnerPlatforms?.nodes.map(({ name, humanReadableName, architectures }) => {
            return {
              name,
              humanReadableName,
              architectures: architectures?.nodes || [],
            };
          }) ?? []
        );
      },
      result() {
        // If found, select the defaultSelectedPlatform.
        // Otherwise, select the first available platform
        const platform =
          this.platforms?.find(({ name }) => this.defaultPlatformName === name) ||
          this.platforms?.[0];

        this.selectPlatform(platform);
      },
      error() {
        this.toggleAlert(true);
      },
    },
  },
  data() {
    return {
      shown: false,
      platforms: [],
      selectedPlatform: null,
      showAlert: false,
      platformsButtonGroupVertical: false,
    };
  },
  computed: {
    instructionsComponent() {
      if (this.selectedPlatform?.architectures?.length) {
        return RunnerCliInstructions;
      }
      switch (this.selectedPlatform?.name) {
        case PLATFORM_DOCKER:
          return RunnerDockerInstructions;
        case PLATFORM_KUBERNETES:
          return RunnerKubernetesInstructions;
        case PLATFORM_AWS:
          return RunnerAwsInstructions;
        default:
          return null;
      }
    },
  },
  updated() {
    // Refocus on dom changes, after loading data
    this.refocusSelectedPlatformButton();
  },
  methods: {
    show() {
      this.$refs.modal.show();
    },
    close() {
      this.$refs.modal.close();
    },
    onClose() {
      this.close();
    },
    onShown() {
      this.shown = true;
      this.refocusSelectedPlatformButton();
    },
    refocusSelectedPlatformButton() {
      // On modal opening, the first focusable element is auto-focused by bootstrap-vue
      // This can be confusing for users, because the wrong platform button can
      // get focused when setting a `defaultPlatformName`.
      // This method refocuses the expected button.
      // See more about this auto-focus: https://bootstrap-vue.org/docs/components/modal#auto-focus-on-open
      this.$refs[this.selectedPlatform?.name]?.[0].$el.focus();
    },
    selectPlatform(platform) {
      this.selectedPlatform = platform;
    },
    isPlatformSelected(platform) {
      return this.selectedPlatform.name === platform.name;
    },
    toggleAlert(state) {
      this.showAlert = state;
    },
    onPlatformsButtonResize() {
      if (bp.getBreakpointSize() === 'xs') {
        this.platformsButtonGroupVertical = true;
      } else {
        this.platformsButtonGroupVertical = false;
      }
    },
  },
  i18n: {
    environment: __('Environment'),
    installARunner: s__('Runners|Install a runner'),
    downloadInstallBinary: s__('Runners|Download and install binary'),
    downloadLatestBinary: s__('Runners|Download latest binary'),
    fetchError: s__('Runners|An error has occurred fetching instructions'),
  },
};
</script>
<template>
  <gl-modal
    ref="modal"
    :modal-id="modalId"
    :title="$options.i18n.installARunner"
    v-bind="$attrs"
    hide-footer
    v-on="$listeners"
    @shown="onShown"
  >
    <gl-alert v-if="showAlert" variant="danger" @dismiss="toggleAlert(false)">
      {{ $options.i18n.fetchError }}
    </gl-alert>

    <gl-skeleton-loader v-if="!platforms.length && $apollo.loading" />

    <template v-if="platforms.length">
      <h5>
        {{ $options.i18n.environment }}
      </h5>
      <div v-gl-resize-observer="onPlatformsButtonResize">
        <gl-button-group
          :vertical="platformsButtonGroupVertical"
          :class="{ 'gl-w-full': platformsButtonGroupVertical }"
          class="gl-mb-3"
          data-testid="platform-buttons"
        >
          <gl-button
            v-for="platform in platforms"
            :key="platform.name"
            :ref="platform.name"
            :selected="isPlatformSelected(platform)"
            @click="selectPlatform(platform)"
          >
            {{ platform.humanReadableName }}
          </gl-button>
        </gl-button-group>
      </div>
    </template>

    <keep-alive>
      <component
        :is="instructionsComponent"
        :registration-token="registrationToken"
        :platform="selectedPlatform"
        @close="onClose"
        @error="toggleAlert(true)"
      />
    </keep-alive>
  </gl-modal>
</template>