2021-01-17 08:52:40 +01:00
|
|
|
<!--
|
|
|
|
- TransactionCategory.vue
|
|
|
|
- Copyright (c) 2021 james@firefly-iii.org
|
|
|
|
-
|
|
|
|
- This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
-
|
|
|
|
- This program is free software: you can redistribute it and/or modify
|
|
|
|
- it under the terms of the GNU Affero General Public License as
|
|
|
|
- published by the Free Software Foundation, either version 3 of the
|
|
|
|
- License, or (at your option) any later version.
|
|
|
|
-
|
|
|
|
- This program is distributed in the hope that it will be useful,
|
|
|
|
- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
- GNU Affero General Public License for more details.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the GNU Affero General Public License
|
|
|
|
- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
-->
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div class="form-group">
|
|
|
|
<div class="text-xs d-none d-lg-block d-xl-block">
|
|
|
|
{{ $t('firefly.category') }}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<vue-typeahead-bootstrap
|
2021-02-14 19:13:42 +01:00
|
|
|
v-model="category"
|
2021-01-17 08:52:40 +01:00
|
|
|
:data="categories"
|
2021-01-31 07:26:52 +01:00
|
|
|
:inputClass="errors.length > 0 ? 'is-invalid' : ''"
|
2021-01-17 08:52:40 +01:00
|
|
|
:minMatchingChars="3"
|
2021-02-26 06:39:20 +01:00
|
|
|
:placeholder="$t('firefly.category')"
|
2021-01-17 08:52:40 +01:00
|
|
|
:serializer="item => item.name"
|
2021-02-26 06:39:20 +01:00
|
|
|
:showOnFocus=true
|
|
|
|
inputName="category[]"
|
2021-01-17 08:52:40 +01:00
|
|
|
@hit="selectedCategory = $event"
|
|
|
|
@input="lookupCategory"
|
|
|
|
>
|
|
|
|
<template slot="append">
|
|
|
|
<div class="input-group-append">
|
2021-02-26 06:39:20 +01:00
|
|
|
<button class="btn btn-outline-secondary" tabindex="-1" type="button" v-on:click="clearCategory"><i class="far fa-trash-alt"></i></button>
|
2021-01-17 08:52:40 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</vue-typeahead-bootstrap>
|
2021-01-31 07:26:52 +01:00
|
|
|
<span v-if="errors.length > 0">
|
|
|
|
<span v-for="error in errors" class="text-danger small">{{ error }}<br/></span>
|
|
|
|
</span>
|
2021-01-17 08:52:40 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap';
|
|
|
|
import {debounce} from "lodash";
|
|
|
|
|
|
|
|
export default {
|
2021-01-31 07:26:52 +01:00
|
|
|
props: ['value', 'index', 'errors'],
|
2021-01-17 08:52:40 +01:00
|
|
|
components: {VueTypeaheadBootstrap},
|
|
|
|
name: "TransactionCategory",
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
categories: [],
|
2021-02-14 19:13:42 +01:00
|
|
|
initialSet: [],
|
2021-02-25 06:27:43 +01:00
|
|
|
category: this.value,
|
|
|
|
emitEvent: true
|
2021-01-17 08:52:40 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
|
|
|
|
// initial list of accounts:
|
|
|
|
axios.get(this.getACURL(''))
|
|
|
|
.then(response => {
|
|
|
|
this.categories = response.data;
|
|
|
|
this.initialSet = response.data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
clearCategory: function () {
|
2021-02-25 06:27:43 +01:00
|
|
|
this.category = '';
|
2021-01-17 08:52:40 +01:00
|
|
|
},
|
|
|
|
getACURL: function (query) {
|
|
|
|
// update autocomplete URL:
|
|
|
|
return document.getElementsByTagName('base')[0].href + 'api/v1/autocomplete/categories?query=' + query;
|
|
|
|
},
|
|
|
|
lookupCategory: debounce(function () {
|
|
|
|
// update autocomplete URL:
|
2021-01-17 19:52:53 +01:00
|
|
|
axios.get(this.getACURL(this.value))
|
2021-01-17 08:52:40 +01:00
|
|
|
.then(response => {
|
|
|
|
this.categories = response.data;
|
|
|
|
})
|
|
|
|
}, 300)
|
|
|
|
},
|
|
|
|
watch: {
|
2021-02-25 06:27:43 +01:00
|
|
|
value: function (value) {
|
|
|
|
this.emitEvent = false;
|
|
|
|
this.category = value ?? '';
|
|
|
|
},
|
2021-02-14 19:13:42 +01:00
|
|
|
category: function (value) {
|
2021-02-23 06:45:09 +01:00
|
|
|
this.$emit('set-field', {field: 'category', index: this.index, value: value});
|
2021-01-17 08:52:40 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
selectedCategory: {
|
|
|
|
get() {
|
|
|
|
return this.categories[this.index].name;
|
|
|
|
},
|
|
|
|
set(value) {
|
2021-02-14 19:13:42 +01:00
|
|
|
this.category = value.name;
|
2021-01-17 08:52:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|