summaryrefslogtreecommitdiffstats
path: root/src/components/CopyableInput.vue
blob: 951fded94f7c5fba9cffd82eeabf41200d89f295 (plain)
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
<template>
    <div class="input-group">
        <input
            :id="id"
            ref="input"
            v-model="model"
            :type="type"
            class="form-control"
            :placeholder="placeholder"
            :autocomplete="autocomplete"
            :required="required"
            :readonly="readonly"
            :disabled="disabled"
        >

        <!-- A hidden textarea for copying text on non-https -->
        <textarea ref="hiddenTextarea" style="position: fixed; left: -999999px; top: -999999px;"></textarea>

        <a class="btn btn-outline-primary" @click="copyToClipboard(model)">
            <font-awesome-icon :icon="icon" />
        </a>
    </div>
</template>

<script>

let timeout;

export default {
    props: {
        /** ID of this input */
        id: {
            type: String,
            default: ""
        },
        /** Type of input */
        type: {
            type: String,
            default: "text"
        },
        /** The value of the input */
        modelValue: {
            type: String,
            default: ""
        },
        /** A placeholder to use */
        placeholder: {
            type: String,
            default: ""
        },
        /** Should the field auto complete */
        autocomplete: {
            type: String,
            default: undefined,
        },
        /** Is the input required? */
        required: {
            type: Boolean
        },
        /** Should the input be read only? */
        readonly: {
            type: String,
            default: undefined,
        },
        /** Is the input disabled? */
        disabled: {
            type: String,
            default: undefined,
        },
    },
    emits: [ "update:modelValue" ],
    data() {
        return {
            visibility: "password",
            icon: "copy",
        };
    },
    computed: {
        model: {
            get() {
                return this.modelValue;
            },
            set(value) {
                this.$emit("update:modelValue", value);
            }
        }
    },
    created() {

    },
    methods: {

        /**
         * Show the input
         * @returns {void}
         */
        showInput() {
            this.visibility = "text";
        },

        /**
         * Hide the input
         * @returns {void}
         */
        hideInput() {
            this.visibility = "password";
        },

        /**
         * Copy the provided text to the users clipboard
         * @param {string} textToCopy Text to copy to clipboard
         * @returns {Promise<void>}
         */
        copyToClipboard(textToCopy) {
            this.icon = "check";

            clearTimeout(timeout);
            timeout = setTimeout(() => {
                this.icon = "copy";
            }, 3000);

            // navigator clipboard api needs a secure context (https)
            // For http, use the text area method (else part)
            if (navigator.clipboard && window.isSecureContext) {
                // navigator clipboard api method'
                return navigator.clipboard.writeText(textToCopy);
            } else {
                // text area method
                let textArea = this.$refs.hiddenTextarea;
                textArea.value = textToCopy;
                textArea.focus();
                textArea.select();
                return new Promise((res, rej) => {
                    // here the magic happens
                    document.execCommand("copy") ? res() : rej();
                });
            }
        }

    }
};
</script>