2019-05-04 20:58:11 +02:00
|
|
|
<!--
|
|
|
|
- PiggyBank.vue
|
2020-01-25 06:08:56 +01:00
|
|
|
- Copyright (c) 2019 james@firefly-iii.org
|
2019-05-04 20:58:11 +02:00
|
|
|
-
|
2019-10-02 06:43:38 +02:00
|
|
|
- This file is part of Firefly III (https://github.com/firefly-iii).
|
2019-05-04 20:58:11 +02:00
|
|
|
-
|
2019-10-02 06:43:38 +02:00
|
|
|
- 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.
|
2019-05-04 20:58:11 +02:00
|
|
|
-
|
2019-10-02 06:43:38 +02:00
|
|
|
- This program is distributed in the hope that it will be useful,
|
2019-05-04 20:58:11 +02:00
|
|
|
- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:43:38 +02:00
|
|
|
- GNU Affero General Public License for more details.
|
2019-05-04 20:58:11 +02:00
|
|
|
-
|
2019-10-02 06:43:38 +02:00
|
|
|
- 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/>.
|
2019-05-04 20:58:11 +02:00
|
|
|
-->
|
|
|
|
<template>
|
2019-05-24 05:29:04 +02:00
|
|
|
<div class="form-group"
|
|
|
|
v-bind:class="{ 'has-error': hasError()}"
|
|
|
|
v-if="typeof this.transactionType !== 'undefined' && this.transactionType === 'Transfer'">
|
2020-06-27 13:32:40 +02:00
|
|
|
<div class="col-sm-12 text-sm">
|
|
|
|
{{ $t('firefly.piggy_bank') }}
|
|
|
|
|
|
|
|
</div>
|
2019-05-04 20:58:11 +02:00
|
|
|
<div class="col-sm-12">
|
2020-06-27 13:32:40 +02:00
|
|
|
<select name="piggy_bank[]" ref="piggy" @input="handleInput" class="form-control">
|
|
|
|
<optgroup v-for="(option, key) in this.piggies" v-bind:label="key">
|
|
|
|
<option v-for="piggy in option.piggies" :label="piggy.name_with_amount" :value="piggy.id">{{piggy.name_with_amount}}</option>
|
|
|
|
</optgroup>
|
2019-05-04 20:58:11 +02:00
|
|
|
</select>
|
2019-05-24 05:29:04 +02:00
|
|
|
<ul class="list-unstyled" v-for="error in this.error">
|
|
|
|
<li class="text-danger">{{ error }}</li>
|
|
|
|
</ul>
|
2019-05-04 20:58:11 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
name: "PiggyBank",
|
2019-10-05 12:44:05 +02:00
|
|
|
props: ['value','transactionType','error', 'no_piggy_bank'],
|
2019-05-04 20:58:11 +02:00
|
|
|
mounted() {
|
|
|
|
this.loadPiggies();
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
piggies: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-05-11 05:32:09 +02:00
|
|
|
handleInput(e) {
|
|
|
|
this.$emit('input', this.$refs.piggy.value);
|
|
|
|
},
|
2019-05-24 05:29:04 +02:00
|
|
|
hasError: function () {
|
|
|
|
return this.error.length > 0;
|
|
|
|
},
|
2019-05-04 20:58:11 +02:00
|
|
|
loadPiggies: function () {
|
|
|
|
let URI = document.getElementsByTagName('base')[0].href + "json/piggy-banks";
|
|
|
|
axios.get(URI, {}).then((res) => {
|
2020-06-27 13:32:40 +02:00
|
|
|
let tempList = {
|
|
|
|
0: {
|
|
|
|
group: {
|
|
|
|
title: this.$t('firefly.default_group_title_name')
|
|
|
|
},
|
|
|
|
piggies: [
|
|
|
|
{
|
|
|
|
name_with_amount: this.no_piggy_bank,
|
|
|
|
id: 0,
|
|
|
|
}
|
|
|
|
],
|
2019-05-04 20:58:11 +02:00
|
|
|
}
|
2020-06-27 13:32:40 +02:00
|
|
|
};
|
2019-05-04 20:58:11 +02:00
|
|
|
for (const key in res.data) {
|
|
|
|
if (res.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
|
2020-06-27 13:32:40 +02:00
|
|
|
// add to temp list
|
|
|
|
let currentPiggy = res.data[key];
|
|
|
|
if (currentPiggy.objectGroup) {
|
|
|
|
let groupOrder = currentPiggy.objectGroup.order;
|
|
|
|
if (!tempList[groupOrder]) {
|
|
|
|
tempList[groupOrder] = {
|
|
|
|
group: {
|
|
|
|
title: currentPiggy.objectGroup.title
|
|
|
|
},
|
|
|
|
piggies: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
tempList[groupOrder].piggies.push({name_with_amount: currentPiggy.name_with_amount, id: currentPiggy.id});
|
|
|
|
}
|
|
|
|
if (!currentPiggy.objectGroup) {
|
|
|
|
// add to empty one:
|
|
|
|
tempList[0].piggies.push({name_with_amount: currentPiggy.name_with_amount, id: currentPiggy.id});
|
|
|
|
}
|
|
|
|
//console.log(currentPiggy);
|
2019-05-04 20:58:11 +02:00
|
|
|
this.piggies.push(res.data[key]);
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 13:32:40 +02:00
|
|
|
const ordered = {};
|
|
|
|
Object.keys(tempList).sort().forEach(function(key) {
|
|
|
|
let groupName = tempList[key].group.title;
|
|
|
|
ordered[groupName] = tempList[key];
|
|
|
|
});
|
|
|
|
// final list:
|
|
|
|
|
|
|
|
this.piggies = ordered;
|
|
|
|
console.log(ordered);
|
2019-05-04 20:58:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
2020-06-27 13:32:40 +02:00
|
|
|
</style>
|