|
| 1 | +<template> |
| 2 | + <div class="copy-tools"> |
| 3 | + <copy-type-switch |
| 4 | + v-if="clipboardTarget!=='.xmp'" |
| 5 | + @change-copy-type="changeCopyType" |
| 6 | + /> |
| 7 | + <v-button |
| 8 | + v-if="clipboardTarget!=='.xmp'" |
| 9 | + class="donate small copy-button" |
| 10 | + :data-clipboard-target="clipboardTarget" |
| 11 | + @click="handleCopy" |
| 12 | + > |
| 13 | + {{ copyLabel }} |
| 14 | + </v-button> |
| 15 | + <v-button |
| 16 | + v-else |
| 17 | + class="donate small copy-button is-xmp" |
| 18 | + > |
| 19 | + {{ xmpLabel }} |
| 20 | + </v-button> |
| 21 | + </div> |
| 22 | +</template> |
| 23 | + |
| 24 | +<script> |
| 25 | +import CopyTypeSwitch from '@/components/CopyTypeSwitch' |
| 26 | +import Clipboard from 'clipboard' |
| 27 | +export default { |
| 28 | + name: 'CopyTools', |
| 29 | + components: { CopyTypeSwitch }, |
| 30 | + props: { |
| 31 | + clipboardTarget: { |
| 32 | + type: String, |
| 33 | + default: '.license-text' |
| 34 | + } |
| 35 | + }, |
| 36 | + data() { |
| 37 | + return { |
| 38 | + copyType: 'short', |
| 39 | + copyLabel: this.$t('license-use.copy-label'), |
| 40 | + xmpLabel: this.$t('license-use.xmp-label') |
| 41 | + } |
| 42 | + }, |
| 43 | + mounted() { |
| 44 | + this.clipboard = new Clipboard('.copy-button') |
| 45 | + this.clipboard.on('success', this.onCopySuccess) |
| 46 | + this.clipboard.on('error', this.onCopyError) |
| 47 | + }, |
| 48 | + destroyed() { |
| 49 | + this.clipboard.destroy() |
| 50 | + }, |
| 51 | + methods: { |
| 52 | + changeCopyType() { |
| 53 | + this.copyType = this.copyType === 'short' ? 'full' : 'short' |
| 54 | + this.$emit('change-copy-type', this.copyType) |
| 55 | + }, |
| 56 | + handleCopy() { |
| 57 | + this.copyLabel = this.$t('license-use.copied-label') |
| 58 | + setTimeout(() => { |
| 59 | + this.copyLabel = this.$t('license-use.copy-label') |
| 60 | + }, 2000) |
| 61 | + }, |
| 62 | + onCopySuccess(e) { |
| 63 | + this.success = true |
| 64 | + if (process.env.NODE_ENV === 'production') { |
| 65 | + const fieldsFilled = {} |
| 66 | + for (const detail in this.attributionDetails) { |
| 67 | + fieldsFilled[detail] = this.attributionDetails[detail] !== '' |
| 68 | + } |
| 69 | + const copiedLicense = { |
| 70 | + license: this.shortName, |
| 71 | + // codeType can be either rich-text or html |
| 72 | + codeType: 'plaintext', |
| 73 | + fieldsFilled: fieldsFilled |
| 74 | + } |
| 75 | + this.$ga.event({ |
| 76 | + eventCategory: 'Attribution', |
| 77 | + eventAction: 'copied', |
| 78 | + eventLabel: JSON.stringify(copiedLicense) |
| 79 | + }) |
| 80 | + } |
| 81 | + setTimeout(() => { |
| 82 | + this.success = false |
| 83 | + }, 2000) |
| 84 | + e.clearSelection() |
| 85 | + }, |
| 86 | + onCopyError(e) { |
| 87 | + e.clearSelection() |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +</script> |
| 92 | + |
| 93 | +<style lang="scss" scoped> |
| 94 | +.copy-tools { |
| 95 | + display: flex; |
| 96 | + flex-direction: row; |
| 97 | + justify-content: space-between; |
| 98 | + align-items: center; |
| 99 | + border: 0.125rem solid #d8d8d8; |
| 100 | + border-top: none; |
| 101 | + background-color: white; |
| 102 | + padding: 0 1.5rem 1.5rem; |
| 103 | + border-bottom-left-radius: 0.25rem; |
| 104 | + border-bottom-right-radius: 0.25rem; |
| 105 | +} |
| 106 | +.button.donate.small { |
| 107 | + justify-content: center; |
| 108 | + &.is-xmp { |
| 109 | + width: fit-content; |
| 110 | + } |
| 111 | +} |
| 112 | +</style> |
0 commit comments