|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div> |
| 4 | + <v-btn icon :dark="dark" @click="onClick"> |
| 5 | + <v-icon medium>{{icon}}</v-icon> |
| 6 | + {{text}} |
| 7 | + </v-btn> |
| 8 | + <textarea id="permalinkurl" ref="permalinkurl" style="display: none; position: relative"></textarea> |
| 9 | + </div> |
| 10 | + <v-alert |
| 11 | + class="wgu-alert" |
| 12 | + :value="alert" |
| 13 | + :dark="dark" |
| 14 | + type="info" |
| 15 | + transition="fade-transition" |
| 16 | + > |
| 17 | + {{alertText}} |
| 18 | + </v-alert> |
| 19 | + </div> |
| 20 | +</template> |
| 21 | + |
| 22 | +<script> |
| 23 | +
|
| 24 | +import { Mapable } from '../../mixins/Mapable'; |
| 25 | +
|
| 26 | +export default { |
| 27 | + name: 'wgu-share-btn', |
| 28 | + mixins: [Mapable], |
| 29 | + props: { |
| 30 | + icon: {type: String, required: false, default: 'share'}, |
| 31 | + text: {type: String, required: false, default: ''}, |
| 32 | + alertText: {type: String, required: false, default: 'Link copied!'}, |
| 33 | + alertTimeMillis: {type: Number, required: false, default: 750}, |
| 34 | + dark: {type: Boolean, required: false, default: false} |
| 35 | + }, |
| 36 | + data () { |
| 37 | + return { |
| 38 | + alert: false |
| 39 | + } |
| 40 | + }, |
| 41 | + methods: { |
| 42 | + onClick () { |
| 43 | + // Get permalink URL and copy to clipboard |
| 44 | + const permalinkController = this.map.get('permalinkcontroller'); |
| 45 | + if (!permalinkController) { |
| 46 | + console.error('no permalink controller available!'); |
| 47 | + return true; |
| 48 | + } |
| 49 | + try { |
| 50 | + // Copy to clipboard: selected text needs to be visible so |
| 51 | + // toggle textarea short time |
| 52 | + const url = permalinkController.getShareUrl(); |
| 53 | + const textArea = this.$refs.permalinkurl; |
| 54 | + // Show and select textarea |
| 55 | + textArea.style.display = 'block'; |
| 56 | + textArea.value = url; |
| 57 | + textArea.select(); |
| 58 | +
|
| 59 | + // Copy to clipboard |
| 60 | + document.execCommand('copy'); |
| 61 | + // Hide textarea |
| 62 | + textArea.value = ''; |
| 63 | + textArea.style.display = 'none'; |
| 64 | +
|
| 65 | + // Show short alert |
| 66 | + this.alert = true; |
| 67 | + window.setTimeout(() => { |
| 68 | + this.alert = false; |
| 69 | + }, this.alertTimeMillis); |
| 70 | + } catch (error) { |
| 71 | + console.error(error); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +
|
| 77 | +</script> |
| 78 | + |
| 79 | +<!-- Add "scoped" attribute to limit CSS to this component only --> |
| 80 | +<style> |
| 81 | + .v-alert.wgu-alert { |
| 82 | + position: fixed; |
| 83 | + top: 5em; |
| 84 | + right: 4em; |
| 85 | + z-index: 1; |
| 86 | + } |
| 87 | + |
| 88 | +</style> |
0 commit comments