Resolve "Show a popup to let user know that the subsonic api password has been copied"

This commit is contained in:
Marcos Peña 2021-10-17 11:31:20 +00:00 committed by JuniorJPDJ
parent b942ed7a62
commit 89b3789c76
3 changed files with 96 additions and 31 deletions

View File

@ -0,0 +1 @@
Displays toast when subsonic password is copied (#1496)

View File

@ -1,58 +1,82 @@
<template> <template>
<div class="ui fluid action input"> <div class="ui fluid action input">
<input <input
required required
name="password" name="password"
:type="passwordInputType" :type="passwordInputType"
@input="$emit('input', $event.target.value)" @input="$emit('input', $event.target.value)"
:id="fieldId" :id="fieldId"
:value="value"> :value="value"
<button @click.prevent="showPassword = !showPassword" type="button" :title="labels.title" class="ui icon button"> />
<button
@click.prevent="showPassword = !showPassword"
type="button"
:title="labels.title"
class="ui icon button"
>
<i class="eye icon"></i> <i class="eye icon"></i>
</button> </button>
<button v-if="copyButton" @click.prevent="copy" type="button" class="ui icon button" :title="labels.copy"> <button
v-if="copyButton"
@click.prevent="copyPassword"
type="button"
class="ui icon button"
:title="labels.copy"
>
<i class="copy icon"></i> <i class="copy icon"></i>
</button> </button>
</div> </div>
</template> </template>
<script> <script>
function copyStringToClipboard (str) {
// cf https://techoverflow.net/2018/03/30/copying-strings-to-the-clipboard-using-pure-javascript/
let el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style = {position: 'absolute', left: '-9999px'};
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
export default { export default {
props: ['value', 'defaultShow', 'copyButton', 'fieldId'], props: ["value", "defaultShow", "copyButton", "fieldId"],
data () { data() {
return { return {
showPassword: this.defaultShow || false, showPassword: this.defaultShow || false,
} };
}, },
computed: { computed: {
labels () { labels () {
return { return {
title: this.$pgettext('Content/Settings/Button.Tooltip/Verb', 'Show/hide password'), title: this.$pgettext(
copy: this.$pgettext('*/*/Button.Label/Short, Verb', 'Copy') "Content/Settings/Button.Tooltip/Verb",
"Show/hide password"
),
copy: this.$pgettext("*/*/Button.Label/Short, Verb", "Copy"),
} }
}, },
passwordInputType () { passwordInputType() {
if (this.showPassword) { if (this.showPassword) {
return 'text' return "text";
} }
return 'password' return "password";
} }
}, },
methods: { methods: {
copy () { copyPassword () {
copyStringToClipboard(this.value) try {
this._copyStringToClipboard(this.value)
this.$store.commit('ui/addMessage', {
content: this.$pgettext(
'Content/*/Paragraph',
'Text copied to clipboard!'
),
date: new Date()
})
} catch ($e) {
console.error('Cannot copy', $e)
}
},
_copyStringToClipboard (str) {
// cf https://techoverflow.net/2018/03/30/copying-strings-to-the-clipboard-using-pure-javascript/
const el = document.createElement('textarea')
el.value = str
el.setAttribute('readonly', '')
el.style = { position: 'absolute', left: '-9999px' }
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
} }
} }
} }

View File

@ -0,0 +1,40 @@
import { expect } from 'chai'
import PasswordInput from '@/components/forms/PasswordInput.vue'
import { shallowMount } from '@vue/test-utils'
const sinon = require('sinon')
describe('PasswordInput', () => {
const password = 'password'
let sandbox
beforeEach(function () {
sandbox = sinon.createSandbox()
})
afterEach(function () {
sandbox.restore()
})
const wrapper = shallowMount(PasswordInput, {
mocks: {
$pgettext: () => 'dummy',
$store: {
commit: () => { }
}
}
})
wrapper.setProps({ value: password, copyButton: true })
it('password input has passed value', () => {
const inputElement = wrapper.find('input')
expect(inputElement.element.value).to.equal(password)
})
it('copy password function called', () => {
const spy = sandbox.spy()
wrapper.setMethods({
copyPassword: spy
})
sandbox.stub(PasswordInput.methods, '_copyStringToClipboard').callsFake()
const copyButton = wrapper.findAll('button').at(1)
copyButton.trigger('click')
sandbox.assert.calledOnce(spy)
})
})