Commit 36ce17e4 authored by Andrew Fontaine's avatar Andrew Fontaine

Remove Use of jQuery Tooltip in Modal Copy Button

This currently relies on a Bootstrap root event to hide the tooltip, as
code in `app/assets/javascripts/behaviors/copy_to_clipboard.js` shows a
new tooltip with the `Copied` text on success.

Until this is changed so the tooltip state is all local, the easiest way
is to use a root event.
parent 5cf068a4
<script> <script>
import $ from 'jquery';
import { GlButton, GlTooltipDirective } from '@gitlab/ui'; import { GlButton, GlTooltipDirective } from '@gitlab/ui';
import Clipboard from 'clipboard'; import Clipboard from 'clipboard';
import { __ } from '~/locale'; import { uniqueId } from 'lodash';
export default { export default {
components: { components: {
...@@ -17,6 +16,11 @@ export default { ...@@ -17,6 +16,11 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
id: {
type: String,
required: false,
default: () => uniqueId('modal-copy-button-'),
},
container: { container: {
type: String, type: String,
required: false, required: false,
...@@ -52,7 +56,6 @@ export default { ...@@ -52,7 +56,6 @@ export default {
default: null, default: null,
}, },
}, },
copySuccessText: __('Copied'),
computed: { computed: {
modalDomId() { modalDomId() {
return this.modalId ? `#${this.modalId}` : ''; return this.modalId ? `#${this.modalId}` : '';
...@@ -68,11 +71,11 @@ export default { ...@@ -68,11 +71,11 @@ export default {
}); });
this.clipboard this.clipboard
.on('success', e => { .on('success', e => {
this.updateTooltip(e.trigger); this.$root.$emit('bv::hide::tooltip', this.id);
this.$emit('success', e); this.$emit('success', e);
// Clear the selection and blur the trigger so it loses its border // Clear the selection and blur the trigger so it loses its border
e.clearSelection(); e.clearSelection();
$(e.trigger).blur(); e.trigger.blur();
}) })
.on('error', e => this.$emit('error', e)); .on('error', e => this.$emit('error', e));
}); });
...@@ -82,29 +85,11 @@ export default { ...@@ -82,29 +85,11 @@ export default {
this.clipboard.destroy(); 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> </script>
<template> <template>
<gl-button <gl-button
:id="id"
v-gl-tooltip="{ placement: tooltipPlacement, container: tooltipContainer }" v-gl-tooltip="{ placement: tooltipPlacement, container: tooltipContainer }"
:class="cssClasses" :class="cssClasses"
:data-clipboard-target="target" :data-clipboard-target="target"
......
import Vue from 'vue'; import { shallowMount, createWrapper } from '@vue/test-utils';
import { shallowMount } from '@vue/test-utils'; import ModalCopyButton from '~/vue_shared/components/modal_copy_button.vue';
import modalCopyButton from '~/vue_shared/components/modal_copy_button.vue';
describe('modal copy button', () => { describe('modal copy button', () => {
const Component = Vue.extend(modalCopyButton);
let wrapper; let wrapper;
afterEach(() => { afterEach(() => {
...@@ -11,16 +9,18 @@ describe('modal copy button', () => { ...@@ -11,16 +9,18 @@ describe('modal copy button', () => {
}); });
beforeEach(() => { beforeEach(() => {
wrapper = shallowMount(Component, { wrapper = shallowMount(ModalCopyButton, {
propsData: { propsData: {
text: 'copy me', text: 'copy me',
title: 'Copy this value', title: 'Copy this value',
id: 'test-id',
}, },
}); });
}); });
describe('clipboard', () => { describe('clipboard', () => {
it('should fire a `success` event on click', () => { it('should fire a `success` event on click', () => {
const root = createWrapper(wrapper.vm.$root);
document.execCommand = jest.fn(() => true); document.execCommand = jest.fn(() => true);
window.getSelection = jest.fn(() => ({ window.getSelection = jest.fn(() => ({
toString: jest.fn(() => 'test'), toString: jest.fn(() => 'test'),
...@@ -31,6 +31,7 @@ describe('modal copy button', () => { ...@@ -31,6 +31,7 @@ describe('modal copy button', () => {
return wrapper.vm.$nextTick().then(() => { return wrapper.vm.$nextTick().then(() => {
expect(wrapper.emitted().success).not.toBeEmpty(); expect(wrapper.emitted().success).not.toBeEmpty();
expect(document.execCommand).toHaveBeenCalledWith('copy'); expect(document.execCommand).toHaveBeenCalledWith('copy');
expect(root.emitted('bv::hide::tooltip')).toEqual([['test-id']]);
}); });
}); });
it("should propagate the clipboard error event if execCommand doesn't work", () => { it("should propagate the clipboard error event if execCommand doesn't work", () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment