summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/modal_copy_button.vue
blob: bf59a6abf3f05bfcd6ce627179367f10d61c288b (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
<script>
import $ from 'jquery';
import { GlButton, GlTooltipDirective } from '@gitlab/ui';
import { __ } from '~/locale';
import Icon from '~/vue_shared/components/icon.vue';
import Clipboard from 'clipboard';

export default {
  components: {
    GlButton,
    Icon,
  },

  directives: {
    GlTooltip: GlTooltipDirective,
  },

  props: {
    text: {
      type: String,
      required: false,
      default: '',
    },
    container: {
      type: String,
      required: false,
      default: '',
    },
    modalId: {
      type: String,
      required: false,
      default: '',
    },
    target: {
      type: String,
      required: false,
      default: '',
    },
    title: {
      type: String,
      required: true,
    },
    tooltipPlacement: {
      type: String,
      required: false,
      default: 'top',
    },
    tooltipContainer: {
      type: String,
      required: false,
      default: null,
    },
  },

  copySuccessText: __('Copied'),

  computed: {
    modalDomId() {
      return this.modalId ? `#${this.modalId}` : '';
    },
  },

  mounted() {
    this.$nextTick(() => {
      this.clipboard = new Clipboard(this.$el, {
        container:
          document.querySelector(`${this.modalDomId} div.modal-content`) ||
          document.getElementById(this.container) ||
          document.body,
      });
      this.clipboard
        .on('success', e => {
          this.updateTooltip(e.trigger);
          this.$emit('success', e);
          // Clear the selection and blur the trigger so it loses its border
          e.clearSelection();
          $(e.trigger).blur();
        })
        .on('error', e => this.$emit('error', e));
    });
  },

  destroyed() {
    if (this.clipboard) {
      this.clipboard.destroy();
    }
  },

  methods: {
    updateTooltip(target) {
      const $target = $(target);
      const originalTitle = $target.data('originalTitle');

      if ($target.tooltip) {
        /**
         *  The original tooltip will continue staying there unless we remove it by hand.
         *  $target.tooltip('hide') isn't working.
         */
        $('.tooltip').remove();
        $target.attr('title', this.$options.copySuccessText);
        $target.tooltip('_fixTitle');
        $target.tooltip('show');
        $target.attr('title', originalTitle);
        $target.tooltip('_fixTitle');
      }
    },
  },
};
</script>
<template>
  <gl-button
    v-gl-tooltip="{ placement: tooltipPlacement, container: tooltipContainer }"
    :data-clipboard-target="target"
    :data-clipboard-text="text"
    :title="title"
  >
    <slot>
      <icon name="duplicate" />
    </slot>
  </gl-button>
</template>