2021-09-30 16:09:43 +00:00
|
|
|
<template>
|
2021-10-01 08:43:11 +00:00
|
|
|
<div class="input-group">
|
2021-09-30 16:09:43 +00:00
|
|
|
<input
|
|
|
|
:id="id"
|
|
|
|
ref="input"
|
|
|
|
v-model="model"
|
|
|
|
:type="type"
|
|
|
|
class="form-control"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:autocomplete="autocomplete"
|
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:disabled="disabled"
|
|
|
|
>
|
|
|
|
|
2023-03-24 11:16:12 +00:00
|
|
|
<!-- A hidden textarea for copying text on non-https -->
|
|
|
|
<textarea ref="hiddenTextarea" style="position: fixed; left: -999999px; top: -999999px;"></textarea>
|
|
|
|
|
2021-09-30 16:09:43 +00:00
|
|
|
<a class="btn btn-outline-primary" @click="copyToClipboard(model)">
|
|
|
|
<font-awesome-icon :icon="icon" />
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
let timeout;
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
2022-06-01 23:32:05 +00:00
|
|
|
/** ID of this input */
|
2021-09-30 16:09:43 +00:00
|
|
|
id: {
|
|
|
|
type: String,
|
|
|
|
default: ""
|
|
|
|
},
|
2022-06-01 23:32:05 +00:00
|
|
|
/** Type of input */
|
2021-09-30 16:09:43 +00:00
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
default: "text"
|
|
|
|
},
|
2022-06-01 23:32:05 +00:00
|
|
|
/** The value of the input */
|
2021-09-30 16:09:43 +00:00
|
|
|
modelValue: {
|
|
|
|
type: String,
|
|
|
|
default: ""
|
|
|
|
},
|
2022-06-01 23:32:05 +00:00
|
|
|
/** A placeholder to use */
|
2021-09-30 16:09:43 +00:00
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
default: ""
|
|
|
|
},
|
2022-06-01 23:32:05 +00:00
|
|
|
/** Should the field auto complete */
|
2021-09-30 16:09:43 +00:00
|
|
|
autocomplete: {
|
|
|
|
type: String,
|
|
|
|
default: undefined,
|
|
|
|
},
|
2022-06-01 23:32:05 +00:00
|
|
|
/** Is the input required? */
|
2021-09-30 16:09:43 +00:00
|
|
|
required: {
|
|
|
|
type: Boolean
|
|
|
|
},
|
2022-06-01 23:32:05 +00:00
|
|
|
/** Should the input be read only? */
|
2021-09-30 16:09:43 +00:00
|
|
|
readonly: {
|
|
|
|
type: String,
|
|
|
|
default: undefined,
|
|
|
|
},
|
2022-06-01 23:32:05 +00:00
|
|
|
/** Is the input disabled? */
|
2021-09-30 16:09:43 +00:00
|
|
|
disabled: {
|
|
|
|
type: String,
|
|
|
|
default: undefined,
|
|
|
|
},
|
|
|
|
},
|
2022-04-17 07:27:35 +00:00
|
|
|
emits: [ "update:modelValue" ],
|
2021-09-30 16:09:43 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
visibility: "password",
|
|
|
|
icon: "copy",
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
model: {
|
|
|
|
get() {
|
|
|
|
return this.modelValue;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
this.$emit("update:modelValue", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
|
2023-08-11 07:46:41 +00:00
|
|
|
/**
|
|
|
|
* Show the input
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-09-30 16:09:43 +00:00
|
|
|
showInput() {
|
|
|
|
this.visibility = "text";
|
|
|
|
},
|
|
|
|
|
2023-08-11 07:46:41 +00:00
|
|
|
/**
|
|
|
|
* Hide the input
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-09-30 16:09:43 +00:00
|
|
|
hideInput() {
|
|
|
|
this.visibility = "password";
|
|
|
|
},
|
|
|
|
|
2022-06-01 23:32:05 +00:00
|
|
|
/**
|
|
|
|
* Copy the provided text to the users clipboard
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {string} textToCopy Text to copy to clipboard
|
2022-06-01 23:32:05 +00:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2021-09-30 16:09:43 +00:00
|
|
|
copyToClipboard(textToCopy) {
|
|
|
|
this.icon = "check";
|
|
|
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
this.icon = "copy";
|
|
|
|
}, 3000);
|
|
|
|
|
|
|
|
// navigator clipboard api needs a secure context (https)
|
2023-03-24 11:16:12 +00:00
|
|
|
// For http, use the text area method (else part)
|
2021-09-30 16:09:43 +00:00
|
|
|
if (navigator.clipboard && window.isSecureContext) {
|
|
|
|
// navigator clipboard api method'
|
|
|
|
return navigator.clipboard.writeText(textToCopy);
|
|
|
|
} else {
|
|
|
|
// text area method
|
2023-03-24 11:16:12 +00:00
|
|
|
let textArea = this.$refs.hiddenTextarea;
|
2021-09-30 16:09:43 +00:00
|
|
|
textArea.value = textToCopy;
|
|
|
|
textArea.focus();
|
|
|
|
textArea.select();
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
// here the magic happens
|
|
|
|
document.execCommand("copy") ? res() : rej();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|