summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/projects/shared/permissions/components/project_feature_setting.vue
blob: 9b13b2a524f6c255a42d7c73aae01a558b4da97f (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
<script>
  import projectFeatureToggle from '../../../../../vue_shared/components/toggle_button.vue';

  export default {
    components: {
      projectFeatureToggle,
    },

    model: {
      prop: 'value',
      event: 'change',
    },

    props: {
      name: {
        type: String,
        required: false,
        default: '',
      },
      options: {
        type: Array,
        required: false,
        default: () => [],
      },
      value: {
        type: Number,
        required: false,
        default: 0,
      },
      disabledInput: {
        type: Boolean,
        required: false,
        default: false,
      },
    },

    computed: {
      featureEnabled() {
        return this.value !== 0;
      },

      displayOptions() {
        if (this.featureEnabled) {
          return this.options;
        }
        return [
          [0, 'Enable feature to choose access level'],
        ];
      },

      displaySelectInput() {
        return this.disabledInput || !this.featureEnabled || this.displayOptions.length < 2;
      },
    },

    methods: {
      toggleFeature(featureEnabled) {
        if (featureEnabled === false || this.options.length < 1) {
          this.$emit('change', 0);
        } else {
          const [firstOptionValue] = this.options[this.options.length - 1];
          this.$emit('change', firstOptionValue);
        }
      },

      selectOption(e) {
        this.$emit('change', Number(e.target.value));
      },
    },
  };
</script>

<template>
  <div
    class="project-feature-controls"
    :data-for="name"
  >
    <input
      v-if="name"
      type="hidden"
      :name="name"
      :value="value"
    />
    <project-feature-toggle
      :value="featureEnabled"
      @change="toggleFeature"
      :disabled-input="disabledInput"
    />
    <div class="select-wrapper">
      <select
        class="form-control project-repo-select select-control"
        @change="selectOption"
        :disabled="displaySelectInput"
      >
        <option
          v-for="[optionValue, optionName] in displayOptions"
          :key="optionValue"
          :value="optionValue"
          :selected="optionValue === value"
        >
          {{ optionName }}
        </option>
      </select>
      <i
        aria-hidden="true"
        class="fa fa-chevron-down"
      >
      </i>
    </div>
  </div>
</template>