All files / ext.wikilambda.app/components/base ClipboardManager.vue

90.24% Statements 74/82
87.5% Branches 7/8
100% Functions 6/6
90.24% Lines 74/82

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 831x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x                 1x 1x 8x 1x 1x 8x 8x 1x 1x  
<!--
	WikiLambda Vue component for Clipboard Manager
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<!-- Placeholder -->
	<div></div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const clipboardUtils = require( '../../mixins/clipboardUtils.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-clipboard-manager',
	mixins: [ clipboardUtils ],
	props: {
		classNames: {
			type: Array,
			required: false,
			default: function () {
				return [];
			}
		}
	},
	methods: {
		/**
		 * Display the copied message for the specified element.
		 *
		 * @param {HTMLElement} element - The element to display the copied message for.
		 */
		displayCopiedMessage: function ( element ) {
			element.setAttribute( 'data-copied', 'true' );
			element.innerText = this.getCopiedText;
		},
 
		/**
		 * Clear the copied message for the specified element.
		 *
		 * @param {HTMLElement} element - The element to clear the copied message for.
		 * @param {string} value - The value to set as the inner text of the element.
		 */
		clearCopiedMessage: function ( element, value ) {
			element.removeAttribute( 'data-copied' );
			element.innerText = value;
		},
 
		/**
		 * Handle the window click event.
		 *
		 * @param {MouseEvent} event - The click event.
		 */
		handleWindowClick: function ( event ) {
			const element = event.target;
 
			// If the click was on an element that has the data-copied attribute (meaning it was just copied),
			// or if the click was not on an element with the specified class names, do nothing
			if (
				element.hasAttribute( 'data-copied' ) ||
				!this.classNames.some( ( className ) => element.classList.contains( className ) )
			) {
				return;
			}

			const value = element.textContent;
			this.copyToClipboard(
				value,
				this.displayCopiedMessage.bind( this, element ),
				this.clearCopiedMessage.bind( this, element, value )
			);
		}
	},
	mounted() {
		window.addEventListener( 'click', this.handleWindowClick );
	},
	beforeUnmount() {
		window.removeEventListener( 'click', this.handleWindowClick );
	}
} );
</script>