2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/ui/components/List/List.vue

131 lines
3.2 KiB
Vue
Raw Normal View History

2018-06-27 14:38:27 +00:00
<template>
<div class="frappe-list">
<list-actions
:doctype="doctype"
:showDelete="checkList.length"
@new="newDoc"
@delete="deleteCheckedItems"
/>
<ul class="list-group">
<list-item v-for="doc of data" :key="doc.name"
:id="doc.name"
:isActive="doc.name === $route.params.name"
:isChecked="isChecked(doc.name)"
@clickItem="openForm(doc.name)"
@checkItem="toggleCheck(doc.name)"
>
<indicator v-if="hasIndicator" :color="getIndicatorColor(doc)" />
<span class="d-inline-block ml-2">
{{ doc[meta.titleField || 'name'] }}
</span>
2018-06-27 14:38:27 +00:00
</list-item>
</ul>
</div>
</template>
<script>
import frappe from 'frappejs';
import ListActions from './ListActions';
import ListItem from './ListItem';
export default {
name: 'List',
2018-07-12 15:17:46 +00:00
props: ['doctype'],
2018-06-27 14:38:27 +00:00
components: {
ListActions,
ListItem
2018-06-27 14:38:27 +00:00
},
data() {
return {
data: [],
checkList: [],
activeItem: ''
};
2018-06-27 14:38:27 +00:00
},
computed: {
meta() {
return frappe.getMeta(this.doctype);
},
hasIndicator() {
return Boolean(this.meta.indicators);
}
2018-06-27 14:38:27 +00:00
},
created() {
frappe.db.on(`change:${this.doctype}`, () => {
this.updateList();
});
2018-07-12 15:17:46 +00:00
this.$root.$on('search', this.updateList)
this.$root.$emit('newList')
2018-06-27 14:38:27 +00:00
},
mounted() {
this.updateList();
},
methods: {
async newDoc() {
let doc = await frappe.getNewDoc(this.doctype);
this.$router.push(`/edit/${this.doctype}/${doc.name}`);
2018-06-27 14:38:27 +00:00
},
2018-07-12 15:17:46 +00:00
async updateList(query=null) {
let filters = null
if (query) {
filters = {
keywords : ['like', query]
}
}
const indicatorField = this.hasIndicator ? this.meta.indicators.key : null;
const fields = [
'name',
indicatorField,
this.meta.titleField,
...this.meta.keywordFields
].filter(Boolean);
2018-06-27 14:38:27 +00:00
const data = await frappe.db.getAll({
doctype: this.doctype,
fields,
2018-07-12 15:17:46 +00:00
filters: filters || null
2018-06-27 14:38:27 +00:00
});
this.data = data;
},
openForm(name) {
this.activeItem = name;
this.$router.push(`/edit/${this.doctype}/${name}`);
2018-06-27 14:38:27 +00:00
},
async deleteCheckedItems() {
await frappe.db.deleteMany(this.doctype, this.checkList);
this.checkList = [];
},
toggleCheck(name) {
if (this.checkList.includes(name)) {
this.checkList = this.checkList.filter(docname => docname !== name);
2018-06-27 14:38:27 +00:00
} else {
this.checkList = this.checkList.concat(name);
2018-06-27 14:38:27 +00:00
}
},
isChecked(name) {
return this.checkList.includes(name);
},
getIndicatorColor(doc) {
return this.meta.getIndicatorColor(doc);
2018-06-27 14:38:27 +00:00
}
}
};
2018-06-27 14:38:27 +00:00
</script>
<style lang="scss" scoped>
@import "../../styles/variables";
.list-group-item {
border-left: none;
border-right: none;
border-radius: 0;
2018-06-27 14:38:27 +00:00
}
.list-group-item:first-child {
border-top: none;
2018-06-27 14:38:27 +00:00
}
.list-group-item:not(.active):hover {
background-color: $light;
2018-06-27 14:38:27 +00:00
}
</style>