summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_merge_request_widget/components/action_buttons.vue
blob: 917ed259dd0c9720509d116a9ab227b933e13591 (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
<script>
import { GlButton, GlDropdown, GlDropdownItem, GlTooltipDirective } from '@gitlab/ui';
import { sprintf, __ } from '~/locale';

export default {
  components: {
    GlButton,
    GlDropdown,
    GlDropdownItem,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    widget: {
      type: String,
      required: false,
      default: '',
    },
    tertiaryButtons: {
      type: Array,
      required: false,
      default: () => [],
    },
  },
  data: () => {
    return {
      timeout: null,
      updatingTooltip: false,
    };
  },
  computed: {
    dropdownLabel() {
      if (!this.widget) return undefined;

      return sprintf(__('%{widget} options'), { widget: this.widget });
    },
    hasOneOption() {
      return this.tertiaryButtons.length === 1;
    },
    hasMultipleOptions() {
      return this.tertiaryButtons.length > 1;
    },
  },
  methods: {
    onClickAction(action) {
      this.$emit('clickedAction', action);

      if (action.onClick) {
        action.onClick();
      }

      if (action.tooltipOnClick) {
        this.updatingTooltip = true;
        this.$root.$emit('bv::show::tooltip', action.id);

        clearTimeout(this.timeout);

        this.timeout = setTimeout(() => {
          this.updatingTooltip = false;
          this.$root.$emit('bv::hide::tooltip', action.id);
        }, 1000);
      }
    },
    setTooltip(btn) {
      if (this.updatingTooltip && btn.tooltipOnClick) {
        return btn.tooltipOnClick;
      }

      return btn.tooltipText;
    },
    actionButtonQaSelector(btn) {
      if (btn.dataQaSelector) {
        return btn.dataQaSelector;
      }
      return 'mr_widget_extension_actions_button';
    },
  },
};
</script>

<template>
  <div class="gl-display-flex gl-align-items-flex-start">
    <template v-if="hasOneOption">
      <gl-button
        v-for="(btn, index) in tertiaryButtons"
        :id="btn.id"
        :key="index"
        v-gl-tooltip.hover
        :title="setTooltip(btn)"
        :href="btn.href"
        :target="btn.target"
        :class="[{ 'gl-mr-3': index !== tertiaryButtons.length - 1 }, btn.class]"
        :data-clipboard-text="btn.dataClipboardText"
        :data-qa-selector="actionButtonQaSelector(btn)"
        :data-method="btn.dataMethod"
        :icon="btn.icon"
        :data-testid="btn.testId || 'extension-actions-button'"
        :variant="btn.variant || 'confirm'"
        :loading="btn.loading"
        :disabled="btn.loading"
        category="tertiary"
        size="small"
        class="gl-md-display-block gl-float-left"
        @click="onClickAction(btn)"
      >
        {{ btn.text }}
      </gl-button>
    </template>
    <template v-if="hasMultipleOptions">
      <gl-dropdown
        v-gl-tooltip
        :title="__('Options')"
        :text="dropdownLabel"
        icon="ellipsis_v"
        no-caret
        category="tertiary"
        right
        lazy
        text-sr-only
        size="small"
        toggle-class="gl-p-2!"
        class="gl-display-block gl-md-display-none!"
      >
        <gl-dropdown-item
          v-for="(btn, index) in tertiaryButtons"
          :key="index"
          :href="btn.href"
          :target="btn.target"
          :data-clipboard-text="btn.dataClipboardText"
          :data-method="btn.dataMethod"
          @click="onClickAction(btn)"
        >
          {{ btn.text }}
        </gl-dropdown-item>
      </gl-dropdown>
      <gl-button
        v-for="(btn, index) in tertiaryButtons"
        :id="btn.id"
        :key="index"
        v-gl-tooltip.hover
        :title="setTooltip(btn)"
        :href="btn.href"
        :target="btn.target"
        :class="[{ 'gl-mr-3': index !== tertiaryButtons.length - 1 }, btn.class]"
        :data-clipboard-text="btn.dataClipboardText"
        :data-qa-selector="actionButtonQaSelector(btn)"
        :data-method="btn.dataMethod"
        :icon="btn.icon"
        :data-testid="btn.testId || 'extension-actions-button'"
        :variant="btn.variant || 'confirm'"
        :loading="btn.loading"
        :disabled="btn.loading"
        category="tertiary"
        size="small"
        class="gl-display-none gl-md-display-block gl-float-left"
        @click="onClickAction(btn)"
      >
        {{ btn.text }}
      </gl-button>
    </template>
  </div>
</template>