owntone-server/web-src/src/components/SettingsTextfield.vue

124 lines
2.8 KiB
Vue
Raw Normal View History

<template>
<fieldset :disabled="disabled">
<div class="field">
<label class="label has-text-weight-normal">
<slot name="label" />
2022-05-20 07:44:22 -04:00
<i class="is-size-7" :class="{ 'has-text-info': is_success, 'has-text-danger': is_error }" v-text="info" />
</label>
<div class="control">
2022-05-20 07:44:22 -04:00
<input ref="setting" class="input" type="text" :placeholder="placeholder" :value="value" @input="set_update_timer" />
</div>
<p v-if="$slots['info']" class="help">
<slot name="info" />
</p>
</div>
</fieldset>
</template>
<script>
import webapi from '@/webapi'
import * as types from '@/store/mutation_types'
export default {
name: 'SettingsTextfield',
props: ['category_name', 'option_name', 'placeholder', 'disabled'],
data() {
return {
timerDelay: 2000,
timerId: -1,
statusUpdate: ''
}
},
computed: {
category() {
return this.$store.state.settings.categories.find(
(elem) => elem.name === this.category_name
)
},
option() {
if (!this.category) {
return {}
}
return this.category.options.find(
(elem) => elem.name === this.option_name
)
},
value() {
return this.option.value
},
info() {
if (this.statusUpdate === 'success') {
2022-05-20 07:44:22 -04:00
return this.$t('setting.saved')
} else if (this.statusUpdate === 'error') {
2022-05-20 07:44:22 -04:00
return this.$t('setting.not-saved')
}
return ''
2022-05-20 07:44:22 -04:00
},
is_success() {
return this.statusUpdate === 'success'
},
is_error() {
return this.statusUpdate === 'error'
}
},
methods: {
set_update_timer() {
if (this.timerId > 0) {
window.clearTimeout(this.timerId)
this.timerId = -1
}
this.statusUpdate = ''
2022-05-20 07:44:22 -04:00
const newValue = this.$refs.setting.value
if (newValue !== this.value) {
this.timerId = window.setTimeout(this.update_setting, this.timerDelay)
}
},
update_setting() {
this.timerId = -1
2022-05-20 07:44:22 -04:00
const newValue = this.$refs.setting.value
if (newValue === this.value) {
this.statusUpdate = ''
return
}
const option = {
category: this.category.name,
name: this.option_name,
value: newValue
}
webapi
.settings_update(this.category.name, option)
.then(() => {
this.$store.commit(types.UPDATE_SETTINGS_OPTION, option)
this.statusUpdate = 'success'
})
.catch(() => {
this.statusUpdate = 'error'
2022-05-20 07:44:22 -04:00
this.$refs.setting.value = this.value
})
.finally(() => {
this.timerId = window.setTimeout(this.clear_status, this.timerDelay)
})
},
clear_status: function () {
this.statusUpdate = ''
}
}
}
</script>
<style></style>