manual_variables_form.vue 5.6 KB
Newer Older
M
Matija Čupić 已提交
1
<script>
2
/* eslint-disable vue/no-v-html */
3
import { uniqueId } from 'lodash';
M
Matija Čupić 已提交
4
import { mapActions } from 'vuex';
5
import { GlButton } from '@gitlab/ui';
M
Matija Čupić 已提交
6 7 8 9 10
import { s__, sprintf } from '~/locale';

export default {
  name: 'ManualVariablesForm',
  components: {
11
    GlButton,
M
Matija Čupić 已提交
12 13 14 15 16 17 18 19 20
  },
  props: {
    action: {
      type: Object,
      required: false,
      default: null,
      validator(value) {
        return (
          value === null ||
21 22 23
          (Object.prototype.hasOwnProperty.call(value, 'path') &&
            Object.prototype.hasOwnProperty.call(value, 'method') &&
            Object.prototype.hasOwnProperty.call(value, 'button_title'))
M
Matija Čupić 已提交
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
        );
      },
    },
    variablesSettingsUrl: {
      type: String,
      required: true,
      default: '',
    },
  },
  inputTypes: {
    key: 'key',
    value: 'value',
  },
  i18n: {
    keyPlaceholder: s__('CiVariables|Input variable key'),
    valuePlaceholder: s__('CiVariables|Input variable value'),
  },
  data() {
    return {
      variables: [],
      key: '',
      secretValue: '',
    };
  },
  computed: {
    helpText() {
      return sprintf(
        s__(
          'CiVariables|Specify variable values to be used in this run. The values specified in %{linkStart}CI/CD settings%{linkEnd} will be used as default',
        ),
        {
          linkStart: `<a href="${this.variablesSettingsUrl}">`,
          linkEnd: '</a>',
        },
        false,
      );
    },
  },
  watch: {
    key(newVal) {
      this.handleValueChange(newVal, this.$options.inputTypes.key);
    },
    secretValue(newVal) {
      this.handleValueChange(newVal, this.$options.inputTypes.value);
    },
  },
  methods: {
    ...mapActions(['triggerManualJob']),
    handleValueChange(newValue, type) {
      if (newValue !== '') {
        this.createNewVariable(type);
        this.resetForm();
      }
    },
    createNewVariable(type) {
      const newVariable = {
        key: this.key,
        secret_value: this.secretValue,
82
        id: uniqueId(),
M
Matija Čupić 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      };

      this.variables.push(newVariable);

      return this.$nextTick().then(() => {
        this.$refs[`${this.$options.inputTypes[type]}-${newVariable.id}`][0].focus();
      });
    },
    resetForm() {
      this.key = '';
      this.secretValue = '';
    },
    deleteVariable(id) {
      this.variables.splice(this.variables.findIndex(el => el.id === id), 1);
    },
  },
};
</script>
<template>
102
  <div class="col-12" data-testid="manual-vars-form">
M
Matija Čupić 已提交
103 104 105 106 107 108 109 110 111 112 113
    <label>{{ s__('CiVariables|Variables') }}</label>

    <div class="ci-table">
      <div class="gl-responsive-table-row table-row-header pb-0 pt-0 border-0" role="row">
        <div class="table-section section-50" role="rowheader">{{ s__('CiVariables|Key') }}</div>
        <div class="table-section section-50" role="rowheader">{{ s__('CiVariables|Value') }}</div>
      </div>

      <div v-for="variable in variables" :key="variable.id" class="gl-responsive-table-row">
        <div class="table-section section-50">
          <div class="table-mobile-header" role="rowheader">{{ s__('Pipeline|Key') }}</div>
114
          <div class="table-mobile-content gl-mr-3">
M
Matija Čupić 已提交
115 116 117 118 119 120 121 122 123 124 125
            <input
              :ref="`${$options.inputTypes.key}-${variable.id}`"
              v-model="variable.key"
              :placeholder="$options.i18n.keyPlaceholder"
              class="ci-variable-body-item form-control"
            />
          </div>
        </div>

        <div class="table-section section-50">
          <div class="table-mobile-header" role="rowheader">{{ s__('Pipeline|Value') }}</div>
126
          <div class="table-mobile-content gl-mr-3">
M
Matija Čupić 已提交
127 128 129 130 131 132 133 134 135 136 137 138
            <input
              :ref="`${$options.inputTypes.value}-${variable.id}`"
              v-model="variable.secret_value"
              :placeholder="$options.i18n.valuePlaceholder"
              class="ci-variable-body-item form-control"
            />
          </div>
        </div>

        <div class="table-section section-10">
          <div class="table-mobile-header" role="rowheader"></div>
          <div class="table-mobile-content justify-content-end">
139 140 141 142
            <gl-button
              category="tertiary"
              icon="clear"
              :aria-label="__('Delete variable')"
143
              @click="deleteVariable(variable.id)"
144
            />
M
Matija Čupić 已提交
145 146 147 148 149 150
          </div>
        </div>
      </div>
      <div class="gl-responsive-table-row">
        <div class="table-section section-50">
          <div class="table-mobile-header" role="rowheader">{{ s__('Pipeline|Key') }}</div>
151
          <div class="table-mobile-content gl-mr-3">
M
Matija Čupić 已提交
152 153 154 155 156 157 158 159 160 161 162
            <input
              ref="inputKey"
              v-model="key"
              class="js-input-key form-control"
              :placeholder="$options.i18n.keyPlaceholder"
            />
          </div>
        </div>

        <div class="table-section section-50">
          <div class="table-mobile-header" role="rowheader">{{ s__('Pipeline|Value') }}</div>
163
          <div class="table-mobile-content gl-mr-3">
M
Matija Čupić 已提交
164 165 166 167 168 169 170 171 172 173
            <input
              ref="inputSecretValue"
              v-model="secretValue"
              class="ci-variable-body-item form-control"
              :placeholder="$options.i18n.valuePlaceholder"
            />
          </div>
        </div>
      </div>
    </div>
174
    <div class="d-flex gl-mt-3 justify-content-center">
M
Matija Čupić 已提交
175 176 177
      <p class="text-muted" v-html="helpText"></p>
    </div>
    <div class="d-flex justify-content-center">
178 179 180 181 182 183
      <gl-button
        variant="info"
        category="primary"
        :aria-label="__('Trigger manual job')"
        @click="triggerManualJob(variables)"
      >
M
Matija Čupić 已提交
184
        {{ action.button_title }}
185
      </gl-button>
M
Matija Čupić 已提交
186 187 188
    </div>
  </div>
</template>