firefly-iii/resources/assets/js/components/transactions/Category.vue

131 lines
4.2 KiB
Vue
Raw Normal View History

<!--
- Category.vue
- Copyright (c) 2019 thegrumpydictator@gmail.com
-
- This file is part of Firefly III.
-
- Firefly III is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Firefly III 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 General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
2019-05-23 22:29:04 -05:00
<div class="form-group" v-bind:class="{ 'has-error': hasError()}">
<div class="col-sm-12">
<div class="input-group">
<input
ref="input"
2019-05-10 22:32:09 -05:00
:value="value"
@input="handleInput"
type="text"
placeholder="Category"
autocomplete="off"
data-role="input"
v-on:keypress="handleEnter"
class="form-control"
v-on:submit.prevent
name="category[]"
title="Category">
<span class="input-group-btn">
<button
v-on:click="clearCategory"
class="btn btn-default"
type="button"><i class="fa fa-trash-o"></i></button>
</span>
</div>
<typeahead
:open-on-empty=true
:open-on-focus=true
v-on:input="selectedItem"
:async-src="categoryAutoCompleteURI"
v-model="name"
:target="target"
item-key="name"
></typeahead>
2019-05-23 22:29:04 -05:00
<ul class="list-unstyled" v-for="error in this.error">
<li class="text-danger">{{ error }}</li>
</ul>
</div>
</div>
</template>
<script>
export default {
name: "Category",
props: {
2019-05-10 22:32:09 -05:00
value: String,
inputName: String,
2019-05-23 22:29:04 -05:00
error: Array,
accountName: {
type: String,
default: ''
},
},
data() {
return {
categoryAutoCompleteURI: null,
name: null,
target: null,
}
},
ready() {
this.name = this.accountName;
},
mounted() {
this.target = this.$refs.input;
2019-08-01 22:25:24 -05:00
this.categoryAutoCompleteURI = document.getElementsByTagName('base')[0].href + "json/categories?search=";
},
methods: {
2019-05-23 22:29:04 -05:00
hasError: function () {
return this.error.length > 0;
},
2019-05-10 22:32:09 -05:00
handleInput(e) {
if (typeof this.$refs.input.value === 'string') {
this.$emit('input', this.$refs.input.value);
return;
}
this.$emit('input', this.$refs.input.value.name);
},
clearCategory: function () {
//props.value = '';
this.name = '';
// some event?
this.$emit('clear:category')
},
selectedItem: function (e) {
if (typeof this.name === 'undefined') {
return;
}
// emit the fact that the user selected a type of account
// (influencing the destination)
this.$emit('select:category', this.name);
2019-05-10 22:32:09 -05:00
if (typeof this.name === 'string') {
this.$emit('input', this.name);
return;
}
this.$emit('input', this.name.name);
},
handleEnter: function (e) {
// todo feels sloppy
if (e.keyCode === 13) {
e.preventDefault();
}
}
}
}
</script>
<style scoped>
</style>