All files / app/assets/javascripts/vue_shared/components clipboard_button.vue

100% Statements 13/13
100% Branches 1/1
100% Functions 6/6
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  162x                                                                                                                                                                               533x               536x 26x   510x     533x               533x       3x 3x   3x   3x 3x 3x                                                          
<script>
/**
 * Falls back to the code used in `copy_to_clipboard.js`
 *
 * Renders a button with a clipboard icon that copies the content of `data-clipboard-text`
 * when clicked.
 *
 * @example
 * <clipboard-button
 *   title="Copy"
 *   text="Content to be copied"
 *    css-class="btn-transparent"
 * />
 */
import { GlButton, GlTooltipDirective } from '@gitlab/ui';
import { uniqueId } from 'lodash';
 
import { __ } from '~/locale';
import {
  CLIPBOARD_SUCCESS_EVENT,
  CLIPBOARD_ERROR_EVENT,
  I18N_ERROR_MESSAGE,
} from '~/behaviors/copy_to_clipboard';
 
export default {
  name: 'ClipboardButton',
  i18n: {
    copied: __('Copied'),
    error: I18N_ERROR_MESSAGE,
  },
  CLIPBOARD_SUCCESS_EVENT,
  CLIPBOARD_ERROR_EVENT,
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  components: {
    GlButton,
  },
  props: {
    text: {
      type: String,
      required: true,
    },
    gfm: {
      type: String,
      required: false,
      default: null,
    },
    title: {
      type: String,
      required: true,
    },
    tooltipPlacement: {
      type: String,
      required: false,
      default: 'top',
    },
    tooltipContainer: {
      type: [String, Boolean],
      required: false,
      default: false,
    },
    tooltipBoundary: {
      type: String,
      required: false,
      default: null,
    },
    cssClass: {
      type: String,
      required: false,
      default: null,
    },
    category: {
      type: String,
      required: false,
      default: 'secondary',
    },
    size: {
      type: String,
      required: false,
      default: 'medium',
    },
    variant: {
      type: String,
      required: false,
      default: 'default',
    },
  },
  data() {
    return {
      localTitle: this.title,
      titleTimeout: null,
      id: null,
    };
  },
  computed: {
    clipboardText() {
      if (this.gfm !== null) {
        return JSON.stringify({ text: this.text, gfm: this.gfm });
      }
      return this.text;
    },
    tooltipDirectiveOptions() {
      return {
        placement: this.tooltipPlacement,
        container: this.tooltipContainer,
        boundary: this.tooltipBoundary,
      };
    },
  },
  created() {
    this.id = uniqueId('clipboard-button-');
  },
  methods: {
    updateTooltip(title) {
      this.localTitle = title;
      this.$root.$emit('bv::show::tooltip', this.id);
 
      clearTimeout(this.titleTimeout);
 
      this.titleTimeout = setTimeout(() => {
        this.localTitle = this.title;
        this.$root.$emit('bv::hide::tooltip', this.id);
      }, 1000);
    },
  },
};
</script>
 
<template>
  <gl-button
    :id="id"
    ref="copyButton"
    v-gl-tooltip.hover.focus.click.viewport="tooltipDirectiveOptions"
    :class="cssClass"
    :title="localTitle"
    :data-clipboard-text="clipboardText"
    data-clipboard-handle-tooltip="false"
    :category="category"
    :size="size"
    icon="copy-to-clipboard"
    :variant="variant"
    :aria-label="localTitle"
    aria-live="polite"
    @[$options.CLIPBOARD_SUCCESS_EVENT]="updateTooltip($options.i18n.copied)"
    @[$options.CLIPBOARD_ERROR_EVENT]="updateTooltip($options.i18n.error)"
    v-on="$listeners"
  >
    <slot></slot>
  </gl-button>
</template>